Created
May 8, 2017 21:39
-
-
Save akhoury/6dd63505071e56f6041afc5bf62a240a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// tested with NodeBB v1.4.6+ | |
var path = require('path'); | |
var url = require('url'); | |
var nconf = require('nconf'); | |
var async = require('async'); | |
var pkg = require('./package.json'); | |
nconf.file({ file: path.join(__dirname, './config.json') }); | |
nconf.defaults({ | |
base_dir: __dirname, | |
themes_path: path.join(__dirname, 'node_modules'), | |
upload_path: 'public/uploads', | |
views_dir: path.join(__dirname, 'build/public/templates'), | |
version: pkg.version | |
}); | |
var dbType = nconf.get('database'); | |
var productionDbConfig = nconf.get(dbType); | |
nconf.set(dbType, productionDbConfig); | |
var db = require('./src/database'); | |
var mysql = require('./node_modules/nodebb-plugin-import-lithium/node_modules/mysql'); | |
var connection = mysql.createConnection({ | |
host: '127.0.0.1', | |
user: 'root', | |
password: 'password', | |
database: 'lithium', | |
port: 3307 | |
, socketPath: '/Applications/MAMP/tmp/mysql/mysql.sock' | |
}); | |
connection.connect(); | |
setInterval(function() { | |
connection.query("SELECT 1", function(){}); | |
}, 60000); | |
console.log('\n\n\n==== did you set the maximumChatMessageLength = Number.MAX_SAFE_INTEGER in the messaging/create????'); | |
var csvToArray = function(v) { | |
return !Array.isArray(v) ? ('' + v).split(',').map(function(s) { return s.trim(); }) : v; | |
}; | |
db.init(function() { | |
var batch = require('./src/batch'); | |
var Messages = require('./node_modules/nodebb-plugin-import/server/augmented/messages.js'); | |
var bbcodeToMarkdown = require('./node_modules/nodebb-plugin-import/node_modules/bbcode-to-markdown'); | |
async.series([ | |
function (next) { | |
// return next(); | |
Messages.deleteEachImported(function(err, data) { | |
if (err) throw err; | |
console.log('deleted %' + data.percentage ); | |
}, next); | |
}, | |
function (next) { | |
// return next(); | |
Messages.each(function (message, next) { | |
Messages.deleteMessage(message.mid, message.roomId, function (err) { | |
if (err) { | |
return next(err) | |
} | |
console.log('deleted', message.mid, message.roomId); | |
next(); | |
}) | |
}, {}, next); | |
}, | |
function (next) { | |
var prefix = ''; | |
var query = '' | |
+ 'SELECT ' | |
+ prefix + 'tblia_notes_content.note_id as _mid, \n' | |
+ prefix + 'tblia_notes_content.body as _content, \n' | |
+ prefix + 'tblia_notes_content.sender_user_id as _fromuid, \n' | |
+ 'GROUP_CONCAT(DISTINCT tblia_notes_inbox.recipient_user_id) as _touids, \n' | |
+ prefix + 'tblia_notes_content.sent_time as _timestamp \n' | |
+ 'FROM ' + prefix + 'tblia_notes_content \n' | |
+ 'LEFT JOIN ' + prefix + 'tblia_notes_inbox on ' + prefix + 'tblia_notes_inbox.note_id = ' + prefix + 'tblia_notes_content.note_id \n' | |
+ 'LEFT JOIN ' + prefix + 'tblia_notes_outbox on ' + prefix + 'tblia_notes_outbox.note_id = ' + prefix + 'tblia_notes_content.note_id \n' | |
+ 'GROUP BY 1 '; | |
connection.query(query, function (err, rows) { | |
if (err) { | |
return next(err); | |
} | |
Messages.batchImport( | |
rows.map(function (row) { | |
row._touids = csvToArray(row._touids); | |
row._touids = row._touids.filter(function (_touid, index) { | |
return !!_touid && this.indexOf(_touid) == index; | |
}, row._touids); | |
if (row._touids.length > 1) { | |
console.log(row); | |
} | |
return row; | |
}), | |
{}, | |
function (err, result) { | |
if (!result.original) { | |
result.original = {}; | |
} | |
if (!result.imported) { | |
result.imported = {}; | |
} | |
var log = '_mid>mid: ' + result.original._mid + '>' + result.imported.mid | |
+ ', count at: ' + result.index + '/' + rows.length; | |
if (err) { | |
console.warn('[WARNING] ' + err + ', ' + log); | |
} else { | |
process.stdout.write(log + '\n'); | |
} | |
}, | |
next); | |
}); | |
}, | |
function (next) { | |
Messages.each(function (message, next) { | |
var content = (JSON.parse(message.__imported_original_data__) || {})._content || message.content || ''; | |
if (!(JSON.parse(message.__imported_original_data__) || {})._content) { | |
console.warn('[warning] message:' + message.mid + ', ' + message.__imported_original_data__); | |
} | |
// pre | |
content = content.replace(/(http.*)?\/t\d*\/image\/serverpage\/image-id\/(\w+)\/image-size\/.*\?>/ig, '/uploads/_imported_images/$2\?'); | |
content = content.replace(/(http.*)?\/legacyfs\/online\/avatars\/(.*)>/ig, '/uploads/_imported_images/legacy_avatars/$2\?'); | |
content = content.replace(/<li-image[^>]+id='?"?([^'"\s>]+)'?"?/ig, '<li-image src="/uploads/_imported_images/$1"'); | |
content = content.replace(/li-image/ig, 'img'); | |
content = bbcodeToMarkdown(content); | |
// post | |
content = content.replace(/(\[.*?\])\((.+?)\)/g, function (match, m1, m2, offset, string) { | |
return m1 + '(' + url.resolve(m2, '') + ')'; | |
}); | |
Messages.setMessageFields(message.mid, { | |
content: content | |
}, function (err) { | |
process.stdout.write('converted: ' + message.mid + '\n'); | |
next(err); | |
}); | |
}, {}, next); | |
} | |
], function (e) { | |
if (e) { | |
throw e; | |
} | |
console.log('ALL DONE!'); | |
process.exit(0); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment