Movable Type
Movable Type

MT是我使用时间最长的一个博客程序。它基于Perl,功能强大而且可靠性十分稳妥。然而,在快餐文化异常普及的当下,它显得有点疲态百出。它没有php程序的灵活、部署简易,直白点说是折腾。因为站点移到国外的VPS的原因,国内连接速度实在不敢恭维,因此MT也成了我困难丛丛的选择。无奈只能转投效率优先的typecho。

因为博客思路设计的差异,我仅仅转换了原来的文章及相关评论。涉及到的数据表也只有3个。有需要的朋友可以参考下。将下面的源码保存为php文件,放在typecho博客根目录然后运行即可。(建议做好相关数据库备份后,在本地转换成功后再上传)

    <?php
    /**
     * Typecho Blog Platform
     *
     * @copyright  Copyright (c) 2008 Typecho team (http://www.typecho.org)
     * @license    GNU General Public License 2.0
     * @version    $Id: index.php 1153 2017-03-12 10:53:22Z static.ezo.biz $
     */
    
    /** 载入配置支持 */
    if (!defined('__TYPECHO_ROOT_DIR__') && !@include_once 'config.inc.php') {
        file_exists('./install.php') ? header('Location: install.php') : print('Missing Config File');
        exit;
    }
    
    /** 初始化组件 */
    #Typecho_Widget::widget('Widget_Init');
    $d_MT = "blog";
    /**
     * 初始化mt数据库
     */
    $dbMT = new Typecho_Db('Mysql', 'mt_');
    $dbMT->addServer(array (
       'host' => '127.0.0.1',
       'user' => 'root',
       'password' => '123456',
       'charset' => 'utf8',
       'port' => '3306',
       'database' => $d_MT,
     ), Typecho_Db::READ | Typecho_Db::WRITE);
     Typecho_Db::set($dbMT);
    //清空原有数据
    $db->query("DELETE FROM `typecho_contents`;");
    $db->query("DELETE FROM `typecho_relationships`;");
    $db->query("DELETE FROM `typecho_comments`;");
    
    $a=$dbMT->query("SELECT entry_id,entry_created_on,entry_modified_on,entry_title,Entry_text,entry_text_more,entry_basename FROM mt_entry;");
    $posts = $dbMT->fetchAll($a);
    foreach($posts as $post){
      unset($sql);
      $post['entry_created_on'] = strtotime($post['entry_created_on']);
      $post['entry_modified_on'] = strtotime($post['entry_modified_on']);
      if(!empty($post['entry_text_more']) || ($post['entry_text_more'] !="")){
        $post['Entry_text'] = $post['Entry_text'].$post['entry_text_more'];
      }
      $post['Entry_text'] = preg_replace('/(http|https)(.*?)(ezo\.biz\/b\/).*?(\/)((?:[a-z0-9_-|\.|-]*)(\.)(gif|jpeg|jpg|png))/i', "https://static.ezo.biz/b_img/$5", $post['Entry_text']);
      $post['Entry_text'] = str_replace("http://static.ezo.biz","https://static.ezo.biz",$post['Entry_text']);
      $post['Entry_text'] = str_replace("http://www.static.ezo.biz","https://www.static.ezo.biz",$post['Entry_text']);
      $post['Entry_text'] = mysql_real_escape_string($post['Entry_text']);
      $post['entry_title'] = mysql_real_escape_string($post['entry_title']);
    
    
      /**
       * 修复typecho slug主键唯一的问题
       */
      $d = gmdate('Ymd',$post['entry_created_on']);
      $post['entry_basename'] = $post['entry_basename']."-$d";
    
    
      $sql = "INSERT INTO `typecho_contents` (`cid`, `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`, `views`, `viewsNum`, `likesNum`)  VALUES (NULL, '$post[entry_title]', '$post[entry_basename]', '$post[entry_created_on]', '$post[entry_modified_on]', '$post[Entry_text]', '0', '1', NULL, 'post', 'publish', NULL, '0', '1', '1', '1', '0', '0', '0', '0');";
      //返回文章id
      $cid = $db->query($sql,Typecho_Db::WRITE,Typecho_Db::INSERT);
      //绑定目录
      $db->query("INSERT INTO `typecho_relationships` (`cid`, `mid`) VALUES ('$cid', '1');");
    
      /**
       * 转换评论
       */
      $b = $dbMT->query("SELECT comment_author,comment_created_on,comment_email,comment_entry_id,comment_ip,comment_text,comment_url FROM mt_comment WHERE comment_visible=1 AND comment_entry_id=$post[entry_id];");
      $comments = $dbMT->fetchALL($b);
      foreach ($comments as $comment) {
        # 批量导入评论
        $comment['comment_created_on'] = strtotime($comment['comment_created_on']);
        $db->query("INSERT INTO `typecho_comments` (`coid`, `cid`, `created`, `author`, `authorId`, `ownerId`, `mail`, `url`, `ip`, `agent`, `text`, `type`, `status`, `parent`) VALUES (NULL, '$cid', '$comment[comment_created_on]', '$comment[comment_author]', '0', '0', '$comment[comment_email]', '$comment[comment_url]', '$comment[comment_ip]', NULL, '$comment[comment_text]', 'comment', 'approved', '0');");
      }
    }