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.

1174 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
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. 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 === page.path ? opts.destinationPath : 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. destinationHash
  668. })
  669. // -> Rename in Storage
  670. if (!opts.skipStorage) {
  671. await WIKI.models.storage.pageEvent({
  672. event: 'renamed',
  673. page: {
  674. ...page,
  675. destinationPath: opts.destinationPath,
  676. destinationLocaleCode: opts.destinationLocale,
  677. destinationHash,
  678. moveAuthorId: opts.user.id,
  679. moveAuthorName: opts.user.name,
  680. moveAuthorEmail: opts.user.email
  681. }
  682. })
  683. }
  684. // -> Reconnect Links : Changing old links to the new path
  685. await WIKI.models.pages.reconnectLinks({
  686. sourceLocale: page.localeCode,
  687. sourcePath: page.path,
  688. locale: opts.destinationLocale,
  689. path: opts.destinationPath,
  690. mode: 'move'
  691. })
  692. // -> Reconnect Links : Validate invalid links to the new path
  693. await WIKI.models.pages.reconnectLinks({
  694. locale: opts.destinationLocale,
  695. path: opts.destinationPath,
  696. mode: 'create'
  697. })
  698. }
  699. /**
  700. * Delete an Existing Page
  701. *
  702. * @param {Object} opts Page Properties
  703. * @returns {Promise} Promise with no value
  704. */
  705. static async deletePage(opts) {
  706. const page = await WIKI.models.pages.getPageFromDb(_.has(opts, 'id') ? opts.id : opts)
  707. if (!page) {
  708. throw new WIKI.Error.PageNotFound()
  709. }
  710. // -> Check for page access
  711. if (!WIKI.auth.checkAccess(opts.user, ['delete:pages'], {
  712. locale: page.locale,
  713. path: page.path
  714. })) {
  715. throw new WIKI.Error.PageDeleteForbidden()
  716. }
  717. // -> Create version snapshot
  718. await WIKI.models.pageHistory.addVersion({
  719. ...page,
  720. action: 'deleted',
  721. versionDate: page.updatedAt
  722. })
  723. // -> Delete page
  724. await WIKI.models.pages.query().delete().where('id', page.id)
  725. await WIKI.models.pages.deletePageFromCache(page.hash)
  726. WIKI.events.outbound.emit('deletePageFromCache', page.hash)
  727. // -> Rebuild page tree
  728. await WIKI.models.pages.rebuildTree()
  729. // -> Delete from Search Index
  730. await WIKI.data.searchEngine.deleted(page)
  731. // -> Delete from Storage
  732. if (!opts.skipStorage) {
  733. await WIKI.models.storage.pageEvent({
  734. event: 'deleted',
  735. page
  736. })
  737. }
  738. // -> Reconnect Links
  739. await WIKI.models.pages.reconnectLinks({
  740. locale: page.localeCode,
  741. path: page.path,
  742. mode: 'delete'
  743. })
  744. }
  745. /**
  746. * Reconnect links to new/move/deleted page
  747. *
  748. * @param {Object} opts - Page parameters
  749. * @param {string} opts.path - Page Path
  750. * @param {string} opts.locale - Page Locale Code
  751. * @param {string} [opts.sourcePath] - Previous Page Path (move only)
  752. * @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
  753. * @param {string} opts.mode - Page Update mode (create, move, delete)
  754. * @returns {Promise} Promise with no value
  755. */
  756. static async reconnectLinks (opts) {
  757. const pageHref = `/${opts.locale}/${opts.path}`
  758. let replaceArgs = {
  759. from: '',
  760. to: ''
  761. }
  762. switch (opts.mode) {
  763. case 'create':
  764. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  765. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  766. break
  767. case 'move':
  768. const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
  769. replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-valid-page">`
  770. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  771. break
  772. case 'delete':
  773. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  774. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  775. break
  776. default:
  777. return false
  778. }
  779. let affectedHashes = []
  780. // -> Perform replace and return affected page hashes (POSTGRES only)
  781. if (WIKI.config.db.type === 'postgres') {
  782. const qryHashes = await WIKI.models.pages.query()
  783. .returning('hash')
  784. .patch({
  785. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  786. })
  787. .whereIn('pages.id', function () {
  788. this.select('pageLinks.pageId').from('pageLinks').where({
  789. 'pageLinks.path': opts.path,
  790. 'pageLinks.localeCode': opts.locale
  791. })
  792. })
  793. affectedHashes = qryHashes.map(h => h.hash)
  794. } else {
  795. // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
  796. await WIKI.models.pages.query()
  797. .patch({
  798. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  799. })
  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. const qryHashes = await WIKI.models.pages.query()
  807. .column('hash')
  808. .whereIn('pages.id', function () {
  809. this.select('pageLinks.pageId').from('pageLinks').where({
  810. 'pageLinks.path': opts.path,
  811. 'pageLinks.localeCode': opts.locale
  812. })
  813. })
  814. affectedHashes = qryHashes.map(h => h.hash)
  815. }
  816. for (const hash of affectedHashes) {
  817. await WIKI.models.pages.deletePageFromCache(hash)
  818. WIKI.events.outbound.emit('deletePageFromCache', hash)
  819. }
  820. }
  821. /**
  822. * Rebuild page tree for new/updated/deleted page
  823. *
  824. * @returns {Promise} Promise with no value
  825. */
  826. static async rebuildTree() {
  827. const rebuildJob = await WIKI.scheduler.registerJob({
  828. name: 'rebuild-tree',
  829. immediate: true,
  830. worker: true
  831. })
  832. return rebuildJob.finished
  833. }
  834. /**
  835. * Trigger the rendering of a page
  836. *
  837. * @param {Object} page Page Model Instance
  838. * @returns {Promise} Promise with no value
  839. */
  840. static async renderPage(page) {
  841. const renderJob = await WIKI.scheduler.registerJob({
  842. name: 'render-page',
  843. immediate: true,
  844. worker: true
  845. }, page.id)
  846. return renderJob.finished
  847. }
  848. /**
  849. * Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
  850. *
  851. * @param {Object} opts Page Properties
  852. * @returns {Promise} Promise of the Page Model Instance
  853. */
  854. static async getPage(opts) {
  855. // -> Get from cache first
  856. let page = await WIKI.models.pages.getPageFromCache(opts)
  857. if (!page) {
  858. // -> Get from DB
  859. page = await WIKI.models.pages.getPageFromDb(opts)
  860. if (page) {
  861. if (page.render) {
  862. // -> Save render to cache
  863. await WIKI.models.pages.savePageToCache(page)
  864. } else {
  865. // -> No render? Possible duplicate issue
  866. /* TODO: Detect duplicate and delete */
  867. throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try 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. }