This repository has been archived by the owner on Mar 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmigrator-comment.js
285 lines (263 loc) · 8.15 KB
/
migrator-comment.js
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use strict';
const fs = require('fs');
const yaml = require('js-yaml');
const md5 = require('blueimp-md5');
const { Sequelize } = require("sequelize");
const marked = require('marked');
// 设置Markdown解析参数
marked.setOptions({
renderer: new marked.Renderer(),
gfm: true,
tables: true,
breaks: true,
pedantic: false,
sanitize: true,
smartLists: true,
smartypants: true,
silent: true
});
// 获取配置
var config = yaml.safeLoad(fs.readFileSync(process.argv[2] ? process.argv[2] : 'config.yml'));
// 连接数据库
var database = new Sequelize(Object.assign({ logging: false }, config.source));
/**
* 格式化文本
* @param {object} args 关键字
* @returns {string} 格式化结果
*/
String.prototype.format = function (args) {
let result = this;
for (let arg in args) {
result = result.replace(new RegExp(`{${arg}}`, 'g'), args[arg]);
}
return result;
}
/**
* 计算哈希值
* @param {string} value 值
* @returns {string} 哈希值
*/
function hash(value) {
if (!value) return null;
let prefix = config.salt ? `${config.salt}-` : '';
return md5(`${prefix}${value}`);
}
/**
* 为数字补零
* @param {int} number 数字
* @param {int} count 位数
* @returns {string} 补零结果
*/
function fillZero (number, count) {
return (Array(count).join(0) + number).slice(-count);
}
/**
* 按配置文件替换缩略名
* @param {string} slug 缩略名
* @returns {string} 替换结果
*/
function slugReplace(slug) {
if (config.slugReplace && config.slugReplace[slug]) {
return config.slugReplace[slug];
} else {
return slug;
}
}
/**
* 在数据库执行SQL语句
* @param {string} sql SQL语句
* @returns {Promise<object>} 执行结果
*/
async function executeSQL(sql) {
return (await database.query(sql))[0];
}
/**
* 获取忽略列表以外的全部评论
* @returns {Promise<object>} 评论
*/
async function getComments() {
let where = config.ignoreCID && config.ignoreCID.length > 0 ? ` WHERE cid NOT IN(${config.ignoreCID.join(',')})` : '';
let results = await executeSQL(`SELECT * FROM \`${config.prefix}comments\`${where}`);
return results;
}
/**
* 获取分类元数据
* @param {int} mid 元数据ID
* @returns {Promise<object>} 分类元数据
*/
async function getCategoryMeta(mid) {
if (mid.length === 0) return [];
return await executeSQL(`SELECT * FROM \`${config.prefix}metas\` WHERE \`mid\` IN(${mid}) AND \`type\`="category"`);
}
/**
* 通过内容获取关联的分类元数据
* @param {int} cid 内容ID
* @returns {Promise<object>} 分类元数据
*/
async function getCategoryMetaFromCID(cid) {
if (cid.length === 0) return [];
let relationships = await executeSQL(`SELECT * FROM \`${config.prefix}relationships\` WHERE \`cid\` IN(${cid})`);
let mids = [];
for (let relationship of relationships) {
mids.push(relationship.mid);
}
return await getCategoryMeta(mids.join(','));
}
/**
* 通过内容获取关联的分类缩略名
* @param {int} cid 内容ID
* @returns {Promise<string>} 分类缩略名
*/
async function getCategory(cid) {
let metas = await getCategoryMetaFromCID(cid);
return metas[0] ? metas[0].slug : '';
}
/**
* 通过内容获取关联的多级分类缩略名
* @param {int} cid 内容ID
* @returns {Promise<string>} 多级分类缩略名
*/
async function getDirectory(cid) {
let metas = await getCategoryMetaFromCID(cid);
while (metas[0] && metas[0].parent != 0) {
let parent = await getCategoryMeta(metas[0].parent);
if (parent[0]) {
metas.unshift(parent[0]);
} else {
break;
}
}
let result = [];
for (let meta of metas) {
result.push(slugReplace(meta.slug));
}
return result.join('/');
}
/**
* 获取内容的相对链接
* @param {int} cid 内容ID
* @returns {Promise<string>} 相对链接
*/
async function getURL(cid) {
let result = (await executeSQL(`SELECT * FROM \`${config.prefix}contents\` WHERE \`cid\` IN(${cid})`))[0];
if (!result) throw { code: 'not_found' };
let permalink;
switch (result.type) {
case 'post':
permalink = config.postPermalink;
break;
case 'page':
permalink = config.pagePermalink;
break;
default:
permalink = "/{cid}/";
}
let created = new Date(result.created * 1000);
return permalink.format({
cid: result.cid,
slug: slugReplace(result.slug),
category: await getCategory(result.cid),
directory: await getDirectory(result.cid),
year: created.getFullYear(),
month: fillZero(created.getMonth() + 1, 2),
day: fillZero(created.getDate(), 2)
});
}
/**
* 获取上层评论的ID
* @param {Map} comments 评论集合
* @param {object} comment 查询评论
* @param {boolean} root 是否追溯到最顶层评论
* @returns {int} 评论ID
*/
function getParentID(comments, comment, root) {
let split = config.conflictHandle && config.conflictHandle.splitDiffPage;
let result = comment;
do {
let data = comments.get(result.parent);
if (!data || (split && data.cid !== comment.cid)) break;
result = data;
} while (root && result.parent != 0);
return result !== comment ? result.coid : null;
}
/**
* 清理为空值的预设字段
* @param {object} data 数据
* @returns {object} 清理结果
*/
function clean(data) {
let result = {... data};
const target = ['pid', 'rid', 'isSpam'];
const value = [null, undefined];
for (let index of target) {
if (value.includes(result[index])) delete result[index];
}
return result;
}
/**
* 转换Typecho评论为Twikoo评论
* @returns {Promise<string>} Twikoo评论数据
*/
async function convert() {
let output = '';
let data = await getComments();
let comments = new Map();
for (let row of data) {
comments.set(row.coid, row);
}
for (let comment of comments.values()) {
try {
let url;
try {
url = await getURL(comment.cid);
} catch (error) {
if (error.code !== 'not_found') throw error;
url = `/typecho/delete/${comment.cid}/`;
console.warn(`在处理评论ID为 ${comment.coid} 的评论时出现问题。\n无法获取并生成内容ID为 ${comment.cid} 的链接。\n已用 ${url} 代替。`);
}
let created = new Date(comment.created * 1000).getTime();
let twikoo = clean({
/* 标识符 */
_id: hash(comment.coid),
/* 评论人数据 */
nick: comment.author,
mail: comment.mail ? comment.mail.toLowerCase() : null,
mailMd5: comment.mail ? md5(comment.mail.toLowerCase()) : null,
link: comment.url,
/* 评论数据 */
ua: comment.agent || '',
ip: comment.ip,
master: comment.mail && config.email ? comment.mail.toLowerCase() === config.email.toLowerCase() : false,
url: url,
href: config.site ? `${config.site}${url}` : null,
comment: marked(comment.text),
isSpam: comment.status === 'spam' || null,
/* 回复数据 */
pid: hash(getParentID(comments, comment)),
rid: hash(getParentID(comments, comment, true)),
/* 时间 */
created: created,
updated: created
});
output += `${JSON.stringify(twikoo)}\n`;
console.info(`已处理评论ID为 ${comment.coid} 的评论。\n${comment.author}:\n${comment.text}\n`);
} catch (error) {
console.error(`在处理评论ID为 ${comment.coid} 的评论时发生错误。\n${JSON.stringify(comment)}\n`);
throw error;
}
}
console.info(`共处理 ${comments.size} 条数据。`);
return output;
}
/**
* 程序入口
* @returns {Promise<void>}
*/
async function run() {
try {
fs.writeFileSync('comment.json', await convert());
} finally {
database.close();
}
}
run();