Skip to content

Instantly share code, notes, and snippets.

@barisusakli
Last active September 20, 2017 17:03
Show Gist options
  • Save barisusakli/ebb0ab47633b27ac02e8722308f2e6f3 to your computer and use it in GitHub Desktop.
Save barisusakli/ebb0ab47633b27ac02e8722308f2e6f3 to your computer and use it in GitHub Desktop.
set topics to en if they have no language
/*globals require, console, process */
'use strict';
var nconf = require('nconf');
var async = require('async');
nconf.file({
file: 'config.json'
});
nconf.defaults({
base_dir: __dirname,
});
var db = require('./src/database');
var counter = 0;
db.init(function(err) {
if (err) {
console.log("NodeBB could not connect to your database. Error: " + err.message);
process.exit();
}
setTopicLanguageToEnglish(function (err) {
if (err) {
console.error(err);
process.exit();
}
console.log('done, total = ' + counter);
process.exit();
});
});
function setTopicLanguageToEnglish(callback) {
var batch = require('./src/batch');
batch.processSortedSet('topics:tid', function (tids, next) {
counter += tids.length;
var keys = tids.map(function (tid) {
return 'topic:' + tid;
});
db.getObjectsFields(keys, ['tid', 'cid', 'language', 'postcount', 'lastposttime'], function (err, topicData) {
if (err) {
return callback(err);
}
topicData.forEach(function (topicData) {
topicData.language = topicData.language || 'en';
});
changeTopicsLanguage(topicData, next);
});
}, {}, callback);
}
//modified from languageHub.js
function changeTopicsLanguage(topicData, callback) {
async.eachSeries(topicData, function (topicData, next) {
var tasks = [];
if (topicData.language) {
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':lang:' + topicData.language + ':tids', topicData.lastposttime, topicData.tid));
tasks.push(async.apply(db.sortedSetAdd, 'cid:' + topicData.cid + ':lang:' + topicData.language + ':tids:posts', topicData.postcount, topicData.tid));
tasks.push(async.apply(db.sortedSetAdd, 'tids:language:' + topicData.language, topicData.lastposttime, topicData.tid));
tasks.push(async.apply(db.setObjectField, 'topic:' + topicData.tid, 'language', topicData.language));
}
async.series(tasks, next);
}, callback);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment