WordPress博客评论合并工具

上篇,这里共享我写的一个用来合并 WordPress 博客评论的小工具。该工具可以将两个镜像 WordPress 博客上对同一篇文章的评论合并起来。

下面先介绍合并的步骤:

1. 首先到这里下载我修改的 WordPress 导入插件,并按照安装一般 WordPress 插件的方式,安装并启用该插件。

2. 然后在 WP 管理后台选择“工具->导入->WordPress”,然后上传从镜像 WP 博客导出的 xml 文件。

3. 在下一步选择“Only Merge Comments” 很重要!!!

Wordpress博客评论合并工具

4. submit,稍等片刻即可。

其实我没有重新制造轮子,只是修改了一下 WordPress 默认的博客导入工具 WordPress Importer,给它加了点儿功能。只要选中“Only Merge Comments”,使用这个工具是很安全的,它只会将 xml 中与当前博客中存在的文章对应的评论添加上去,而不处理任何不存在的文章,也不会重复添加已有的评论,而且会过滤某些垃圾评论。用这个选项,你可以重复导入很多次 :)

可能的缺陷有:这个工具判断文章是否存在的唯一标准是文章标题,因此如果有多篇文章标题一样,可能会存在问题(未测试)。本人不保证它是充分测试的,因此在应用之前最好还是在本地的镜像测试后进行;如果没有进行测试,请一定在合并之前对博客进行备份

下面是我修改的 patch:

--- wordpress-importer/wordpress-importer.php    2010-06-02 00:38:23.000000000 +0800
+++ ../../www/blog/wp-content/plugins/wordpress-importer/wordpress-importer.php    2010-09-29 19:33:57.953790929 +0800
@@ -49,2 +49,3 @@
     var $fetch_attachments = false;
+    var $only_merge_comments = false;
     var $url_remap = array ();
@@ -258,2 +259,7 @@

+<h2><?php _e('Only Merge Comments', 'wordpress-importer'); ?></h2>
+<p>
+    <input type="checkbox" value="1" name="comments" id="merge-comments" />
+    <label for="merge-comments"><?php _e('Only merge comments, ignore post, tags...', 'wordpress-importer') ?></label>
+</p>
<?php
@@ -483,3 +489,7 @@

-        $post_exists = post_exists($post_title, '', $post_date);
+        if ($this->only_merge_comments) {
+            $post_exists = post_exists($post_title, '', '');
+        } else {
+            $post_exists = post_exists($post_title, '', $post_date);
+        }

@@ -489,4 +499,7 @@
             $comment_post_ID = $post_id = $post_exists;
-        } else {
-
+        } else if ( $this->only_merge_comments) {
+            echo '<li>';
+            printf(__('Post <em>%s</em> not found, comments not updated.', 'wordpress-importer'), stripslashes($post_title));
+            $comment_post_ID = $post_id = $post_exists;
+        } else {
             // If it has parent, process parent first.
@@ -605,3 +618,11 @@
                 // if this is a new post we can skip the comment_exists() check
-                if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) {
+                if ($this->only_merge_comments) {
+                    if ( $post_exists && !comment_exists($comment['comment_author'], $comment['comment_date']) && $comment['comment_author'] != 'Unknown') {
+                        if (isset($inserted_comments[$comment['comment_parent']]))
+                            $comment['comment_parent'] = $inserted_comments[$comment['comment_parent']];
+                        $comment = wp_filter_comment($comment);
+                        $inserted_comments[$key] = wp_insert_comment($comment);
+                        $num_comments++;
+                    }
+                } else if ( !$post_exists || !comment_exists($comment['comment_author'], $comment['comment_date']) ) {
                     if (isset($inserted_comments[$comment['comment_parent']]))
@@ -847,5 +868,7 @@
         $this->get_entries();
-        $this->process_categories();
-        $this->process_tags();
-        $this->process_terms();
+        if ($this->only_merge_comments) {
+            $this->process_categories();
+            $this->process_tags();
+            $this->process_terms();
+        }
         $result = $this->process_posts();
@@ -891,2 +914,4 @@
                 $fetch_attachments = ! empty( $_POST['attachments'] );
+                $only_merge_comments = ! empty( $_POST['comments'] );
+                $this->only_merge_comments = (bool) $only_merge_comments;
                 $result = $this->import( $_GET['id'], $fetch_attachments);

悲剧的 MSN Space

MSN Space 总算倒了,所有用户都要求被迁往 wordpress.com,或者下载备份文件。话说我第一个用得顺手的 blog 还是 MSN Space,也用了很长的时间,不免觉得有些悲凉。

今天有同事问我,MSN Space 为什么混到这个地步?我说,本来 MSN Space 还凑合可用,但是每一次改版、每一次改名,都让你觉得更加难用。产品能做到这个份上,也真是不容易,不过无独有偶,MSN 也算得上跟它的绝配了!不知道什么时候能看到 MSN 整体搬迁用户到 AIM 或者改版到 XMPP?

哦,我又忘了,国内还有个飞信呢!不愧是外包给 MSN 做的产品,看看现在的飞信 4.x,我好怀念飞信 3.x 啊!

这件事情让我感兴趣的一点是,我总算看到一个可能,可以将以前在 MSN Space 上的评论,合并到现在的博客中了。或许需要自己写个小工具,假期可以尝试做一下。