pages.js 35 KB

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