pages.js 34 KB

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