Last active
September 20, 2017 17:03
-
-
Save barisusakli/ebb0ab47633b27ac02e8722308f2e6f3 to your computer and use it in GitHub Desktop.
set topics to en if they have no language
This file contains 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
/*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