导出WordPress留言数据到搜狐畅言所需Json数据

由于最近把手头一个网站前端页面大部分都静态化了,所以交互这块就得交给第三方了,目前看来,多说已经关停,网易还未成长起来,就搜狐畅言还算比较靠谱的。安装插件之后,直接从后台导出wordpress本地评论到畅言出错,网上找来一个脚本挺好用,php写的,我直接贴代码:

<?php #将本文件放到WordPress根目录,访问一次即可在同目录生成对应的Json。可以直接在畅言后台导入,保留盖楼回复关系。 //取数据库留言输出到搜狐畅言Json格式 include("wp-blog-header.php"); $dsn = "mysql:host=".DB_HOST.";dbname=".DB_NAME; $db = new PDO($dsn,DB_USER,DB_PASSWORD); $db->exec("set names utf8");
$rs = $db->query("SELECT distinct(comment_post_ID) FROM `wp_comments`");
if ($db->errorCode() != '00000'){echo join("\t",$db->errorInfo());}
$result = $rs->fetchAll();
$commentArr=array();
$uidarray=array();
foreach($result as $k=>$v){
$postid=$v['comment_post_ID'];
$postlink=get_permalink($postid);
$rs = $db->query("SELECT post_date,guid,post_title FROM `wp_posts` where ID={$postid}");
$post = $rs->fetchAll();
$post=$post[0];
$posttime=strtotime($post['post_date'])*1000;
$rs = $db->query("SELECT * FROM `wp_comments` where comment_post_ID={$postid}");
$comments = $rs->fetchAll();
$tmpc=array();
foreach($comments as $val){
$ctime=strtotime($val['comment_date'])*1000;
if(!isset($uidarray[$val['comment_author']])){$uidarray[$val['comment_author']]=count($uidarray)+1;}
if($val['comment_parent']==0){$val['comment_parent']='';}
$tmpc[]=array(
"cmtid"=>"{$val['comment_ID']}",
"ctime"=>$ctime,
"content"=>"{$val['comment_content']}",
"replyid"=>"{$val['comment_parent']}",
"user"=>array(
"userId"=>"{$uidarray[$val['comment_author']]}",
"nickname"=>"{$val['comment_author']}",
"usericon"=>"",
"userurl"=>"",
"usermetadata"=>array("area"=> "","gender"=> "","kk"=> "","level"=> 1)
),
"ip"=>"{$val['comment_author_IP']}",
"useragent"=>"{$val['comment_agent']}",
"channeltype"=>"1",
"from"=>"",
"spcount"=>"",
"opcount"=>"",
"attachment"=>array(0=>array( "type"=>"1", "desc"=>"","url"=>""))
);
}
$commentArr[]=array(
"title"=>"{$post['post_title']}",
"url"=>"{$postlink}",
"ttime"=>$posttime,
"sourceid"=>"{$postid}",
"parentid"=>"",
"categoryid"=>"",
"ownerid"=>"1",
"metadata"=>"",
"comments"=>$tmpc
);
}
if(file_exists("changyan.json")){unlink("changyan.json");}
$fp=fopen("changyan.json", "a+");
foreach($commentArr as $v){
fwrite($fp,json_encode($v)."\r\n");
}
fclose($fp);
echo "over";

将代码另存为任意名称的php文件,上传到网站根目录,访问这个文件,脚本会自动在同目录下生成changyan.json文件,下载这个json文件并到畅言管理面板导入功能中,将这个文件导入即可。按照畅言后台提示即可管理导入的评论了。

原始脚本存放在(github)上,不过墙最近又在抽风,把github屏蔽了,所以就看我这个代码吧。

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注