diff --git a/emlog2duoshuo.php b/emlog2duoshuo.php new file mode 100644 index 0000000..415017e --- /dev/null +++ b/emlog2duoshuo.php @@ -0,0 +1,240 @@ +'duoshuo','version'=>'0.1'); + } + $ds_split[$key][] = $v; + if($ds_count==SPLIT_SIZE || $arr_count==$arr_len){ + $ds_split = json_encode($ds_split); + file_put_contents('content/backup/emlog_comment_'.$file_count.'.json',$ds_split); + ++$file_count; + unset($ds_split); + $ds_count = 0; + } + ++$ds_count; + ++$arr_count; + } + //返回最后一个当前已保存的文件序号 + return $file_count-1; + } + +/* 程序界面 */ + + //指定编码 + header('content-type:text/html; charset=utf-8'); + + //默认页面 + if(empty($_GET)){ +?> + +
☆ 到作者博客
+提示:为了您的数据安全,建议您操作完成后删除本程序。
+提示:对于评论量较大的用户,建议您选择分卷保存到服务器。
+ 当前程序以条评论进行分割,生成的每个分卷应该均不超过200K。
+ 如果实际生成的分卷过小或过大,您可以手动修改本程序的分割参数。
+ 导出文件保存地址:emlog目录/content/backup/emlog_comment_序号.json
性能测试结果:
'; + $str .= '您网站中的评论数量为:'.$test['count'].'
实际查询数量为:'.$test['db']['count'].'
有评论的文章数量为:'.$test['gid'].'
实际查询数量为:'.$test['th']['count'].'
编码后的json结束符检查:'.$json_check.'
'; + $str .= '内存使用:最大值('. $mem.'K) 当前('.$mem_curr.'K)
'; + $str .= ''; + echo $str; + + exit; + } diff --git a/emlog2typecho3.py b/emlog2typecho3.py new file mode 100644 index 0000000..043e8fc --- /dev/null +++ b/emlog2typecho3.py @@ -0,0 +1,163 @@ +#! /usr/bin/env python +# -*- coding: utf-8 -*- +# +# Descrption:emlog2typecho 是一个用python写的脚本,用来迁移Emlog的数据库到Typecho。 +# + + +# 一些设置项 + +# Emlog 数据库名 +emlog_database_name = 'emlog' +# Typecho 数据库名 +typecho_database_name = 'typecho' +# 数据库地址 +database_host = 'localhost' +# 数据库用户名 +database_port = 3306 +# 数据库用户名 +database_user_name = 'root' +# 数据库用户名 +database_user_password = 'root' +# 字符集 +database_charset = 'utf8' + +################################################################################# +import mysql.connector #用mysql.connector替换MySQLdb +#import MySQLdb + +# 连接数据库... +conn=mysql.connector.connect(host = database_host, + user = database_user_name, + passwd = database_user_password, + port = database_port, + charset = database_charset, + database = emlog_database_name) + +# 切换emlog数据库... +#conn.select_db(emlog_database_name) +cur=conn.cursor() + +# 读取emlog所有分类 +cur.execute('select sid, sortname, alias, ( select count( * ) from emlog_blog where sortid = sid ) AS count FROM emlog_sort') +emlog_sort_list = cur.fetchall() + +# 读取Emlog所有Tag +cur.execute('select tid,tagname,gid from emlog_tag') +emlog_tag_list = [] +for row in cur.fetchall(): + tagname = row[1] + gid_list = row[2].split(',') + + # 移除列表中为空字符串的项 + for gid in gid_list: + if gid == '': + gid_list.remove(gid) + # 组装 + tag = {'tagname':tagname,'gidlist':gid_list} + emlog_tag_list.append(tag) + +# 读取emlog blog表... +cur.execute('select gid,title,date,content,excerpt,alias,sortid,type,allow_remark from emlog_blog') +emlog_blog_list = cur.fetchall() + +# 读取Emlog comment表 +cur.execute("SELECT * FROM `emlog_comment`") +emlog_comment_list = cur.fetchall() + +# ------------------------------------------ +# --- Emlog表读取完毕,切换Typecho表进行写入 --- +# ------------------------------------------ + +# 切换Typecho数据库... +#conn.select_db(typecho_database_name) +conn=mysql.connector.connect(host = database_host, + user = database_user_name, + passwd = database_user_password, + port = database_port, + charset = database_charset, + database = typecho_database_name) +cur=conn.cursor() + +# 删除Typecho 所有分类和标签... +cur.execute('delete from typecho_metas') + +# 插入emlog所有分类 +for sort in emlog_sort_list: + sort_id = sort[0] + sort_name = sort[1] + sort_sulg = sort[2] # sort[0] if sort[1] == '' else sort[1] + sort_count = sort[3] + cur.execute("insert into typecho_metas (mid, name, slug, type, description, `count`, `order`) VALUES (%s, %s, %s, 'category', NULL, %s, 0)" , (sort_id,sort_name, sort_sulg,sort_count)) + +# 删除Typecho 所有文章... +cur.execute('delete from typecho_contents') +# 删除文章所有关系 +cur.execute('delete from typecho_relationships') +# 删除所有评论 +cur.execute('delete from typecho_comments') + +# 转移所有文章 +for blog in emlog_blog_list: + print(blog[0],"Done!") + blog_id = blog[0] + blog_title = blog[1] + blog_create_date = blog[2] + blog_content = blog[3] + blog_excerpt = blog[4] + + # 不能为空字符串 + blog_alias = blog[5] + if blog_alias == '': + blog_alias = None + + # emlog --> blog page + # typecho --> post page + if blog[7] == 'blog': + blog_type = 'post' + else: + blog_type = 'page' + + # allow comment + if blog[8] == 'y': + blog_allow_comment = '1' + else: + blog_allow_comment = '0' + + params = (blog_id,blog_title,blog_alias,blog_create_date,blog_content,blog_type,blog_allow_comment) + cur.execute("insert into `typecho_contents` (`cid`, `title`, `slug`, `created`, `modified`, `text`, `order`, `authorId`, `template`, `type`, `status`, `password`, `commentsNum`, `allowComment`, `allowPing`, `allowFeed`, `parent`) VALUES (%s, %s, %s, %s, NULL, %s, '0', '1', NULL, %s, 'publish', NULL, '0', %s, '0', '0', '0')",params) + + # 添加文章的relationships + blog_sortid = blog[6] + + # emlog 中 分类id -1 为页面 + if blog_sortid == -1: + continue + cur.execute('insert into `typecho_relationships` (`cid`, `mid`) VALUES (%s, %s)',(blog_id,blog_sortid)) + +# 插入所有Tag(和关系) +cur.execute("select MAX( mid ) FROM `typecho_metas`") +sort_max_id = (cur.fetchall()[0][0]) + 1 + +# 从刚插入的分类最后一个ID+1作为ID开始循环插入 +for tag in emlog_tag_list: + cur.execute("insert into `typecho_metas` (`mid`, `name`, `slug`, `type`, `description`, `count`, `order`) VALUES (%s, %s, %s, 'tag', NULL, %s, '0');",(sort_max_id,tag['tagname'],tag['tagname'],len(tag['gidlist']))) + for gid in tag['gidlist']: + params = (int(gid),sort_max_id) + # !有时会遇到重复项插入失败跳过 + try: + cur.execute('insert into `typecho_relationships` (`cid`, `mid`) VALUES (%s, %s)',params) + except: + print('失败一条Tag:%s,%s' % (params)) + sort_max_id = sort_max_id + 1 + +# 插入评论 +for comment in emlog_comment_list: + params = (comment[0],comment[1],comment[3],comment[4],comment[6],comment[7],comment[8],comment[5],comment[2]) + cur.execute("INSERT INTO `typecho_comments` (`coid`, `cid`, `created`, `author`, `authorId`, `ownerId`, `mail`, `url`, `ip`, `agent`, `text`, `type`, `status`, `parent`) VALUES (%s, %s, %s, %s, '0', '1', %s, %s, %s, NULL, %s, 'comment' , 'approved', %s)",params) + +# 关闭数据库连接 +cur.close() +conn.close() + +print('转移完成...')