Guest User

Untitled

a guest
Aug 22nd, 2018
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.90 KB | None | 0 0
  1. <?php
  2.  
  3. /**
  4. * 关于
  5. * 1. 请谨记,本程序使用 GPL v3 授权
  6. * 2. 作者 Zhang-Siyang、香菇
  7. */
  8.  
  9. /**
  10. * 使用步骤
  11. * 1. 补完下方数据库和Markdown文件目录信息
  12. * 2. 保存到站点根目录,名称随意,如 md2typecho.php
  13. * 3. 访问 yourdomain.name/md2typecho.php
  14. */
  15.  
  16. //TODO 从文件读取
  17. $mysql_server_name = '';//服务器地址
  18. $mysql_username = '';//数据库用户名
  19. $mysql_password = '';//数据库密码
  20. $mysql_database = '';//数据库名称
  21. $dir = '';//Markdwon 文件目录
  22.  
  23. if (!file_exists($dir)) {
  24. print("目录 $dir 不存在");
  25. return;
  26. }
  27. try {
  28. $dbh = new PDO("mysql:host=$mysql_server_name;dbname=$mysql_database;charset=utf8", $mysql_username, $mysql_password);
  29. } catch (PDOException $e) {
  30. print("数据库异常 " . $e->getMessage());
  31. return;
  32. }
  33.  
  34. if(0){
  35. //清空文章、评论、分类等,主题保留,设定保留
  36. $dbh->query("TRUNCATE TABLE typecho_relationships;");
  37. $dbh->query("TRUNCATE TABLE typecho_metas;");
  38. $dbh->query("TRUNCATE TABLE typecho_contents;");
  39. }
  40.  
  41. //开始
  42. //获取metas最新元素,为标签入库做准备
  43. $getMaxOrder = $dbh->prepare("SELECT max(typecho_metas.order) AS order_max FROM `typecho_metas`");
  44. $getMaxOrder->execute();
  45. $maxOrder = $getMaxOrder->fetch()[order_max];
  46. if (!$maxOrder) {
  47. //typecho_metas 表为空时,插入一个默认分类
  48. $maxOrder = 1;
  49. $dbh->query("INSERT INTO `typecho_metas` (`name`,`slug`,`type`,`order`) VALUES ('默认分类','default','category',1)");
  50. }
  51. //开始遍历 Markdown 文件
  52. $files = scandir($dir);
  53. if (!$files) {
  54. print("Markdown 文件未找到,当前目录为$dir");
  55. return;
  56. }
  57. //init
  58. $pushPost = $dbh->prepare("INSERT INTO `typecho_contents` (`title`,`slug`,`created`,`modified`,`text`,`authorId`,`type`,`status`,`commentsNum`,`allowComment`,`allowPing`,`allowFeed`) VALUES (:TITLE,:SLUG,:DATE,:DATE,:TEXT,1,'post','publish',0,'1','1','1')");
  59. foreach ($files as $file) {
  60. if ($file[0] == ".") {
  61. continue;
  62. }
  63. $fullDoc = file_get_contents($dir . "/" . $file); //Markdown 文档内容
  64. $slug = str_ireplace(".md", "", substr($file, 9));//slug,用于URL
  65. //按照 Hexo 标准,分出文件头和正文
  66. $docs = explode("---", $fullDoc);
  67. //拿到 标题、时间和标签 的数组
  68. $heads = explode("\n", $docs[1]);
  69. $title = substr($heads[1], 7);
  70. $date = strtotime(substr($heads[2], 6));
  71. $tags = explode(",", trim(substr($heads[3], 6), "[] ,")); //截去日期标志、分隔符并分组
  72. //tag入库
  73. for ($index = 0; $tags[$index] != NULL; $index++) {
  74. ++$maxOrder;
  75. $tags[$index] = trim($tags[$index]);
  76. $checkPush = $dbh->query("INSERT INTO `typecho_metas` (`name`,`slug`,`type`,`order`) VALUES ('$tags[$index]','" . str_replace(" ", "-" , $tags[$index]) . "','category',$maxOrder)");
  77. }
  78. //获取正文内容
  79. $text = "<!--markdown-->" . $docs[2];
  80. //文章入库
  81. $pushPost->execute(array(':TITLE' => $title, ':SLUG' => $slug, ':DATE' => $date, ':TEXT' => $text));
  82. //验证文章入库;
  83. $checkPush = $dbh->prepare("SELECT cid FROM `typecho_contents` WHERE `created`=:DATE");
  84. $checkPush->execute(array(':DATE' => $date));
  85. $cidFromDB = $checkPush->fetch();
  86. if (!$cidFromDB) {
  87. print("导入失败!文件名:" . $file . "\n");
  88. continue;
  89. }
  90. //文章加分类(取第一个tag)
  91. $postID = $cidFromDB[cid];
  92. $getCateMid = $dbh->prepare("SELECT mid FROM `typecho_metas` WHERE `name`=:CATE");
  93. $getCateMid->execute(array(':CATE'=>$tags[0]));
  94. $mid = $getCateMid->fetch()[mid];
  95. if (!$mid) {
  96. $mid = 1;//没有对应的分类,绑到默认分类去
  97. }
  98. $categoryPost = $dbh->query("INSERT INTO `typecho_relationships` (`cid`,`mid`) VALUES ($postID, $mid)");
  99. $updateTheCounterOfCate = $dbh->query("UPDATE `typecho_metas` SET count=count+1 WHERE mid = $mid");
  100. print("好了! " . $file . "\r\n"); //这么说是不是太皮了? ahahahah
  101. }
Add Comment
Please, Sign In to add comment