由于多说停止服务,所有有部分使用多说的emlog用户想要将在多说导出的json评论数据倒进自己的博客中,使用本脚本可以实现这个功能。详细代码如下,如果你使用的是emlog博客程序,即可以使用这个脚本进行导入。注意,导入后会丢失评论的父子关系。文章的ID是通过 thread_key 参数指定的,如果你没有开启 thread_key 设置,不要使用这个脚本。本脚本会将评论数据添加到指定文章下。且会自动更新对应文章的评论数。

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
<?php
error\_reporting(0);
header("Content-type:text/html;charset=utf-8");
date\_default\_timezone\_set('Etc/GMT-8');
?>
<form action="?ok=post" method="get">
<input type="hidden" name="ok" value="post">
<div style="margin:30px 0px;height:16px;line-height:16px;">
&nbsp;数据库地址:
<input id="host" name="host" type="text">一般为localhost
</div>
<div style="margin:30px 0px;height:16px;line-height:16px;">
&nbsp;数据库名:<input id="name" name="name" type="text">填写emlog博客所在的数据库
</div>
<div style="margin:30px 0px;height:16px;line-height:16px;">
&nbsp;数据库账号:<input id="user" name="user" type="text">
</div>
<div style="margin:30px 0px;height:16px;line-height:16px;">
&nbsp;数据库密码:<input id="pass" name="pass" type="text">
</div>
<div style="margin:30px 0px;height:16px;line-height:16px;">
&nbsp;数据库前缀:<input id="prefix" name="prefix" type="text">一定要正确,如 emlog\_
</div>
<input style="border:1px solid #ccc;width:112px;height:30px;line-height:30px;background:#fff;" type="submit" value="确定">确认无误后点击确定即可。
</form>
<?php
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 '参数不完整,请填写完整参数<br>';
exit;
}else{
$con = mysql\_connect($host,$user,$pass); //连接数据库
if(!$con){die('不能连接数据库服务器:'.mysql\_error());}
mysql\_select\_db($name,$con);//选择数据库
mysql\_query("set names 'utf8'");
echo '连接数据库成功<br>';
}
//判断数据表是否存在
$sql="show tables like '".$prefix."comment'";
$result = mysql\_query($sql,$con);
if(mysql\_num\_rows($result)){
echo '评论数据表找到<br>';
}else{
echo '<span style="color:#f00;">评论数据表不存在,可能是前缀填写错误</span><br>';
exit;
}
$sql="show tables like '".$prefix."blog'";
$result = mysql\_query($sql,$con);
if(mysql\_num\_rows($result)){
echo '文章数据表找到<br>';
}else{
echo '<span style="color:#f00;">文章数据表不存在,可能是前缀填写错误</span><br>';
exit;
}
echo '开始写入评论数据<br>';
//唯一需要修改的地方,将在多说导出的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.' 更新成功<br>';
$jishu++;
}
echo $jishu.'条评论数据已写入数据库,现在你可以关闭本页面了。<br>';
}