Xiaoxia[PG]

终于用上WP

Internet、我的代码29 条评论

花了一天时间,奋战10多个小时。
终于比较成功地把原博客的数据库转移到现在的wordpress上来!
感觉wordpress真的慢得可怜,在速度上肯定比之前的慢很多,不过功能上也满足了。

我之前以为要直接把mysql里wp的数据库挖出来看看,后来发现通过后台管理面板的导入工具,可以很方便得导入符合一定格式的xml文件。所以,我花了一点心思写了一段php代码,把之前的数据库内容导出来,以wp格式输出来。
不过不是很顺利,我发现wp的数据库里没有单个帖子访问量的统计,需要使用插件才能实现这个功能。这个比较棘手,我后来直接把原来访问量的数据扔掉了,有点可惜,呵呵~

php代码:

<?php

include_once 'conf.php';

$buf = '<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:excerpt="http://wordpress.org/export/1.0/excerpt/"
	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:wp="http://wordpress.org/export/1.0/"
>

<channel>
	<title>Xiaoxia[PG]</title>
	<link>http://blog.xiaoxia.org</link>
	<description>= I am the one! =</description>
	<pubDate>Sat, 16 Oct 2010 21:51:19 +0000</pubDate>
	<generator>http://wordpress.org/?v=3.0.1</generator>
	<language>en</language>
	<wp:wxr_version>1.0</wp:wxr_version>
	<wp:base_site_url>http://blog.xiaoxia.org</wp:base_site_url>
	<wp:base_blog_url>http://blog.xiaoxia.org</wp:base_blog_url>
	<generator>http://wordpress.org/?v=3.0.1</generator>';

$condition = " ORDER BY `ctime`";
$query = "SELECT `topic`.*, `type`.`style`, `type`.`name` AS type_name from `topic` 
	LEFT JOIN `type` ON `type`.`type_id`=`topic`.`type_id`
	$condition";
$result  = mysql_query($query) or die('Error, query failed' .$query . "<p>".mysql_error()."</p>");

while( $row = mysql_fetch_array($result, MYSQL_ASSOC) )
{
	$buf .= "<item>\n";
	
	if( $row['style'] ==0 ){
		$buf .= "<title>" . $row['title'] . "</title>\n";
	}else{
		$buf .= "<title>" . $row['name'] . "</title>\n";
	}
	$buf .="<link>http://blog.xiaoxia.org/?p=" . $row['topic_id'] . "</link>\n";
	$buf .="<pubDate>" . date("D, j M Y G:i:s +0800", strtotime($row['ctime']) ) . "</pubDate>\n";
	$buf .="<dc:creator><![CDATA[xiaoxia]]></dc:creator>\n";
	$buf .="<category><![CDATA[".$row['type_name']. "]]></category>\n";
	$buf .="<category domain=\"category\" nicename=\"".$row['type_id']."\"><![CDATA[".$row['type_name']. "]]></category>\n";
	$buf .="<guid isPermaLink=\"false\">http://blog.xiaoxia.org/?p=" . $row['topic_id'] . "</guid>\n";
	$buf .="<description></description>\n";
	if( $row['style'] ==0 ){
		$buf .="<content:encoded><![CDATA[" . $row['content'] . "]]></content:encoded>\n";
	}else{
		$buf .="<content:encoded><![CDATA[" . $row['title'] . "\n<!--more-->\n" .$row['content'] . "]]></content:encoded>\n";
	}
	$buf .="<excerpt:encoded><![CDATA[]]></excerpt:encoded>\n";
	$buf .="<wp:post_id>{$row['topic_id']}</wp:post_id>\n";
	$buf .="<wp:post_date>{$row['ctime']}</wp:post_date>\n";
//	$buf .="<wp:post_date_gmt>{$row['ctime']}</wp:post_date_gmt>\n";
	$buf .="<wp:comment_status>open</wp:comment_status>\n";
	$buf .="<wp:ping_status>open</wp:ping_status>\n";
	$buf .="<wp:post_name>{$row['name']}</wp:post_name>\n";
	if( $row['private'] )
		$buf .="<wp:status>private</wp:status>\n";
	else
		$buf .="<wp:status>publish</wp:status>\n";
	$buf .="<wp:post_parent>0</wp:post_parent>\n";
	$buf .="<wp:menu_order>0</wp:menu_order>\n";
	$buf .="<wp:post_type>post</wp:post_type>\n";
	$buf .="<wp:post_password></wp:post_password>\n";
	$buf .="<wp:is_sticky>0</wp:is_sticky>\n";
	
	$condition = " WHERE `topic_id`={$row['topic_id']} ORDER BY `ctime`";
	$query = "SELECT * from `comment` $condition";
	$result2  = mysql_query($query) or die('Error, query failed!' .$query . "<p>".mysql_error()."</p>");
	while( $row = mysql_fetch_array($result2, MYSQL_ASSOC) )
	{
		$buf .= "<wp:comment>\n";
		$buf .= "<wp:comment_id>{$row['comment_id']}</wp:comment_id>\n";
		if( $row['name']=='xiaoxia' || $row['name']=='Xiaoxia' || $row['name']=='小虾'){
			$buf .= "<wp:comment_user_id>1</wp:comment_user_id>\n";
			$row['name']='Xiaoxia';
			$row['mail']='[email protected]';
		}else{
			$buf .= "<wp:comment_user_id>0</wp:comment_user_id>\n";
		}
		$buf .= "<wp:comment_author><![CDATA[{$row['name']}]]></wp:comment_author>\n";
		$buf .= "<wp:comment_author_email><![CDATA[{$row['mail']}]]></wp:comment_author_email>\n";
		$buf .= "<wp:comment_author_url></wp:comment_author_url>\n";
		$buf .= "<wp:comment_author_IP>{$row['ip']}</wp:comment_author_IP>\n";
		$buf .= "<wp:comment_date>{$row['ctime']}</wp:comment_date>\n";
		$buf .= "<wp:comment_content><![CDATA[{$row['content']}]]></wp:comment_content>\n";
		$buf .= "<wp:comment_approved>" . (1 - $row['disabled'] ) . "</wp:comment_approved>\n";
		$buf .= "<wp:comment_type></wp:comment_type>\n";
		$buf .= "<wp:comment_parent>0</wp:comment_parent>\n";
		$buf .= "</wp:comment>\n";
	}
	$buf .= "</item>\n";
}

$buf .= '<item>
		<title>留言板</title>
		<link>http://blog.xiaoxia.org/?page_id=2</link>
		<pubDate>Sat, 16 Oct 2010 21:20:52 +0000</pubDate>
		<dc:creator><![CDATA[jessica]]></dc:creator>
		
		<guid isPermaLink="false">http://blog.xiaoxia.org/?page_id=2</guid>
		<description></description>
		<content:encoded><![CDATA[这里是留言板。]]></content:encoded>
		<excerpt:encoded><![CDATA[]]></excerpt:encoded>
		<wp:post_id>2</wp:post_id>
		<wp:post_date>2010-10-16 21:20:52</wp:post_date>
		<wp:post_date_gmt>2010-10-16 21:20:52</wp:post_date_gmt>
		<wp:comment_status>open</wp:comment_status>
		<wp:ping_status>open</wp:ping_status>
		<wp:post_name>留言</wp:post_name>
		<wp:status>publish</wp:status>
		<wp:post_parent>0</wp:post_parent>
		<wp:menu_order>0</wp:menu_order>
		<wp:post_type>page</wp:post_type>
		<wp:post_password></wp:post_password>
		<wp:is_sticky>0</wp:is_sticky>';

$condition = " WHERE `topic_id`=0 ORDER BY `ctime`";
$query = "SELECT * from `comment` $condition";
$result2  = mysql_query($query) or die('Error, query failed!' .$query . "<p>".mysql_error()."</p>");
while( $row = mysql_fetch_array($result2, MYSQL_ASSOC) )
{
	$buf .= "<wp:comment>\n";
	$buf .= "<wp:comment_id>{$row['comment_id']}</wp:comment_id>\n";
	if( $row['name']=='xiaoxia' || $row['name']=='Xiaoxia' || $row['name']=='小虾'){
		$buf .= "<wp:comment_user_id>1</wp:comment_user_id>\n";
		$row['name']='Xiaoxia';
		$row['mail']='[email protected]';
	}else{
		$buf .= "<wp:comment_user_id>0</wp:comment_user_id>\n";
	}
	$buf .= "<wp:comment_author><![CDATA[{$row['name']}]]></wp:comment_author>\n";
	$buf .= "<wp:comment_author_email><![CDATA[{$row['mail']}]]></wp:comment_author_email>\n";
	$buf .= "<wp:comment_author_url></wp:comment_author_url>\n";
	$buf .= "<wp:comment_author_IP>{$row['ip']}</wp:comment_author_IP>\n";
	$buf .= "<wp:comment_date>{$row['ctime']}</wp:comment_date>\n";
	$buf .= "<wp:comment_content><![CDATA[{$row['content']}]]></wp:comment_content>\n";
	$buf .= "<wp:comment_approved>" . (1 - $row['disabled'] ) . "</wp:comment_approved>\n";
	$buf .= "<wp:comment_type></wp:comment_type>\n";
	$buf .= "<wp:comment_parent>0</wp:comment_parent>\n";
	$buf .= "<wp:comment_user_id>0</wp:comment_user_id>\n";
	$buf .= "</wp:comment>\n";
}
$buf .= '</item>';

$buf .= '	</channel>
</rss>';

$file_pointer = fopen("wp.xml","w");
fwrite($file_pointer,$buf);
fclose($file_pointer);
?>
OK

29 条评论

  1. 额,第一个啊,不错,但分类目录显示有问题

    1. 分类目录显示有什么问题? 我好像没发现到。

      1. 我这里显示,分类目录在下面,而不是在右边,你不会是故意的放下面的吧

        1. 不是啊。。分类目录是在右边栏的。你是使用哪个浏览器?要不要按Ctrl+F5刷新一下?

  2. 页脚的版权信息在IE8下成一坨了。。。

    1. 谢谢!这个我知道了,某些插件有冲突。已解决。

  3. 你应该早写的,不然我就不必一条一条地copy到wp了。杯具...

    1. 悲剧。。。

  4. 我用的IE9页脚部分都是一坨

  5. 我用的IE9页脚部分都是一坨
    ,还有我回复的时候他说我已经发布过相同的回复....

    1. 你的确发布了类似的回复 - -!

  6. 推荐 typecho, 这个比wp快多,代码也很多注释,不过主题少了点。

    1. 嗯,好东西! 不过我可不想再花时间去转移一次数据库啦。

  7. 不知道博主重阳有没有登山呢。

    1. 没~ 过一段时间再登。为啥这样问?

      1. 因为那是广告

        1. 噢。。。

    2. 没仔细看还以为说黄重阳呢。。。

      1. 要配新眼镜了...

  8. 不错啊,刚开BLOG不久就有广告商来串门罗.GAGA

  9. 同dump2的推荐。

    WP确实是比较慢,有兴趣的话,可以试试Typecho。

    1. 嗯,好。

  10. 来看看。顺便带瓶酱油。

  11. wordpress功能强大啊

    1. 楼上好巧~

  12. 恭喜乔迁。不知道小虾大牛是不是看雪的版主小虾?

  13. 恩,感觉焕然一新,很好啊!

    突然发现我怎么成了活跃评论家了,呵呵。

  14. 的确焕然一新,不过速度变慢了好多。

  15. 从90ju而来....路过留脚印

发表评论

评论会经过自动审核,链接请勿超过 3 个。