将多说的json评论数据导入到emlog博客程序的数据库

由于多说将于近期停止服务,所有有部分使用多说的emlog用户想要将在多说导出的json评论数据倒进自己的博客中,使用本脚本可以实现这个功能。详细代码如下,如果你使用的是emlog博客程序,即可以使用这个脚本进行导入。

注意,导入后会丢失评论的父子关系。文章的ID是通过 thread_key 参数指定的,如果你没有开启 thread_key 设置,不要使用这个脚本。本脚本会将评论数据添加到指定文章下。且会自动更新对应文章的评论数。本代码需要修改一个地方,认真看注释!

将多说的json评论数据导入到emlog博客程序的数据库_第1张图片

以下为全部代码,将其保存为php文件上传到服务器执行即可。


error_reporting(0);

header("Content-type:text/html;charset=utf-8");

date_default_timezone_set('Etc/GMT-8');

?>

数据库地址:

一般为localhost

数据库名:填写emlog博客所在的数据库

数据库账号:

数据库密码:

数据库前缀:一定要正确,如 emlog_

确认无误后点击确定即可。


if($_GET['ok'] == 'post'){

$host = $_GET['host'] ? $_GET['host'] : '';

$name = $_GET['name'] ? $_GET['name'] : '';

$user = $_GET['user'] ? $_GET['user'] : '';

$pass = $_GET['pass'] ? $_GET['pass'] : '';

$prefix = $_GET['prefix'] ? $_GET['prefix'] : '';

if($host == '' or  $name == '' or $user == '' or $pass == '' or $prefix == ''){

echo '参数不完整,请填写完整参数
';

exit;

}else{

$con = mysql_connect($host,$user,$pass); //连接数据库

if(!$con){die('不能连接数据库服务器:'.mysql_error());}

mysql_select_db($name,$con);//选择数据库

mysql_query("set names 'utf8'");

echo '连接数据库成功
';

}

//判断数据表是否存在

$sql="show tables like '".$prefix."comment'";

$result = mysql_query($sql,$con);

if(mysql_num_rows($result)){

echo '评论数据表找到
';

}else{

echo '评论数据表不存在,可能是前缀填写错误
';

exit;

}

$sql="show tables like '".$prefix."blog'";

$result = mysql_query($sql,$con);

if(mysql_num_rows($result)){

echo '文章数据表找到
';

}else{

echo '文章数据表不存在,可能是前缀填写错误
';

exit;

}

echo '开始写入评论数据
';

//唯一需要修改的地方,将在多说导出的json数据全部复制到‘’中间,替换其中的内容

$json = '{"generator":"duoshuo","version":"0.1","threads":[{"site_id":1176228,"thread_id":12...}';

$unjson = json_decode($json,true);

$jishu = 0;

$number = count($unjson['posts']);

while($jishu < $number){

$gid = $unjson['posts'][$jishu]['thread_key'] ? $unjson['posts'][$jishu]['thread_key'] : -1;

$pid = 0;

$date = mktime(substr($unjson['posts'][$jishu]['created_at'],11,2),substr($unjson['posts'][$jishu]['created_at'],14,2),substr($unjson['posts'][$jishu]['created_at'],17,2),substr($unjson['posts'][$jishu]['created_at'],5,2),substr($unjson['posts'][$jishu]['created_at'],8,2),substr($unjson['posts'][$jishu]['created_at'],0,4));

if($date == ''){$date = time();}

$poster = $unjson['posts'][$jishu]['author_name'] ? $unjson['posts'][$jishu]['author_name'] : '匿名';

$comment = $unjson['posts'][$jishu]['message'] ? $unjson['posts'][$jishu]['message'] : '';

$mail = $unjson['posts'][$jishu]['author_email'] ? $unjson['posts'][$jishu]['author_email'] : '';

$url = $unjson['posts'][$jishu]['author_url'] ? $unjson['posts'][$jishu]['author_url'] : '';

$ip = $unjson['posts'][$jishu]['ip'] ? $unjson['posts'][$jishu]['ip'] : '';

$hide = 'n';

$sql="INSERT INTO ".$prefix."comment (gid,pid,date,poster ,comment,mail,url,ip,hide) VALUES ({$gid},{$pid},{$date},'{$poster}','{$comment}','{$mail}','{$url}','{$ip}','n')";

mysql_query($sql,$con);//写入评论

$sql="UPDATE ".$prefix."blog SET comnum=comnum+1 WHERE gid = {$gid}";

mysql_query($sql,$con);//更新文章评论数

//echo $jishu.' 更新成功
';

$jishu++;

}

echo $jishu.'条评论数据已写入数据库,现在你可以关闭本页面了。
';

}

你可能感兴趣的:(将多说的json评论数据导入到emlog博客程序的数据库)