Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions languages/en-GB/dbsearch.json
Original file line number Diff line number Diff line change
@@ -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"
}
17 changes: 17 additions & 0 deletions languages/zh-CN/dbsearch.json
Original file line number Diff line number Diff line change
@@ -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": "选择要从索引中排除的分类"
}
63 changes: 34 additions & 29 deletions lib/dbsearch.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')}`);

Expand All @@ -38,6 +39,7 @@ const languageLookup = {
es: 'spanish',
sv: 'swedish',
tr: 'turkish',
zh: 'chinese_zh',
};

const defaultPostLimit = 500;
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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);
Expand All @@ -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) => {
Expand Down Expand Up @@ -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') {
Expand All @@ -417,7 +419,7 @@ async function searchRemove(key, ids) {
}
}

async function reIndexTids(tids) {
async function reIndexTids (tids) {
if (!Array.isArray(tids) || !tids.length) {
return;
}
Expand All @@ -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);
Expand All @@ -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;
Expand All @@ -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) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't have to manually translate title I think, you can just set results.plugin.title = '[[dbsearch:admin.title]] and it should be translated.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK

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,
Expand All @@ -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;
Expand All @@ -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(),
Expand Down Expand Up @@ -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(),
Expand Down Expand Up @@ -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,
});
Expand All @@ -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);
}, {
Expand All @@ -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) {

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no need for manualy translation just set name to [[dbsearch:admin.menuname]]

custom_header.plugins.push({
route: '/plugins/dbsearch',
icon: 'fa-search',
name: translated,
});
callback(null, custom_header);
});

callback(null, custom_header);
};

search.admin = admin;
2 changes: 1 addition & 1 deletion lib/postgres.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keep the default as english


pubsub.on('dbsearch-language-changed', (e) => {
searchLanguage = e.data;
Expand Down
5 changes: 5 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"license": "BSD-2-Clause",
"dependencies": {
"lodash": "4.17.21",
"nodebb-plugin-dbsearch": "file:",

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what's this doing?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I changed it to a local reference because I placed the plugin code in the src/plugins directory of the NodeBB project and bundled it for testing using the file: method. You can easily revert it to the original NPM installation method—just restore the plugin version number in the package.json (or remove this line entirely to return to the default dependency), then reinstall it.

"redisearch": "^1.0.3"
},
"devDependencies": {
Expand Down
3 changes: 2 additions & 1 deletion plugin.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,5 +41,6 @@
"modules": {
"../admin/plugins/dbsearch.js": "public/admin.js"
},
"templates": "./templates"
"templates": "./templates",
"languages": "./languages"
}
Loading