pages.js 33 KB

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