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')
|
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]*)*/,
|
|
|
|
legacy: /^(<!-- TITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)*([\w\W]*)*/i,
|
|
|
|
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
|
|
|
|
const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
|
|
|
|
|
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'}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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()
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
|
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',
|
|
|
|
isPrivate: 'boolean',
|
|
|
|
isPublished: 'boolean',
|
|
|
|
publishEndDate: 'string',
|
|
|
|
publishStartDate: 'string',
|
|
|
|
render: 'string',
|
2019-09-02 22:32:33 +00:00
|
|
|
tags: [
|
|
|
|
{
|
|
|
|
tag: 'string',
|
|
|
|
title: '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
|
|
|
|
*/
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
getFileExtension() {
|
|
|
|
switch (this.contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
return 'md'
|
|
|
|
case 'html':
|
|
|
|
return 'html'
|
|
|
|
default:
|
|
|
|
return 'txt'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
static parseMetadata (raw, contentType) {
|
|
|
|
let result
|
|
|
|
switch (contentType) {
|
|
|
|
case 'markdown':
|
|
|
|
result = frontmatterRegex.markdown.exec(raw)
|
|
|
|
if (result[2]) {
|
|
|
|
return {
|
|
|
|
...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]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
case 'html':
|
|
|
|
result = frontmatterRegex.html.exec(raw)
|
|
|
|
if (result[2]) {
|
|
|
|
return {
|
|
|
|
...yaml.safeLoad(result[2]),
|
|
|
|
content: result[3]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
content: raw
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-22 20:25:39 +00:00
|
|
|
static async createPage(opts) {
|
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-08-27 03:19:39 +00:00
|
|
|
if (!opts.content || _.trim(opts.content).length < 1) {
|
|
|
|
throw new WIKI.Error.PageEmptyContent()
|
|
|
|
}
|
|
|
|
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().insert({
|
2018-07-22 20:25:39 +00:00
|
|
|
authorId: opts.authorId,
|
|
|
|
content: opts.content,
|
2018-07-23 01:13:01 +00:00
|
|
|
creatorId: opts.authorId,
|
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,
|
|
|
|
toc: '[]'
|
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,
|
|
|
|
userId: opts.authorId,
|
|
|
|
isPrivate: opts.isPrivate
|
|
|
|
})
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-09-01 22:33:36 +00:00
|
|
|
// -> Save Tags
|
|
|
|
if (opts.tags.length > 0) {
|
|
|
|
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
|
|
|
|
|
|
|
// -> 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
|
|
|
|
2018-07-23 01:13:01 +00:00
|
|
|
return page
|
|
|
|
}
|
|
|
|
|
|
|
|
static async updatePage(opts) {
|
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
|
|
|
|
|
|
|
if (!opts.content || _.trim(opts.content).length < 1) {
|
|
|
|
throw new WIKI.Error.PageEmptyContent()
|
|
|
|
}
|
|
|
|
|
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,
|
2019-01-26 23:35:56 +00:00
|
|
|
action: 'updated'
|
|
|
|
})
|
2018-09-16 00:58:23 +00:00
|
|
|
await WIKI.models.pages.query().patch({
|
2018-07-30 02:23:33 +00:00
|
|
|
authorId: opts.authorId,
|
|
|
|
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 || '',
|
2018-07-23 01:13:01 +00:00
|
|
|
title: opts.title
|
2018-09-16 00:58:23 +00:00
|
|
|
}).where('id', ogPage.id)
|
2018-09-16 04:35:03 +00:00
|
|
|
const page = await WIKI.models.pages.getPageFromDb({
|
|
|
|
path: ogPage.path,
|
|
|
|
locale: ogPage.localeCode,
|
|
|
|
userId: ogPage.authorId,
|
|
|
|
isPrivate: ogPage.isPrivate
|
|
|
|
})
|
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)
|
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
|
|
|
|
})
|
|
|
|
}
|
2018-07-22 20:25:39 +00:00
|
|
|
return page
|
|
|
|
}
|
2018-08-09 04:07:15 +00:00
|
|
|
|
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 {
|
|
|
|
page = await await WIKI.models.pages.query().findOne({
|
|
|
|
path: opts.path,
|
|
|
|
localeCode: opts.locale
|
|
|
|
})
|
|
|
|
}
|
2019-01-26 23:35:56 +00:00
|
|
|
if (!page) {
|
|
|
|
throw new Error('Invalid Page Id')
|
|
|
|
}
|
|
|
|
await WIKI.models.pageHistory.addVersion({
|
|
|
|
...page,
|
|
|
|
action: 'deleted'
|
|
|
|
})
|
|
|
|
await WIKI.models.pages.query().delete().where('id', page.id)
|
|
|
|
await WIKI.models.pages.deletePageFromCache(page)
|
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-01-26 23:35:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
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([
|
|
|
|
'pages.*',
|
|
|
|
{
|
|
|
|
authorName: 'author.name',
|
|
|
|
authorEmail: 'author.email',
|
|
|
|
creatorName: 'creator.name',
|
|
|
|
creatorEmail: 'creator.email'
|
|
|
|
}
|
|
|
|
])
|
|
|
|
.joinRelation('author')
|
|
|
|
.joinRelation('creator')
|
|
|
|
.eagerAlgorithm(Model.JoinEagerAlgorithm)
|
|
|
|
.eager('tags(selectTags)', {
|
|
|
|
selectTags: builder => {
|
|
|
|
builder.select('tag', 'title')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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
|
|
|
}
|
|
|
|
|
|
|
|
static async savePageToCache(page) {
|
|
|
|
const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
|
|
|
|
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,
|
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
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
static async getPageFromCache(opts) {
|
|
|
|
const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
|
|
|
|
const cachePath = path.join(process.cwd(), `data/cache/${pageHash}.bin`)
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
static async deletePageFromCache(page) {
|
|
|
|
return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
|
|
|
|
}
|
2019-03-24 04:27:42 +00:00
|
|
|
|
2019-07-06 21:06:42 +00:00
|
|
|
static async flushCache() {
|
|
|
|
return fs.emptyDir(path.join(process.cwd(), `data/cache`))
|
|
|
|
}
|
|
|
|
|
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-03-24 04:27:42 +00:00
|
|
|
static cleanHTML(rawHTML = '') {
|
|
|
|
return striptags(rawHTML || '')
|
|
|
|
.replace(emojiRegex(), '')
|
|
|
|
.replace(htmlEntitiesRegex, '')
|
|
|
|
.replace(punctuationRegex, ' ')
|
|
|
|
.replace(/(\r\n|\n|\r)/gm, ' ')
|
|
|
|
.replace(/\s\s+/g, ' ')
|
|
|
|
.split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
|
|
|
|
}
|
2018-05-20 22:50:51 +00:00
|
|
|
}
|