You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

125 lines
4.1 KiB

  1. const Model = require('objection').Model
  2. const path = require('path')
  3. const fs = require('fs-extra')
  4. const _ = require('lodash')
  5. const yaml = require('js-yaml')
  6. const commonHelper = require('../helpers/common')
  7. /* global WIKI */
  8. /**
  9. * SearchEngine model
  10. */
  11. module.exports = class SearchEngine extends Model {
  12. static get tableName() { return 'searchEngines' }
  13. static get idColumn() { return 'key' }
  14. static get jsonSchema () {
  15. return {
  16. type: 'object',
  17. required: ['key', 'isEnabled'],
  18. properties: {
  19. key: {type: 'string'},
  20. isEnabled: {type: 'boolean'},
  21. level: {type: 'string'}
  22. }
  23. }
  24. }
  25. static get jsonAttributes() {
  26. return ['config']
  27. }
  28. static async getSearchEngines() {
  29. return WIKI.models.searchEngines.query()
  30. }
  31. static async refreshSearchEnginesFromDisk() {
  32. let trx
  33. try {
  34. const dbSearchEngines = await WIKI.models.searchEngines.query()
  35. // -> Fetch definitions from disk
  36. const searchEnginesDirs = await fs.readdir(path.join(WIKI.SERVERPATH, 'modules/search'))
  37. let diskSearchEngines = []
  38. for (let dir of searchEnginesDirs) {
  39. const def = await fs.readFile(path.join(WIKI.SERVERPATH, 'modules/search', dir, 'definition.yml'), 'utf8')
  40. diskSearchEngines.push(yaml.safeLoad(def))
  41. }
  42. WIKI.data.searchEngines = diskSearchEngines.map(searchEngine => ({
  43. ...searchEngine,
  44. props: commonHelper.parseModuleProps(searchEngine.props)
  45. }))
  46. // -> Insert new searchEngines
  47. let newSearchEngines = []
  48. for (let searchEngine of WIKI.data.searchEngines) {
  49. if (!_.some(dbSearchEngines, ['key', searchEngine.key])) {
  50. newSearchEngines.push({
  51. key: searchEngine.key,
  52. isEnabled: false,
  53. config: _.transform(searchEngine.props, (result, value, key) => {
  54. _.set(result, key, value.default)
  55. return result
  56. }, {})
  57. })
  58. } else {
  59. const searchEngineConfig = _.get(_.find(dbSearchEngines, ['key', searchEngine.key]), 'config', {})
  60. await WIKI.models.searchEngines.query().patch({
  61. config: _.transform(searchEngine.props, (result, value, key) => {
  62. if (!_.has(result, key)) {
  63. _.set(result, key, value.default)
  64. }
  65. return result
  66. }, searchEngineConfig)
  67. }).where('key', searchEngine.key)
  68. }
  69. }
  70. if (newSearchEngines.length > 0) {
  71. trx = await WIKI.models.Objection.transaction.start(WIKI.models.knex)
  72. for (let searchEngine of newSearchEngines) {
  73. await WIKI.models.searchEngines.query(trx).insert(searchEngine)
  74. }
  75. await trx.commit()
  76. WIKI.logger.info(`Loaded ${newSearchEngines.length} new search engines: [ OK ]`)
  77. } else {
  78. WIKI.logger.info(`No new search engines found: [ SKIPPED ]`)
  79. }
  80. } catch (err) {
  81. WIKI.logger.error(`Failed to scan or load new search engines: [ FAILED ]`)
  82. WIKI.logger.error(err)
  83. if (trx) {
  84. trx.rollback()
  85. }
  86. }
  87. }
  88. static async initEngine({ activate = false } = {}) {
  89. const searchEngine = await WIKI.models.searchEngines.query().findOne('isEnabled', true)
  90. if (searchEngine) {
  91. WIKI.data.searchEngine = require(`../modules/search/${searchEngine.key}/engine`)
  92. WIKI.data.searchEngine.key = searchEngine.key
  93. WIKI.data.searchEngine.config = searchEngine.config
  94. if (activate) {
  95. try {
  96. await WIKI.data.searchEngine.activate()
  97. } catch (err) {
  98. // -> Revert to basic engine
  99. if (err instanceof WIKI.Error.SearchActivationFailed) {
  100. await WIKI.models.searchEngines.query().patch({ isEnabled: false }).where('key', searchEngine.key)
  101. await WIKI.models.searchEngines.query().patch({ isEnabled: true }).where('key', 'db')
  102. await WIKI.models.searchEngines.initEngine()
  103. }
  104. throw err
  105. }
  106. }
  107. try {
  108. await WIKI.data.searchEngine.init()
  109. } catch (err) {
  110. WIKI.logger.warn(err)
  111. }
  112. }
  113. }
  114. }