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.

66 lines
1.9 KiB

  1. const _ = require('lodash')
  2. /* global WIKI */
  3. module.exports = async (pageId) => {
  4. WIKI.logger.info(`Rebuilding page tree...`)
  5. try {
  6. WIKI.models = require('../core/db').init()
  7. await WIKI.configSvc.loadFromDb()
  8. await WIKI.configSvc.applyFlags()
  9. const pages = await WIKI.models.pages.query().select('id', 'path', 'localeCode', 'title', 'isPrivate', 'privateNS').orderBy(['localeCode', 'path'])
  10. let tree = []
  11. let pik = 0
  12. for (const page of pages) {
  13. const pagePaths = page.path.split('/')
  14. let currentPath = ''
  15. let depth = 0
  16. let parentId = null
  17. for (const part of pagePaths) {
  18. depth++
  19. const isFolder = (depth < pagePaths.length)
  20. currentPath = currentPath ? `${currentPath}/${part}` : part
  21. const found = _.find(tree, {
  22. localeCode: page.localeCode,
  23. path: currentPath
  24. })
  25. if (!found) {
  26. pik++
  27. tree.push({
  28. id: pik,
  29. localeCode: page.localeCode,
  30. path: currentPath,
  31. depth: depth,
  32. title: isFolder ? part : page.title,
  33. isFolder: isFolder,
  34. isPrivate: !isFolder && page.isPrivate,
  35. privateNS: !isFolder ? page.privateNS : null,
  36. parent: parentId,
  37. pageId: isFolder ? null : page.id
  38. })
  39. parentId = pik
  40. } else if (isFolder && !found.isFolder) {
  41. found.isFolder = true
  42. parentId = found.id
  43. } else {
  44. parentId = found.id
  45. }
  46. }
  47. }
  48. await WIKI.models.knex.table('pageTree').truncate()
  49. if (tree.length > 0) {
  50. await WIKI.models.knex.table('pageTree').insert(tree)
  51. }
  52. await WIKI.models.knex.destroy()
  53. WIKI.logger.info(`Rebuilding page tree: [ COMPLETED ]`)
  54. } catch (err) {
  55. WIKI.logger.error(`Rebuilding page tree: [ FAILED ]`)
  56. WIKI.logger.error(err.message)
  57. }
  58. }