pages.js 33 KB

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