feat: rebuild page tree worker
This commit is contained in:
63
server/jobs/rebuild-tree.js
Normal file
63
server/jobs/rebuild-tree.js
Normal file
@@ -0,0 +1,63 @@
|
||||
const _ = require('lodash')
|
||||
|
||||
/* global WIKI */
|
||||
|
||||
module.exports = async (pageId) => {
|
||||
WIKI.logger.info(`Rebuilding page tree...`)
|
||||
|
||||
try {
|
||||
WIKI.models = require('../core/db').init()
|
||||
await WIKI.configSvc.loadFromDb()
|
||||
await WIKI.configSvc.applyFlags()
|
||||
|
||||
await WIKI.models.knex.table('pageTree').truncate()
|
||||
const pages = await WIKI.models.pages.query().select('id', 'path', 'localeCode', 'title', 'isPrivate', 'privateNS').orderBy(['localeCode', 'path'])
|
||||
let tree = []
|
||||
let pik = 0
|
||||
|
||||
for (const page of pages) {
|
||||
const pagePaths = page.path.split('/')
|
||||
let currentPath = ''
|
||||
let depth = 0
|
||||
let parentId = null
|
||||
for (const part of pagePaths) {
|
||||
depth++
|
||||
const isFolder = (depth < pagePaths.length)
|
||||
currentPath = currentPath ? `${currentPath}/${part}` : part
|
||||
const found = _.find(tree, {
|
||||
localeCode: page.localeCode,
|
||||
path: currentPath
|
||||
})
|
||||
if (!found) {
|
||||
pik++
|
||||
tree.push({
|
||||
id: pik,
|
||||
localeCode: page.localeCode,
|
||||
path: currentPath,
|
||||
depth: depth,
|
||||
title: isFolder ? part : page.title,
|
||||
isFolder: isFolder,
|
||||
isPrivate: !isFolder && page.isPrivate,
|
||||
privateNS: !isFolder ? page.privateNS : null,
|
||||
parent: parentId,
|
||||
pageId: isFolder ? null : page.id
|
||||
})
|
||||
parentId = pik
|
||||
} else {
|
||||
parentId = found.id
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (tree.length > 0) {
|
||||
await WIKI.models.knex.table('pageTree').insert(tree)
|
||||
}
|
||||
|
||||
await WIKI.models.knex.destroy()
|
||||
|
||||
WIKI.logger.info(`Rebuilding page tree: [ COMPLETED ]`)
|
||||
} catch (err) {
|
||||
WIKI.logger.error(`Rebuilding page tree: [ FAILED ]`)
|
||||
WIKI.logger.error(err.message)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user