wikijs-fork/server/modules/search/db/engine.js

100 lines
2.1 KiB
JavaScript
Raw Normal View History

/* global WIKI */
2019-03-09 05:51:02 +00:00
2018-09-01 03:42:14 +00:00
module.exports = {
activate() {
2019-03-09 05:51:02 +00:00
// not used
2018-09-01 03:42:14 +00:00
},
deactivate() {
2019-03-09 05:51:02 +00:00
// not used
2018-09-01 03:42:14 +00:00
},
2019-03-09 05:51:02 +00:00
/**
* INIT
*/
init() {
// not used
2018-09-01 03:42:14 +00:00
},
2019-03-09 05:51:02 +00:00
/**
* QUERY
*
* @param {String} q Query
* @param {Object} opts Additional options
*/
async query(q, opts) {
const results = await WIKI.models.pages.query()
.column('id', 'title', 'description', 'path', 'localeCode as locale')
.where(builder => {
builder.where('isPublished', true)
if (opts.locale) {
builder.andWhere('localeCode', opts.locale)
}
if (opts.path) {
builder.andWhere('path', 'like', `${opts.path}%`)
}
// TODO: Add user permissions filtering
2019-09-08 16:39:05 +00:00
builder.andWhere(builderSub => {
switch (WIKI.config.db.type) {
2019-03-09 05:51:02 +00:00
case 'postgres':
2019-09-08 16:39:05 +00:00
builderSub.where('title', 'ILIKE', `%${q}%`)
builderSub.orWhere('description', 'ILIKE', `%${q}%`)
2019-03-09 05:51:02 +00:00
break
case 'mysql':
case 'mariadb':
2019-09-08 16:39:05 +00:00
builderSub.whereRaw(`title LIKE '%?%' COLLATE utf8_general_ci`, [q])
builderSub.orWhereRaw(`description LIKE '%?%' COLLATE utf8_general_ci`, [q])
2019-03-09 05:51:02 +00:00
break
2018-09-01 03:42:14 +00:00
2019-03-09 05:51:02 +00:00
// TODO: MSSQL handling
default:
2019-09-08 16:39:05 +00:00
builderSub.where('title', 'LIKE', `%${q}%`)
builderSub.orWhere('description', 'LIKE', `%${q}%`)
2019-03-09 05:51:02 +00:00
break
}
})
})
.limit(WIKI.config.search.maxHits)
return {
results,
suggestions: [],
totalHits: results.length
}
2018-09-01 03:42:14 +00:00
},
2019-03-09 05:51:02 +00:00
/**
* CREATE
*
* @param {Object} page Page to create
*/
async created(page) {
// not used
2018-09-01 03:42:14 +00:00
},
2019-03-09 05:51:02 +00:00
/**
* UPDATE
*
* @param {Object} page Page to update
*/
async updated(page) {
// not used
2018-09-01 03:42:14 +00:00
},
2019-03-09 05:51:02 +00:00
/**
* DELETE
*
* @param {Object} page Page to delete
*/
async deleted(page) {
// not used
},
/**
* RENAME
*
* @param {Object} page Page to rename
*/
async renamed(page) {
// not used
},
/**
* REBUILD INDEX
*/
async rebuild() {
// not used
2018-09-01 03:42:14 +00:00
}
}