Typecho 导出评论为 WXR

Author Avatar
黎明余光 11月3日
  • 在其它设备中阅读本文章

最近看着 SukkaW/DisqusJS 发布了,就想着换上 Disqus, 那样我就可以抛弃掉主题的评论大坑了,好耶

为啥

然而万恶的 Disqus 评论导入只支持 WordPress eXtended RSS (WXR),Typecho 找了一圈也没发现能导出 WXR 的工具,就自己动手了(我永远喜欢 Typecho
目前这个工具已经在主题中实现了,这里放一下主要代码

08011-uqf8vh8p3x.png

Code

Main

header("Content-Type: text/xml");
header('Content-Disposition: attachment; filename="'.Typecho_Widget::widget('Widget_Options')->title.'-comments-wxr-'.gmdate('Y-m-d').'.xml"');
$tool = new Comment_Expert();
$db = Typecho_Db::get();
$query = $db->select('*')->from('table.contents')->where('type = ?', 'post')->orWhere('type = ?', 'page');
$result = $db->fetchAll($query);
foreach ($result as $item) {
    $tool->addBlock($item);
}
$comment_query = $db->select('*')->from('table.comments');
$comments = $db->fetchAll($comment_query);
foreach ($comments as $comment) {
    if ($comment['status'] !== 'spam') {
         $tool->addComment($comment);
    }
}
echo $tool->getResult();

Lib.php

<?php

Class Comment_Expert {
    protected $struct = '<?xml version="1.0" encoding="UTF-8"?>
      <!-- This is a WordPress eXtended RSS file generated from Typecho by idawnlight/typecho-theme-material as an export of comments of your site. -->
<rss version="2.0"
  xmlns:content="http://purl.org/rss/1.0/modules/content/"
  xmlns:dsq="http://www.disqus.com/"
  xmlns:dc="http://purl.org/dc/elements/1.1/"
  xmlns:wp="http://wordpress.org/export/1.0/"
>
  <channel>
  </channel>
</rss>';

    protected $wxr;

    protected $post_blocks;
    protected $comments_blocks;

    public function getResult() {
        $content = $this->wxr->asXML();
        return $content;
    }

    public function addBlock($post) {
        $this->post_blocks[$post['cid']] = $this->wxr->channel->addChild('item');
        $this->post_blocks[$post['cid']]->addChild('title', $post['title']);
        $widget = new arrayToClass(Typecho_Widget::widget('Widget_Abstract_Contents')->push($post));
        $this->post_blocks[$post['cid']]->addChild('link', $widget->permalink);
        $this->post_blocks[$post['cid']]->addChildWithCDATA('encoded', $widget->text, "http://purl.org/rss/1.0/modules/content/");
        $this->post_blocks[$post['cid']]->addChild('thread_identifier', $post['cid'], "http://www.disqus.com/");
        $this->post_blocks[$post['cid']]->addChild('post_date_gmt', gmdate('Y-m-d h:i:s', $widget->created), "http://wordpress.org/export/1.0/");
        $this->post_blocks[$post['cid']]->addChild('comment_status', $widget->allowComment ? 'open' : 'closed', "http://wordpress.org/export/1.0/");
    }

    public function addComment($comment) {
        $comment = new arrayToClass($comment);
        if (isset($this->post_blocks[$comment->cid])) {
            $block = $this->post_blocks[$comment->cid]->addChild('comment', null, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_id', $comment->coid, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_author', $comment->author, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_author_email', $comment->mail, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_author_url', $comment->url, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_author_IP', $comment->ip, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_date_gmt', gmdate('Y-m-d h:i:s', $comment->created), "http://wordpress.org/export/1.0/");
            $block->addChild('comment_id', $comment->coid, "http://wordpress.org/export/1.0/");
            $block->addChildWithCDATA('comment_content', $comment->text, "http://wordpress.org/export/1.0/");
            $block->addChild('comment_approved', $comment->status === 'approved' ? '1' : '0', "http://wordpress.org/export/1.0/");
            $block->addChild('comment_parent', $comment->parent, "http://wordpress.org/export/1.0/");
        }
    }

    public function __construct() {
        $this->wxr = new SimpleXMLElementExtended($this->struct);
    }
}

Class SimpleXMLElementExtended extends SimpleXMLElement {
    /**
     * Adds a child with $value inside CDATA
     * @param string $name
     * @param string $value
     * @param string $namespace
     * @return SimpleXMLElement
     * https://stackoverflow.com/questions/6260224/how-to-write-cdata-using-simplexmlelement
     */
    public function addChildWithCDATA($name, $value = NULL, $namespace = null) {
        $new_child = $this->addChild($name, null, $namespace);

        if ($new_child !== NULL) {
            $node = dom_import_simplexml($new_child);
            $no   = $node->ownerDocument;
            $node->appendChild($no->createCDATASection($value));
        }

        return $new_child;
    }
}

Class arrayToClass {
    private $_data;

    public function __construct(array $properties = []){
        $this->_data = $properties;
    }

    public function __set($property, $value){
        return $this->_data[$property] = $value;
    }

    public function __get($property){
        return array_key_exists($property, $this->_data) ? $this->_data[$property] : null;
    }
}

StackOverflow 真棒

-EOF-

本文链接:https://blog.lim-light.com/archives/expert-typecho-comments-to-wxr.html
本文采用 CC BY-NC-SA 3.0 CN 协议进行许可,阅读 相关说明

    ohmyga
    ohmyga  2018-11-30, 20:40

    我永远爱着Typecho(+10086

    Ice-Hazymoon
    Ice-Hazymoon  2018-11-06, 18:01

    所以来用wordpress吧(

      黎明余光
      黎明余光  2018-11-18, 11:05

      Wordpress 没我 Typecho 快(

        Ghosin
        Ghosin  2018-11-23, 11:56

        所以来用hexo吧 跟disqus超配的(迫真)

          黎明余光
          黎明余光  2018-11-25, 10:09

          不听不听我不听(Hexo 管理太麻烦