pages.js 33 KB

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