如何在“多说不能导出评论”的情况下愉快的回归 Typecho 原生评论?

多说社评->原生评论

因为写(仿?)了新的主题的关系,再加上多说已经抽风很长时间了,所以顺便想迁移回到 Typecho 的原生评论。

经过在群里声嘶力竭的呼喊终于由老K夫斯基和泽泽发出 Typecho 多说同步插件的链接:http://ysido.com/duoshuo.html,然而并没有什么卵用。

于是香菇又喋喋不休起来,泽泽终于想起另一个多说评论导入 Typecho 插件:https://muguang.me/guff/duoshuo2typecho.html,可惜多说已经“不支持”导出评论 json 了。

怎么办捏?香菇想起每次多说管理后台加载时,评论数据总是延迟显示,这!说!明!多说后台的评论也是通过请求 API 获取的,于是很自然的找到了如下链接(已经强行打码233):

Request URL:http://***.duoshuo.com/api/posts/list.json?order=desc&source=duoshuo%2Crepost&max_depth=1&limit=30&related%5B%5D=thread&related%5B%5D=iplocation&nonce=***&status=all

把以上链接中的limit=30参数值改到比实际评论数大例如limit=999在访问该链接即可获得完整的评论数据:list.json(后来在暮光大佬文章的评论回复中也发现此途径)。

但实际上这样获得的数据和以前直接在多说后台导出的数据格式是不同的,因此并不支持上述第二个插件直接使用,既然这样,就自己写呗:

<?php

class comm{
  var $coid;
  var $cid;
 var $created;
 var $author;
  var $authorId;
  var $ownerId;
 var $mail;
  var $url;
 var $ip;
  var $agent;
 var $text;
  var $type;
  var $status;
  var $parent;
  function __construct($a,$b,$c,$d,$e,$f,$g,$h,$i,$j,$k,$l,$m,$n){
    $this->coid = $a;
   $this->cid = $b;
    $this->created = $c;
    $this->author = $d;
   $this->authorId = $e;
   $this->ownerId = $f;
    $this->mail = $g;
   $this->url = $h;
    $this->ip = $i;
   $this->agent = $j;
    $this->text = $k;
   $this->type = $l;
   $this->status = $m;
   $this->parent = $n;
 }
}

$json = file_get_contents('./list.json');
$obj = json_decode($json);
$res = $obj->response;

$comms = array();
$idaid = array();
$idaid[0] = 0;

$mysql_server_name='localhost';
$mysql_username='root';
$mysql_password='1234';
$mysql_database='typecho';

$pdo = new PDO('mysql:host='.$mysql_server_name.';dbname='.$mysql_database.';port=3306',$mysql_username,$mysql_password);
$pdo->exec('set names utf8'); //编码

for($i = count($res) - 1; $i >= 0; $i--){

  $idaid[$res[$i]->post_id] = count($res) - $i;
 
  $authorId = 0;
  $authorMail = 'default@sucx.cn';
  $authorUrl = null;
  
  // 这里用于对应判定多说中的“注册用户”和本地的注册用户
 if($res[$i]->author->user_id == '11566236'){
    $authorId = 1;
    $authorMail = 'me@sucx.cn';
   $authorUrl = 'http://siitake.cn';
 }elseif($res[$i]->author->user_id == '13293804'){
   $authorId = 2;
    $authorMail = 'mogu@sucx.cn';
   $authorUrl = 'http://siitake.cn';
 }
 $comm = new comm(count($res) - $i,
          $res[$i]->thread->thread_key,
         strtotime($res[$i]->created_at),
          $res[$i]->author->name,
         $authorId,
          1,
          $authorMail,
          $authorUrl,
         $res[$i]->ip,
         $res[$i]->agent,
          $res[$i]->message,
          'comment',
          'approved',
         $idaid[$res[$i]->parent_id]);
 $comms[] = $comm;
 $sql = "INSERT INTO `typecho_comments` (`coid`,`cid`,`created`,`author`,`authorId`,`ownerId`,`mail`,`url`,`ip`,`agent`,`text`,`type`,`status`,`parent`) VALUES ($comm->coid,$comm->cid,$comm->created,'$comm->author',$comm->authorId,$comm->ownerId,'$comm->mail','$comm->url','$comm->ip','$comm->agent','$comm->text','$comm->type','$comm->status',$comm->parent)";
 $pdo->query($sql);
}


$sql = "SELECT `cid` FROM `typecho_contents`";
$cids = $pdo->query($sql)->fetchAll(PDO::FETCH_ASSOC);

foreach ($cids as $cid) {
  $cid = $cid['cid'];
 $sql = "UPDATE `typecho_contents` SET `commentsNum` = (SELECT COUNT(*) FROM `typecho_comments` WHERE `cid` = $cid) WHERE `cid` = $cid";
 $pdo->query($sql);
};


$pdo = null; //关闭

和上次的“Hexo to Typecho”用法相似,先清空评论表typecho_comments数据,然后将该代码保存成.php文件并上传至博客根目录,修改好数据库相关参数,再上传获取到的list.json到相同目录,最后访问(执行)该文件即可。代码执行完成后会自动修正文章评论数。

版权声明:原创内容未经允许请勿转载。

标签: none

已有 53 条评论

  1. 大神,可以啊

  2. 这个字体下的代码真的考验想象力

    1. 果然是这样吧(╯‵□′)╯︵┻━┻

  3. 导出还是没问题的,只是不能导入了

  4. 用多说的时候就经常点同步到数据库,表示完全不方╮( ̄▽ ̄"")╭

    1. 正是由于我没有备份同步的习惯以至于丢过很多次评论数据说起多说停止服务也是在意料之中的事情

  5. 路过……没表情,差评

    1. 只支持纯手冻颜文字输入略略略( ̄y▽ ̄)~

评论已关闭