2018-05-20 22:50:51 +00:00
|
|
|
const Model = require('objection').Model
|
2018-08-19 05:22:59 +00:00
|
|
|
const _ = require('lodash')
|
2018-09-16 00:58:23 +00:00
|
|
|
const JSBinType = require('js-binary').Type
|
|
|
|
const pageHelper = require('../helpers/page')
|
|
|
|
const path = require('path')
|
|
|
|
const fs = require('fs-extra')
|
2019-02-25 04:48:28 +00:00
|
|
|
const yaml = require('js-yaml')
|
2019-03-24 04:27:42 +00:00
|
|
|
const striptags = require('striptags')
|
|
|
|
const emojiRegex = require('emoji-regex')
|
2019-09-21 14:36:09 +00:00
|
|
|
const he = require('he')
|
2020-06-20 05:11:05 +00:00
|
|
|
const CleanCSS = require('clean-css')
|
2021-04-02 04:39:11 +00:00
|
|
|
const TurndownService = require('turndown')
|
|
|
|
const turndownPluginGfm = require('@joplin/turndown-plugin-gfm').gfm
|
|
|
|
const cheerio = require('cheerio')
|
2018-05-20 22:50:51 +00:00
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
/* global WIKI */
|
|
|
|
|
2019-02-25 04:48:28 +00:00
|
|
|
const frontmatterRegex = {
|
|
|
|
html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
|
2019-11-04 02:59:58 +00:00
|
|
|
legacy: /^(<!-- TITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)*([\w\W]*)*/i,
|
2019-02-25 04:48:28 +00:00
|
|
|
markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
|
|
|
|
}
|
|
|
|
|
2019-03-24 04:27:42 +00:00
|
|
|
const punctuationRegex = /[!,:;/\\_+\-=()&#@<>$~%^*[\]{}"'|]+|(\.\s)|(\s\.)/ig
|
2019-09-21 14:36:09 +00:00
|
|
|
// const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
/**
|
|
|
|
* Pages model
|
|
|
|
*/
|
|
|
|
module.exports = class Page extends Model {
|
|
|
|
static get tableName() { return 'pages' }
|
|
|
|
|
|
|
|
static get jsonSchema () {
|
|
|
|
return {
|
|
|
|
type: 'object',
|
|
|
|
required: ['path', 'title'],
|
|
|
|
|
|
|
|
properties: {
|
|
|
|
id: {type: 'integer'},
|
|
|
|
path: {type: 'string'},
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
title: {type: 'string'},
|
|
|
|
description: {type: 'string'},
|
|
|
|
isPublished: {type: 'boolean'},
|
2018-08-19 05:22:59 +00:00
|
|
|
privateNS: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
publishStartDate: {type: 'string'},
|
|
|
|
publishEndDate: {type: 'string'},
|
|
|
|
content: {type: 'string'},
|
2018-07-30 02:23:33 +00:00
|
|
|
contentType: {type: 'string'},
|
2018-05-20 22:50:51 +00:00
|
|
|
|
|
|
|
createdAt: {type: 'string'},
|
|
|
|
updatedAt: {type: 'string'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:39:36 +00:00
|
|
|
static get jsonAttributes() {
|
|
|
|
return ['extra']
|
|
|
|
}
|
|
|
|
|
2018-05-20 22:50:51 +00:00
|
|
|
static get relationMappings() {
|
|
|
|
return {
|
|
|
|
tags: {
|
|
|
|
relation: Model.ManyToManyRelation,
|
|
|
|
modelClass: require('./tags'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.id',
|
|
|
|
through: {
|
|
|
|
from: 'pageTags.pageId',
|
|
|
|
to: 'pageTags.tagId'
|
|
|
|
},
|
|
|
|
to: 'tags.id'
|
|
|
|
}
|
|
|
|
},
|
2019-09-02 22:32:33 +00:00
|
|
|
links: {
|
2019-09-03 02:18:59 +00:00
|
|
|
relation: Model.HasManyRelation,
|
|
|
|
modelClass: require('./pageLinks'),
|
2019-09-02 22:32:33 +00:00
|
|
|
join: {
|
|
|
|
from: 'pages.id',
|
2019-09-03 02:18:59 +00:00
|
|
|
to: 'pageLinks.pageId'
|
2019-09-02 22:32:33 +00:00
|
|
|
}
|
|
|
|
},
|
2018-05-20 22:50:51 +00:00
|
|
|
author: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.authorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
2018-07-23 01:13:01 +00:00
|
|
|
creator: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./users'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.creatorId',
|
|
|
|
to: 'users.id'
|
|
|
|
}
|
|
|
|
},
|
2018-07-22 04:29:39 +00:00
|
|
|
editor: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./editors'),
|
|
|
|
join: {
|
|
|
|
from: 'pages.editorKey',
|
|
|
|
to: 'editors.key'
|
|
|
|
}
|
|
|
|
},
|
2018-05-20 22:50:51 +00:00
|
|
|
locale: {
|
|
|
|
relation: Model.BelongsToOneRelation,
|
|
|
|
modelClass: require('./locales'),
|
|
|
|
join: {
|
2018-07-22 04:29:39 +00:00
|
|
|
from: 'pages.localeCode',
|
2018-05-20 22:50:51 +00:00
|
|
|
to: 'locales.code'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$beforeUpdate() {
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
|
|
|
$beforeInsert() {
|
|
|
|
this.createdAt = new Date().toISOString()
|
|
|
|
this.updatedAt = new Date().toISOString()
|
|
|
|
}
|
2020-07-29 23:39:47 +00:00
|
|
|
/**
|
|
|
|
* Solving the violates foreign key constraint using cascade strategy
|
|
|
|
* using static hooks
|
|
|
|
* @see https://vincit.github.io/objection.js/api/types/#type-statichookarguments
|
|
|
|
*/
|
|
|
|
static async beforeDelete({ asFindQuery }) {
|
|
|
|
const page = await asFindQuery().select('id')
|
|
|
|
await WIKI.models.comments.query().delete().where('pageId', page[0].id)
|
|
|
|
}
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Cache Schema
|
|
|
|
*/
|
2018-09-16 00:58:23 +00:00
|
|
|
static get cacheSchema() {
|
|
|
|
return new JSBinType({
|
2019-01-26 23:35:56 +00:00
|
|
|
id: 'uint',
|
2018-09-16 00:58:23 +00:00
|
|
|
authorId: 'uint',
|
|
|
|
authorName: 'string',
|
|
|
|
createdAt: 'string',
|
|
|
|
creatorId: 'uint',
|
|
|
|
creatorName: 'string',
|
|
|
|
description: 'string',
|
2021-04-02 04:39:11 +00:00
|
|
|
editorKey: 'string',
|
2018-09-16 00:58:23 +00:00
|
|
|
isPrivate: 'boolean',
|
|
|
|
isPublished: 'boolean',
|
|
|
|
publishEndDate: 'string',
|
|
|
|
publishStartDate: 'string',
|
|
|
|
render: 'string',
|
2019-09-02 22:32:33 +00:00
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: 'string',
|
|
|
|
title: 'string'
|
|
|
|
}
|
|
|
|
],
|
2020-06-20 20:39:36 +00:00
|
|
|
extra: {
|
|
|
|
js: 'string',
|
|
|
|
css: 'string'
|
|
|
|
},
|
2018-09-16 00:58:23 +00:00
|
|
|
title: 'string',
|
2018-11-18 04:03:58 +00:00
|
|
|
toc: 'string',
|
2018-09-16 00:58:23 +00:00
|
|
|
updatedAt: 'string'
|
|
|
|
})
|
2018-08-19 05:22:59 +00:00
|
|
|
}
|
|
|
|
|
2019-02-10 23:06:03 +00:00
|
|
|
/**
|
|
|
|
* Inject page metadata into contents
|
2019-09-14 17:14:07 +00:00
|
|
|
*
|
|
|
|
* @returns {string} Page Contents with Injected Metadata
|
2019-02-10 23:06:03 +00:00
|
|
|
*/
|
|
|
|
injectMetadata () {
|
2019-04-06 23:05:47 +00:00
|
|
|
return pageHelper.injectPageMetadata(this)
|
2019-02-10 23:06:03 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 01:11:25 +00:00
|
|
|
/**
|
|
|
|
* Get the page's file extension based on content type
|
2019-09-14 17:14:07 +00:00
|
|
|
*
|
|
|
|
* @returns {string} File Extension
|
2019-09-09 01:11:25 +00:00
|
|
|
*/
|
|
|
|
getFileExtension() {
|
2019-09-27 18:17:12 +00:00
|
|
|
return pageHelper.getFileExtension(this.contentType)
|
2019-09-09 01:11:25 +00:00
|
|
|
}
|
|
|
|
|
2019-02-25 04:48:28 +00:00
|
|
|
/**
|
|
|
|
* Parse injected page metadata from raw content
|
|
|
|
*
|
|
|
|
* @param {String} raw Raw file contents
|
|
|
|
* @param {String} contentType Content Type
|
2019-09-14 17:14:07 +00:00
|
|
|
* @returns {Object} Parsed Page Metadata with Raw Content
|
2019-02-25 04:48:28 +00:00
|
|
|
*/
|
|
|
|
static parseMetadata (raw, contentType) {
|
|
|
|
let result
|
2022-04-03 19:59:58 +00:00
|
|
|
try {
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
result = frontmatterRegex.markdown.exec(raw)
|
2019-02-25 04:48:28 +00:00
|
|
|
if (result[2]) {
|
|
|
|
return {
|
2022-04-03 19:59:58 +00:00
|
|
|
...yaml.safeLoad(result[2]),
|
|
|
|
content: result[3]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Attempt legacy v1 format
|
|
|
|
result = frontmatterRegex.legacy.exec(raw)
|
|
|
|
if (result[2]) {
|
|
|
|
return {
|
|
|
|
title: result[2],
|
|
|
|
description: result[4],
|
|
|
|
content: result[5]
|
|
|
|
}
|
2019-02-25 04:48:28 +00:00
|
|
|
}
|
|
|
|
}
|
2022-04-03 19:59:58 +00:00
|
|
|
break
|
|
|
|
case 'html':
|
|
|
|
result = frontmatterRegex.html.exec(raw)
|
|
|
|
if (result[2]) {
|
|
|
|
return {
|
|
|
|
...yaml.safeLoad(result[2]),
|
|
|
|
content: result[3]
|
|
|
|
}
|
2019-02-25 04:48:28 +00:00
|
|
|
}
|
2022-04-03 19:59:58 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn('Failed to parse page metadata. Invalid syntax.')
|
2019-02-25 04:48:28 +00:00
|
|
|
}
|
|
|
|
return {
|
|
|
|
content: raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Create a New Page
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
2018-07-22 20:25:39 +00:00
|
|
|
static async createPage(opts) {
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Validate path
|
2020-10-14 15:16:27 +00:00
|
|
|
if (opts.path.includes('.') || opts.path.includes(' ') || opts.path.includes('\\') || opts.path.includes('//')) {
|
2019-10-02 04:44:18 +00:00
|
|
|
throw new WIKI.Error.PageIllegalPath()
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:39:07 +00:00
|
|
|
// -> Remove trailing slash
|
2020-04-25 06:58:00 +00:00
|
|
|
if (opts.path.endsWith('/')) {
|
2020-04-25 00:39:07 +00:00
|
|
|
opts.path = opts.path.slice(0, -1)
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:52:36 +00:00
|
|
|
// -> Remove starting slash
|
|
|
|
if (opts.path.startsWith('/')) {
|
|
|
|
opts.path = opts.path.slice(1)
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Check for page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageDeleteForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check for duplicate
|
2019-06-29 20:45:27 +00:00
|
|
|
const dupCheck = await WIKI.models.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
|
|
|
|
if (dupCheck) {
|
|
|
|
throw new WIKI.Error.PageDuplicateCreate()
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Check for empty content
|
2019-08-27 03:19:39 +00:00
|
|
|
if (!opts.content || _.trim(opts.content).length < 1) {
|
|
|
|
throw new WIKI.Error.PageEmptyContent()
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:39:36 +00:00
|
|
|
// -> Format CSS Scripts
|
2020-06-20 05:11:05 +00:00
|
|
|
let scriptCss = ''
|
|
|
|
if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
if (!_.isEmpty(opts.scriptCss)) {
|
|
|
|
scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
|
|
|
|
} else {
|
|
|
|
scriptCss = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Format JS Scripts
|
|
|
|
let scriptJs = ''
|
|
|
|
if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
scriptJs = opts.scriptJs || ''
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Create page
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().insert({
|
2019-10-13 23:59:50 +00:00
|
|
|
authorId: opts.user.id,
|
2018-07-22 20:25:39 +00:00
|
|
|
content: opts.content,
|
2019-10-13 23:59:50 +00:00
|
|
|
creatorId: opts.user.id,
|
2018-08-20 05:02:57 +00:00
|
|
|
contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
|
2018-07-22 20:25:39 +00:00
|
|
|
description: opts.description,
|
|
|
|
editorKey: opts.editor,
|
2018-09-16 00:58:23 +00:00
|
|
|
hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
|
2018-07-22 20:25:39 +00:00
|
|
|
isPrivate: opts.isPrivate,
|
|
|
|
isPublished: opts.isPublished,
|
|
|
|
localeCode: opts.locale,
|
|
|
|
path: opts.path,
|
2018-11-18 04:03:58 +00:00
|
|
|
publishEndDate: opts.publishEndDate || '',
|
|
|
|
publishStartDate: opts.publishStartDate || '',
|
|
|
|
title: opts.title,
|
2020-06-20 05:11:05 +00:00
|
|
|
toc: '[]',
|
|
|
|
extra: JSON.stringify({
|
|
|
|
js: scriptJs,
|
|
|
|
css: scriptCss
|
|
|
|
})
|
2018-07-22 20:25:39 +00:00
|
|
|
})
|
2018-09-16 04:35:03 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: opts.path,
|
|
|
|
locale: opts.locale,
|
2019-10-13 23:59:50 +00:00
|
|
|
userId: opts.user.id,
|
2018-09-16 04:35:03 +00:00
|
|
|
isPrivate: opts.isPrivate
|
|
|
|
})
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-09-01 22:33:36 +00:00
|
|
|
// -> Save Tags
|
2019-10-07 04:06:47 +00:00
|
|
|
if (opts.tags && opts.tags.length > 0) {
|
2019-09-01 22:33:36 +00:00
|
|
|
await WIKI.models.tags.associateTags({ tags: opts.tags, page })
|
|
|
|
}
|
|
|
|
|
2019-03-24 04:27:42 +00:00
|
|
|
// -> Render page to HTML
|
2018-09-10 00:33:10 +00:00
|
|
|
await WIKI.models.pages.renderPage(page)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-10-14 02:35:42 +00:00
|
|
|
// -> Rebuild page tree
|
|
|
|
await WIKI.models.pages.rebuildTree()
|
|
|
|
|
2019-03-24 04:27:42 +00:00
|
|
|
// -> Add to Search Index
|
|
|
|
const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
|
|
|
|
page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
|
2019-03-11 04:47:27 +00:00
|
|
|
await WIKI.data.searchEngine.created(page)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
|
|
|
// -> Add to Storage
|
2019-02-25 04:48:28 +00:00
|
|
|
if (!opts.skipStorage) {
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'created',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
}
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
// -> Reconnect Links
|
|
|
|
await WIKI.models.pages.reconnectLinks({
|
|
|
|
locale: page.localeCode,
|
|
|
|
path: page.path,
|
|
|
|
mode: 'create'
|
|
|
|
})
|
|
|
|
|
2020-03-21 23:18:08 +00:00
|
|
|
// -> Get latest updatedAt
|
|
|
|
page.updatedAt = await WIKI.models.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
|
|
|
|
|
2018-07-23 01:13:01 +00:00
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Update an Existing Page
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
2018-07-23 01:13:01 +00:00
|
|
|
static async updatePage(opts) {
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Fetch original page
|
2018-07-30 02:23:33 +00:00
|
|
|
const ogPage = await WIKI.models.pages.query().findById(opts.id)
|
2018-07-23 01:13:01 +00:00
|
|
|
if (!ogPage) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
2019-08-27 03:19:39 +00:00
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Check for page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
|
2022-02-20 23:46:03 +00:00
|
|
|
locale: ogPage.localeCode,
|
|
|
|
path: ogPage.path
|
2019-10-13 23:59:50 +00:00
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageUpdateForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check for empty content
|
2019-08-27 03:19:39 +00:00
|
|
|
if (!opts.content || _.trim(opts.content).length < 1) {
|
|
|
|
throw new WIKI.Error.PageEmptyContent()
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Create version snapshot
|
2019-01-26 23:35:56 +00:00
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...ogPage,
|
2019-02-22 22:05:18 +00:00
|
|
|
isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
|
2020-03-01 04:40:07 +00:00
|
|
|
action: opts.action ? opts.action : 'updated',
|
|
|
|
versionDate: ogPage.updatedAt
|
2019-01-26 23:35:56 +00:00
|
|
|
})
|
2019-10-13 23:59:50 +00:00
|
|
|
|
2020-06-20 05:11:05 +00:00
|
|
|
// -> Format Extra Properties
|
|
|
|
if (!_.isPlainObject(ogPage.extra)) {
|
|
|
|
ogPage.extra = {}
|
|
|
|
}
|
|
|
|
|
2020-06-20 20:39:36 +00:00
|
|
|
// -> Format CSS Scripts
|
2020-06-20 05:11:05 +00:00
|
|
|
let scriptCss = _.get(ogPage, 'extra.css', '')
|
|
|
|
if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
if (!_.isEmpty(opts.scriptCss)) {
|
|
|
|
scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
|
|
|
|
} else {
|
|
|
|
scriptCss = ''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Format JS Scripts
|
|
|
|
let scriptJs = _.get(ogPage, 'extra.js', '')
|
|
|
|
if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
scriptJs = opts.scriptJs || ''
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Update page
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().patch({
|
2019-10-13 23:59:50 +00:00
|
|
|
authorId: opts.user.id,
|
2018-07-30 02:23:33 +00:00
|
|
|
content: opts.content,
|
|
|
|
description: opts.description,
|
2019-02-25 04:48:28 +00:00
|
|
|
isPublished: opts.isPublished === true || opts.isPublished === 1,
|
2018-11-18 04:03:58 +00:00
|
|
|
publishEndDate: opts.publishEndDate || '',
|
|
|
|
publishStartDate: opts.publishStartDate || '',
|
2020-06-20 05:11:05 +00:00
|
|
|
title: opts.title,
|
|
|
|
extra: JSON.stringify({
|
|
|
|
...ogPage.extra,
|
|
|
|
js: scriptJs,
|
|
|
|
css: scriptCss
|
|
|
|
})
|
2018-09-16 00:58:23 +00:00
|
|
|
}).where('id', ogPage.id)
|
2020-03-21 23:18:08 +00:00
|
|
|
let page = await WIKI.models.pages.getPageFromDb(ogPage.id)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-09-01 22:33:36 +00:00
|
|
|
// -> Save Tags
|
|
|
|
await WIKI.models.tags.associateTags({ tags: opts.tags, page })
|
|
|
|
|
2019-03-24 04:27:42 +00:00
|
|
|
// -> Render page to HTML
|
2018-09-10 00:33:10 +00:00
|
|
|
await WIKI.models.pages.renderPage(page)
|
2020-04-20 02:41:19 +00:00
|
|
|
WIKI.events.outbound.emit('deletePageFromCache', page.hash)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
|
|
|
// -> Update Search Index
|
|
|
|
const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
|
|
|
|
page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
|
2019-03-11 04:47:27 +00:00
|
|
|
await WIKI.data.searchEngine.updated(page)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
|
|
|
// -> Update on Storage
|
2019-02-25 04:48:28 +00:00
|
|
|
if (!opts.skipStorage) {
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'updated',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
}
|
2019-10-13 23:59:50 +00:00
|
|
|
|
|
|
|
// -> Perform move?
|
2019-10-15 03:44:37 +00:00
|
|
|
if ((opts.locale && opts.locale !== page.localeCode) || (opts.path && opts.path !== page.path)) {
|
2022-02-20 23:46:03 +00:00
|
|
|
// -> Check target path access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
|
|
|
|
locale: opts.locale,
|
|
|
|
path: opts.path
|
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageMoveForbidden()
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
await WIKI.models.pages.movePage({
|
|
|
|
id: page.id,
|
|
|
|
destinationLocale: opts.locale,
|
|
|
|
destinationPath: opts.path,
|
|
|
|
user: opts.user
|
|
|
|
})
|
2019-10-14 02:35:42 +00:00
|
|
|
} else {
|
|
|
|
// -> Update title of page tree entry
|
|
|
|
await WIKI.models.knex.table('pageTree').where({
|
|
|
|
pageId: page.id
|
|
|
|
}).update('title', page.title)
|
2019-10-13 23:59:50 +00:00
|
|
|
}
|
|
|
|
|
2020-03-21 23:18:08 +00:00
|
|
|
// -> Get latest updatedAt
|
|
|
|
page.updatedAt = await WIKI.models.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
|
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
return page
|
|
|
|
}
|
2018-08-09 04:07:15 +00:00
|
|
|
|
2021-04-02 04:39:11 +00:00
|
|
|
/**
|
|
|
|
* Convert an Existing Page
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
|
|
|
static async convertPage(opts) {
|
|
|
|
// -> Fetch original page
|
|
|
|
const ogPage = await WIKI.models.pages.query().findById(opts.id)
|
|
|
|
if (!ogPage) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
|
|
|
|
2021-04-02 19:15:35 +00:00
|
|
|
if (ogPage.editorKey === opts.editor) {
|
|
|
|
throw new Error('Page is already using this editor. Nothing to convert.')
|
|
|
|
}
|
|
|
|
|
2021-04-02 04:39:11 +00:00
|
|
|
// -> Check for page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
|
|
|
|
locale: ogPage.localeCode,
|
|
|
|
path: ogPage.path
|
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageUpdateForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check content type
|
|
|
|
const sourceContentType = ogPage.contentType
|
|
|
|
const targetContentType = _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text')
|
|
|
|
const shouldConvert = sourceContentType !== targetContentType
|
|
|
|
let convertedContent = null
|
|
|
|
|
|
|
|
// -> Convert content
|
|
|
|
if (shouldConvert) {
|
|
|
|
// -> Markdown => HTML
|
|
|
|
if (sourceContentType === 'markdown' && targetContentType === 'html') {
|
|
|
|
if (!ogPage.render) {
|
|
|
|
throw new Error('Aborted conversion because rendered page content is empty!')
|
|
|
|
}
|
|
|
|
convertedContent = ogPage.render
|
|
|
|
|
|
|
|
const $ = cheerio.load(convertedContent, {
|
|
|
|
decodeEntities: true
|
|
|
|
})
|
|
|
|
|
|
|
|
if ($.root().children().length > 0) {
|
2021-04-02 21:30:04 +00:00
|
|
|
// Remove header anchors
|
2021-04-02 04:39:11 +00:00
|
|
|
$('.toc-anchor').remove()
|
|
|
|
|
2021-04-02 21:30:04 +00:00
|
|
|
// Attempt to convert tabsets
|
|
|
|
$('tabset').each((tabI, tabElm) => {
|
|
|
|
const tabHeaders = []
|
|
|
|
// -> Extract templates
|
|
|
|
$(tabElm).children('template').each((tmplI, tmplElm) => {
|
|
|
|
if ($(tmplElm).attr('v-slot:tabs') === '') {
|
|
|
|
$(tabElm).before('<ul class="tabset-headers">' + $(tmplElm).html() + '</ul>')
|
|
|
|
} else {
|
|
|
|
$(tabElm).after('<div class="markdown-tabset">' + $(tmplElm).html() + '</div>')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
// -> Parse tab headers
|
|
|
|
$(tabElm).prev('.tabset-headers').children((i, elm) => {
|
|
|
|
tabHeaders.push($(elm).html())
|
|
|
|
})
|
|
|
|
$(tabElm).prev('.tabset-headers').remove()
|
|
|
|
// -> Inject tab headers
|
|
|
|
$(tabElm).next('.markdown-tabset').children((i, elm) => {
|
|
|
|
if (tabHeaders.length > i) {
|
|
|
|
$(elm).prepend(`<h2>${tabHeaders[i]}</h2>`)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
$(tabElm).next('.markdown-tabset').prepend('<h1>Tabset</h1>')
|
|
|
|
$(tabElm).remove()
|
|
|
|
})
|
|
|
|
|
2021-04-02 04:39:11 +00:00
|
|
|
convertedContent = $.html('body').replace('<body>', '').replace('</body>', '').replace(/&#x([0-9a-f]{1,6});/ig, (entity, code) => {
|
|
|
|
code = parseInt(code, 16)
|
|
|
|
|
|
|
|
// Don't unescape ASCII characters, assuming they're encoded for a good reason
|
|
|
|
if (code < 0x80) return entity
|
|
|
|
|
|
|
|
return String.fromCodePoint(code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> HTML => Markdown
|
|
|
|
} else if (sourceContentType === 'html' && targetContentType === 'markdown') {
|
|
|
|
const td = new TurndownService({
|
|
|
|
bulletListMarker: '-',
|
|
|
|
codeBlockStyle: 'fenced',
|
|
|
|
emDelimiter: '*',
|
|
|
|
fence: '```',
|
|
|
|
headingStyle: 'atx',
|
|
|
|
hr: '---',
|
|
|
|
linkStyle: 'inlined',
|
|
|
|
preformattedCode: true,
|
|
|
|
strongDelimiter: '**'
|
|
|
|
})
|
|
|
|
|
|
|
|
td.use(turndownPluginGfm)
|
|
|
|
|
|
|
|
td.keep(['kbd'])
|
|
|
|
|
|
|
|
td.addRule('subscript', {
|
|
|
|
filter: ['sub'],
|
|
|
|
replacement: c => `~${c}~`
|
|
|
|
})
|
|
|
|
|
|
|
|
td.addRule('superscript', {
|
|
|
|
filter: ['sup'],
|
|
|
|
replacement: c => `^${c}^`
|
|
|
|
})
|
|
|
|
|
|
|
|
td.addRule('underline', {
|
|
|
|
filter: ['u'],
|
|
|
|
replacement: c => `_${c}_`
|
|
|
|
})
|
|
|
|
|
2021-04-02 19:15:35 +00:00
|
|
|
td.addRule('taskList', {
|
|
|
|
filter: (n, o) => {
|
|
|
|
return n.nodeName === 'INPUT' && n.getAttribute('type') === 'checkbox'
|
|
|
|
},
|
|
|
|
replacement: (c, n) => {
|
|
|
|
return n.getAttribute('checked') ? '[x] ' : '[ ] '
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2021-04-02 04:39:11 +00:00
|
|
|
td.addRule('removeTocAnchors', {
|
|
|
|
filter: (n, o) => {
|
|
|
|
return n.nodeName === 'A' && n.classList.contains('toc-anchor')
|
|
|
|
},
|
|
|
|
replacement: c => ''
|
|
|
|
})
|
|
|
|
|
|
|
|
convertedContent = td.turndown(ogPage.content)
|
|
|
|
// -> Unsupported
|
|
|
|
} else {
|
|
|
|
throw new Error('Unsupported source / destination content types combination.')
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Create version snapshot
|
|
|
|
if (shouldConvert) {
|
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...ogPage,
|
|
|
|
isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
|
|
|
|
action: 'updated',
|
|
|
|
versionDate: ogPage.updatedAt
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Update page
|
|
|
|
await WIKI.models.pages.query().patch({
|
|
|
|
contentType: targetContentType,
|
|
|
|
editorKey: opts.editor,
|
|
|
|
...(convertedContent ? { content: convertedContent } : {})
|
|
|
|
}).where('id', ogPage.id)
|
|
|
|
const page = await WIKI.models.pages.getPageFromDb(ogPage.id)
|
|
|
|
|
|
|
|
await WIKI.models.pages.deletePageFromCache(page.hash)
|
|
|
|
WIKI.events.outbound.emit('deletePageFromCache', page.hash)
|
|
|
|
|
|
|
|
// -> Update on Storage
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'updated',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
/**
|
|
|
|
* Move a Page
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
|
|
|
static async movePage(opts) {
|
2021-09-01 16:11:33 +00:00
|
|
|
let page
|
|
|
|
if (_.has(opts, 'id')) {
|
|
|
|
page = await WIKI.models.pages.query().findById(opts.id)
|
|
|
|
} else {
|
|
|
|
page = await WIKI.models.pages.query().findOne({
|
|
|
|
path: opts.path,
|
|
|
|
localeCode: opts.locale
|
|
|
|
})
|
|
|
|
}
|
2019-10-13 23:59:50 +00:00
|
|
|
if (!page) {
|
|
|
|
throw new WIKI.Error.PageNotFound()
|
|
|
|
}
|
|
|
|
|
2020-04-25 00:39:07 +00:00
|
|
|
// -> Validate path
|
2020-10-14 15:16:27 +00:00
|
|
|
if (opts.destinationPath.includes('.') || opts.destinationPath.includes(' ') || opts.destinationPath.includes('\\') || opts.destinationPath.includes('//')) {
|
2020-04-25 00:39:07 +00:00
|
|
|
throw new WIKI.Error.PageIllegalPath()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Remove trailing slash
|
2020-04-25 06:58:00 +00:00
|
|
|
if (opts.destinationPath.endsWith('/')) {
|
2020-04-25 00:39:07 +00:00
|
|
|
opts.destinationPath = opts.destinationPath.slice(0, -1)
|
|
|
|
}
|
|
|
|
|
2020-06-05 18:52:36 +00:00
|
|
|
// -> Remove starting slash
|
|
|
|
if (opts.destinationPath.startsWith('/')) {
|
|
|
|
opts.destinationPath = opts.destinationPath.slice(1)
|
|
|
|
}
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Check for source page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['manage:pages'], {
|
2020-04-22 15:08:46 +00:00
|
|
|
locale: page.localeCode,
|
|
|
|
path: page.path
|
2019-10-13 23:59:50 +00:00
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageMoveForbidden()
|
|
|
|
}
|
|
|
|
// -> Check for destination page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
|
|
|
|
locale: opts.destinationLocale,
|
|
|
|
path: opts.destinationPath
|
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageMoveForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Check for existing page at destination path
|
2020-10-14 15:16:27 +00:00
|
|
|
const destPage = await WIKI.models.pages.query().findOne({
|
2019-10-13 23:59:50 +00:00
|
|
|
path: opts.destinationPath,
|
|
|
|
localeCode: opts.destinationLocale
|
|
|
|
})
|
|
|
|
if (destPage) {
|
|
|
|
throw new WIKI.Error.PagePathCollision()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Create version snapshot
|
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...page,
|
2020-03-01 04:40:07 +00:00
|
|
|
action: 'moved',
|
|
|
|
versionDate: page.updatedAt
|
2019-10-13 23:59:50 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
const destinationHash = pageHelper.generateHash({ path: opts.destinationPath, locale: opts.destinationLocale, privateNS: opts.isPrivate ? 'TODO' : '' })
|
|
|
|
|
|
|
|
// -> Move page
|
2021-09-01 16:11:33 +00:00
|
|
|
const destinationTitle = (page.title === page.path ? opts.destinationPath : page.title)
|
2019-10-13 23:59:50 +00:00
|
|
|
await WIKI.models.pages.query().patch({
|
|
|
|
path: opts.destinationPath,
|
|
|
|
localeCode: opts.destinationLocale,
|
2021-09-01 16:11:33 +00:00
|
|
|
title: destinationTitle,
|
2019-10-13 23:59:50 +00:00
|
|
|
hash: destinationHash
|
|
|
|
}).findById(page.id)
|
2020-04-20 00:26:26 +00:00
|
|
|
await WIKI.models.pages.deletePageFromCache(page.hash)
|
|
|
|
WIKI.events.outbound.emit('deletePageFromCache', page.hash)
|
2019-10-13 23:59:50 +00:00
|
|
|
|
2019-10-14 02:35:42 +00:00
|
|
|
// -> Rebuild page tree
|
|
|
|
await WIKI.models.pages.rebuildTree()
|
|
|
|
|
2019-10-13 23:59:50 +00:00
|
|
|
// -> Rename in Search Index
|
2020-12-13 19:51:44 +00:00
|
|
|
const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
|
|
|
|
page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
|
2019-10-13 23:59:50 +00:00
|
|
|
await WIKI.data.searchEngine.renamed({
|
|
|
|
...page,
|
|
|
|
destinationPath: opts.destinationPath,
|
|
|
|
destinationLocaleCode: opts.destinationLocale,
|
|
|
|
destinationHash
|
|
|
|
})
|
|
|
|
|
|
|
|
// -> Rename in Storage
|
|
|
|
if (!opts.skipStorage) {
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'renamed',
|
|
|
|
page: {
|
|
|
|
...page,
|
|
|
|
destinationPath: opts.destinationPath,
|
|
|
|
destinationLocaleCode: opts.destinationLocale,
|
|
|
|
destinationHash,
|
|
|
|
moveAuthorId: opts.user.id,
|
|
|
|
moveAuthorName: opts.user.name,
|
|
|
|
moveAuthorEmail: opts.user.email
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-06-29 01:08:17 +00:00
|
|
|
// -> Reconnect Links : Changing old links to the new path
|
2019-10-13 23:59:50 +00:00
|
|
|
await WIKI.models.pages.reconnectLinks({
|
|
|
|
sourceLocale: page.localeCode,
|
|
|
|
sourcePath: page.path,
|
|
|
|
locale: opts.destinationLocale,
|
|
|
|
path: opts.destinationPath,
|
|
|
|
mode: 'move'
|
|
|
|
})
|
2020-10-14 15:16:27 +00:00
|
|
|
|
2020-06-29 01:08:17 +00:00
|
|
|
// -> Reconnect Links : Validate invalid links to the new path
|
|
|
|
await WIKI.models.pages.reconnectLinks({
|
|
|
|
locale: opts.destinationLocale,
|
|
|
|
path: opts.destinationPath,
|
|
|
|
mode: 'create'
|
|
|
|
})
|
2019-10-13 23:59:50 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Delete an Existing Page
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
2019-01-26 23:35:56 +00:00
|
|
|
static async deletePage(opts) {
|
2019-02-25 04:48:28 +00:00
|
|
|
let page
|
|
|
|
if (_.has(opts, 'id')) {
|
|
|
|
page = await WIKI.models.pages.query().findById(opts.id)
|
|
|
|
} else {
|
2020-10-14 15:16:27 +00:00
|
|
|
page = await WIKI.models.pages.query().findOne({
|
2019-02-25 04:48:28 +00:00
|
|
|
path: opts.path,
|
|
|
|
localeCode: opts.locale
|
|
|
|
})
|
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
if (!page) {
|
2021-09-01 16:11:33 +00:00
|
|
|
throw new WIKI.Error.PageNotFound()
|
2019-01-26 23:35:56 +00:00
|
|
|
}
|
2019-10-13 23:59:50 +00:00
|
|
|
|
|
|
|
// -> Check for page access
|
|
|
|
if (!WIKI.auth.checkAccess(opts.user, ['delete:pages'], {
|
|
|
|
locale: page.locale,
|
|
|
|
path: page.path
|
|
|
|
})) {
|
|
|
|
throw new WIKI.Error.PageDeleteForbidden()
|
|
|
|
}
|
|
|
|
|
|
|
|
// -> Create version snapshot
|
2019-01-26 23:35:56 +00:00
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...page,
|
2020-03-01 04:40:07 +00:00
|
|
|
action: 'deleted',
|
|
|
|
versionDate: page.updatedAt
|
2019-01-26 23:35:56 +00:00
|
|
|
})
|
2019-10-13 23:59:50 +00:00
|
|
|
|
|
|
|
// -> Delete page
|
2019-01-26 23:35:56 +00:00
|
|
|
await WIKI.models.pages.query().delete().where('id', page.id)
|
2020-04-20 00:26:26 +00:00
|
|
|
await WIKI.models.pages.deletePageFromCache(page.hash)
|
|
|
|
WIKI.events.outbound.emit('deletePageFromCache', page.hash)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-10-14 02:35:42 +00:00
|
|
|
// -> Rebuild page tree
|
|
|
|
await WIKI.models.pages.rebuildTree()
|
|
|
|
|
2019-03-24 04:27:42 +00:00
|
|
|
// -> Delete from Search Index
|
2019-03-11 04:47:27 +00:00
|
|
|
await WIKI.data.searchEngine.deleted(page)
|
2019-03-24 04:27:42 +00:00
|
|
|
|
|
|
|
// -> Delete from Storage
|
2019-02-25 04:48:28 +00:00
|
|
|
if (!opts.skipStorage) {
|
|
|
|
await WIKI.models.storage.pageEvent({
|
|
|
|
event: 'deleted',
|
|
|
|
page
|
|
|
|
})
|
|
|
|
}
|
2019-09-14 17:14:07 +00:00
|
|
|
|
|
|
|
// -> Reconnect Links
|
|
|
|
await WIKI.models.pages.reconnectLinks({
|
|
|
|
locale: page.localeCode,
|
|
|
|
path: page.path,
|
|
|
|
mode: 'delete'
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-14 02:35:42 +00:00
|
|
|
* Reconnect links to new/move/deleted page
|
2019-09-14 17:14:07 +00:00
|
|
|
*
|
|
|
|
* @param {Object} opts - Page parameters
|
|
|
|
* @param {string} opts.path - Page Path
|
|
|
|
* @param {string} opts.locale - Page Locale Code
|
|
|
|
* @param {string} [opts.sourcePath] - Previous Page Path (move only)
|
|
|
|
* @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
|
2019-10-14 02:35:42 +00:00
|
|
|
* @param {string} opts.mode - Page Update mode (create, move, delete)
|
2019-09-14 17:14:07 +00:00
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
|
|
|
static async reconnectLinks (opts) {
|
|
|
|
const pageHref = `/${opts.locale}/${opts.path}`
|
|
|
|
let replaceArgs = {
|
|
|
|
from: '',
|
|
|
|
to: ''
|
|
|
|
}
|
|
|
|
switch (opts.mode) {
|
|
|
|
case 'create':
|
|
|
|
replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
|
|
|
|
replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
|
|
|
|
break
|
|
|
|
case 'move':
|
|
|
|
const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
|
2020-06-29 01:08:17 +00:00
|
|
|
replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-valid-page">`
|
2019-09-14 17:14:07 +00:00
|
|
|
replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
|
|
|
|
break
|
|
|
|
case 'delete':
|
|
|
|
replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
|
|
|
|
replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
|
|
|
|
break
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let affectedHashes = []
|
2019-10-26 03:02:33 +00:00
|
|
|
// -> Perform replace and return affected page hashes (POSTGRES only)
|
|
|
|
if (WIKI.config.db.type === 'postgres') {
|
2020-01-26 18:11:33 +00:00
|
|
|
const qryHashes = await WIKI.models.pages.query()
|
2019-09-14 17:14:07 +00:00
|
|
|
.returning('hash')
|
|
|
|
.patch({
|
|
|
|
render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
|
|
|
|
})
|
|
|
|
.whereIn('pages.id', function () {
|
|
|
|
this.select('pageLinks.pageId').from('pageLinks').where({
|
|
|
|
'pageLinks.path': opts.path,
|
|
|
|
'pageLinks.localeCode': opts.locale
|
|
|
|
})
|
|
|
|
})
|
2020-01-26 18:11:33 +00:00
|
|
|
affectedHashes = qryHashes.map(h => h.hash)
|
2019-09-14 17:14:07 +00:00
|
|
|
} else {
|
2019-10-26 03:02:33 +00:00
|
|
|
// -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
|
2019-09-14 17:14:07 +00:00
|
|
|
await WIKI.models.pages.query()
|
|
|
|
.patch({
|
|
|
|
render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
|
|
|
|
})
|
|
|
|
.whereIn('pages.id', function () {
|
|
|
|
this.select('pageLinks.pageId').from('pageLinks').where({
|
|
|
|
'pageLinks.path': opts.path,
|
|
|
|
'pageLinks.localeCode': opts.locale
|
|
|
|
})
|
|
|
|
})
|
2020-01-26 18:11:33 +00:00
|
|
|
const qryHashes = await WIKI.models.pages.query()
|
2019-09-14 17:14:07 +00:00
|
|
|
.column('hash')
|
|
|
|
.whereIn('pages.id', function () {
|
|
|
|
this.select('pageLinks.pageId').from('pageLinks').where({
|
|
|
|
'pageLinks.path': opts.path,
|
|
|
|
'pageLinks.localeCode': opts.locale
|
|
|
|
})
|
|
|
|
})
|
2020-01-26 18:11:33 +00:00
|
|
|
affectedHashes = qryHashes.map(h => h.hash)
|
2019-09-14 17:14:07 +00:00
|
|
|
}
|
|
|
|
for (const hash of affectedHashes) {
|
2020-04-20 00:26:26 +00:00
|
|
|
await WIKI.models.pages.deletePageFromCache(hash)
|
|
|
|
WIKI.events.outbound.emit('deletePageFromCache', hash)
|
2019-09-14 17:14:07 +00:00
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 02:35:42 +00:00
|
|
|
/**
|
|
|
|
* Rebuild page tree for new/updated/deleted page
|
|
|
|
*
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
|
|
|
static async rebuildTree() {
|
|
|
|
const rebuildJob = await WIKI.scheduler.registerJob({
|
|
|
|
name: 'rebuild-tree',
|
|
|
|
immediate: true,
|
|
|
|
worker: true
|
|
|
|
})
|
|
|
|
return rebuildJob.finished
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Trigger the rendering of a page
|
|
|
|
*
|
|
|
|
* @param {Object} page Page Model Instance
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
2018-09-10 00:33:10 +00:00
|
|
|
static async renderPage(page) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const renderJob = await WIKI.scheduler.registerJob({
|
|
|
|
name: 'render-page',
|
|
|
|
immediate: true,
|
|
|
|
worker: true
|
|
|
|
}, page.id)
|
|
|
|
return renderJob.finished
|
2018-08-09 04:07:15 +00:00
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
2018-09-16 00:58:23 +00:00
|
|
|
static async getPage(opts) {
|
2019-06-26 02:48:23 +00:00
|
|
|
// -> Get from cache first
|
2018-09-16 00:58:23 +00:00
|
|
|
let page = await WIKI.models.pages.getPageFromCache(opts)
|
|
|
|
if (!page) {
|
2019-06-26 02:48:23 +00:00
|
|
|
// -> Get from DB
|
2018-09-16 00:58:23 +00:00
|
|
|
page = await WIKI.models.pages.getPageFromDb(opts)
|
2018-09-16 22:36:15 +00:00
|
|
|
if (page) {
|
2019-06-26 02:48:23 +00:00
|
|
|
if (page.render) {
|
|
|
|
// -> Save render to cache
|
|
|
|
await WIKI.models.pages.savePageToCache(page)
|
|
|
|
} else {
|
|
|
|
// -> No render? Possible duplicate issue
|
|
|
|
/* TODO: Detect duplicate and delete */
|
|
|
|
throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try again.')
|
|
|
|
}
|
2018-09-16 22:36:15 +00:00
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
}
|
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Fetch an Existing Page from the Database
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
2018-09-16 00:58:23 +00:00
|
|
|
static async getPageFromDb(opts) {
|
2019-02-13 22:20:46 +00:00
|
|
|
const queryModeID = _.isNumber(opts)
|
2019-09-02 22:32:33 +00:00
|
|
|
try {
|
|
|
|
return WIKI.models.pages.query()
|
|
|
|
.column([
|
2019-10-25 22:31:35 +00:00
|
|
|
'pages.id',
|
|
|
|
'pages.path',
|
|
|
|
'pages.hash',
|
|
|
|
'pages.title',
|
|
|
|
'pages.description',
|
|
|
|
'pages.isPrivate',
|
|
|
|
'pages.isPublished',
|
|
|
|
'pages.privateNS',
|
|
|
|
'pages.publishStartDate',
|
|
|
|
'pages.publishEndDate',
|
|
|
|
'pages.content',
|
|
|
|
'pages.render',
|
|
|
|
'pages.toc',
|
|
|
|
'pages.contentType',
|
|
|
|
'pages.createdAt',
|
|
|
|
'pages.updatedAt',
|
|
|
|
'pages.editorKey',
|
|
|
|
'pages.localeCode',
|
|
|
|
'pages.authorId',
|
|
|
|
'pages.creatorId',
|
2020-06-20 20:39:36 +00:00
|
|
|
'pages.extra',
|
2019-09-02 22:32:33 +00:00
|
|
|
{
|
|
|
|
authorName: 'author.name',
|
|
|
|
authorEmail: 'author.email',
|
|
|
|
creatorName: 'creator.name',
|
|
|
|
creatorEmail: 'creator.email'
|
|
|
|
}
|
|
|
|
])
|
2020-01-25 00:20:53 +00:00
|
|
|
.joinRelated('author')
|
|
|
|
.joinRelated('creator')
|
2020-01-26 04:29:46 +00:00
|
|
|
.withGraphJoined('tags')
|
|
|
|
.modifyGraph('tags', builder => {
|
|
|
|
builder.select('tag', 'title')
|
2019-09-02 22:32:33 +00:00
|
|
|
})
|
|
|
|
.where(queryModeID ? {
|
|
|
|
'pages.id': opts
|
|
|
|
} : {
|
|
|
|
'pages.path': opts.path,
|
|
|
|
'pages.localeCode': opts.locale
|
|
|
|
})
|
|
|
|
// .andWhere(builder => {
|
|
|
|
// if (queryModeID) return
|
|
|
|
// builder.where({
|
|
|
|
// 'pages.isPublished': true
|
|
|
|
// }).orWhere({
|
|
|
|
// 'pages.isPublished': false,
|
|
|
|
// 'pages.authorId': opts.userId
|
|
|
|
// })
|
|
|
|
// })
|
|
|
|
// .andWhere(builder => {
|
|
|
|
// if (queryModeID) return
|
|
|
|
// if (opts.isPrivate) {
|
|
|
|
// builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
|
|
|
|
// } else {
|
|
|
|
// builder.where({ 'pages.isPrivate': false })
|
|
|
|
// }
|
|
|
|
// })
|
|
|
|
.first()
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn(err)
|
|
|
|
throw err
|
|
|
|
}
|
2018-09-16 00:58:23 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Save a Page Model Instance to Cache
|
|
|
|
*
|
|
|
|
* @param {Object} page Page Model Instance
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
2018-09-16 00:58:23 +00:00
|
|
|
static async savePageToCache(page) {
|
2019-10-25 22:20:02 +00:00
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${page.hash}.bin`)
|
2018-09-16 00:58:23 +00:00
|
|
|
await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
|
2019-01-26 23:35:56 +00:00
|
|
|
id: page.id,
|
2018-09-16 00:58:23 +00:00
|
|
|
authorId: page.authorId,
|
|
|
|
authorName: page.authorName,
|
|
|
|
createdAt: page.createdAt,
|
|
|
|
creatorId: page.creatorId,
|
|
|
|
creatorName: page.creatorName,
|
|
|
|
description: page.description,
|
2021-04-02 04:39:11 +00:00
|
|
|
editorKey: page.editorKey,
|
2020-06-20 20:39:36 +00:00
|
|
|
extra: {
|
2020-06-23 22:26:29 +00:00
|
|
|
css: _.get(page, 'extra.css', ''),
|
|
|
|
js: _.get(page, 'extra.js', '')
|
2020-06-20 20:39:36 +00:00
|
|
|
},
|
2018-12-02 04:03:14 +00:00
|
|
|
isPrivate: page.isPrivate === 1 || page.isPrivate === true,
|
|
|
|
isPublished: page.isPublished === 1 || page.isPublished === true,
|
2018-09-16 00:58:23 +00:00
|
|
|
publishEndDate: page.publishEndDate,
|
|
|
|
publishStartDate: page.publishStartDate,
|
|
|
|
render: page.render,
|
2019-09-02 22:32:33 +00:00
|
|
|
tags: page.tags.map(t => _.pick(t, ['tag', 'title'])),
|
2018-09-16 00:58:23 +00:00
|
|
|
title: page.title,
|
2018-12-02 04:03:14 +00:00
|
|
|
toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
|
2018-09-16 00:58:23 +00:00
|
|
|
updatedAt: page.updatedAt
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Fetch an Existing Page from Cache
|
|
|
|
*
|
|
|
|
* @param {Object} opts Page Properties
|
|
|
|
* @returns {Promise} Promise of the Page Model Instance
|
|
|
|
*/
|
2018-09-16 00:58:23 +00:00
|
|
|
static async getPageFromCache(opts) {
|
|
|
|
const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
|
2019-10-25 22:20:02 +00:00
|
|
|
const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${pageHash}.bin`)
|
2018-09-16 00:58:23 +00:00
|
|
|
|
|
|
|
try {
|
|
|
|
const pageBuffer = await fs.readFile(cachePath)
|
|
|
|
let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
|
|
|
|
return {
|
|
|
|
...page,
|
|
|
|
path: opts.path,
|
|
|
|
localeCode: opts.locale,
|
|
|
|
isPrivate: opts.isPrivate
|
|
|
|
}
|
|
|
|
} catch (err) {
|
|
|
|
if (err.code === 'ENOENT') {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
WIKI.logger.error(err)
|
|
|
|
throw err
|
|
|
|
}
|
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Delete an Existing Page from Cache
|
|
|
|
*
|
2020-04-20 02:41:19 +00:00
|
|
|
* @param {String} page Page Unique Hash
|
2019-09-14 17:14:07 +00:00
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
2020-04-20 02:41:19 +00:00
|
|
|
static async deletePageFromCache(hash) {
|
|
|
|
return fs.remove(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${hash}.bin`))
|
2019-01-26 23:35:56 +00:00
|
|
|
}
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Flush the contents of the Cache
|
|
|
|
*/
|
2019-07-06 21:06:42 +00:00
|
|
|
static async flushCache() {
|
2019-10-25 22:20:02 +00:00
|
|
|
return fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache`))
|
2019-07-06 21:06:42 +00:00
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Migrate all pages from a source locale to the target locale
|
|
|
|
*
|
|
|
|
* @param {Object} opts Migration properties
|
|
|
|
* @param {string} opts.sourceLocale Source Locale Code
|
|
|
|
* @param {string} opts.targetLocale Target Locale Code
|
|
|
|
* @returns {Promise} Promise with no value
|
|
|
|
*/
|
2019-07-14 20:12:26 +00:00
|
|
|
static async migrateToLocale({ sourceLocale, targetLocale }) {
|
|
|
|
return WIKI.models.pages.query()
|
|
|
|
.patch({
|
|
|
|
localeCode: targetLocale
|
|
|
|
})
|
|
|
|
.where({
|
|
|
|
localeCode: sourceLocale
|
|
|
|
})
|
|
|
|
.whereNotExists(function() {
|
|
|
|
this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-09-14 17:14:07 +00:00
|
|
|
/**
|
|
|
|
* Clean raw HTML from content for use in search engines
|
|
|
|
*
|
|
|
|
* @param {string} rawHTML Raw HTML
|
|
|
|
* @returns {string} Cleaned Content Text
|
|
|
|
*/
|
2019-03-24 04:27:42 +00:00
|
|
|
static cleanHTML(rawHTML = '') {
|
2019-10-31 03:24:18 +00:00
|
|
|
let data = striptags(rawHTML || '', [], ' ')
|
2019-03-24 04:27:42 +00:00
|
|
|
.replace(emojiRegex(), '')
|
2019-09-21 14:36:09 +00:00
|
|
|
// .replace(htmlEntitiesRegex, '')
|
|
|
|
return he.decode(data)
|
2019-03-24 04:27:42 +00:00
|
|
|
.replace(punctuationRegex, ' ')
|
|
|
|
.replace(/(\r\n|\n|\r)/gm, ' ')
|
|
|
|
.replace(/\s\s+/g, ' ')
|
|
|
|
.split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
|
|
|
|
}
|
2020-04-20 00:26:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Subscribe to HA propagation events
|
|
|
|
*/
|
|
|
|
static subscribeToEvents() {
|
|
|
|
WIKI.events.inbound.on('deletePageFromCache', hash => {
|
|
|
|
WIKI.models.pages.deletePageFromCache(hash)
|
|
|
|
})
|
|
|
|
WIKI.events.inbound.on('flushCache', () => {
|
|
|
|
WIKI.models.pages.flushCache()
|
|
|
|
})
|
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|