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.

99 lines
2.1 KiB

  1. /* global WIKI */
  2. module.exports = {
  3. activate() {
  4. // not used
  5. },
  6. deactivate() {
  7. // not used
  8. },
  9. /**
  10. * INIT
  11. */
  12. init() {
  13. // not used
  14. },
  15. /**
  16. * QUERY
  17. *
  18. * @param {String} q Query
  19. * @param {Object} opts Additional options
  20. */
  21. async query(q, opts) {
  22. const results = await WIKI.models.pages.query()
  23. .column('id', 'title', 'description', 'path', 'localeCode as locale')
  24. .where(builder => {
  25. builder.where('isPublished', true)
  26. if (opts.locale) {
  27. builder.andWhere('localeCode', opts.locale)
  28. }
  29. if (opts.path) {
  30. builder.andWhere('path', 'like', `${opts.path}%`)
  31. }
  32. // TODO: Add user permissions filtering
  33. builder.andWhere(builder => {
  34. switch (WIKI.config.db.type) {
  35. case 'postgres':
  36. builder.where('title', 'ILIKE', `%${q}%`)
  37. builder.orWhere('description', 'ILIKE', `%${q}%`)
  38. break
  39. case 'mysql':
  40. case 'mariadb':
  41. builder.whereRaw(`title LIKE '%?%' COLLATE utf8_general_ci`, [q])
  42. builder.orWhereRaw(`description LIKE '%?%' COLLATE utf8_general_ci`, [q])
  43. break
  44. // TODO: MSSQL handling
  45. default:
  46. builder.where('title', 'LIKE', `%${q}%`)
  47. builder.orWhere('description', 'LIKE', `%${q}%`)
  48. break
  49. }
  50. })
  51. })
  52. .limit(WIKI.config.search.maxHits)
  53. return {
  54. results,
  55. suggestions: [],
  56. totalHits: results.length
  57. }
  58. },
  59. /**
  60. * CREATE
  61. *
  62. * @param {Object} page Page to create
  63. */
  64. async created(page) {
  65. // not used
  66. },
  67. /**
  68. * UPDATE
  69. *
  70. * @param {Object} page Page to update
  71. */
  72. async updated(page) {
  73. // not used
  74. },
  75. /**
  76. * DELETE
  77. *
  78. * @param {Object} page Page to delete
  79. */
  80. async deleted(page) {
  81. // not used
  82. },
  83. /**
  84. * RENAME
  85. *
  86. * @param {Object} page Page to rename
  87. */
  88. async renamed(page) {
  89. // not used
  90. },
  91. /**
  92. * REBUILD INDEX
  93. */
  94. async rebuild() {
  95. // not used
  96. }
  97. }