从WordPress导出数据到Movable Type的方法

网上有很多从MT导出数据到WP的方法,但反过来则很少,难道人们都喜欢从MT叛逃吗

搜索了老外的一些资料,加上自己按照需求修改了部分代码,成功地实现了从WP导出数据到MT,具体方法如下:

1,新建一个php文件,内容如下:

<?
    header(’Content-type: text/plain’);
    $hostname=”localhost”;
    $username = “databaseuser”;
    $password = “password”;
    $dbname = “dbname”;
    $db_con = mysql_connect($hostname,$username, $password) or die(”Connetion to database failed!”);
    mysql_select_db($dbname);
    mysql_query(”SET NAMES ‘utf8′”);
    $query = “SELECT
    `wp_posts`.`post_date`,
    `wp_posts`.`post_content`,
    `wp_posts`.`post_title`,
    `wp_posts`.`post_name`,
    `wp_users`.`user_nicename`,
    `wp_post2cat`.`category_id`,
    `wp_categories`.`cat_name`,
    `wp_comments`.`comment_author`,
    `wp_comments`.`comment_content`,
    `wp_comments`.`comment_author_email`,
    `wp_comments`.`comment_author_url`,
    `wp_comments`.`comment_author_IP`,
    `wp_comments`.`comment_date`
    FROM
    `wp_comments`
    RIGHT OUTER JOIN `wp_posts` ON (`wp_comments`.`comment_post_ID` = `wp_posts`.`ID`)
    LEFT OUTER JOIN `wp_users` ON (`wp_posts`.`post_author` = `wp_users`.`ID`)
    INNER  JOIN  `wp_post2cat`  ON (  `wp_posts`.`ID`  =  `wp_post2cat`.`post_ID`  )
    LEFT  OUTER  JOIN  `wp_categories`  ON (  `wp_categories`.`cat_ID`  =  `wp_post2cat`.`category_ID`  )
    “;
    $result = mysql_query($query);
    if ($result) {
        $numOfRows = mysql_num_rows ($result);
        for ($i = 0; $i < $numOfRows; $i++) {
            if ($prev_entry == mysql_result($result, $i, “post_date”)) {
                print_comment($result, $i);
            } else {       
                print_post($result, $i);
                $prev_entry = mysql_result($result, $i, “post_date”);
                if (mysql_result($result, $i, “comment_content”) != “”) {
                    #echo (”—–\n”);
                    print_comment($result, $i);
                }
            }
        }
    }
    mysql_close ();
    function print_post($result, $i) {
        echo (”——–\n”);
            echo (”AUTHOR: “).stripslashes(mysql_result($result, $i, “user_nicename”)).”\n”;
            echo (”TITLE: “).stripslashes(mysql_result($result, $i, “post_title”)).”\n”;
            echo (”STATUS: Publish\n”);
            echo (”ALLOW COMMENTS: 2\n”);
            //echo (”CONVERT BREAKS: markdown\n”);
            echo (”ALLOW PINGS: 0\n”);
            echo (”CATEGORY: “).stripslashes(mysql_result($result, $i, “cat_name”)).”\n”;
            echo (”DATE: “).date(”m/d/Y h:m:s A”,(strtotime(mysql_result($result, $i, “post_date”)))).”\n”;
            echo (”—–\n”);
            echo (”KEYWORDS:\n”);
            echo stripslashes(mysql_result($result, $i, “post_name”)).”\n”;
            echo (”—–\n”);
            echo (”BODY:\n”);
            echo stripslashes(str_replace(”<br />”, “”, mysql_result($result, $i, “post_content”))).”\n”;
            echo (”—–\n”);
    }

    function print_comment($result, $i) {
        $text=stripslashes(str_replace(”<br />”, “”, mysql_result($result, $i, “comment_content”))).”\n”;
        if (0 === strpos($text,’<trackback />’)) {
            preg_match (”!<trackback /><strong>(.+)</strong>\n(.+)!”,$text,$matches);
            echo (”PING:\n”);
            echo (”BLOG NAME: “).stripslashes(mysql_result($result, $i, “comment_author”)).”\n”;
            echo (”URL: “).stripslashes(mysql_result($result, $i, “comment_author_url”)).”\n”;
            echo (”TITLE: $matches[1]\n”);
            $text = “$matches[2]\n”;
        } else {
            echo (”COMMENT:\n”);
            echo (”AUTHOR: “).stripslashes(mysql_result($result, $i, “comment_author”)).”\n”;
            echo (”EMAIL: “).stripslashes(mysql_result($result, $i, “comment_author_email”)).”\n”;
            echo (”URL: “).stripslashes(mysql_result($result, $i, “comment_author_url”)).”\n”;
        }
        echo (”IP: “).stripslashes(mysql_result($result, $i, “comment_author_ip”)).”\n”;
        echo (”DATE: “).date(”m/d/Y h:m:s A”,(strtotime(mysql_result($result, $i, “post_date”)))).”\n”;
        echo $text;
        echo (”—–\n”);
    }
?>

2,修改文件前面几行的代码,将自己WP数据库的连接参数写入。

3,将此php文件上传到你的WP服务器上,通过浏览器访问,将会出现下载php文件提示。将下载的文件保存到本地。

4,登陆MT后台,选择导入数据,然后选择导入刚才下载的文件即可。

 需要注意以下几点:

1,搞清楚你的WP和MT所使用的mysql数据库的编码类型,如果二者不同,则导入导出时要注意编码问题。我的WP和MT都为utf-8编码,所以无需编码转换。如果你使用的数据库编码不是utf-8,则修改此php代码的这一句:

    mysql_query(”SET NAMES ‘utf8′”);

将其改成你的数据库的相应编码。

2,可以通过修改function print_post函数中的这些echo语句来控制导出数据的某些参数,比如我的WP中的文章已经用br换行了,所以我注释掉了echo (”CONVERT BREAKS: markdown\n”);这一句来避免出现导出的数据过滤br而无法断行。

Post a Comment

Your email is never published nor shared.