Instantly share code, notes, and snippets.

Embed
What would you like to do?
将多说评论导入 Disqus
<?php
/*
* 感谢 http://urouge.github.io/migrate-to-disqus/,此脚本来自他的博客
*
* 使用说明:
* 1. 在多说后台下载评论文件,默认文件名是 export.json
* 2. 下载文件 migrate.php,位置与 export.json 同级
* 3. 打开终端,进入文件 migrate.php 所在目录,执行 php -f migrate.php 即可在同级目录生成 disqus.xml 文件
* 4. 在 Disqus 后台选择 Generic(WXR)导入即可,地址是 https://{你的站点名}.disqus.com/admin/discussions/import/platform/wordpress/
* 5. 队列可以在 https://import.disqus.com/ 查看导入状态
*
*
* 注意:
* 1. 导入的用户全是游客身份,并且头像全部丢失,只有默认头像
* 2. 导入的 XML 文件加上 SSO 节点可能能解决游客身份,但是需要 Pro 订阅
*
*
* 可能遇到的坑
* 1. 存在换行的时候可能会报错,可以尝试将 \n 全部替换为 \r
* 2. threads 里 url 为空会报错
* 3. 域名精确匹配的,需要注意最后存在的 /
* 4. 每条评论必须大于 3 字符,否则导入失败
* 5. 上级评论找不到时,会导入失败
*/
header('content-type:text/html; charset=utf-8');
ini_set('date.timezone', 'Asia/Shanghai');
/**
* 获取多说评论
* @var json
*/
$export = file_get_contents('./export.json');
/**
* 转化为数组
* @var array
*/
$threadsAndPosts = json_decode($export, true, 512, JSON_BIGINT_AS_STRING);
/**
* 获取有评论的文章id
* @var array
*/
$postsID = array();
foreach ($threadsAndPosts['posts'] as $v) {
$postsID[] = $v['thread_id'];
}
/**
* 根据条件删除文章
*/
foreach ($threadsAndPosts['threads'] as $threadsKey => $threadsValue) {
if (empty($threadsValue['thread_key']) || !in_array($threadsValue['thread_id'], $postsID)) {
unset($threadsAndPosts['threads'][$threadsKey]);
}
}
/**
* 处理评论关系
*/
foreach ($threadsAndPosts['posts'] as $k => $v) {
if (!empty($v['parents'])) {
$threadsAndPosts['posts'][$k]['parents'] = end($v['parents']);
}
}
$xml = new XMLWriter();
$xml->openMemory();
$xml->setIndent(true);
$xml->setIndentString(' ');
$xml->startDocument('1.0', 'UTF-8', 'yes');
/**
* 根据Disqus官方文档生成xml节点
*/
$xml->startElement('rss');
$xml->writeAttribute('version', '2.0');
$xml->writeAttributeNS('xmlns', 'content', null, 'http://purl.org/rss/1.0/modules/content/');
$xml->writeAttributeNS('xmlns', 'dsq', null, 'http://www.disqus.com');
$xml->writeAttributeNS('xmlns', 'dc', null, 'http://purl.org/dc/elements/1.1/');
$xml->writeAttributeNS('xmlns', 'wp', null, 'http://wordpress.org/export/1.0/');
$xml->startElement('channel');
foreach($threadsAndPosts['threads'] as $threadsKey => $threadsValue) {
// 移除没有 url 的文章
if(empty($threadsValue['url'])){
continue;
}
$xml->startElement('item');
$xml->writeElement('title', $threadsValue['title']);
$xml->writeElement('link', $threadsValue['url']);
$xml->writeElement('wp:content');
$xml->writeElement('dsq:thread_identifier', $threadsValue['thread_key']);
$xml->writeElement('wp:post_date_gmt', date('Y-m-d H:i:s', strtotime($threadsValue['created_at'])));
$xml->writeElement('comment_status', 'open');
foreach($threadsAndPosts['posts'] as $postsKey => $postValue) {
if ($threadsValue['thread_id'] == $postValue['thread_id']) {
$xml->startElement('wp:comment');
$xml->writeElement('wp:comment_id', $postValue['post_id']);
$xml->writeElement('wp:comment_author', $postValue['author_name']);
$xml->writeElement('wp:comment_author_email', $postValue['author_email']);
$xml->writeElement('wp:comment_author_url', $postValue['author_url']);
$xml->writeElement('wp:comment_author_IP', $postValue['ip']);
$xml->writeElement('wp:comment_date_gmt', date('Y-m-d H:i:s',strtotime($postValue['created_at'])));
$xml->startElement('wp:comment_content');
$xml->writeCData(mb_strlen($postValue['message'], 'UTF-8') > 2 ? $postValue['message'] : $postValue['message'].'...');
$xml->endElement();
$xml->writeElement('wp:comment_approved', 1);
$parents = (empty($postValue['parents'])) ? '' : $postValue['parents'];
$xml->writeElement('wp:comment_parent', $parents);
$xml->endElement();
}
}
$xml->endElement();
}
$xml->endElement();
$xml->endElement();
$xml->endDocument();
$output = $xml->outputMemory();
file_put_contents('./disqus.xml', $output);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment