weixin

2019年文章迁移到typecho

日期: January 1, 2019 作者:网站维护

本文由网站技术人员撰写

写了一段程序把之前的hexo上的文章自动写入到新的博客系统的数据库里。

<?php
require 'vendor/autoload.php';
require 'base.php';

use League\HTMLToMarkdown\HtmlConverter;

error_reporting(0);
$filename = trim(fgets(STDIN)); 

$doc = new DOMDocument();
$doc->loadHTMLFile("C:\\Users\\Ac\\Desktop\\mize-mize-master\\mize-master\\casbaidu\\$filename.html");
$divs = $doc->getElementsByTagName('div');
$h1 = $doc->getElementsByTagName('h1');

foreach ($h1 as $c){
    $divClassname = $c->getAttribute('class');
    if ($divClassname  == "article-title") {
        $title=trim($c->textContent);
    }
}

$converter = new HtmlConverter();

$time = $doc->getElementsByTagName('time');
$time = strtotime($time->item(0)->textContent);


foreach ($divs as $c){
    $divClassname = $c->getAttribute('class');
    if ($divClassname  == "article-entry") {
        $html = DOMinnerHTML($c);
        $markdown = $converter->convert($html);
    }
}

$markdown = "<!--markdown-->".$markdown;

$prep = $dbh->prepare("INSERT INTO `typecho_contents` (`cid`, `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) VALUES('',:title,:filename,:date,'',:content,'0','1',NULL,'post','publish',NULL,'0','1','1','1','0');") ;

$prep->execute(array(':title'=>$title,':date'=>$time,':content'=>$markdown, 'filename'=>$filename));

echo "\nPDOStatement::errorInfo():\n";
$arr = $prep->errorInfo();
print_r($arr);

/****/
function DOMinnerHTML(DOMNode $element) 
{ 
    $innerHTML = ""; 
    $children  = $element->childNodes;

    foreach ($children as $child) 
    {
        if ($child instanceof DOMElement){
            if ($child->getAttribute('class') != "article-pop" ) {
                $innerHTML .= $element->ownerDocument->saveHTML($child);
            } 
        }
    }

    return $innerHTML; 
} 
?>  

广告内容为平台自动生成