Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- * 关于
- * 1. 请谨记,本程序使用 GPL v3 授权
- * 2. 作者 Zhang-Siyang、香菇
- */
- /**
- * 使用步骤
- * 1. 补完下方数据库和Markdown文件目录信息
- * 2. 保存到站点根目录,名称随意,如 md2typecho.php
- * 3. 访问 yourdomain.name/md2typecho.php
- */
- //TODO 从文件读取
- $mysql_server_name = '';//服务器地址
- $mysql_username = '';//数据库用户名
- $mysql_password = '';//数据库密码
- $mysql_database = '';//数据库名称
- $dir = '';//Markdwon 文件目录
- if (!file_exists($dir)) {
- print("目录 $dir 不存在");
- return;
- }
- try {
- $dbh = new PDO("mysql:host=$mysql_server_name;dbname=$mysql_database;charset=utf8", $mysql_username, $mysql_password);
- } catch (PDOException $e) {
- print("数据库异常 " . $e->getMessage());
- return;
- }
- if(0){
- //清空文章、评论、分类等,主题保留,设定保留
- $dbh->query("TRUNCATE TABLE typecho_relationships;");
- $dbh->query("TRUNCATE TABLE typecho_metas;");
- $dbh->query("TRUNCATE TABLE typecho_contents;");
- }
- //开始
- //获取metas最新元素,为标签入库做准备
- $getMaxOrder = $dbh->prepare("SELECT max(typecho_metas.order) AS order_max FROM `typecho_metas`");
- $getMaxOrder->execute();
- $maxOrder = $getMaxOrder->fetch()[order_max];
- if (!$maxOrder) {
- //typecho_metas 表为空时,插入一个默认分类
- $maxOrder = 1;
- $dbh->query("INSERT INTO `typecho_metas` (`name`,`slug`,`type`,`order`) VALUES ('默认分类','default','category',1)");
- }
- //开始遍历 Markdown 文件
- $files = scandir($dir);
- if (!$files) {
- print("Markdown 文件未找到,当前目录为$dir");
- return;
- }
- //init
- $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')");
- foreach ($files as $file) {
- if ($file[0] == ".") {
- continue;
- }
- $fullDoc = file_get_contents($dir . "/" . $file); //Markdown 文档内容
- $slug = str_ireplace(".md", "", substr($file, 9));//slug,用于URL
- //按照 Hexo 标准,分出文件头和正文
- $docs = explode("---", $fullDoc);
- //拿到 标题、时间和标签 的数组
- $heads = explode("\n", $docs[1]);
- $title = substr($heads[1], 7);
- $date = strtotime(substr($heads[2], 6));
- $tags = explode(",", trim(substr($heads[3], 6), "[] ,")); //截去日期标志、分隔符并分组
- //tag入库
- for ($index = 0; $tags[$index] != NULL; $index++) {
- ++$maxOrder;
- $tags[$index] = trim($tags[$index]);
- $checkPush = $dbh->query("INSERT INTO `typecho_metas` (`name`,`slug`,`type`,`order`) VALUES ('$tags[$index]','" . str_replace(" ", "-" , $tags[$index]) . "','category',$maxOrder)");
- }
- //获取正文内容
- $text = "<!--markdown-->" . $docs[2];
- //文章入库
- $pushPost->execute(array(':TITLE' => $title, ':SLUG' => $slug, ':DATE' => $date, ':TEXT' => $text));
- //验证文章入库;
- $checkPush = $dbh->prepare("SELECT cid FROM `typecho_contents` WHERE `created`=:DATE");
- $checkPush->execute(array(':DATE' => $date));
- $cidFromDB = $checkPush->fetch();
- if (!$cidFromDB) {
- print("导入失败!文件名:" . $file . "\n");
- continue;
- }
- //文章加分类(取第一个tag)
- $postID = $cidFromDB[cid];
- $getCateMid = $dbh->prepare("SELECT mid FROM `typecho_metas` WHERE `name`=:CATE");
- $getCateMid->execute(array(':CATE'=>$tags[0]));
- $mid = $getCateMid->fetch()[mid];
- if (!$mid) {
- $mid = 1;//没有对应的分类,绑到默认分类去
- }
- $categoryPost = $dbh->query("INSERT INTO `typecho_relationships` (`cid`,`mid`) VALUES ($postID, $mid)");
- $updateTheCounterOfCate = $dbh->query("UPDATE `typecho_metas` SET count=count+1 WHERE mid = $mid");
- print("好了! " . $file . "\r\n"); //这么说是不是太皮了? ahahahah
- }
Add Comment
Please, Sign In to add comment