Movable Type悲剧了,不知道咋的,Perl整不起来。

折腾好几年了,Blog也一直不能更新。

终于想放弃Movable Type了。

但是Blog怎么迁移呢?打不开Movable Type的后台。

还好有数据库在,还好WordPress可以导入。

WordPress的安装很简单,Movable Type太变态了。

因为不知道Movable Type的导出格式,只能先下载一份WordPress的备份xml,依葫芦画瓢吧。

上代码吧

<?php
ini_set('display_errors', true);
error_reporting(E_ALL);

header("Content-type:text/xml");
echo('<?xml version=\'1.0\' encoding=\'UTF-8\'?>'."\n");
?>
<!-- This is a WordPress eXtended RSS file generated by WordPress as an export of your site. -->
<!-- It contains information about your site's posts, pages, comments, categories, and other content. -->
<!-- You may use this file to transfer that content from one site to another. -->
<!-- This file is not intended to serve as a complete backup of your site. -->
<!-- To import this information into a WordPress site follow these steps: -->
<!-- 1. Log in to that site as an administrator. -->
<!-- 2. Go to Tools: Import in the WordPress admin panel. -->
<!-- 3. Install the "WordPress" importer from the list. -->
<!-- 4. Activate & Run Importer. -->
<!-- 5. Upload this file using the form provided on that page. -->
<!-- 6. You will first be asked to map the authors in this export file to users -->
<!-- on the site. For each author, you may choose to map to an -->
<!-- existing user on the site or to create a new user. -->
<!-- 7. WordPress will then import each of the posts, pages, comments, categories, etc. -->
<!-- contained in this file into your site. -->
<!-- generator="WordPress/4.9.6" created="<?php echo(date('Y-m-d H:i')); ?>" -->
<rss version="2.0"
	xmlns:excerpt="http://wordpress.org/export/1.2/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.2/"
>
<channel>
	<title>David&#039;s Blog</title>
	<link>http://wangblog.org/wordpress</link>
	<description>Just another WordPress site</description>
	<pubDate><?php echo(date(DATE_RFC2822)); ?></pubDate>
	<language>en-US</language>
	<wp:wxr_version>1.2</wp:wxr_version>
	<wp:base_site_url>http://wangblog.org/wordpress</wp:base_site_url>
	<wp:base_blog_url>http://wangblog.org/wordpress</wp:base_blog_url>

	<wp:author><wp:author_id>1</wp:author_id><wp:author_login><![CDATA[david]]></wp:author_login><wp:author_email><![CDATA[david.inv@gmail.com]]></wp:author_email><wp:author_display_name><![CDATA[david]]></wp:author_display_name><wp:author_first_name><![CDATA[]]></wp:author_first_name><wp:author_last_name><![CDATA[]]></wp:author_last_name></wp:author>

<?php
$pdo = new PDO("mysql:host=localhost;dbname=***", 'username', 'userpassword'); /* 连接MT数据库 */

$sql = "SELECT * FROM `mt_category` WHERE `category_blog_id`=2";
$stmt = $pdo->query($sql);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $row){
	echo("<wp:category>\n");
	echo("	<wp:term_id>".$row['category_id']."</wp:term_id>\n");
	echo("	<wp:category_nicename><![CDATA[".$row['category_basename']."]]></wp:category_nicename>\n");
	echo("	<wp:category_parent><![CDATA[".$row['category_description']."]]></wp:category_parent>\n");
	echo("	<wp:cat_name><![CDATA[".$row['category_label']."]]></wp:cat_name>\n");
	echo("</wp:category>\n");
}

$sql = "SELECT t.tag_id,t.tag_name FROM `mt_tag` t,`mt_objecttag` o WHERE t.tag_id=o.objecttag_tag_id AND o.objecttag_blog_id=2 GROUP BY t.tag_id ORDER BY o.objecttag_tag_id";
$stmt = $pdo->query($sql);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $row){
	echo("<wp:tag>\n");
	echo("	<wp:term_id>10".$row['tag_id']."</wp:term_id>\n"); /* 怕ID跟WP冲突,前面加10 */
	echo("	<wp:tag_slug><![CDATA[".str_replace(' ', '-', $row['tag_name'])."]]></wp:tag_slug>\n");
	echo("	<wp:tag_name><![CDATA[".$row['tag_name']."]]></wp:tag_name>\n");
	echo("</wp:tag>\n");
}

echo("\n<generator>https://wordpress.org/?v=4.9.6</generator>\n\n");

$sql = "SELECT * FROM `mt_entry` WHERE `entry_blog_id`=2 ORDER BY `entry_authored_on` LIMIT 0,3000";
$stmt = $pdo->query($sql);
$rs = $stmt->fetchAll(PDO::FETCH_ASSOC);
foreach($rs as $row){
	echo("<item>\n");
	echo("	<title>".htmlspecialchars($row['entry_title'], ENT_QUOTES)."</title>\n");
	echo("	<link>http://wangblog.org/wordpress/index.php/".date('Y/m/d', strtotime($row['entry_authored_on']))."/".$row['entry_basename']."/</link>\n");
	echo("	<pubDate>".date(DATE_RFC2822, strtotime($row['entry_authored_on']))."</pubDate>\n");
	echo("	<dc:creator><![CDATA[david]]></dc:creator>\n");
	echo("	<guid isPermaLink=\"false\">http://wangblog.org/wordpress/?p=10".$row['entry_id']."</guid>\n");
	echo("	<description></description>\n");
	echo("	<content:encoded><![CDATA[".$row['entry_text']);
	if($row['entry_text_more']){
		echo("<!--more-->".$row['entry_text_more']);
	}
	echo("]]></content:encoded>\n");
	echo("	<excerpt:encoded><![CDATA[]]></excerpt:encoded>\n");
	echo("	<wp:post_id>10".$row['entry_id']."</wp:post_id>\n");
	echo("	<wp:post_date><![CDATA[".date('Y-m-d H:i:s', strtotime($row['entry_authored_on']))."]]></wp:post_date>\n");
	echo("	<wp:post_date_gmt><![CDATA[".date('Y-m-d H:i:s', strtotime($row['entry_authored_on']))."]]></wp:post_date_gmt>\n");
	echo("	<wp:comment_status><![CDATA[open]]></wp:comment_status>\n");
	echo("	<wp:ping_status><![CDATA[open]]></wp:ping_status>\n");
	echo("	<wp:post_name><![CDATA[".$row['entry_basename']."]]></wp:post_name>\n");
	if($row['entry_status'] == 2)
		echo("	<wp:status><![CDATA[publish]]></wp:status>\n");
	else
		echo("	<wp:status><![CDATA[draft]]></wp:status>\n");
	echo("	<wp:post_parent>0</wp:post_parent>\n");
	echo("	<wp:menu_order>0</wp:menu_order>\n");
	if($row['entry_status'] == 2){
		if($row['entry_class'] == 'page')
			echo("	<wp:post_type><![CDATA[page]]></wp:post_type>\n");
		else
			echo("	<wp:post_type><![CDATA[post]]></wp:post_type>\n");
	}else{
		echo("	<wp:post_type><![CDATA[page]]></wp:post_type>\n");
	}
	echo("	<wp:post_password><![CDATA[]]></wp:post_password>\n");
	echo("	<wp:is_sticky>0</wp:is_sticky>\n");
	$sql = "SELECT t.tag_name FROM `mt_objecttag` o,`mt_tag` t WHERE o.objecttag_object_id=".$row['entry_id']." AND o.objecttag_tag_id=t.tag_id AND t.tag_id<>3 ORDER BY t.tag_id"; /* t.tag_id<>3 这是我要过滤掉的一个tag */
	$stmt3 = $pdo->query($sql);
	$rs3 = $stmt3->fetchAll(PDO::FETCH_ASSOC);
	foreach($rs3 as $row3){
		echo("	<category domain=\"post_tag\" nicename=\"".str_replace(' ', '-', $row3['tag_name'])."\"><![CDATA[".$row3['tag_name']."]]></category>\n");
	}
	$sql = "SELECT c.category_basename,c.category_label FROM `mt_placement` p,`mt_category` c WHERE p.placement_entry_id=".$row['entry_id']." AND p.placement_category_id=c.category_id ORDER BY c.category_id";
	$stmt4 = $pdo->query($sql);
	$rs4 = $stmt4->fetchAll(PDO::FETCH_ASSOC);
	foreach($rs4 as $row4){
		echo("	<category domain=\"category\" nicename=\"".$row4['category_basename']."\"><![CDATA[".$row4['category_label']."]]></category>\n");
	}
	if($row['entry_comment_count']>0){
		$sql = "SELECT * FROM `mt_comment` WHERE `comment_entry_id`=".$row['entry_id']." ORDER BY `comment_created_on`";
		$stmt2 = $pdo->query($sql);
		$rs2 = $stmt2->fetchAll(PDO::FETCH_ASSOC);
		foreach($rs2 as $row2){
			echo("<wp:comment>\n");
			echo("	<wp:comment_id>".$row2['comment_id']."</wp:comment_id>\n");
			echo("	<wp:comment_author><![CDATA[".$row2['comment_author']."]]></wp:comment_author>\n");
			echo("	<wp:comment_author_email><![CDATA[".$row2['comment_email']."]]></wp:comment_author_email>\n");
			echo("	<wp:comment_author_url><![CDATA[".$row2['comment_url']."]]></wp:comment_author_url>\n");
			echo("	<wp:comment_author_IP><![CDATA[".$row2['comment_ip']."]]></wp:comment_author_IP>\n");
			echo("	<wp:comment_date><![CDATA[".date('Y-m-d H:i:s', strtotime($row2['comment_created_on']))."]]></wp:comment_date>\n");
			echo("	<wp:comment_date_gmt><![CDATA[".date('Y-m-d H:i:s', strtotime($row2['comment_created_on']))."]]></wp:comment_date_gmt>\n");
			echo("	<wp:comment_content><![CDATA[".$row2['comment_text']."]]></wp:comment_content>\n");
				if($row2['comment_junk_status'] == -1){
					$row2['comment_junk_status'] = 'spam';
				}else{
					if($row2['comment_visible'] == 1)
						$row2['comment_junk_status'] = 1;
					else
						$row2['comment_junk_status'] = 0;
				}
			echo("	<wp:comment_approved><![CDATA[".$row2['comment_junk_status']."]]></wp:comment_approved>\n");
			echo("	<wp:comment_type><![CDATA[]]></wp:comment_type>\n");
			echo("	<wp:comment_parent>0</wp:comment_parent>\n");
			echo("	<wp:comment_user_id>0</wp:comment_user_id>\n");
			echo("</wp:comment>\n");
		}
	}
	echo("</item>\n");
}

//$db->commit();
?>
</channel>
</rss>

代码结束,MT数据库有些字段的意思,自己去看看去研究研究。

OK

2 Comments

  • david

    WordPress的导入导出就不用我说了吧,网上教材一大堆!

  • This can however depend a great deal in your particular needs and what type of a player you
    are.

Leave a Reply