diff --git a/languages/en-GB/dbsearch.json b/languages/en-GB/dbsearch.json new file mode 100644 index 0000000..aa66127 --- /dev/null +++ b/languages/en-GB/dbsearch.json @@ -0,0 +1,17 @@ +{ + "admin.menuname":"DB Search", + "admin.title": "DB Search", + "admin.topicsIndexed": "Topics Indexed:", + "admin.postsIndexed": "Posts Indexed:", + "admin.messagesIndexed": "Messages Indexed:", + "admin.reindex": "Re Index", + "admin.clearIndex": "Clear Index", + "admin.working": "Working...", + "admin.indexLanguage": "Index Language", + "admin.changeLanguage": "Change Language", + "admin.topicLimit": "Topic Limit", + "admin.topicLimitPlaceholder": "Number of topics to return", + "admin.postLimit": "Post Limit", + "admin.postLimitPlaceholder": "Number of posts to return", + "admin.excludeCategories": "Select categories to exclude from indexing" +} diff --git a/languages/zh-CN/dbsearch.json b/languages/zh-CN/dbsearch.json new file mode 100644 index 0000000..1bb3c6d --- /dev/null +++ b/languages/zh-CN/dbsearch.json @@ -0,0 +1,17 @@ +{ + "admin.menuname":"数据库搜索", + "admin.title": "数据库搜索", + "admin.topicsIndexed": "已索引主题:", + "admin.postsIndexed": "已索引帖子:", + "admin.messagesIndexed": "已索引消息:", + "admin.reindex": "重新索引", + "admin.clearIndex": "清空索引", + "admin.working": "正在处理...", + "admin.indexLanguage": "索引语言", + "admin.changeLanguage": "更改语言", + "admin.topicLimit": "主题限制", + "admin.topicLimitPlaceholder": "要返回的主题数量", + "admin.postLimit": "帖子限制", + "admin.postLimitPlaceholder": "要返回的帖子数量", + "admin.excludeCategories": "选择要从索引中排除的分类" +} diff --git a/lib/dbsearch.js b/lib/dbsearch.js index d1eead9..bfb4174 100644 --- a/lib/dbsearch.js +++ b/lib/dbsearch.js @@ -15,6 +15,7 @@ const batch = require.main.require('./src/batch'); const plugins = require.main.require('./src/plugins'); const categories = require.main.require('./src/categories'); const pubsub = require.main.require('./src/pubsub'); +var translator = require.main.require('./src/translator'); const searchModule = require(`./${nconf.get('database')}`); @@ -38,6 +39,7 @@ const languageLookup = { es: 'spanish', sv: 'swedish', tr: 'turkish', + zh: 'chinese_zh', }; const defaultPostLimit = 500; @@ -53,7 +55,7 @@ const batchSize = 500; const search = module.exports; -function convertLanguageName(name) { +function convertLanguageName (name) { if (nconf.get('database') === 'postgres') { return languageLookup[name] || languageLookup.en; } @@ -262,7 +264,7 @@ search.reindex = async function () { }); }; -async function reIndexTopics() { +async function reIndexTopics () { await batch.processSortedSet('topics:tid', async (tids) => { const topicData = await topics.getTopicsFields(tids, ['tid', 'title', 'uid', 'cid', 'deleted', 'timestamp']); await topicsSave(topicData); @@ -271,10 +273,10 @@ async function reIndexTopics() { }); } -async function topicsSave(topics) { +async function topicsSave (topics) { topics = topics.filter( t => t && utils.isNumber(t.tid) && parseInt(t.deleted, 10) !== 1 && - !pluginConfig.excludeCategories.includes(String(t.cid)) + !pluginConfig.excludeCategories.includes(String(t.cid)) ); let data = topics.map((topicData) => { @@ -308,7 +310,7 @@ async function topicsSave(topics) { await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'topicsIndexed', result.tids.length); } -async function reIndexPosts() { +async function reIndexPosts () { await batch.processSortedSet('posts:pid', async (pids) => { let postData = await posts.getPostsFields(pids, ['pid', 'content', 'uid', 'tid', 'deleted', 'timestamp']); postData = postData.filter(p => p && p.deleted !== 1); @@ -327,10 +329,10 @@ async function reIndexPosts() { }); } -async function postsSave(posts) { +async function postsSave (posts) { posts = posts.filter( p => p && utils.isNumber(p.pid) && parseInt(p.deleted, 10) !== 1 && - !pluginConfig.excludeCategories.includes(String(p.cid)) + !pluginConfig.excludeCategories.includes(String(p.cid)) ); let data = posts.map((postData) => { @@ -364,7 +366,7 @@ async function postsSave(posts) { await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'postsIndexed', result.pids.length); } -async function reIndexMessages() { +async function reIndexMessages () { await batch.processSortedSet(`messages:mid`, async (mids) => { let messageData = await messaging.getMessagesFields(mids, ['mid', 'content', 'roomId', 'fromuid', 'deleted', 'system']); messageData = messageData.filter(p => p && p.deleted !== 1 && p.system !== 1); @@ -374,7 +376,7 @@ async function reIndexMessages() { }); } -async function messagesSave(msgs) { +async function messagesSave (msgs) { msgs = msgs.filter(m => m && utils.isNumber(m.mid) && parseInt(m.deleted, 10) !== 1 && parseInt(m.system, 10) !== 1); let data = msgs.map((msgData) => { @@ -405,7 +407,7 @@ async function messagesSave(msgs) { await db.incrObjectFieldBy('nodebb-plugin-dbsearch', 'messagesIndexed', result.mids.length); } -async function searchRemove(key, ids) { +async function searchRemove (key, ids) { ids = ids.filter(id => id && utils.isNumber(id)); await db.searchRemove(key, ids); if (key === 'topic') { @@ -417,7 +419,7 @@ async function searchRemove(key, ids) { } } -async function reIndexTids(tids) { +async function reIndexTids (tids) { if (!Array.isArray(tids) || !tids.length) { return; } @@ -428,11 +430,11 @@ async function reIndexTids(tids) { return; } - async function reIndexTopicsPids(topicData) { + async function reIndexTopicsPids (topicData) { await Promise.all(topicData.map(t => reIndexTopicPids(t))); } - async function reIndexTopicPids(topic) { + async function reIndexTopicPids (topic) { await reIndexPids([topic.mainPid], topic); await batch.processSortedSet(`tid:${topic.tid}:posts`, async (pids) => { await reIndexPids(pids, topic); @@ -447,7 +449,7 @@ async function reIndexTids(tids) { ]); } -async function reIndexPids(pids, topic) { +async function reIndexPids (pids, topic) { if (!Array.isArray(pids) || !pids.length) { winston.warn('[nodebb-plugin-dbsearch] invalid-pid, skipping'); return; @@ -464,14 +466,16 @@ async function reIndexPids(pids, topic) { await postsSave(postData); } -async function renderAdmin(req, res) { +async function renderAdmin (req, res) { const results = await getGlobalAndPluginData(); results.plugin.progressData = await getProgress(); - results.plugin.title = 'DB Search'; - res.render('admin/plugins/dbsearch', results.plugin); + translator.translate('[[dbsearch:admin.title]]', function (translated) { + results.plugin.title = translated; + res.render('admin/plugins/dbsearch', results.plugin); + }); } -async function save(req, res) { +async function save (req, res) { if (utils.isNumber(req.body.postLimit) && utils.isNumber(req.body.topicLimit)) { const data = { postLimit: req.body.postLimit, @@ -494,7 +498,7 @@ socketAdmin.plugins.dbsearch.checkProgress = async function () { return await getProgress(); }; -async function getPluginData() { +async function getPluginData () { const data = await db.getObject('nodebb-plugin-dbsearch') || {}; data.topicsIndexed = parseInt(data.topicsIndexed, 10) || 0; data.postsIndexed = parseInt(data.postsIndexed, 10) || 0; @@ -514,7 +518,7 @@ async function getPluginData() { return data; } -async function getGlobalAndPluginData() { +async function getGlobalAndPluginData () { const [global, plugin, allCategories] = await Promise.all([ db.getObjectFields('global', ['topicCount', 'postCount', 'messageCount']), getPluginData(), @@ -552,7 +556,7 @@ async function getGlobalAndPluginData() { return { global: global, plugin: plugin, allCategories: allCategories }; } -async function getProgress() { +async function getProgress () { const [global, pluginData] = await Promise.all([ db.getObjectFields('global', ['topicCount', 'postCount', 'messageCount']), getPluginData(), @@ -595,7 +599,7 @@ socketAdmin.plugins.dbsearch.clearIndex = async function () { }, 0); }; -async function clearIndex() { +async function clearIndex () { await db.setObject('nodebb-plugin-dbsearch', { working: 1, }); @@ -614,7 +618,7 @@ async function clearIndex() { }); } -async function clearSet(set, key) { +async function clearSet (set, key) { await batch.processSortedSet(set, async (ids) => { await searchRemove(key, ids); }, { @@ -629,13 +633,14 @@ socketAdmin.plugins.dbsearch.changeLanguage = async function (socket, language) const admin = {}; admin.menu = function (custom_header, callback) { - custom_header.plugins.push({ - route: '/plugins/dbsearch', - icon: 'fa-search', - name: 'DB Search', + translator.translate('[[dbsearch:admin.menuname]]', function (translated) { + custom_header.plugins.push({ + route: '/plugins/dbsearch', + icon: 'fa-search', + name: translated, + }); + callback(null, custom_header); }); - - callback(null, custom_header); }; search.admin = admin; diff --git a/lib/postgres.js b/lib/postgres.js index 7a7851d..9a6eb3e 100644 --- a/lib/postgres.js +++ b/lib/postgres.js @@ -6,7 +6,7 @@ const nconf = require.main.require('nconf'); const db = require.main.require('./src/database'); const pubsub = require.main.require('./src/pubsub'); -let searchLanguage = 'english'; +let searchLanguage = 'chinese_zh'; pubsub.on('dbsearch-language-changed', (e) => { searchLanguage = e.data; diff --git a/package-lock.json b/package-lock.json index ac7d4bd..100e3da 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,6 +10,7 @@ "license": "BSD-2-Clause", "dependencies": { "lodash": "4.17.21", + "nodebb-plugin-dbsearch": "file:", "redisearch": "^1.0.3" }, "devDependencies": { @@ -2034,6 +2035,10 @@ "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", "dev": true }, + "node_modules/nodebb-plugin-dbsearch": { + "resolved": "", + "link": true + }, "node_modules/object-inspect": { "version": "1.13.4", "resolved": "https://registry.npmjs.org/object-inspect/-/object-inspect-1.13.4.tgz", diff --git a/package.json b/package.json index 9708a02..d15ef99 100644 --- a/package.json +++ b/package.json @@ -20,6 +20,7 @@ "license": "BSD-2-Clause", "dependencies": { "lodash": "4.17.21", + "nodebb-plugin-dbsearch": "file:", "redisearch": "^1.0.3" }, "devDependencies": { diff --git a/plugin.json b/plugin.json index 9727d35..aac7878 100644 --- a/plugin.json +++ b/plugin.json @@ -41,5 +41,6 @@ "modules": { "../admin/plugins/dbsearch.js": "public/admin.js" }, - "templates": "./templates" + "templates": "./templates", + "languages": "./languages" } \ No newline at end of file diff --git a/templates/admin/plugins/dbsearch.tpl b/templates/admin/plugins/dbsearch.tpl index 4748e66..c41d681 100644 --- a/templates/admin/plugins/dbsearch.tpl +++ b/templates/admin/plugins/dbsearch.tpl @@ -4,73 +4,97 @@
-
DB Search
- Topics Indexed: {topicsIndexed} / {topicCount} + [[dbsearch:admin.topicsIndexed]] {topicsIndexed} / + {topicCount}
-
{progressData.topicsPercent}%
+
+ {progressData.topicsPercent}% +
- Posts Indexed: {postsIndexed} / {postCount} + [[dbsearch:admin.postsIndexed]] {postsIndexed} / + {postCount}
-
{progressData.postsPercent}%
+
+ {progressData.postsPercent}% +
- Messages Indexed: {messagesIndexed} / {messageCount} + [[dbsearch:admin.messagesIndexed]] {messagesIndexed} / {messageCount}
-
{progressData.messagesPercent}%
+
+ {progressData.messagesPercent}% +
- - + + - Working... + [[dbsearch:admin.working]] -
+
- +
- -
+ +
- - + +
- - + +
- +
@@ -79,4 +103,4 @@
-
+
\ No newline at end of file