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.

855 lines
24 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
  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. /* global WIKI */
  12. const frontmatterRegex = {
  13. html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
  14. legacy: /^(<!-- TITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) ?-{2}>)?(?:\n|\r)*([\w\W]*)*/i,
  15. markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
  16. }
  17. const punctuationRegex = /[!,:;/\\_+\-=()&#@<>$~%^*[\]{}"'|]+|(\.\s)|(\s\.)/ig
  18. // const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
  19. /**
  20. * Pages model
  21. */
  22. module.exports = class Page extends Model {
  23. static get tableName() { return 'pages' }
  24. static get jsonSchema () {
  25. return {
  26. type: 'object',
  27. required: ['path', 'title'],
  28. properties: {
  29. id: {type: 'integer'},
  30. path: {type: 'string'},
  31. hash: {type: 'string'},
  32. title: {type: 'string'},
  33. description: {type: 'string'},
  34. isPublished: {type: 'boolean'},
  35. privateNS: {type: 'string'},
  36. publishStartDate: {type: 'string'},
  37. publishEndDate: {type: 'string'},
  38. content: {type: 'string'},
  39. contentType: {type: 'string'},
  40. createdAt: {type: 'string'},
  41. updatedAt: {type: 'string'}
  42. }
  43. }
  44. }
  45. static get relationMappings() {
  46. return {
  47. tags: {
  48. relation: Model.ManyToManyRelation,
  49. modelClass: require('./tags'),
  50. join: {
  51. from: 'pages.id',
  52. through: {
  53. from: 'pageTags.pageId',
  54. to: 'pageTags.tagId'
  55. },
  56. to: 'tags.id'
  57. }
  58. },
  59. links: {
  60. relation: Model.HasManyRelation,
  61. modelClass: require('./pageLinks'),
  62. join: {
  63. from: 'pages.id',
  64. to: 'pageLinks.pageId'
  65. }
  66. },
  67. author: {
  68. relation: Model.BelongsToOneRelation,
  69. modelClass: require('./users'),
  70. join: {
  71. from: 'pages.authorId',
  72. to: 'users.id'
  73. }
  74. },
  75. creator: {
  76. relation: Model.BelongsToOneRelation,
  77. modelClass: require('./users'),
  78. join: {
  79. from: 'pages.creatorId',
  80. to: 'users.id'
  81. }
  82. },
  83. editor: {
  84. relation: Model.BelongsToOneRelation,
  85. modelClass: require('./editors'),
  86. join: {
  87. from: 'pages.editorKey',
  88. to: 'editors.key'
  89. }
  90. },
  91. locale: {
  92. relation: Model.BelongsToOneRelation,
  93. modelClass: require('./locales'),
  94. join: {
  95. from: 'pages.localeCode',
  96. to: 'locales.code'
  97. }
  98. }
  99. }
  100. }
  101. $beforeUpdate() {
  102. this.updatedAt = new Date().toISOString()
  103. }
  104. $beforeInsert() {
  105. this.createdAt = new Date().toISOString()
  106. this.updatedAt = new Date().toISOString()
  107. }
  108. /**
  109. * Cache Schema
  110. */
  111. static get cacheSchema() {
  112. return new JSBinType({
  113. id: 'uint',
  114. authorId: 'uint',
  115. authorName: 'string',
  116. createdAt: 'string',
  117. creatorId: 'uint',
  118. creatorName: 'string',
  119. description: 'string',
  120. isPrivate: 'boolean',
  121. isPublished: 'boolean',
  122. publishEndDate: 'string',
  123. publishStartDate: 'string',
  124. render: 'string',
  125. tags: [
  126. {
  127. tag: 'string',
  128. title: 'string'
  129. }
  130. ],
  131. title: 'string',
  132. toc: 'string',
  133. updatedAt: 'string'
  134. })
  135. }
  136. /**
  137. * Inject page metadata into contents
  138. *
  139. * @returns {string} Page Contents with Injected Metadata
  140. */
  141. injectMetadata () {
  142. return pageHelper.injectPageMetadata(this)
  143. }
  144. /**
  145. * Get the page's file extension based on content type
  146. *
  147. * @returns {string} File Extension
  148. */
  149. getFileExtension() {
  150. return pageHelper.getFileExtension(this.contentType)
  151. }
  152. /**
  153. * Parse injected page metadata from raw content
  154. *
  155. * @param {String} raw Raw file contents
  156. * @param {String} contentType Content Type
  157. * @returns {Object} Parsed Page Metadata with Raw Content
  158. */
  159. static parseMetadata (raw, contentType) {
  160. let result
  161. switch (contentType) {
  162. case 'markdown':
  163. result = frontmatterRegex.markdown.exec(raw)
  164. if (result[2]) {
  165. return {
  166. ...yaml.safeLoad(result[2]),
  167. content: result[3]
  168. }
  169. } else {
  170. // Attempt legacy v1 format
  171. result = frontmatterRegex.legacy.exec(raw)
  172. if (result[2]) {
  173. return {
  174. title: result[2],
  175. description: result[4],
  176. content: result[5]
  177. }
  178. }
  179. }
  180. break
  181. case 'html':
  182. result = frontmatterRegex.html.exec(raw)
  183. if (result[2]) {
  184. return {
  185. ...yaml.safeLoad(result[2]),
  186. content: result[3]
  187. }
  188. }
  189. break
  190. }
  191. return {
  192. content: raw
  193. }
  194. }
  195. /**
  196. * Create a New Page
  197. *
  198. * @param {Object} opts Page Properties
  199. * @returns {Promise} Promise of the Page Model Instance
  200. */
  201. static async createPage(opts) {
  202. // -> Validate path
  203. if (opts.path.indexOf('.') >= 0 || opts.path.indexOf(' ') >= 0) {
  204. throw new WIKI.Error.PageIllegalPath()
  205. }
  206. // -> Check for page access
  207. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  208. locale: opts.locale,
  209. path: opts.path
  210. })) {
  211. throw new WIKI.Error.PageDeleteForbidden()
  212. }
  213. // -> Check for duplicate
  214. const dupCheck = await WIKI.models.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
  215. if (dupCheck) {
  216. throw new WIKI.Error.PageDuplicateCreate()
  217. }
  218. // -> Check for empty content
  219. if (!opts.content || _.trim(opts.content).length < 1) {
  220. throw new WIKI.Error.PageEmptyContent()
  221. }
  222. // -> Create page
  223. await WIKI.models.pages.query().insert({
  224. authorId: opts.user.id,
  225. content: opts.content,
  226. creatorId: opts.user.id,
  227. contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
  228. description: opts.description,
  229. editorKey: opts.editor,
  230. hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
  231. isPrivate: opts.isPrivate,
  232. isPublished: opts.isPublished,
  233. localeCode: opts.locale,
  234. path: opts.path,
  235. publishEndDate: opts.publishEndDate || '',
  236. publishStartDate: opts.publishStartDate || '',
  237. title: opts.title,
  238. toc: '[]'
  239. })
  240. const page = await WIKI.models.pages.getPageFromDb({
  241. path: opts.path,
  242. locale: opts.locale,
  243. userId: opts.user.id,
  244. isPrivate: opts.isPrivate
  245. })
  246. // -> Save Tags
  247. if (opts.tags && opts.tags.length > 0) {
  248. await WIKI.models.tags.associateTags({ tags: opts.tags, page })
  249. }
  250. // -> Render page to HTML
  251. await WIKI.models.pages.renderPage(page)
  252. // -> Rebuild page tree
  253. await WIKI.models.pages.rebuildTree()
  254. // -> Add to Search Index
  255. const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
  256. page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
  257. await WIKI.data.searchEngine.created(page)
  258. // -> Add to Storage
  259. if (!opts.skipStorage) {
  260. await WIKI.models.storage.pageEvent({
  261. event: 'created',
  262. page
  263. })
  264. }
  265. // -> Reconnect Links
  266. await WIKI.models.pages.reconnectLinks({
  267. locale: page.localeCode,
  268. path: page.path,
  269. mode: 'create'
  270. })
  271. return page
  272. }
  273. /**
  274. * Update an Existing Page
  275. *
  276. * @param {Object} opts Page Properties
  277. * @returns {Promise} Promise of the Page Model Instance
  278. */
  279. static async updatePage(opts) {
  280. // -> Fetch original page
  281. const ogPage = await WIKI.models.pages.query().findById(opts.id)
  282. if (!ogPage) {
  283. throw new Error('Invalid Page Id')
  284. }
  285. // -> Check for page access
  286. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  287. locale: opts.locale,
  288. path: opts.path
  289. })) {
  290. throw new WIKI.Error.PageUpdateForbidden()
  291. }
  292. // -> Check for empty content
  293. if (!opts.content || _.trim(opts.content).length < 1) {
  294. throw new WIKI.Error.PageEmptyContent()
  295. }
  296. // -> Create version snapshot
  297. await WIKI.models.pageHistory.addVersion({
  298. ...ogPage,
  299. isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
  300. action: opts.action ? opts.action : 'updated',
  301. versionDate: ogPage.updatedAt
  302. })
  303. // -> Update page
  304. await WIKI.models.pages.query().patch({
  305. authorId: opts.user.id,
  306. content: opts.content,
  307. description: opts.description,
  308. isPublished: opts.isPublished === true || opts.isPublished === 1,
  309. publishEndDate: opts.publishEndDate || '',
  310. publishStartDate: opts.publishStartDate || '',
  311. title: opts.title
  312. }).where('id', ogPage.id)
  313. let page = await WIKI.models.pages.getPageFromDb({
  314. path: ogPage.path,
  315. locale: ogPage.localeCode,
  316. userId: ogPage.authorId,
  317. isPrivate: ogPage.isPrivate
  318. })
  319. // -> Save Tags
  320. await WIKI.models.tags.associateTags({ tags: opts.tags, page })
  321. // -> Render page to HTML
  322. await WIKI.models.pages.renderPage(page)
  323. // -> Update Search Index
  324. const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
  325. page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
  326. await WIKI.data.searchEngine.updated(page)
  327. // -> Update on Storage
  328. if (!opts.skipStorage) {
  329. await WIKI.models.storage.pageEvent({
  330. event: 'updated',
  331. page
  332. })
  333. }
  334. // -> Perform move?
  335. if ((opts.locale && opts.locale !== page.localeCode) || (opts.path && opts.path !== page.path)) {
  336. await WIKI.models.pages.movePage({
  337. id: page.id,
  338. destinationLocale: opts.locale,
  339. destinationPath: opts.path,
  340. user: opts.user
  341. })
  342. } else {
  343. // -> Update title of page tree entry
  344. await WIKI.models.knex.table('pageTree').where({
  345. pageId: page.id
  346. }).update('title', page.title)
  347. }
  348. return page
  349. }
  350. /**
  351. * Move a Page
  352. *
  353. * @param {Object} opts Page Properties
  354. * @returns {Promise} Promise with no value
  355. */
  356. static async movePage(opts) {
  357. const page = await WIKI.models.pages.query().findById(opts.id)
  358. if (!page) {
  359. throw new WIKI.Error.PageNotFound()
  360. }
  361. // -> Check for source page access
  362. if (!WIKI.auth.checkAccess(opts.user, ['manage:pages'], {
  363. locale: page.sourceLocale,
  364. path: page.sourcePath
  365. })) {
  366. throw new WIKI.Error.PageMoveForbidden()
  367. }
  368. // -> Check for destination page access
  369. if (!WIKI.auth.checkAccess(opts.user, ['write:pages'], {
  370. locale: opts.destinationLocale,
  371. path: opts.destinationPath
  372. })) {
  373. throw new WIKI.Error.PageMoveForbidden()
  374. }
  375. // -> Check for existing page at destination path
  376. const destPage = await await WIKI.models.pages.query().findOne({
  377. path: opts.destinationPath,
  378. localeCode: opts.destinationLocale
  379. })
  380. if (destPage) {
  381. throw new WIKI.Error.PagePathCollision()
  382. }
  383. // -> Create version snapshot
  384. await WIKI.models.pageHistory.addVersion({
  385. ...page,
  386. action: 'moved',
  387. versionDate: page.updatedAt
  388. })
  389. const destinationHash = pageHelper.generateHash({ path: opts.destinationPath, locale: opts.destinationLocale, privateNS: opts.isPrivate ? 'TODO' : '' })
  390. // -> Move page
  391. await WIKI.models.pages.query().patch({
  392. path: opts.destinationPath,
  393. localeCode: opts.destinationLocale,
  394. hash: destinationHash
  395. }).findById(page.id)
  396. await WIKI.models.pages.deletePageFromCache(page)
  397. // -> Rebuild page tree
  398. await WIKI.models.pages.rebuildTree()
  399. // -> Rename in Search Index
  400. await WIKI.data.searchEngine.renamed({
  401. ...page,
  402. destinationPath: opts.destinationPath,
  403. destinationLocaleCode: opts.destinationLocale,
  404. destinationHash
  405. })
  406. // -> Rename in Storage
  407. if (!opts.skipStorage) {
  408. await WIKI.models.storage.pageEvent({
  409. event: 'renamed',
  410. page: {
  411. ...page,
  412. destinationPath: opts.destinationPath,
  413. destinationLocaleCode: opts.destinationLocale,
  414. destinationHash,
  415. moveAuthorId: opts.user.id,
  416. moveAuthorName: opts.user.name,
  417. moveAuthorEmail: opts.user.email
  418. }
  419. })
  420. }
  421. // -> Reconnect Links
  422. await WIKI.models.pages.reconnectLinks({
  423. sourceLocale: page.localeCode,
  424. sourcePath: page.path,
  425. locale: opts.destinationLocale,
  426. path: opts.destinationPath,
  427. mode: 'move'
  428. })
  429. }
  430. /**
  431. * Delete an Existing Page
  432. *
  433. * @param {Object} opts Page Properties
  434. * @returns {Promise} Promise with no value
  435. */
  436. static async deletePage(opts) {
  437. let page
  438. if (_.has(opts, 'id')) {
  439. page = await WIKI.models.pages.query().findById(opts.id)
  440. } else {
  441. page = await await WIKI.models.pages.query().findOne({
  442. path: opts.path,
  443. localeCode: opts.locale
  444. })
  445. }
  446. if (!page) {
  447. throw new Error('Invalid Page Id')
  448. }
  449. // -> Check for page access
  450. if (!WIKI.auth.checkAccess(opts.user, ['delete:pages'], {
  451. locale: page.locale,
  452. path: page.path
  453. })) {
  454. throw new WIKI.Error.PageDeleteForbidden()
  455. }
  456. // -> Create version snapshot
  457. await WIKI.models.pageHistory.addVersion({
  458. ...page,
  459. action: 'deleted',
  460. versionDate: page.updatedAt
  461. })
  462. // -> Delete page
  463. await WIKI.models.pages.query().delete().where('id', page.id)
  464. await WIKI.models.pages.deletePageFromCache(page)
  465. // -> Rebuild page tree
  466. await WIKI.models.pages.rebuildTree()
  467. // -> Delete from Search Index
  468. await WIKI.data.searchEngine.deleted(page)
  469. // -> Delete from Storage
  470. if (!opts.skipStorage) {
  471. await WIKI.models.storage.pageEvent({
  472. event: 'deleted',
  473. page
  474. })
  475. }
  476. // -> Reconnect Links
  477. await WIKI.models.pages.reconnectLinks({
  478. locale: page.localeCode,
  479. path: page.path,
  480. mode: 'delete'
  481. })
  482. }
  483. /**
  484. * Reconnect links to new/move/deleted page
  485. *
  486. * @param {Object} opts - Page parameters
  487. * @param {string} opts.path - Page Path
  488. * @param {string} opts.locale - Page Locale Code
  489. * @param {string} [opts.sourcePath] - Previous Page Path (move only)
  490. * @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
  491. * @param {string} opts.mode - Page Update mode (create, move, delete)
  492. * @returns {Promise} Promise with no value
  493. */
  494. static async reconnectLinks (opts) {
  495. const pageHref = `/${opts.locale}/${opts.path}`
  496. let replaceArgs = {
  497. from: '',
  498. to: ''
  499. }
  500. switch (opts.mode) {
  501. case 'create':
  502. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  503. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  504. break
  505. case 'move':
  506. const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
  507. replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-invalid-page">`
  508. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  509. break
  510. case 'delete':
  511. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  512. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  513. break
  514. default:
  515. return false
  516. }
  517. let affectedHashes = []
  518. // -> Perform replace and return affected page hashes (POSTGRES only)
  519. if (WIKI.config.db.type === 'postgres') {
  520. const qryHashes = await WIKI.models.pages.query()
  521. .returning('hash')
  522. .patch({
  523. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  524. })
  525. .whereIn('pages.id', function () {
  526. this.select('pageLinks.pageId').from('pageLinks').where({
  527. 'pageLinks.path': opts.path,
  528. 'pageLinks.localeCode': opts.locale
  529. })
  530. })
  531. affectedHashes = qryHashes.map(h => h.hash)
  532. } else {
  533. // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, MSSQL, SQLITE only)
  534. await WIKI.models.pages.query()
  535. .patch({
  536. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  537. })
  538. .whereIn('pages.id', function () {
  539. this.select('pageLinks.pageId').from('pageLinks').where({
  540. 'pageLinks.path': opts.path,
  541. 'pageLinks.localeCode': opts.locale
  542. })
  543. })
  544. const qryHashes = await WIKI.models.pages.query()
  545. .column('hash')
  546. .whereIn('pages.id', function () {
  547. this.select('pageLinks.pageId').from('pageLinks').where({
  548. 'pageLinks.path': opts.path,
  549. 'pageLinks.localeCode': opts.locale
  550. })
  551. })
  552. affectedHashes = qryHashes.map(h => h.hash)
  553. }
  554. for (const hash of affectedHashes) {
  555. await WIKI.models.pages.deletePageFromCache({ hash })
  556. }
  557. }
  558. /**
  559. * Rebuild page tree for new/updated/deleted page
  560. *
  561. * @returns {Promise} Promise with no value
  562. */
  563. static async rebuildTree() {
  564. const rebuildJob = await WIKI.scheduler.registerJob({
  565. name: 'rebuild-tree',
  566. immediate: true,
  567. worker: true
  568. })
  569. return rebuildJob.finished
  570. }
  571. /**
  572. * Trigger the rendering of a page
  573. *
  574. * @param {Object} page Page Model Instance
  575. * @returns {Promise} Promise with no value
  576. */
  577. static async renderPage(page) {
  578. const renderJob = await WIKI.scheduler.registerJob({
  579. name: 'render-page',
  580. immediate: true,
  581. worker: true
  582. }, page.id)
  583. return renderJob.finished
  584. }
  585. /**
  586. * Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
  587. *
  588. * @param {Object} opts Page Properties
  589. * @returns {Promise} Promise of the Page Model Instance
  590. */
  591. static async getPage(opts) {
  592. // -> Get from cache first
  593. let page = await WIKI.models.pages.getPageFromCache(opts)
  594. if (!page) {
  595. // -> Get from DB
  596. page = await WIKI.models.pages.getPageFromDb(opts)
  597. if (page) {
  598. if (page.render) {
  599. // -> Save render to cache
  600. await WIKI.models.pages.savePageToCache(page)
  601. } else {
  602. // -> No render? Possible duplicate issue
  603. /* TODO: Detect duplicate and delete */
  604. throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try again.')
  605. }
  606. }
  607. }
  608. return page
  609. }
  610. /**
  611. * Fetch an Existing Page from the Database
  612. *
  613. * @param {Object} opts Page Properties
  614. * @returns {Promise} Promise of the Page Model Instance
  615. */
  616. static async getPageFromDb(opts) {
  617. const queryModeID = _.isNumber(opts)
  618. try {
  619. return WIKI.models.pages.query()
  620. .column([
  621. 'pages.id',
  622. 'pages.path',
  623. 'pages.hash',
  624. 'pages.title',
  625. 'pages.description',
  626. 'pages.isPrivate',
  627. 'pages.isPublished',
  628. 'pages.privateNS',
  629. 'pages.publishStartDate',
  630. 'pages.publishEndDate',
  631. 'pages.content',
  632. 'pages.render',
  633. 'pages.toc',
  634. 'pages.contentType',
  635. 'pages.createdAt',
  636. 'pages.updatedAt',
  637. 'pages.editorKey',
  638. 'pages.localeCode',
  639. 'pages.authorId',
  640. 'pages.creatorId',
  641. {
  642. authorName: 'author.name',
  643. authorEmail: 'author.email',
  644. creatorName: 'creator.name',
  645. creatorEmail: 'creator.email'
  646. }
  647. ])
  648. .joinRelated('author')
  649. .joinRelated('creator')
  650. .withGraphJoined('tags')
  651. .modifyGraph('tags', builder => {
  652. builder.select('tag', 'title')
  653. })
  654. .where(queryModeID ? {
  655. 'pages.id': opts
  656. } : {
  657. 'pages.path': opts.path,
  658. 'pages.localeCode': opts.locale
  659. })
  660. // .andWhere(builder => {
  661. // if (queryModeID) return
  662. // builder.where({
  663. // 'pages.isPublished': true
  664. // }).orWhere({
  665. // 'pages.isPublished': false,
  666. // 'pages.authorId': opts.userId
  667. // })
  668. // })
  669. // .andWhere(builder => {
  670. // if (queryModeID) return
  671. // if (opts.isPrivate) {
  672. // builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
  673. // } else {
  674. // builder.where({ 'pages.isPrivate': false })
  675. // }
  676. // })
  677. .first()
  678. } catch (err) {
  679. WIKI.logger.warn(err)
  680. throw err
  681. }
  682. }
  683. /**
  684. * Save a Page Model Instance to Cache
  685. *
  686. * @param {Object} page Page Model Instance
  687. * @returns {Promise} Promise with no value
  688. */
  689. static async savePageToCache(page) {
  690. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${page.hash}.bin`)
  691. await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
  692. id: page.id,
  693. authorId: page.authorId,
  694. authorName: page.authorName,
  695. createdAt: page.createdAt,
  696. creatorId: page.creatorId,
  697. creatorName: page.creatorName,
  698. description: page.description,
  699. isPrivate: page.isPrivate === 1 || page.isPrivate === true,
  700. isPublished: page.isPublished === 1 || page.isPublished === true,
  701. publishEndDate: page.publishEndDate,
  702. publishStartDate: page.publishStartDate,
  703. render: page.render,
  704. tags: page.tags.map(t => _.pick(t, ['tag', 'title'])),
  705. title: page.title,
  706. toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
  707. updatedAt: page.updatedAt
  708. }))
  709. }
  710. /**
  711. * Fetch an Existing Page from Cache
  712. *
  713. * @param {Object} opts Page Properties
  714. * @returns {Promise} Promise of the Page Model Instance
  715. */
  716. static async getPageFromCache(opts) {
  717. const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
  718. const cachePath = path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${pageHash}.bin`)
  719. try {
  720. const pageBuffer = await fs.readFile(cachePath)
  721. let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
  722. return {
  723. ...page,
  724. path: opts.path,
  725. localeCode: opts.locale,
  726. isPrivate: opts.isPrivate
  727. }
  728. } catch (err) {
  729. if (err.code === 'ENOENT') {
  730. return false
  731. }
  732. WIKI.logger.error(err)
  733. throw err
  734. }
  735. }
  736. /**
  737. * Delete an Existing Page from Cache
  738. *
  739. * @param {Object} page Page Model Instance
  740. * @param {string} page.hash Hash of the Page
  741. * @returns {Promise} Promise with no value
  742. */
  743. static async deletePageFromCache(page) {
  744. return fs.remove(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache/${page.hash}.bin`))
  745. }
  746. /**
  747. * Flush the contents of the Cache
  748. */
  749. static async flushCache() {
  750. return fs.emptyDir(path.resolve(WIKI.ROOTPATH, WIKI.config.dataPath, `cache`))
  751. }
  752. /**
  753. * Migrate all pages from a source locale to the target locale
  754. *
  755. * @param {Object} opts Migration properties
  756. * @param {string} opts.sourceLocale Source Locale Code
  757. * @param {string} opts.targetLocale Target Locale Code
  758. * @returns {Promise} Promise with no value
  759. */
  760. static async migrateToLocale({ sourceLocale, targetLocale }) {
  761. return WIKI.models.pages.query()
  762. .patch({
  763. localeCode: targetLocale
  764. })
  765. .where({
  766. localeCode: sourceLocale
  767. })
  768. .whereNotExists(function() {
  769. this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
  770. })
  771. }
  772. /**
  773. * Clean raw HTML from content for use in search engines
  774. *
  775. * @param {string} rawHTML Raw HTML
  776. * @returns {string} Cleaned Content Text
  777. */
  778. static cleanHTML(rawHTML = '') {
  779. let data = striptags(rawHTML || '', [], ' ')
  780. .replace(emojiRegex(), '')
  781. // .replace(htmlEntitiesRegex, '')
  782. return he.decode(data)
  783. .replace(punctuationRegex, ' ')
  784. .replace(/(\r\n|\n|\r)/gm, ' ')
  785. .replace(/\s\s+/g, ' ')
  786. .split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
  787. }
  788. }