pages.js 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  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. switch (this.contentType) {
  151. case 'markdown':
  152. return 'md'
  153. case 'html':
  154. return 'html'
  155. default:
  156. return 'txt'
  157. }
  158. }
  159. /**
  160. * Parse injected page metadata from raw content
  161. *
  162. * @param {String} raw Raw file contents
  163. * @param {String} contentType Content Type
  164. * @returns {Object} Parsed Page Metadata with Raw Content
  165. */
  166. static parseMetadata (raw, contentType) {
  167. let result
  168. switch (contentType) {
  169. case 'markdown':
  170. result = frontmatterRegex.markdown.exec(raw)
  171. if (result[2]) {
  172. return {
  173. ...yaml.safeLoad(result[2]),
  174. content: result[3]
  175. }
  176. } else {
  177. // Attempt legacy v1 format
  178. result = frontmatterRegex.legacy.exec(raw)
  179. if (result[2]) {
  180. return {
  181. title: result[2],
  182. description: result[4],
  183. content: result[5]
  184. }
  185. }
  186. }
  187. break
  188. case 'html':
  189. result = frontmatterRegex.html.exec(raw)
  190. if (result[2]) {
  191. return {
  192. ...yaml.safeLoad(result[2]),
  193. content: result[3]
  194. }
  195. }
  196. break
  197. }
  198. return {
  199. content: raw
  200. }
  201. }
  202. /**
  203. * Create a New Page
  204. *
  205. * @param {Object} opts Page Properties
  206. * @returns {Promise} Promise of the Page Model Instance
  207. */
  208. static async createPage(opts) {
  209. const dupCheck = await WIKI.models.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
  210. if (dupCheck) {
  211. throw new WIKI.Error.PageDuplicateCreate()
  212. }
  213. if (!opts.content || _.trim(opts.content).length < 1) {
  214. throw new WIKI.Error.PageEmptyContent()
  215. }
  216. await WIKI.models.pages.query().insert({
  217. authorId: opts.authorId,
  218. content: opts.content,
  219. creatorId: opts.authorId,
  220. contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
  221. description: opts.description,
  222. editorKey: opts.editor,
  223. hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
  224. isPrivate: opts.isPrivate,
  225. isPublished: opts.isPublished,
  226. localeCode: opts.locale,
  227. path: opts.path,
  228. publishEndDate: opts.publishEndDate || '',
  229. publishStartDate: opts.publishStartDate || '',
  230. title: opts.title,
  231. toc: '[]'
  232. })
  233. const page = await WIKI.models.pages.getPageFromDb({
  234. path: opts.path,
  235. locale: opts.locale,
  236. userId: opts.authorId,
  237. isPrivate: opts.isPrivate
  238. })
  239. // -> Save Tags
  240. if (opts.tags.length > 0) {
  241. await WIKI.models.tags.associateTags({ tags: opts.tags, page })
  242. }
  243. // -> Render page to HTML
  244. await WIKI.models.pages.renderPage(page)
  245. // -> Add to Search Index
  246. const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
  247. page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
  248. await WIKI.data.searchEngine.created(page)
  249. // -> Add to Storage
  250. if (!opts.skipStorage) {
  251. await WIKI.models.storage.pageEvent({
  252. event: 'created',
  253. page
  254. })
  255. }
  256. // -> Reconnect Links
  257. await WIKI.models.pages.reconnectLinks({
  258. locale: page.localeCode,
  259. path: page.path,
  260. mode: 'create'
  261. })
  262. return page
  263. }
  264. /**
  265. * Update an Existing Page
  266. *
  267. * @param {Object} opts Page Properties
  268. * @returns {Promise} Promise of the Page Model Instance
  269. */
  270. static async updatePage(opts) {
  271. const ogPage = await WIKI.models.pages.query().findById(opts.id)
  272. if (!ogPage) {
  273. throw new Error('Invalid Page Id')
  274. }
  275. if (!opts.content || _.trim(opts.content).length < 1) {
  276. throw new WIKI.Error.PageEmptyContent()
  277. }
  278. await WIKI.models.pageHistory.addVersion({
  279. ...ogPage,
  280. isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
  281. action: 'updated'
  282. })
  283. await WIKI.models.pages.query().patch({
  284. authorId: opts.authorId,
  285. content: opts.content,
  286. description: opts.description,
  287. isPublished: opts.isPublished === true || opts.isPublished === 1,
  288. publishEndDate: opts.publishEndDate || '',
  289. publishStartDate: opts.publishStartDate || '',
  290. title: opts.title
  291. }).where('id', ogPage.id)
  292. const page = await WIKI.models.pages.getPageFromDb({
  293. path: ogPage.path,
  294. locale: ogPage.localeCode,
  295. userId: ogPage.authorId,
  296. isPrivate: ogPage.isPrivate
  297. })
  298. // -> Save Tags
  299. await WIKI.models.tags.associateTags({ tags: opts.tags, page })
  300. // -> Render page to HTML
  301. await WIKI.models.pages.renderPage(page)
  302. // -> Update Search Index
  303. const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
  304. page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
  305. await WIKI.data.searchEngine.updated(page)
  306. // -> Update on Storage
  307. if (!opts.skipStorage) {
  308. await WIKI.models.storage.pageEvent({
  309. event: 'updated',
  310. page
  311. })
  312. }
  313. return page
  314. }
  315. /**
  316. * Delete an Existing Page
  317. *
  318. * @param {Object} opts Page Properties
  319. * @returns {Promise} Promise with no value
  320. */
  321. static async deletePage(opts) {
  322. let page
  323. if (_.has(opts, 'id')) {
  324. page = await WIKI.models.pages.query().findById(opts.id)
  325. } else {
  326. page = await await WIKI.models.pages.query().findOne({
  327. path: opts.path,
  328. localeCode: opts.locale
  329. })
  330. }
  331. if (!page) {
  332. throw new Error('Invalid Page Id')
  333. }
  334. await WIKI.models.pageHistory.addVersion({
  335. ...page,
  336. action: 'deleted'
  337. })
  338. await WIKI.models.pages.query().delete().where('id', page.id)
  339. await WIKI.models.pages.deletePageFromCache(page)
  340. // -> Delete from Search Index
  341. await WIKI.data.searchEngine.deleted(page)
  342. // -> Delete from Storage
  343. if (!opts.skipStorage) {
  344. await WIKI.models.storage.pageEvent({
  345. event: 'deleted',
  346. page
  347. })
  348. }
  349. // -> Reconnect Links
  350. await WIKI.models.pages.reconnectLinks({
  351. locale: page.localeCode,
  352. path: page.path,
  353. mode: 'delete'
  354. })
  355. }
  356. /**
  357. * Reconnect links to new/updated/deleted page
  358. *
  359. * @param {Object} opts - Page parameters
  360. * @param {string} opts.path - Page Path
  361. * @param {string} opts.locale - Page Locale Code
  362. * @param {string} [opts.sourcePath] - Previous Page Path (move only)
  363. * @param {string} [opts.sourceLocale] - Previous Page Locale Code (move only)
  364. * @param {string} opts.mode - Page Update mode (new, move, delete)
  365. * @returns {Promise} Promise with no value
  366. */
  367. static async reconnectLinks (opts) {
  368. const pageHref = `/${opts.locale}/${opts.path}`
  369. let replaceArgs = {
  370. from: '',
  371. to: ''
  372. }
  373. switch (opts.mode) {
  374. case 'create':
  375. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  376. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  377. break
  378. case 'move':
  379. const prevPageHref = `/${opts.sourceLocale}/${opts.sourcePath}`
  380. replaceArgs.from = `<a href="${prevPageHref}" class="is-internal-link is-invalid-page">`
  381. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  382. break
  383. case 'delete':
  384. replaceArgs.from = `<a href="${pageHref}" class="is-internal-link is-valid-page">`
  385. replaceArgs.to = `<a href="${pageHref}" class="is-internal-link is-invalid-page">`
  386. break
  387. default:
  388. return false
  389. }
  390. let affectedHashes = []
  391. // -> Perform replace and return affected page hashes (POSTGRES, MSSQL only)
  392. if (WIKI.config.db.type === 'postgres' || WIKI.config.db.type === 'mssql') {
  393. affectedHashes = await WIKI.models.pages.query()
  394. .returning('hash')
  395. .patch({
  396. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  397. })
  398. .whereIn('pages.id', function () {
  399. this.select('pageLinks.pageId').from('pageLinks').where({
  400. 'pageLinks.path': opts.path,
  401. 'pageLinks.localeCode': opts.locale
  402. })
  403. })
  404. .pluck('hash')
  405. } else {
  406. // -> Perform replace, then query affected page hashes (MYSQL, MARIADB, SQLITE only)
  407. await WIKI.models.pages.query()
  408. .patch({
  409. render: WIKI.models.knex.raw('REPLACE(??, ?, ?)', ['render', replaceArgs.from, replaceArgs.to])
  410. })
  411. .whereIn('pages.id', function () {
  412. this.select('pageLinks.pageId').from('pageLinks').where({
  413. 'pageLinks.path': opts.path,
  414. 'pageLinks.localeCode': opts.locale
  415. })
  416. })
  417. affectedHashes = await WIKI.models.pages.query()
  418. .column('hash')
  419. .whereIn('pages.id', function () {
  420. this.select('pageLinks.pageId').from('pageLinks').where({
  421. 'pageLinks.path': opts.path,
  422. 'pageLinks.localeCode': opts.locale
  423. })
  424. })
  425. .pluck('hash')
  426. }
  427. for (const hash of affectedHashes) {
  428. await WIKI.models.pages.deletePageFromCache({ hash })
  429. }
  430. }
  431. /**
  432. * Trigger the rendering of a page
  433. *
  434. * @param {Object} page Page Model Instance
  435. * @returns {Promise} Promise with no value
  436. */
  437. static async renderPage(page) {
  438. const renderJob = await WIKI.scheduler.registerJob({
  439. name: 'render-page',
  440. immediate: true,
  441. worker: true
  442. }, page.id)
  443. return renderJob.finished
  444. }
  445. /**
  446. * Fetch an Existing Page from Cache if possible, from DB otherwise and save render to Cache
  447. *
  448. * @param {Object} opts Page Properties
  449. * @returns {Promise} Promise of the Page Model Instance
  450. */
  451. static async getPage(opts) {
  452. // -> Get from cache first
  453. let page = await WIKI.models.pages.getPageFromCache(opts)
  454. if (!page) {
  455. // -> Get from DB
  456. page = await WIKI.models.pages.getPageFromDb(opts)
  457. if (page) {
  458. if (page.render) {
  459. // -> Save render to cache
  460. await WIKI.models.pages.savePageToCache(page)
  461. } else {
  462. // -> No render? Possible duplicate issue
  463. /* TODO: Detect duplicate and delete */
  464. throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try again.')
  465. }
  466. }
  467. }
  468. return page
  469. }
  470. /**
  471. * Fetch an Existing Page from the Database
  472. *
  473. * @param {Object} opts Page Properties
  474. * @returns {Promise} Promise of the Page Model Instance
  475. */
  476. static async getPageFromDb(opts) {
  477. const queryModeID = _.isNumber(opts)
  478. try {
  479. return WIKI.models.pages.query()
  480. .column([
  481. 'pages.*',
  482. {
  483. authorName: 'author.name',
  484. authorEmail: 'author.email',
  485. creatorName: 'creator.name',
  486. creatorEmail: 'creator.email'
  487. }
  488. ])
  489. .joinRelation('author')
  490. .joinRelation('creator')
  491. .eagerAlgorithm(Model.JoinEagerAlgorithm)
  492. .eager('tags(selectTags)', {
  493. selectTags: builder => {
  494. builder.select('tag', 'title')
  495. }
  496. })
  497. .where(queryModeID ? {
  498. 'pages.id': opts
  499. } : {
  500. 'pages.path': opts.path,
  501. 'pages.localeCode': opts.locale
  502. })
  503. // .andWhere(builder => {
  504. // if (queryModeID) return
  505. // builder.where({
  506. // 'pages.isPublished': true
  507. // }).orWhere({
  508. // 'pages.isPublished': false,
  509. // 'pages.authorId': opts.userId
  510. // })
  511. // })
  512. // .andWhere(builder => {
  513. // if (queryModeID) return
  514. // if (opts.isPrivate) {
  515. // builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
  516. // } else {
  517. // builder.where({ 'pages.isPrivate': false })
  518. // }
  519. // })
  520. .first()
  521. } catch (err) {
  522. WIKI.logger.warn(err)
  523. throw err
  524. }
  525. }
  526. /**
  527. * Save a Page Model Instance to Cache
  528. *
  529. * @param {Object} page Page Model Instance
  530. * @returns {Promise} Promise with no value
  531. */
  532. static async savePageToCache(page) {
  533. const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
  534. await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
  535. id: page.id,
  536. authorId: page.authorId,
  537. authorName: page.authorName,
  538. createdAt: page.createdAt,
  539. creatorId: page.creatorId,
  540. creatorName: page.creatorName,
  541. description: page.description,
  542. isPrivate: page.isPrivate === 1 || page.isPrivate === true,
  543. isPublished: page.isPublished === 1 || page.isPublished === true,
  544. publishEndDate: page.publishEndDate,
  545. publishStartDate: page.publishStartDate,
  546. render: page.render,
  547. tags: page.tags.map(t => _.pick(t, ['tag', 'title'])),
  548. title: page.title,
  549. toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
  550. updatedAt: page.updatedAt
  551. }))
  552. }
  553. /**
  554. * Fetch an Existing Page from Cache
  555. *
  556. * @param {Object} opts Page Properties
  557. * @returns {Promise} Promise of the Page Model Instance
  558. */
  559. static async getPageFromCache(opts) {
  560. const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
  561. const cachePath = path.join(process.cwd(), `data/cache/${pageHash}.bin`)
  562. try {
  563. const pageBuffer = await fs.readFile(cachePath)
  564. let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
  565. return {
  566. ...page,
  567. path: opts.path,
  568. localeCode: opts.locale,
  569. isPrivate: opts.isPrivate
  570. }
  571. } catch (err) {
  572. if (err.code === 'ENOENT') {
  573. return false
  574. }
  575. WIKI.logger.error(err)
  576. throw err
  577. }
  578. }
  579. /**
  580. * Delete an Existing Page from Cache
  581. *
  582. * @param {Object} page Page Model Instance
  583. * @param {string} page.hash Hash of the Page
  584. * @returns {Promise} Promise with no value
  585. */
  586. static async deletePageFromCache(page) {
  587. return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
  588. }
  589. /**
  590. * Flush the contents of the Cache
  591. */
  592. static async flushCache() {
  593. return fs.emptyDir(path.join(process.cwd(), `data/cache`))
  594. }
  595. /**
  596. * Migrate all pages from a source locale to the target locale
  597. *
  598. * @param {Object} opts Migration properties
  599. * @param {string} opts.sourceLocale Source Locale Code
  600. * @param {string} opts.targetLocale Target Locale Code
  601. * @returns {Promise} Promise with no value
  602. */
  603. static async migrateToLocale({ sourceLocale, targetLocale }) {
  604. return WIKI.models.pages.query()
  605. .patch({
  606. localeCode: targetLocale
  607. })
  608. .where({
  609. localeCode: sourceLocale
  610. })
  611. .whereNotExists(function() {
  612. this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
  613. })
  614. }
  615. /**
  616. * Clean raw HTML from content for use in search engines
  617. *
  618. * @param {string} rawHTML Raw HTML
  619. * @returns {string} Cleaned Content Text
  620. */
  621. static cleanHTML(rawHTML = '') {
  622. let data = striptags(rawHTML || '')
  623. .replace(emojiRegex(), '')
  624. // .replace(htmlEntitiesRegex, '')
  625. return he.decode(data)
  626. .replace(punctuationRegex, ' ')
  627. .replace(/(\r\n|\n|\r)/gm, ' ')
  628. .replace(/\s\s+/g, ' ')
  629. .split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
  630. }
  631. }