【懒人向】如何快速转移一个亿的 Hexo 文章到 Typecho 数据库?
正所谓新年新气象,为了换个心情,同时也为了优化慢的不行的博客,香菇使用了新的博客程序(大雾) Typecho 整合 i / old / note 三站同时作为新年发文载体。可是文章迁移什么的成了头疼的问题,gmlog2 和 note 里边共计 16 篇文章手冻迁移并不是什么大问题,然而采用 Hexo 部署的老站中的 69 篇文章貌似是个稍大的工程,秉承菇星人热爱折腾的优良传统,香菇尝试写了个小工具用作 Hexo 到 Typecho 的文章迁移。
使用全宇宙最好的编程语言 PHP 编写,代码如下:
<?php
$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='123456';
$mysql_database='typecho';
$dir = './_posts';
if(file_exists($dir)){
$files = scandir($dir);
$conn = mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting"); //连接
mysql_query("set names 'utf8'"); //编码
mysql_select_db($mysql_database, $conn); //打开数据库
$cid = 0;
print "<pre>";
foreach($files as $file){
if($file != "." && $file != ".."){
$post = file_get_contents($dir."/".$file);
$infos = explode("---", $post);
//print $infos[0];
$heads = explode("\n", $infos[0]);
$TITLE = str_replace("\r", "", substr($heads[0], 7));
$TIME = strtotime(substr($heads[1], 6));
$SLUG = str_ireplace(".md", "", substr($file, 11));
$lines = explode("\n", $post);
$text = "<!--markdown-->";
for($i = 0; $i < count($lines); $i++){
if($i > 3) {
$text .= $lines[$i]."\n";
}
}
$TEXT = $text;
$sql = "INSERT INTO `typecho_contents` (`title`,`slug`,`created`,`modified`,`text`,`order`,`authorId`,`template`,`type`,`status`,`password`,`commentsNum`,`allowComment`,`allowPing`,`allowFeed`,`parent`) VALUES ('$TITLE','$SLUG',$TIME,$TIME,'$TEXT',0,1,NULL,'post','publish',NULL,0,'1','1','1',0)";
mysql_query($sql);
$isIst = mysql_num_rows(mysql_query("SELECT * FROM `typecho_contents` WHERE `slug`='$SLUG'"));
//print $isIst;
if(!$isIst){
$sql = "INSERT INTO `typecho_contents` (`title`,`slug`,`created`,`modified`,`text`,`order`,`authorId`,`template`,`type`,`status`,`password`,`commentsNum`,`allowComment`,`allowPing`,`allowFeed`,`parent`) VALUES ('$SLUG','$SLUG',$TIME,$TIME,'待编辑!',0,1,NULL,'post','publish',NULL,0,'1','1','1',0)";
mysql_query($sql);
}
$cid += 1;
$sql = "INSERT INTO `typecho_relationships` (`cid`,`mid`) VALUES ($cid,1)";
mysql_query($sql);
print "Down!($cid)\n";
}
}
print "</pre>";
mysql_close($conn); //关闭
}
使用方法:
- 全新安装 Typecho 程序,选用 Mysql 数据库;
- 清空库中 contents 和 relationships 两表的内容;
- 将 Hexo 的文章(markdown文件)目录放到和此代码文件相同目录下;
- 修改好代码中数据库用户名、密码、库名、文章路径、表前缀等信息;
- 用浏览器访问(执行)此代码即可。
注:因为 SQL 拼接的关系,部分数据可能会导入失败,此时该代码会实时将导入失败的文章检测出来并作标记,方便手动调整。
另:代码未考虑性能,大概一个亿的数据是转换不得的。
版权声明:原创内容未经允许请勿转载。
typecho正式版好像还定格在14年,这个对使用有影响吗
没啥影响啊,Typecho主打简洁的,说白了就是个博客架子,扩展性好,模板编辑简单,就看使用者怎么装饰了。
我也是最近迷上了Markdown,搜来搜去还是只有typecho可以用,看到官网的wp转tp的插件还是wp3.几年代的,不知道还能不能正常使用啊
试试呗~
居然完全成功了,new.aotxland.com 又可以开始一波新的折腾了,大神求罩啊
虽然markdown很简洁但是感觉目前各种样式层出不穷也是头疼
好吧,当我没说,问题挺多的