nodebb-plugin-import-esotalk2
Version:
An esoTalk forum exporter to import-ready files
387 lines (318 loc) • 13.6 kB
JavaScript
var async = require('async');
var mysql = require('mysql');
var _ = require('underscore');
var noop = function(){};
var logPrefix = '[nodebb-plugin-import-esotalk2]';
(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 || 'et_'
};
Exporter.config(_config);
Exporter.config('prefix', config.prefix || config.tablePrefix || '');
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 + 'member.memberId as _uid, '
+ prefix + 'member.username as _username, '
+ prefix + 'member.email as _email, '
+ prefix + 'member.joinTime as _joindate, '
+ prefix + 'member.account as _level '
+ 'FROM ' + prefix + 'member '
+ (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(query, err);
return callback(err);
}
//normalize here
var map = {};
rows.forEach(function(row) {
// 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();
// get rid of 'member' and 'suspended' values, which are meaningless in nodebb,
// but keep the administrators.
if (row._level != 'administrator')
delete row._level;
map[row._uid] = 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 + 'channel.channelId as _cid, '
+ prefix + 'channel.title as _name, '
+ prefix + 'channel.slug as _slug, '
+ prefix + 'channel.description as _description, '
+ prefix + 'channel.parentId as _parentCid '
+ 'FROM ' + prefix + 'channel '
+ (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(query, 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;
// make it very clear when there is no parent
if (row.parentCid == 0) row.parentCid = null;
map[row._cid] = row;
});
callback(null, map);
});
};
Exporter.getTopics = function(callback) {
return Exporter.getPaginatedTopics(0, -1, callback);
};
Exporter.getPaginatedTopics = function(start, limit, callback) {
callback = !_.isFunction(callback) ? noop : callback;
var err;
var prefix = Exporter.config('prefix');
var startms = +new Date();
var query =
'SELECT '
+ prefix + 'conversation.conversationId as _tid, '
// aka category id, or cid
+ prefix + 'conversation.channelId 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 is EXCLUDED in the getPosts() function
+ prefix + 'post.postId as _pid, '
+ prefix + 'conversation.startMemberId as _uid, '
+ prefix + 'conversation.title as _title, '
+ prefix + 'conversation.startTime as _timestamp, '
+ prefix + 'conversation.sticky as _pinned, '
// this should be == to the _tid on top of this query
+ prefix + 'post.conversationId as _post_tid, '
// and there is the content I need !!
+ prefix + 'post.content as _content '
+ 'FROM ' + prefix + 'conversation, ' + prefix + 'post '
// _conversation in esotalk doesn’t have a pointer to the first post,
// so I’m selecting it by time. this feels a bit dangerous but seems to work.
+ 'WHERE ' + prefix + 'conversation.startTime=' + prefix + 'post.time '
// and this one must be a parent
// + 'AND ' + prefix + 'POSTS.POST_PARENT_ID=0 '
+ (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(query, err);
return callback(err);
}
//normalize here
var map = {};
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);
});
};
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 ' + prefix + 'post.postId as _pid, '
+ prefix + 'post.conversationId as _tid, '
+ prefix + 'post.memberId as _uid, '
+ prefix + 'post.time as _timestamp, '
// not being used
+ prefix + 'post.title as _subject, '
+ prefix + 'post.content as _content, '
+ prefix + 'conversation.startTime as _topic_timestamp '
+ 'FROM ' + prefix + 'post, ' + prefix + 'conversation '
// this post cannot be a its topic's main post, it MUST be a reply-post
// see https://github.com/akhoury/nodebb-plugin-import#important-note-on-topics-and-posts
// However, I’m throwing the main posts out below, can’t do it in mysql due to esoTalks
// database structure ... this will not work well for many records of course, so if you
// have a BIG esoTalk Forum, you should develop a better solution!
+ 'WHERE ' + prefix + 'post.conversationId=' + prefix + 'conversation.conversationId '
+ (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);
}
console.log(query);
Exporter.connection.query(query,
function(err, rows) {
var i;
if (err) {
Exporter.error(query, err);
return callback(err);
}
// throw out the topic-starting posts ...
i = 0;
while (i < rows.length) {
while (rows[i]._timestamp == rows[i]._topic_timestamp) {
rows.splice(i, 1);
}
delete rows[i]._topic_timestamp;
++i;
}
// cut to limits here - too late, i know ...
if (start >= 0 && limit >= 0) {
rows = rows.slice(start, limit);
}
//normalize here
var map = {};
rows.forEach(function(row) {
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(0, 50000, 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);