pages.js 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158
  1. const Model = require('objection').Model
  2. const _ = require('lodash')
  3. const JSBinType = require('js-binary').Type
  4. const pageHelper = require('../helpers/page')
  5. const path = require('path')
  6. const fs = require('fs-extra')
  7. const yaml = require('js-yaml')
  8. const striptags = require('striptags')
  9. const emojiRegex = require('emoji-regex')
  10. const he = require('he')
  11. const CleanCSS = require('clean-css')
  12. const TurndownService = require('turndown')
  13. const turndownPluginGfm = require('@joplin/turndown-plugin-gfm').gfm
  14. const cheerio = require('cheerio')
  15. const frontmatterRegex = {
  16. html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
  17. legacy: /^(<!-- TITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)*([\w\W]*)*/i,
  18. markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
  19. }
  20. const punctuationRegex = /[!,:;/\\_+\-=()&#@<>$~%^*[\]{}"'|]+|(\.\s)|(\s\.)/ig
  21. // const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
  22. /**
  23. * Pages model
  24. */
  25. module.exports = class Page extends Model {
  26. static get tableName() { return 'pages' }
  27. static get jsonSchema () {
  28. return {
  29. type: 'object',
  30. required: ['path', 'title'],
  31. properties: {
  32. id: {type: 'string'},
  33. path: {type: 'string'},
  34. hash: {type: 'string'},
  35. title: {type: 'string'},
  36. description: {type: 'string'},
  37. publishState: {type: 'string'},
  38. publishStartDate: {type: 'string'},
  39. publishEndDate: {type: 'string'},
  40. content: {type: 'string'},
  41. contentType: {type: 'string'},
  42. siteId: {type: 'string'},
  43. createdAt: {type: 'string'},
  44. updatedAt: {type: 'string'}
  45. }
  46. }
  47. }
  48. static get jsonAttributes() {
  49. return ['extra']
  50. }
  51. static get relationMappings() {
  52. return {
  53. tags: {
  54. relation: Model.ManyToManyRelation,
  55. modelClass: require('./tags'),
  56. join: {
  57. from: 'pages.id',
  58. through: {
  59. from: 'pageTags.pageId',
  60. to: 'pageTags.tagId'
  61. },
  62. to: 'tags.id'
  63. }
  64. },
  65. links: {
  66. relation: Model.HasManyRelation,
  67. modelClass: require('./pageLinks'),
  68. join: {
  69. from: 'pages.id',
  70. to: 'pageLinks.pageId'
  71. }
  72. },
  73. author: {
  74. relation: Model.BelongsToOneRelation,
  75. modelClass: require('./users'),
  76. join: {
  77. from: 'pages.authorId',
  78. to: 'users.id'
  79. }
  80. },
  81. creator: {
  82. relation: Model.BelongsToOneRelation,
  83. modelClass: require('./users'),
  84. join: {
  85. from: 'pages.creatorId',
  86. to: 'users.id'
  87. }
  88. },
  89. locale: {
  90. relation: Model.BelongsToOneRelation,
  91. modelClass: require('./locales'),
  92. join: {
  93. from: 'pages.localeCode',
  94. to: 'locales.code'
  95. }
  96. }
  97. }
  98. }
  99. $beforeUpdate() {
  100. this.updatedAt = new Date().toISOString()
  101. }
  102. $beforeInsert() {
  103. this.createdAt = new Date().toISOString()
  104. this.updatedAt = new Date().toISOString()
  105. }
  106. /**
  107. * Solving the violates foreign key constraint using cascade strategy
  108. * using static hooks
  109. * @see https://vincit.github.io/objection.js/api/types/#type-statichookarguments
  110. */
  111. static async beforeDelete({ asFindQuery }) {
  112. const page = await asFindQuery().select('id')
  113. await WIKI.db.comments.query().delete().where('pageId', page[0].id)
  114. }
  115. /**
  116. * Cache Schema
  117. */
  118. static get cacheSchema() {
  119. return new JSBinType({
  120. id: 'string',
  121. authorId: 'string',
  122. authorName: 'string',
  123. createdAt: 'string',
  124. creatorId: 'string',
  125. creatorName: 'string',
  126. description: 'string',
  127. editor: 'string',
  128. publishState: 'string',
  129. publishEndDate: 'string',
  130. publishStartDate: 'string',
  131. render: 'string',
  132. siteId: 'string',
  133. tags: [
  134. {
  135. tag: 'string'
  136. }
  137. ],
  138. extra: {
  139. js: 'string',
  140. css: 'string'
  141. },
  142. title: 'string',
  143. toc: 'string',
  144. updatedAt: 'string'
  145. })
  146. }
  147. /**
  148. * Inject page metadata into contents
  149. *
  150. * @returns {string} Page Contents with Injected Metadata
  151. */
  152. injectMetadata () {
  153. return pageHelper.injectPageMetadata(this)
  154. }
  155. /**
  156. * Get the page's file extension based on content type
  157. *
  158. * @returns {string} File Extension
  159. */
  160. getFileExtension() {
  161. return pageHelper.getFileExtension(this.contentType)
  162. }
  163. /**
  164. * Parse injected page metadata from raw content
  165. *
  166. * @param {String} raw Raw file contents
  167. * @param {String} contentType Content Type
  168. * @returns {Object} Parsed Page Metadata with Raw Content
  169. */
  170. static parseMetadata (raw, contentType) {
  171. let result
  172. try {
  173. switch (contentType) {
  174. case 'markdown':
  175. result = frontmatterRegex.markdown.exec(raw)
  176. if (result[2]) {
  177. return {
  178. ...yaml.safeLoad(result[2]),
  179. content: result[3]
  180. }
  181. } else {
  182. // Attempt legacy v1 format
  183. result = frontmatterRegex.legacy.exec(raw)
  184. if (result[2]) {
  185. return {
  186. title: result[2],
  187. description: result[4],
  188. content: result[5]
  189. }
  190. }
  191. }
  192. break
  193. case 'html':
  194. result = frontmatterRegex.html.exec(raw)
  195. if (result[2]) {
  196. return {
  197. ...yaml.safeLoad(result[2]),
  198. content: result[3]
  199. }
  200. }
  201. break
  202. }
  203. } catch (err) {
  204. WIKI.logger.warn('Failed to parse page metadata. Invalid syntax.')
  205. }
  206. return {
  207. content: raw
  208. }
  209. }
  210. /**
  211. * Create a New Page
  212. *
  213. * @param {Object} opts Page Properties
  214. * @returns {Promise} Promise of the Page Model Instance
  215. */
  216. static async createPage(opts) {
  217. // -> Validate site
  218. if (!WIKI.sites[opts.siteId]) {
  219. throw new WIKI.Error.Custom('InvalidSiteId', 'Site ID is invalid.')
  220. }
  221. // -> Validate path
  222. if (opts.path.includes('.') || opts.path.includes(' ') || opts.path.includes('\\') || opts.path.includes('//')) {
  223. throw new WIKI.Error.PageIllegalPath()
  224. }
  225. // -> Remove trailing slash
  226. if (opts.path.endsWith('/')) {
  227. opts.path = opts.path.slice(0, -1)
  228. }
  229. // -> Remove starting slash
  230. if (opts.path.startsWith('/')) {
  231. opts.path = opts.path.slice(1)
  232. }
  233. // -> Check for page access
  234. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  235. locale: opts.locale,
  236. path: opts.path
  237. })) {
  238. throw new WIKI.Error.PageDeleteForbidden()
  239. }
  240. // -> Check for duplicate
  241. const dupCheck = await WIKI.db.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
  242. if (dupCheck) {
  243. throw new WIKI.Error.PageDuplicateCreate()
  244. }
  245. // -> Check for empty content
  246. if (!opts.content || _.trim(opts.content).length < 1) {
  247. throw new WIKI.Error.PageEmptyContent()
  248. }
  249. // -> Format CSS Scripts
  250. let scriptCss = ''
  251. if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
  252. locale: opts.locale,
  253. path: opts.path
  254. })) {
  255. if (!_.isEmpty(opts.scriptCss)) {
  256. scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
  257. } else {
  258. scriptCss = ''
  259. }
  260. }
  261. // -> Format JS Scripts
  262. let scriptJs = ''
  263. if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
  264. locale: opts.locale,
  265. path: opts.path
  266. })) {
  267. scriptJs = opts.scriptJs || ''
  268. }
  269. // -> Create page
  270. await WIKI.db.pages.query().insert({
  271. authorId: opts.user.id,
  272. content: opts.content,
  273. creatorId: opts.user.id,
  274. contentType: _.get(WIKI.data.editors[opts.editor], 'contentType', 'text'),
  275. description: opts.description,
  276. editor: opts.editor,
  277. hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale }),
  278. publishState: opts.publishState,
  279. localeCode: opts.locale,
  280. path: opts.path,
  281. publishEndDate: opts.publishEndDate?.toISO(),
  282. publishStartDate: opts.publishStartDate?.toISO(),
  283. siteId: opts.siteId,
  284. title: opts.title,
  285. toc: '[]',
  286. extra: JSON.stringify({
  287. js: scriptJs,
  288. css: scriptCss
  289. })
  290. })
  291. const page = await WIKI.db.pages.getPageFromDb({
  292. path: opts.path,
  293. locale: opts.locale,
  294. userId: opts.user.id
  295. })
  296. // -> Save Tags
  297. if (opts.tags && opts.tags.length > 0) {
  298. await WIKI.db.tags.associateTags({ tags: opts.tags, page })
  299. }
  300. // -> Render page to HTML
  301. await WIKI.db.pages.renderPage(page)
  302. return page
  303. // TODO: Handle remaining flow
  304. // -> Rebuild page tree
  305. await WIKI.db.pages.rebuildTree()
  306. // -> Add to Search Index
  307. const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  308. page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  309. await WIKI.data.searchEngine.created(page)
  310. // -> Add to Storage
  311. if (!opts.skipStorage) {
  312. await WIKI.db.storage.pageEvent({
  313. event: 'created',
  314. page
  315. })
  316. }
  317. // -> Reconnect Links
  318. await WIKI.db.pages.reconnectLinks({
  319. locale: page.localeCode,
  320. path: page.path,
  321. mode: 'create'
  322. })
  323. // -> Get latest updatedAt
  324. page.updatedAt = await WIKI.db.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
  325. return page
  326. }
  327. /**
  328. * Update an Existing Page
  329. *
  330. * @param {Object} opts Page Properties
  331. * @returns {Promise} Promise of the Page Model Instance
  332. */
  333. static async updatePage(opts) {
  334. // -> Fetch original page
  335. const ogPage = await WIKI.db.pages.query().findById(opts.id)
  336. if (!ogPage) {
  337. throw new Error('Invalid Page Id')
  338. }
  339. // -> Check for page access
  340. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  341. locale: ogPage.localeCode,
  342. path: ogPage.path
  343. })) {
  344. throw new WIKI.Error.PageUpdateForbidden()
  345. }
  346. // -> Check for empty content
  347. if (!opts.content || _.trim(opts.content).length < 1) {
  348. throw new WIKI.Error.PageEmptyContent()
  349. }
  350. // -> Create version snapshot
  351. await WIKI.db.pageHistory.addVersion({
  352. ...ogPage,
  353. action: opts.action ? opts.action : 'updated',
  354. versionDate: ogPage.updatedAt
  355. })
  356. // -> Format Extra Properties
  357. if (!_.isPlainObject(ogPage.extra)) {
  358. ogPage.extra = {}
  359. }
  360. // -> Format CSS Scripts
  361. let scriptCss = _.get(ogPage, 'extra.css', '')
  362. if (WIKI.auth.checkAccess(opts.user, ['write:styles'], {
  363. locale: opts.locale,
  364. path: opts.path
  365. })) {
  366. if (!_.isEmpty(opts.scriptCss)) {
  367. scriptCss = new CleanCSS({ inline: false }).minify(opts.scriptCss).styles
  368. } else {
  369. scriptCss = ''
  370. }
  371. }
  372. // -> Format JS Scripts
  373. let scriptJs = _.get(ogPage, 'extra.js', '')
  374. if (WIKI.auth.checkAccess(opts.user, ['write:scripts'], {
  375. locale: opts.locale,
  376. path: opts.path
  377. })) {
  378. scriptJs = opts.scriptJs || ''
  379. }
  380. // -> Update page
  381. await WIKI.db.pages.query().patch({
  382. authorId: opts.user.id,
  383. content: opts.content,
  384. description: opts.description,
  385. publishState: opts.publishState,
  386. publishEndDate: opts.publishEndDate?.toISO(),
  387. publishStartDate: opts.publishStartDate?.toISO(),
  388. title: opts.title,
  389. extra: JSON.stringify({
  390. ...ogPage.extra,
  391. js: scriptJs,
  392. css: scriptCss
  393. })
  394. }).where('id', ogPage.id)
  395. let page = await WIKI.db.pages.getPageFromDb(ogPage.id)
  396. // -> Save Tags
  397. await WIKI.db.tags.associateTags({ tags: opts.tags, page })
  398. // -> Render page to HTML
  399. await WIKI.db.pages.renderPage(page)
  400. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  401. // // -> Update Search Index
  402. // const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  403. // page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  404. // await WIKI.data.searchEngine.updated(page)
  405. // -> Update on Storage
  406. // if (!opts.skipStorage) {
  407. // await WIKI.db.storage.pageEvent({
  408. // event: 'updated',
  409. // page
  410. // })
  411. // }
  412. // -> Perform move?
  413. if ((opts.locale && opts.locale !== page.localeCode) || (opts.path && opts.path !== page.path)) {
  414. // -> Check target path access
  415. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  416. locale: opts.locale,
  417. path: opts.path
  418. })) {
  419. throw new WIKI.Error.PageMoveForbidden()
  420. }
  421. await WIKI.db.pages.movePage({
  422. id: page.id,
  423. destinationLocale: opts.locale,
  424. destinationPath: opts.path,
  425. user: opts.user
  426. })
  427. } else {
  428. // -> Update title of page tree entry
  429. await WIKI.db.knex.table('pageTree').where({
  430. pageId: page.id
  431. }).update('title', page.title)
  432. }
  433. // -> Get latest updatedAt
  434. page.updatedAt = await WIKI.db.pages.query().findById(page.id).select('updatedAt').then(r => r.updatedAt)
  435. return page
  436. }
  437. /**
  438. * Convert an Existing Page
  439. *
  440. * @param {Object} opts Page Properties
  441. * @returns {Promise} Promise of the Page Model Instance
  442. */
  443. static async convertPage(opts) {
  444. // -> Fetch original page
  445. const ogPage = await WIKI.db.pages.query().findById(opts.id)
  446. if (!ogPage) {
  447. throw new Error('Invalid Page Id')
  448. }
  449. if (ogPage.editor === opts.editor) {
  450. throw new Error('Page is already using this editor. Nothing to convert.')
  451. }
  452. // -> Check for page access
  453. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  454. locale: ogPage.localeCode,
  455. path: ogPage.path
  456. })) {
  457. throw new WIKI.Error.PageUpdateForbidden()
  458. }
  459. // -> Check content type
  460. const sourceContentType = ogPage.contentType
  461. const targetContentType = _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text')
  462. const shouldConvert = sourceContentType !== targetContentType
  463. let convertedContent = null
  464. // -> Convert content
  465. if (shouldConvert) {
  466. // -> Markdown => HTML
  467. if (sourceContentType === 'markdown' && targetContentType === 'html') {
  468. if (!ogPage.render) {
  469. throw new Error('Aborted conversion because rendered page content is empty!')
  470. }
  471. convertedContent = ogPage.render
  472. const $ = cheerio.load(convertedContent, {
  473. decodeEntities: true
  474. })
  475. if ($.root().children().length > 0) {
  476. // Remove header anchors
  477. $('.toc-anchor').remove()
  478. // Attempt to convert tabsets
  479. $('tabset').each((tabI, tabElm) => {
  480. const tabHeaders = []
  481. // -> Extract templates
  482. $(tabElm).children('template').each((tmplI, tmplElm) => {
  483. if ($(tmplElm).attr('v-slot:tabs') === '') {
  484. $(tabElm).before('<ul class="tabset-headers">' + $(tmplElm).html() + '</ul>')
  485. } else {
  486. $(tabElm).after('<div class="markdown-tabset">' + $(tmplElm).html() + '</div>')
  487. }
  488. })
  489. // -> Parse tab headers
  490. $(tabElm).prev('.tabset-headers').children((i, elm) => {
  491. tabHeaders.push($(elm).html())
  492. })
  493. $(tabElm).prev('.tabset-headers').remove()
  494. // -> Inject tab headers
  495. $(tabElm).next('.markdown-tabset').children((i, elm) => {
  496. if (tabHeaders.length > i) {
  497. $(elm).prepend(`<h2>${tabHeaders[i]}</h2>`)
  498. }
  499. })
  500. $(tabElm).next('.markdown-tabset').prepend('<h1>Tabset</h1>')
  501. $(tabElm).remove()
  502. })
  503. convertedContent = $.html('body').replace('<body>', '').replace('</body>', '').replace(/&#x([0-9a-f]{1,6});/ig, (entity, code) => {
  504. code = parseInt(code, 16)
  505. // Don't unescape ASCII characters, assuming they're encoded for a good reason
  506. if (code < 0x80) return entity
  507. return String.fromCodePoint(code)
  508. })
  509. }
  510. // -> HTML => Markdown
  511. } else if (sourceContentType === 'html' && targetContentType === 'markdown') {
  512. const td = new TurndownService({
  513. bulletListMarker: '-',
  514. codeBlockStyle: 'fenced',
  515. emDelimiter: '*',
  516. fence: '```',
  517. headingStyle: 'atx',
  518. hr: '---',
  519. linkStyle: 'inlined',
  520. preformattedCode: true,
  521. strongDelimiter: '**'
  522. })
  523. td.use(turndownPluginGfm)
  524. td.keep(['kbd'])
  525. td.addRule('subscript', {
  526. filter: ['sub'],
  527. replacement: c => `~${c}~`
  528. })
  529. td.addRule('superscript', {
  530. filter: ['sup'],
  531. replacement: c => `^${c}^`
  532. })
  533. td.addRule('underline', {
  534. filter: ['u'],
  535. replacement: c => `_${c}_`
  536. })
  537. td.addRule('taskList', {
  538. filter: (n, o) => {
  539. return n.nodeName === 'INPUT' && n.getAttribute('type') === 'checkbox'
  540. },
  541. replacement: (c, n) => {
  542. return n.getAttribute('checked') ? '[x] ' : '[ ] '
  543. }
  544. })
  545. td.addRule('removeTocAnchors', {
  546. filter: (n, o) => {
  547. return n.nodeName === 'A' && n.classList.contains('toc-anchor')
  548. },
  549. replacement: c => ''
  550. })
  551. convertedContent = td.turndown(ogPage.content)
  552. // -> Unsupported
  553. } else {
  554. throw new Error('Unsupported source / destination content types combination.')
  555. }
  556. }
  557. // -> Create version snapshot
  558. if (shouldConvert) {
  559. await WIKI.db.pageHistory.addVersion({
  560. ...ogPage,
  561. action: 'updated',
  562. versionDate: ogPage.updatedAt
  563. })
  564. }
  565. // -> Update page
  566. await WIKI.db.pages.query().patch({
  567. contentType: targetContentType,
  568. editor: opts.editor,
  569. ...(convertedContent ? { content: convertedContent } : {})
  570. }).where('id', ogPage.id)
  571. const page = await WIKI.db.pages.getPageFromDb(ogPage.id)
  572. await WIKI.db.pages.deletePageFromCache(page.hash)
  573. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  574. // -> Update on Storage
  575. await WIKI.db.storage.pageEvent({
  576. event: 'updated',
  577. page
  578. })
  579. }
  580. /**
  581. * Move a Page
  582. *
  583. * @param {Object} opts Page Properties
  584. * @returns {Promise} Promise with no value
  585. */
  586. static async movePage(opts) {
  587. let page
  588. if (_.has(opts, 'id')) {
  589. page = await WIKI.db.pages.query().findById(opts.id)
  590. } else {
  591. page = await WIKI.db.pages.query().findOne({
  592. path: opts.path,
  593. localeCode: opts.locale
  594. })
  595. }
  596. if (!page) {
  597. throw new WIKI.Error.PageNotFound()
  598. }
  599. // -> Validate path
  600. if (opts.destinationPath.includes('.') || opts.destinationPath.includes(' ') || opts.destinationPath.includes('\\') || opts.destinationPath.includes('//')) {
  601. throw new WIKI.Error.PageIllegalPath()
  602. }
  603. // -> Remove trailing slash
  604. if (opts.destinationPath.endsWith('/')) {
  605. opts.destinationPath = opts.destinationPath.slice(0, -1)
  606. }
  607. // -> Remove starting slash
  608. if (opts.destinationPath.startsWith('/')) {
  609. opts.destinationPath = opts.destinationPath.slice(1)
  610. }
  611. // -> Check for source page access
  612. if (!WIKI.auth.checkAccess(opts.user, ['manage:pages'], {
  613. locale: page.localeCode,
  614. path: page.path
  615. })) {
  616. throw new WIKI.Error.PageMoveForbidden()
  617. }
  618. // -> Check for destination page access
  619. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  620. locale: opts.destinationLocale,
  621. path: opts.destinationPath
  622. })) {
  623. throw new WIKI.Error.PageMoveForbidden()
  624. }
  625. // -> Check for existing page at destination path
  626. const destPage = await WIKI.db.pages.query().findOne({
  627. path: opts.destinationPath,
  628. localeCode: opts.destinationLocale
  629. })
  630. if (destPage) {
  631. throw new WIKI.Error.PagePathCollision()
  632. }
  633. // -> Create version snapshot
  634. await WIKI.db.pageHistory.addVersion({
  635. ...page,
  636. action: 'moved',
  637. versionDate: page.updatedAt
  638. })
  639. const destinationHash = pageHelper.generateHash({ path: opts.destinationPath, locale: opts.destinationLocale })
  640. // -> Move page
  641. const destinationTitle = (page.title === page.path ? opts.destinationPath : page.title)
  642. await WIKI.db.pages.query().patch({
  643. path: opts.destinationPath,
  644. localeCode: opts.destinationLocale,
  645. title: destinationTitle,
  646. hash: destinationHash
  647. }).findById(page.id)
  648. await WIKI.db.pages.deletePageFromCache(page.hash)
  649. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  650. // -> Rebuild page tree
  651. await WIKI.db.pages.rebuildTree()
  652. // -> Rename in Search Index
  653. const pageContents = await WIKI.db.pages.query().findById(page.id).select('render')
  654. page.safeContent = WIKI.db.pages.cleanHTML(pageContents.render)
  655. await WIKI.data.searchEngine.renamed({
  656. ...page,
  657. destinationPath: opts.destinationPath,
  658. destinationLocaleCode: opts.destinationLocale,
  659. destinationHash
  660. })
  661. // -> Rename in Storage
  662. if (!opts.skipStorage) {
  663. await WIKI.db.storage.pageEvent({
  664. event: 'renamed',
  665. page: {
  666. ...page,
  667. destinationPath: opts.destinationPath,
  668. destinationLocaleCode: opts.destinationLocale,
  669. destinationHash,
  670. moveAuthorId: opts.user.id,
  671. moveAuthorName: opts.user.name,
  672. moveAuthorEmail: opts.user.email
  673. }
  674. })
  675. }
  676. // -> Reconnect Links : Changing old links to the new path
  677. await WIKI.db.pages.reconnectLinks({
  678. sourceLocale: page.localeCode,
  679. sourcePath: page.path,
  680. locale: opts.destinationLocale,
  681. path: opts.destinationPath,
  682. mode: 'move'
  683. })
  684. // -> Reconnect Links : Validate invalid links to the new path
  685. await WIKI.db.pages.reconnectLinks({
  686. locale: opts.destinationLocale,
  687. path: opts.destinationPath,
  688. mode: 'create'
  689. })
  690. }
  691. /**
  692. * Delete an Existing Page
  693. *
  694. * @param {Object} opts Page Properties
  695. * @returns {Promise} Promise with no value
  696. */
  697. static async deletePage(opts) {
  698. const page = await WIKI.db.pages.getPageFromDb(_.has(opts, 'id') ? opts.id : opts)
  699. if (!page) {
  700. throw new WIKI.Error.PageNotFound()
  701. }
  702. // -> Check for page access
  703. if (!WIKI.auth.checkAccess(opts.user, ['delete:pages'], {
  704. locale: page.locale,
  705. path: page.path
  706. })) {
  707. throw new WIKI.Error.PageDeleteForbidden()
  708. }
  709. // -> Create version snapshot
  710. await WIKI.db.pageHistory.addVersion({
  711. ...page,
  712. action: 'deleted',
  713. versionDate: page.updatedAt
  714. })
  715. // -> Delete page
  716. await WIKI.db.pages.query().delete().where('id', page.id)
  717. await WIKI.db.pages.deletePageFromCache(page.hash)
  718. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  719. // -> Rebuild page tree
  720. await WIKI.db.pages.rebuildTree()
  721. // -> Delete from Search Index
  722. await WIKI.data.searchEngine.deleted(page)
  723. // -> Delete from Storage
  724. if (!opts.skipStorage) {
  725. await WIKI.db.storage.pageEvent({
  726. event: 'deleted',
  727. page
  728. })
  729. }
  730. // -> Reconnect Links
  731. await WIKI.db.pages.reconnectLinks({
  732. locale: page.localeCode,
  733. path: page.path,
  734. mode: 'delete'
  735. })
  736. }
  737. /**
  738. * Reconnect links to new/move/deleted page
  739. *
  740. * @param {Object} opts - Page parameters
  741. * @param {string} opts.path - Page Path
  742. * @param {string} opts.locale - Page Locale Code
  743. * @param {string} [opts.sourcePath] - Previous Page Path (move only)
  744. * @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
  745. * @param {string} opts.mode - Page Update mode (create, move, delete)
  746. * @returns {Promise} Promise with no value
  747. */
  748. static async reconnectLinks (opts) {
  749. const pageHref = `/${opts.locale}/${opts.path}`
  750. let replaceArgs = {
  751. from: '',
  752. to: ''
  753. }
  754. switch (opts.mode) {
  755. case 'create':
  756. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  757. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  758. break
  759. case 'move':
  760. const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
  761. replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-valid-page">`
  762. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  763. break
  764. case 'delete':
  765. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  766. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  767. break
  768. default:
  769. return false
  770. }
  771. let affectedHashes = []
  772. // -> Perform replace and return affected page hashes (POSTGRES only)
  773. if (WIKI.config.db.type === 'postgres') {
  774. const qryHashes = await WIKI.db.pages.query()
  775. .returning('hash')
  776. .patch({
  777. render: WIKI.db.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  778. })
  779. .whereIn('pages.id', function () {
  780. this.select('pageLinks.pageId').from('pageLinks').where({
  781. 'pageLinks.path': opts.path,
  782. 'pageLinks.localeCode': opts.locale
  783. })
  784. })
  785. affectedHashes = qryHashes.map(h => h.hash)
  786. } else {
  787. // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
  788. await WIKI.db.pages.query()
  789. .patch({
  790. render: WIKI.db.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  791. })
  792. .whereIn('pages.id', function () {
  793. this.select('pageLinks.pageId').from('pageLinks').where({
  794. 'pageLinks.path': opts.path,
  795. 'pageLinks.localeCode': opts.locale
  796. })
  797. })
  798. const qryHashes = await WIKI.db.pages.query()
  799. .column('hash')
  800. .whereIn('pages.id', function () {
  801. this.select('pageLinks.pageId').from('pageLinks').where({
  802. 'pageLinks.path': opts.path,
  803. 'pageLinks.localeCode': opts.locale
  804. })
  805. })
  806. affectedHashes = qryHashes.map(h => h.hash)
  807. }
  808. for (const hash of affectedHashes) {
  809. await WIKI.db.pages.deletePageFromCache(hash)
  810. WIKI.events.outbound.emit('deletePageFromCache', hash)
  811. }
  812. }
  813. /**
  814. * Rebuild page tree for new/updated/deleted page
  815. *
  816. * @returns {Promise} Promise with no value
  817. */
  818. static async rebuildTree() {
  819. const rebuildJob = await WIKI.scheduler.registerJob({
  820. name: 'rebuild-tree',
  821. immediate: true,
  822. worker: true
  823. })
  824. return rebuildJob.finished
  825. }
  826. /**
  827. * Trigger the rendering of a page
  828. *
  829. * @param {Object} page Page Model Instance
  830. * @returns {Promise} Promise with no value
  831. */
  832. static async renderPage(page) {
  833. const renderJob = await WIKI.scheduler.addJob({
  834. task: 'render-page',
  835. payload: {
  836. id: page.id
  837. },
  838. maxRetries: 0,
  839. promise: true
  840. })
  841. return renderJob.promise
  842. }
  843. /**
  844. * Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
  845. *
  846. * @param {Object} opts Page Properties
  847. * @returns {Promise} Promise of the Page Model Instance
  848. */
  849. static async getPage(opts) {
  850. // -> Get from cache first
  851. let page = await WIKI.db.pages.getPageFromCache(opts)
  852. if (!page) {
  853. // -> Get from DB
  854. page = await WIKI.db.pages.getPageFromDb(opts)
  855. if (page) {
  856. if (page.render) {
  857. // -> Save render to cache
  858. await WIKI.db.pages.savePageToCache(page)
  859. } else {
  860. // -> No render? Possible duplicate issue
  861. /* TODO: Detect duplicate and delete */
  862. throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try again.')
  863. }
  864. }
  865. }
  866. return page
  867. }
  868. /**
  869. * Fetch an Existing Page from the Database
  870. *
  871. * @param {Object} opts Page Properties
  872. * @returns {Promise} Promise of the Page Model Instance
  873. */
  874. static async getPageFromDb(opts) {
  875. const queryModeID = typeof opts === 'string'
  876. try {
  877. return WIKI.db.pages.query()
  878. .column([
  879. 'pages.id',
  880. 'pages.path',
  881. 'pages.hash',
  882. 'pages.title',
  883. 'pages.description',
  884. 'pages.publishState',
  885. 'pages.publishStartDate',
  886. 'pages.publishEndDate',
  887. 'pages.content',
  888. 'pages.render',
  889. 'pages.toc',
  890. 'pages.contentType',
  891. 'pages.createdAt',
  892. 'pages.updatedAt',
  893. 'pages.editor',
  894. 'pages.localeCode',
  895. 'pages.authorId',
  896. 'pages.creatorId',
  897. 'pages.siteId',
  898. 'pages.extra',
  899. {
  900. authorName: 'author.name',
  901. authorEmail: 'author.email',
  902. creatorName: 'creator.name',
  903. creatorEmail: 'creator.email'
  904. }
  905. ])
  906. .joinRelated('author')
  907. .joinRelated('creator')
  908. .withGraphJoined('tags')
  909. .modifyGraph('tags', builder => {
  910. builder.select('tag')
  911. })
  912. .where(queryModeID ? {
  913. 'pages.id': opts
  914. } : {
  915. 'pages.path': opts.path,
  916. 'pages.localeCode': opts.locale
  917. })
  918. // .andWhere(builder => {
  919. // if (queryModeID) return
  920. // builder.where({
  921. // 'pages.isPublished': true
  922. // }).orWhere({
  923. // 'pages.isPublished': false,
  924. // 'pages.authorId': opts.userId
  925. // })
  926. // })
  927. .first()
  928. } catch (err) {
  929. WIKI.logger.warn(err)
  930. throw err
  931. }
  932. }
  933. /**
  934. * Save a Page Model Instance to Cache
  935. *
  936. * @param {Object} page Page Model Instance
  937. * @returns {Promise} Promise with no value
  938. */
  939. static async savePageToCache(page) {
  940. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${page.hash}.bin`)
  941. await fs.outputFile(cachePath, WIKI.db.pages.cacheSchema.encode({
  942. id: page.id,
  943. authorId: page.authorId,
  944. authorName: page.authorName,
  945. createdAt: page.createdAt.toISOString(),
  946. creatorId: page.creatorId,
  947. creatorName: page.creatorName,
  948. description: page.description,
  949. editor: page.editor,
  950. extra: {
  951. css: _.get(page, 'extra.css', ''),
  952. js: _.get(page, 'extra.js', '')
  953. },
  954. publishState: page.publishState ?? '',
  955. publishEndDate: page.publishEndDate ?? '',
  956. publishStartDate: page.publishStartDate ?? '',
  957. render: page.render,
  958. siteId: page.siteId,
  959. tags: page.tags.map(t => _.pick(t, ['tag'])),
  960. title: page.title,
  961. toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
  962. updatedAt: page.updatedAt.toISOString()
  963. }))
  964. }
  965. /**
  966. * Fetch an Existing Page from Cache
  967. *
  968. * @param {Object} opts Page Properties
  969. * @returns {Promise} Promise of the Page Model Instance
  970. */
  971. static async getPageFromCache(opts) {
  972. const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale })
  973. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${pageHash}.bin`)
  974. try {
  975. const pageBuffer = await fs.readFile(cachePath)
  976. let page = WIKI.db.pages.cacheSchema.decode(pageBuffer)
  977. return {
  978. ...page,
  979. path: opts.path,
  980. localeCode: opts.locale
  981. }
  982. } catch (err) {
  983. if (err.code === 'ENOENT') {
  984. return false
  985. }
  986. WIKI.logger.error(err)
  987. throw err
  988. }
  989. }
  990. /**
  991. * Delete an Existing Page from Cache
  992. *
  993. * @param {String} page Page Unique Hash
  994. * @returns {Promise} Promise with no value
  995. */
  996. static async deletePageFromCache(hash) {
  997. return fs.remove(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${hash}.bin`))
  998. }
  999. /**
  1000. * Flush the contents of the Cache
  1001. */
  1002. static async flushCache() {
  1003. return fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache`))
  1004. }
  1005. /**
  1006. * Migrate all pages from a source locale to the target locale
  1007. *
  1008. * @param {Object} opts Migration properties
  1009. * @param {string} opts.sourceLocale Source Locale Code
  1010. * @param {string} opts.targetLocale Target Locale Code
  1011. * @returns {Promise} Promise with no value
  1012. */
  1013. static async migrateToLocale({ sourceLocale, targetLocale }) {
  1014. return WIKI.db.pages.query()
  1015. .patch({
  1016. localeCode: targetLocale
  1017. })
  1018. .where({
  1019. localeCode: sourceLocale
  1020. })
  1021. .whereNotExists(function() {
  1022. this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
  1023. })
  1024. }
  1025. /**
  1026. * Clean raw HTML from content for use in search engines
  1027. *
  1028. * @param {string} rawHTML Raw HTML
  1029. * @returns {string} Cleaned Content Text
  1030. */
  1031. static cleanHTML(rawHTML = '') {
  1032. let data = striptags(rawHTML || '', [], ' ')
  1033. .replace(emojiRegex(), '')
  1034. // .replace(htmlEntitiesRegex, '')
  1035. return he.decode(data)
  1036. .replace(punctuationRegex, ' ')
  1037. .replace(/(\r\n|\n|\r)/gm, ' ')
  1038. .replace(/\s\s+/g, ' ')
  1039. .split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
  1040. }
  1041. /**
  1042. * Subscribe to HA propagation events
  1043. */
  1044. static subscribeToEvents() {
  1045. WIKI.events.inbound.on('deletePageFromCache', hash => {
  1046. WIKI.db.pages.deletePageFromCache(hash)
  1047. })
  1048. WIKI.events.inbound.on('flushCache', () => {
  1049. WIKI.db.pages.flushCache()
  1050. })
  1051. }
  1052. }