pages.js 34 KB

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