forked from psychobunny/nodebb-plugin-import-phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
413 lines (351 loc) · 13.3 KB
/
index.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
var async = require('async');
var mysql = require('mysql');
var _ = require('underscore');
var noop = function(){};
var logPrefix = '[nodebb-plugin-import-phpbb]';
(function(Exporter) {
Exporter.setup = function(config, callback) {
Exporter.log('setup');
// mysql db only config
// extract them from the configs passed by the nodebb-plugin-import adapter
var _config = {
host: config.dbhost || config.host || 'localhost',
user: config.dbuser || config.user || 'root',
password: config.dbpass || config.pass || config.password || '',
port: config.dbport || config.port || 3306,
database: config.dbname || config.name || config.database || 'phpbb'
};
Exporter.config(_config);
Exporter.config('prefix', config.prefix || config.tablePrefix || '' /* phpbb_ ? */ );
Exporter.connection = mysql.createConnection(_config);
Exporter.connection.connect();
callback(null, Exporter.config());
};
Exporter.getUsers = function(callback) {
return Exporter.getPaginatedUsers(0, -1, callback);
};
Exporter.getPaginatedUsers = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'users.user_id as _uid, '
+ prefix + 'users.username as _username, '
+ prefix + 'users.email as _registrationEmail, '
//+ prefix + 'users.user_rank as _level, '
+ prefix + 'users.date as _joindate, '
+ prefix + 'users.email as _email, '
+ prefix + 'users.banned as _banned, '
+ prefix + 'users.USER_SIG as _signature, '
+ prefix + 'users.USER_website as _website, '
+ prefix + 'users.USER_OCC as _occupation, '
+ prefix + 'users.USER_FROM as _location, '
+ prefix + 'users.USER_AVATAR as _picture, '
//+ prefix + 'USER_PROFILE.USER_TITLE as _title, '
//+ prefix + 'USER_PROFILE.USER_RATING as _reputation, '
+ prefix + 'users.HITCOUNT as _profileviews '
//+ prefix + 'USER_PROFILE.USER_BIRTHDAY as _birthday '
+ 'FROM ' + prefix + 'users '
+ 'WHERE ' + prefix + 'users.user_id = ' + prefix + 'users.user_id '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
if (!Exporter.connection) {
err = {error: 'MySQL connection is not setup. Run setup(config) first'};
Exporter.error(err.error);
return callback(err);
}
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
// nbb forces signatures to be less than 150 chars
// keeping it HTML see https://github.com/akhoury/nodebb-plugin-import#markdown-note
row._signature = Exporter.truncateStr(row._signature || '', 150);
// from unix timestamp (s) to JS timestamp (ms)
row._joindate = ((row._joindate || 0) * 1000) || startms;
// lower case the email for consistency
row._email = (row._email || '').toLowerCase();
row._username = row._username.replace(/[^a-zA-Z0-9_\-\s]/g, "_")
// I don't know about you about I noticed a lot my users have incomplete urls, urls like: http://
row._picture = Exporter.validateUrl(row._picture);
row._website = Exporter.validateUrl(row._website);
map[row._uid] = row;
});
callback(null, map);
});
};
Exporter.getMessages = function(callback) {
return Exporter.getPaginatedMessages_(0, -1, callback);
}
Exporter.getPaginatedMessages_ = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = `SELECT * FROM (SELECT ${prefix}privmsgs.privmsgs_id as _mid, ${prefix}privmsgs.privmsgs_from_userid as _fromuid, ${prefix}privmsgs.privmsgs_date as _timestamp, ${prefix}privmsgs.privmsgs_to_userid as _touid FROM ${prefix}privmsgs) as a JOIN ${prefix}privmsgs_text ON a._mid = ${prefix}privmsgs_text.privmsgs_text_id`;
if (!Exporter.connection) {
err = {error: 'MySQL connection is not setup. Run setup(config) first'};
Exporter.error(err.error);
return callback(err);
}
Exporter.connection.query(query,
function (err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
getTopicsMainPids(function(err, mpids) {
//normalize here
var map = {};
rows.forEach(function (row) {
if (! mpids[row._pid]) {
row._content = row._content || '';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
map[row._mid] = row;
}
});
callback(null, map);
});
});
};
Exporter.getCategories = function(callback) {
return Exporter.getPaginatedCategories(0, -1, callback);
};
Exporter.getPaginatedCategories = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = 'SELECT '
+ prefix + 'forums.forum_id as _cid, '
+ prefix + 'forums.forum_name as _name, '
+ prefix + 'forums.forum_desc as _description '
+ 'FROM ' + prefix + 'forums '
+ (start >= 0 && limit >= 0 ? 'LIMIT ' + start + ',' + limit : '');
if (!Exporter.connection) {
err = {error: 'MySQL connection is not setup. Run setup(config) first'};
Exporter.error(err.error);
return callback(err);
}
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
row._name = row._name || 'Untitled Category';
row._description = row._description || 'No decsciption available';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
map[row._cid] = row;
});
callback(null, map);
});
};
Exporter.getTopics = function(callback) {
return Exporter.getPaginatedTopicss(0, -1, callback);
};
Exporter.getPaginatedTopicss = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = `SELECT * FROM ( SELECT ${prefix}topics.topic_id AS _tid, ${prefix}topics.forum_id AS _cid, ${prefix}topics.topic_first_post_id AS _pid, ${prefix}topics.topic_views AS _viewcount, ${prefix}topics.topic_title AS _title, ${prefix}topics.topic_time AS _timestamp, ${prefix}topics.topic_status AS _status FROM ${prefix}topics ) AS a JOIN( SELECT ${prefix}posts.post_id AS pidd, ${prefix}posts.poster_id AS _uid, ${prefix}posts.topic_id AS _post_tid FROM ${prefix}posts ) AS b ON b.pidd = a._pid JOIN( SELECT ${prefix}posts_text.post_text AS _content, ${prefix}posts_text.post_id AS p1d FROM ${prefix}posts_text ) AS cd ON a._pid = cd.p1d`
/* var query =
'SELECT '
+ prefix + 'topics.topic_id as _tid, '
+ prefix + 'topics.forum_id as _cid, '
// this is the 'parent-post'
// see https://github.com/akhoury/nodebb-plugin-import#important-note-on-topics-and-posts
// I don't really need it since I just do a simple join and get its content, but I will include for the reference
// remember this post EXCLUDED in the exportPosts() function
+ prefix + 'topics.topic_first_post_id as _pid, '
+ prefix + 'topics.topic_views as _viewcount, '
+ prefix + 'topics.topic_title as _title, '
+ prefix + 'topics.topic_time as _timestamp, '
+ prefix + 'topics.topic_status as _status, '
//+ prefix + 'TOPICS.TOPIC_IS_STICKY as _pinned, '
+ prefix + 'posts.poster_id as _uid, '
// this should be == to the _tid on top of this query
+ prefix + 'posts.topic_id as _post_tid, '
// and there is the content I need !!
+ prefix + 'posts_text.post_text as _content '
+ 'FROM ' + prefix + 'topics, ' + prefix + 'posts, ' + prefix + 'posts_text'
// see
+ ' WHERE ' + prefix + 'topics.topic_first_post_id=' + prefix + 'posts.post_id ' */
//console.log(query)
if (!Exporter.connection) {
err = {error: 'MySQL connection is not setup. Run setup(config) first'};
Exporter.error(err.error);
return callback(err);
}
Exporter.connection.query(query,
function(err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
//normalize here
var map = {};
console.log(rows.length)
rows.forEach(function(row) {
row._title = row._title ? row._title[0].toUpperCase() + row._title.substr(1) : 'Untitled';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
map[row._tid] = row;
});
callback(null, map);
});
};
var getTopicsMainPids = function(callback) {
if (Exporter._topicsMainPids) {
return callback(null, Exporter._topicsMainPids);
}
Exporter.getPaginatedTopicss(0, -1, function(err, topicsMap) {
if (err) return callback(err);
Exporter._topicsMainPids = {};
Object.keys(topicsMap).forEach(function(_tid) {
var topic = topicsMap[_tid];
Exporter._topicsMainPids[topic.topic_first_post_id] = topic._tid;
});
callback(null, Exporter._topicsMainPids);
});
};
Exporter.getPosts = function(callback) {
return Exporter._getPaginatedPosts(0, -1, callback);
};
Exporter._getPaginatedPosts = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query = `SELECT * FROM (SELECT ${prefix}posts.post_id as _pid, ${prefix}posts.topic_id as _tid, ${prefix}posts.post_time as _timestamp, ${prefix}posts.poster_id as _uid FROM ${prefix}posts) as a JOIN (SELECT ${prefix}posts_text.post_text as _content, ${prefix}posts_text.post_id as piddy FROM ${prefix}posts_text) as b ON b.piddy = a._pid`;
if (!Exporter.connection) {
err = {error: 'MySQL connection is not setup. Run setup(config) first'};
Exporter.error(err.error);
return callback(err);
}
Exporter.connection.query(query,
function (err, rows) {
if (err) {
Exporter.error(err);
return callback(err);
}
getTopicsMainPids(function(err, mpids) {
//normalize here
var map = {};
rows.forEach(function (row) {
// make it's not a topic
if (! mpids[row._pid]) {
row._content = row._content || '';
row._timestamp = ((row._timestamp || 0) * 1000) || startms;
map[row._pid] = row;
}
});
callback(null, map);
});
});
};
Exporter.teardown = function(callback) {
Exporter.log('teardown');
Exporter.connection.end();
Exporter.log('Done');
callback();
};
Exporter.testrun = function(config, callback) {
async.series([
function(next) {
Exporter.setup(config, next);
},
function(next) {
Exporter.getUsers(next);
},
function(next) {
Exporter.getCategories(next);
},
function(next) {
Exporter.getTopics(next);
},
function(next) {
Exporter.getPosts(next);
},
function(next) {
Exporter.teardown(next);
}
], callback);
};
Exporter.paginatedTestrun = function(config, callback) {
async.series([
function(next) {
Exporter.setup(config, next);
},
function(next) {
Exporter.getPaginatedUsers(0, 1000, next);
},
function(next) {
Exporter.getPaginatedCategories(0, 1000, next);
},
function(next) {
Exporter.getPaginatedTopics(0, 1000, next);
},
function(next) {
Exporter._getPaginatedPosts(1001, 2000, next);
},
function(next) {
Exporter.teardown(next);
}
], callback);
};
Exporter.warn = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.warn.apply(console, args);
};
Exporter.log = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.log.apply(console, args);
};
Exporter.error = function() {
var args = _.toArray(arguments);
args.unshift(logPrefix);
console.error.apply(console, args);
};
Exporter.config = function(config, val) {
if (config != null) {
if (typeof config === 'object') {
Exporter._config = config;
} else if (typeof config === 'string') {
if (val != null) {
Exporter._config = Exporter._config || {};
Exporter._config[config] = val;
}
return Exporter._config[config];
}
}
return Exporter._config;
};
// from Angular https://github.com/angular/angular.js/blob/master/src/ng/directive/input.js#L11
Exporter.validateUrl = function(url) {
var pattern = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?$/;
return url && url.length < 2083 && url.match(pattern) ? url : '';
};
Exporter.truncateStr = function(str, len) {
if (typeof str != 'string') return str;
len = _.isNumber(len) && len > 3 ? len : 20;
return str.length <= len ? str : str.substr(0, len - 3) + '...';
};
Exporter.whichIsFalsy = function(arr) {
for (var i = 0; i < arr.length; i++) {
if (!arr[i])
return i;
}
return null;
};
})(module.exports);