pages.js 34 KB

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