<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>浪人的窝 &#187; SK2</title>
	<atom:link href="http://wide4.bt4.org/html/archives/tag/sk2/feed" rel="self" type="application/rss+xml" />
	<link>http://wide4.bt4.org</link>
	<description>泥沙不断堆积，掩埋沉在水底的记忆。</description>
	<lastBuildDate>Tue, 27 Jul 2010 04:24:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>SK 2 与 cos-html-cache 共存之道</title>
		<link>http://wide4.bt4.org/html/archives/160.htm</link>
		<comments>http://wide4.bt4.org/html/archives/160.htm#comments</comments>
		<pubDate>Tue, 05 Jun 2007 13:18:13 +0000</pubDate>
		<dc:creator>bt4wang</dc:creator>
				<category><![CDATA[WordPress]]></category>
		<category><![CDATA[SK2]]></category>
		<category><![CDATA[静态化]]></category>

		<guid isPermaLink="false">http://wide4.bt4.org/html/archives/160.htm</guid>
		<description><![CDATA[这几天在忙一个事情，那就是将本站的 home、page 和 post 页面静态化了，全部生成html文件，这样访问时可以尽可能减少数据库访问，加快访问速度，这对我用的这种空间还是有很大意义的。 静... ]]></description>
			<content:encoded><![CDATA[<p id='fp'>这几天在忙一个事情，那就是将本站的 home、page 和 post 页面静态化了，全部生成html文件，这样访问时可以尽可能减少数据库访问，加快访问速度，这对我用的这种空间还是有很大意义的。</p>
<p>静态化主要采用了 cosβ 编写的 <a href="http://www.storyday.com/html/y2007/958_cos-html-cache-10.html">cos-html-cache</a> 插件。由于根据作者的建议，更改了永久链接结构，为了以前的地址能保持继续访问，又安装了 <a href="http://www.deanlee.cn/wordpress/permalinks-migration-plugin/">Dean&#8217;s Permalinks Migration</a>，用来转换链接。关于这两个插件的相关问题，可以分别访问各自的官网寻找。</p>
<p>下面谈谈我遇到的问题。本 BLOG 虽然其貌不扬，知名度不高，但始终有一定量的 spam 攻击，虽然不多，但是很讨厌。以前用 WP 官方的 <a href="http://akismet.com/">Akismet</a> 可以很好的防御这个问题，但是自从其服务器被墙之后，就经常不正常工作。后来就换上了目前使用的 Spam Karma 2 插件，效果是很不错的。</p>
<p>但是这个效果不错的 SK2 似乎和 cos-html-cache 不和，SK2 通过评论后，cos-html-cache 不启动重建缓存。这个问题很郁闷啊，不能有评论我就手动重建啊，SK2 又不能关，只有动手改改了~这里说明下我的 PHP 水平是自己看了几天编程核心思想的书，然后受不了扔了，所以等级可想而知，如有各种离奇错误，还望指正。<span id="more-160"></span></p>
<p>首先研究了下 cos-html-cache 的代码，发现它在评论通过后更新缓存是靠 add_action 实现的，那么既然 SK2 通过评论后没有更新缓存，说明 SK2 通过评论后没有执行与 add_action 的钩子（hook）对应的 do_action（我的想法就是这样，并且根据这个思路解决了问题）。既然这样，我们只要在 SK2 通过评论的代码后加上一个 do_action 就应该可以解决问题。于是又着手研究 SK2 的代码，找到三处代码实现通过评论。下面说说修改方式：</p>
<p>首先编辑 spam_karma_2_plugin.php 文件，找到：</p>
<blockquote><p><code>		elseif ( get_settings('comments_notify'))<br />
		{<br />
			wp_notify_postauthor($comment_ID, $sk2_core->cur_comment->type);<br />
		}</code></p></blockquote>
<p>这一好像是直接通过的评论不做任何处理，修改为：</p>
<blockquote><p><code>		else<br />
		{<br />
			if ( get_settings('comments_notify'))<br />
			{<br />
			wp_notify_postauthor($comment_ID, $sk2_core->cur_comment->type);<br />
			}<br />
		global $wpdb;</p>
<p>		$post_id = (int)$wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$comment_ID' LIMIT 1");<br />
		do_action('comment_approved', $post_id);<br />
		}</code></p></blockquote>
<p>可以看到，这里在通过后，获取通过评论所属文章的ID，平且 do_action。这里没有采用 cos-html-cache 处理评论时的 action（edit_post），主要是怕影响到其他由这个 action 激发的插件。</p>
<p>再找到：</p>
<blockquote><p><code>				if ($cur_section == 'spam')<br />
				{<br />
					$sk2_core->cur_comment->set_karma(15, 'web_UI', __("Manually recovered comment.", 'sk2'));<br />
					do_action('wp_set_comment_status', $sk2_core->cur_comment->ID);<br />
				}</code></p></blockquote>
<p>这部分是手动将 spam 恢复为正常评论的代码，替换为：</p>
<blockquote><p><code>				if ($cur_section == 'spam')<br />
				{<br />
					$sk2_core->cur_comment->set_karma(15, 'web_UI', __("Manually recovered comment.", 'sk2'));<br />
					do_action('wp_set_comment_status', $sk2_core->cur_comment->ID);<br />
					global $wpdb;</p>
<p>					$post_id = (int)$wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$sk2_core->cur_comment->ID' LIMIT 1");<br />
					do_action('comment_approved', $post_id);<br />
				}</code></p></blockquote>
<p>到此 spam_karma_2_plugin.php 编辑完毕，保存，再开启 sk2_core_class.php，查找：</p>
<blockquote><p><code>					if ($this->cur_comment->approved == '1')<br />
					{<br />
						echo __("Thank you. Your comment has been approved.", 'sk2');<br />
						if ( get_settings('comments_notify') )<br />
							wp_notify_postauthor($this->cur_comment->ID, $this->cur_comment->type);<br />
						//TODO redirect to comment page...<br />
					}</code></p></blockquote>
<p>这一段应该是给第二次机会后的处理，相应的替换为：</p>
<blockquote><p><code>					if ($this->cur_comment->approved == '1')<br />
					{<br />
						echo __("Thank you. Your comment has been approved.", 'sk2');<br />
						global $wpdb;</p>
<p>						$post_id = (int)$wpdb->get_var("SELECT comment_post_ID FROM $wpdb->comments WHERE comment_ID='$this->cur_comment->ID' LIMIT 1");<br />
						do_action('comment_approved', $post_id);<br />
						if ( get_settings('comments_notify') )<br />
							wp_notify_postauthor($this->cur_comment->ID, $this->cur_comment->type);<br />
						//TODO redirect to comment page...<br />
					}</code></p></blockquote>
<p>至此 SK2 的改动完毕。前面已经说了，我写的 do_action 没有采用 cos-html-cache 处理评论时的 action，于是还要小改动一下 cos-html-cache 插件，开启 cos-html-cache.php，找到：</p>
<blockquote><p><code>add_action('edit_post', 'htmlCreator');<br />
add_action('edit_post', 'createIndexHTML');</code></p></blockquote>
<p>在其下添加：</p>
<blockquote><p><code>add_action('comment_approved', 'htmlCreator');<br />
add_action('comment_approved', 'createIndexHTML');</code></p></blockquote>
<p>到这里，所有的修改完毕，发布评论并被 SK2 通过后，应该就可以更新缓存了。</p>
<p>这次修改的思路就是在 SK2 里合适位置添加 do_action，使其在通过评论后触发 cos-html-cache 更新缓存。这种改法似乎可以用在更多场合，比如 FlickrRSS 图片更新后，亦可以通过类似的修改实现重新缓存首页什么的。</p>
</p>]]></content:encoded>
			<wfw:commentRss>http://wide4.bt4.org/html/archives/160.htm/feed</wfw:commentRss>
		<slash:comments>13</slash:comments>
		</item>
	</channel>
</rss>
