You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1168 lines
34 KiB

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