| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455 | 
							- const Model = require('objection').Model
 
- const _ = require('lodash')
 
- const JSBinType = require('js-binary').Type
 
- const pageHelper = require('../helpers/page')
 
- const path = require('path')
 
- const fs = require('fs-extra')
 
- const yaml = require('js-yaml')
 
- const striptags = require('striptags')
 
- const emojiRegex = require('emoji-regex')
 
- /* global WIKI */
 
- const frontmatterRegex = {
 
-   html: /^(<!-{2}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{2}>)?(?:\n|\r)*([\w\W]*)*/,
 
-   legacy: /^(<!-- TITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)?(<!-- SUBTITLE: ?([\w\W]+?) -{2}>)?(?:\n|\r)*([\w\W]*)*/i,
 
-   markdown: /^(-{3}(?:\n|\r)([\w\W]+?)(?:\n|\r)-{3})?(?:\n|\r)*([\w\W]*)*/
 
- }
 
- const punctuationRegex = /[!,:;/\\_+\-=()&#@<>$~%^*[\]{}"'|]+|(\.\s)|(\s\.)/ig
 
- const htmlEntitiesRegex = /(&#[0-9]{3};)|(&#x[a-zA-Z0-9]{2};)/ig
 
- /**
 
-  * Pages model
 
-  */
 
- module.exports = class Page extends Model {
 
-   static get tableName() { return 'pages' }
 
-   static get jsonSchema () {
 
-     return {
 
-       type: 'object',
 
-       required: ['path', 'title'],
 
-       properties: {
 
-         id: {type: 'integer'},
 
-         path: {type: 'string'},
 
-         hash: {type: 'string'},
 
-         title: {type: 'string'},
 
-         description: {type: 'string'},
 
-         isPublished: {type: 'boolean'},
 
-         privateNS: {type: 'string'},
 
-         publishStartDate: {type: 'string'},
 
-         publishEndDate: {type: 'string'},
 
-         content: {type: 'string'},
 
-         contentType: {type: 'string'},
 
-         createdAt: {type: 'string'},
 
-         updatedAt: {type: 'string'}
 
-       }
 
-     }
 
-   }
 
-   static get relationMappings() {
 
-     return {
 
-       tags: {
 
-         relation: Model.ManyToManyRelation,
 
-         modelClass: require('./tags'),
 
-         join: {
 
-           from: 'pages.id',
 
-           through: {
 
-             from: 'pageTags.pageId',
 
-             to: 'pageTags.tagId'
 
-           },
 
-           to: 'tags.id'
 
-         }
 
-       },
 
-       author: {
 
-         relation: Model.BelongsToOneRelation,
 
-         modelClass: require('./users'),
 
-         join: {
 
-           from: 'pages.authorId',
 
-           to: 'users.id'
 
-         }
 
-       },
 
-       creator: {
 
-         relation: Model.BelongsToOneRelation,
 
-         modelClass: require('./users'),
 
-         join: {
 
-           from: 'pages.creatorId',
 
-           to: 'users.id'
 
-         }
 
-       },
 
-       editor: {
 
-         relation: Model.BelongsToOneRelation,
 
-         modelClass: require('./editors'),
 
-         join: {
 
-           from: 'pages.editorKey',
 
-           to: 'editors.key'
 
-         }
 
-       },
 
-       locale: {
 
-         relation: Model.BelongsToOneRelation,
 
-         modelClass: require('./locales'),
 
-         join: {
 
-           from: 'pages.localeCode',
 
-           to: 'locales.code'
 
-         }
 
-       }
 
-     }
 
-   }
 
-   $beforeUpdate() {
 
-     this.updatedAt = new Date().toISOString()
 
-   }
 
-   $beforeInsert() {
 
-     this.createdAt = new Date().toISOString()
 
-     this.updatedAt = new Date().toISOString()
 
-   }
 
-   static get cacheSchema() {
 
-     return new JSBinType({
 
-       id: 'uint',
 
-       authorId: 'uint',
 
-       authorName: 'string',
 
-       createdAt: 'string',
 
-       creatorId: 'uint',
 
-       creatorName: 'string',
 
-       description: 'string',
 
-       isPrivate: 'boolean',
 
-       isPublished: 'boolean',
 
-       publishEndDate: 'string',
 
-       publishStartDate: 'string',
 
-       render: 'string',
 
-       title: 'string',
 
-       toc: 'string',
 
-       updatedAt: 'string'
 
-     })
 
-   }
 
-   /**
 
-    * Inject page metadata into contents
 
-    */
 
-   injectMetadata () {
 
-     return pageHelper.injectPageMetadata(this)
 
-   }
 
-   /**
 
-    * Parse injected page metadata from raw content
 
-    *
 
-    * @param {String} raw Raw file contents
 
-    * @param {String} contentType Content Type
 
-    */
 
-   static parseMetadata (raw, contentType) {
 
-     let result
 
-     switch (contentType) {
 
-       case 'markdown':
 
-         result = frontmatterRegex.markdown.exec(raw)
 
-         if (result[2]) {
 
-           return {
 
-             ...yaml.safeLoad(result[2]),
 
-             content: result[3]
 
-           }
 
-         } else {
 
-           // Attempt legacy v1 format
 
-           result = frontmatterRegex.legacy.exec(raw)
 
-           if (result[2]) {
 
-             return {
 
-               title: result[2],
 
-               description: result[4],
 
-               content: result[5]
 
-             }
 
-           }
 
-         }
 
-         break
 
-       case 'html':
 
-         result = frontmatterRegex.html.exec(raw)
 
-         if (result[2]) {
 
-           return {
 
-             ...yaml.safeLoad(result[2]),
 
-             content: result[3]
 
-           }
 
-         }
 
-         break
 
-     }
 
-     return {
 
-       content: raw
 
-     }
 
-   }
 
-   static async createPage(opts) {
 
-     const dupCheck = await WIKI.models.pages.query().select('id').where('localeCode', opts.locale).where('path', opts.path).first()
 
-     if (dupCheck) {
 
-       throw new WIKI.Error.PageDuplicateCreate()
 
-     }
 
-     if (!opts.content || _.trim(opts.content).length < 1) {
 
-       throw new WIKI.Error.PageEmptyContent()
 
-     }
 
-     await WIKI.models.pages.query().insert({
 
-       authorId: opts.authorId,
 
-       content: opts.content,
 
-       creatorId: opts.authorId,
 
-       contentType: _.get(_.find(WIKI.data.editors, ['key', opts.editor]), `contentType`, 'text'),
 
-       description: opts.description,
 
-       editorKey: opts.editor,
 
-       hash: pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' }),
 
-       isPrivate: opts.isPrivate,
 
-       isPublished: opts.isPublished,
 
-       localeCode: opts.locale,
 
-       path: opts.path,
 
-       publishEndDate: opts.publishEndDate || '',
 
-       publishStartDate: opts.publishStartDate || '',
 
-       title: opts.title,
 
-       toc: '[]'
 
-     })
 
-     const page = await WIKI.models.pages.getPageFromDb({
 
-       path: opts.path,
 
-       locale: opts.locale,
 
-       userId: opts.authorId,
 
-       isPrivate: opts.isPrivate
 
-     })
 
-     // -> Render page to HTML
 
-     await WIKI.models.pages.renderPage(page)
 
-     // -> Add to Search Index
 
-     const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
 
-     page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
 
-     await WIKI.data.searchEngine.created(page)
 
-     // -> Add to Storage
 
-     if (!opts.skipStorage) {
 
-       await WIKI.models.storage.pageEvent({
 
-         event: 'created',
 
-         page
 
-       })
 
-     }
 
-     return page
 
-   }
 
-   static async updatePage(opts) {
 
-     const ogPage = await WIKI.models.pages.query().findById(opts.id)
 
-     if (!ogPage) {
 
-       throw new Error('Invalid Page Id')
 
-     }
 
-     if (!opts.content || _.trim(opts.content).length < 1) {
 
-       throw new WIKI.Error.PageEmptyContent()
 
-     }
 
-     await WIKI.models.pageHistory.addVersion({
 
-       ...ogPage,
 
-       isPublished: ogPage.isPublished === true || ogPage.isPublished === 1,
 
-       action: 'updated'
 
-     })
 
-     await WIKI.models.pages.query().patch({
 
-       authorId: opts.authorId,
 
-       content: opts.content,
 
-       description: opts.description,
 
-       isPublished: opts.isPublished === true || opts.isPublished === 1,
 
-       publishEndDate: opts.publishEndDate || '',
 
-       publishStartDate: opts.publishStartDate || '',
 
-       title: opts.title
 
-     }).where('id', ogPage.id)
 
-     const page = await WIKI.models.pages.getPageFromDb({
 
-       path: ogPage.path,
 
-       locale: ogPage.localeCode,
 
-       userId: ogPage.authorId,
 
-       isPrivate: ogPage.isPrivate
 
-     })
 
-     // -> Render page to HTML
 
-     await WIKI.models.pages.renderPage(page)
 
-     // -> Update Search Index
 
-     const pageContents = await WIKI.models.pages.query().findById(page.id).select('render')
 
-     page.safeContent = WIKI.models.pages.cleanHTML(pageContents.render)
 
-     await WIKI.data.searchEngine.updated(page)
 
-     // -> Update on Storage
 
-     if (!opts.skipStorage) {
 
-       await WIKI.models.storage.pageEvent({
 
-         event: 'updated',
 
-         page
 
-       })
 
-     }
 
-     return page
 
-   }
 
-   static async deletePage(opts) {
 
-     let page
 
-     if (_.has(opts, 'id')) {
 
-       page = await WIKI.models.pages.query().findById(opts.id)
 
-     } else {
 
-       page = await await WIKI.models.pages.query().findOne({
 
-         path: opts.path,
 
-         localeCode: opts.locale
 
-       })
 
-     }
 
-     if (!page) {
 
-       throw new Error('Invalid Page Id')
 
-     }
 
-     await WIKI.models.pageHistory.addVersion({
 
-       ...page,
 
-       action: 'deleted'
 
-     })
 
-     await WIKI.models.pages.query().delete().where('id', page.id)
 
-     await WIKI.models.pages.deletePageFromCache(page)
 
-     // -> Delete from Search Index
 
-     await WIKI.data.searchEngine.deleted(page)
 
-     // -> Delete from Storage
 
-     if (!opts.skipStorage) {
 
-       await WIKI.models.storage.pageEvent({
 
-         event: 'deleted',
 
-         page
 
-       })
 
-     }
 
-   }
 
-   static async renderPage(page) {
 
-     const renderJob = await WIKI.scheduler.registerJob({
 
-       name: 'render-page',
 
-       immediate: true,
 
-       worker: true
 
-     }, page.id)
 
-     return renderJob.finished
 
-   }
 
-   static async getPage(opts) {
 
-     // -> Get from cache first
 
-     let page = await WIKI.models.pages.getPageFromCache(opts)
 
-     if (!page) {
 
-       // -> Get from DB
 
-       page = await WIKI.models.pages.getPageFromDb(opts)
 
-       if (page) {
 
-         if (page.render) {
 
-           // -> Save render to cache
 
-           await WIKI.models.pages.savePageToCache(page)
 
-         } else {
 
-           // -> No render? Possible duplicate issue
 
-           /* TODO: Detect duplicate and delete */
 
-           throw new Error('Error while fetching page. Duplicate entry detected. Reload the page to try again.')
 
-         }
 
-       }
 
-     }
 
-     return page
 
-   }
 
-   static async getPageFromDb(opts) {
 
-     const queryModeID = _.isNumber(opts)
 
-     return WIKI.models.pages.query()
 
-       .column([
 
-         'pages.*',
 
-         {
 
-           authorName: 'author.name',
 
-           authorEmail: 'author.email',
 
-           creatorName: 'creator.name',
 
-           creatorEmail: 'creator.email'
 
-         }
 
-       ])
 
-       .joinRelation('author')
 
-       .joinRelation('creator')
 
-       .where(queryModeID ? {
 
-         'pages.id': opts
 
-       } : {
 
-         'pages.path': opts.path,
 
-         'pages.localeCode': opts.locale
 
-       })
 
-       // .andWhere(builder => {
 
-       //   if (queryModeID) return
 
-       //   builder.where({
 
-       //     'pages.isPublished': true
 
-       //   }).orWhere({
 
-       //     'pages.isPublished': false,
 
-       //     'pages.authorId': opts.userId
 
-       //   })
 
-       // })
 
-       // .andWhere(builder => {
 
-       //   if (queryModeID) return
 
-       //   if (opts.isPrivate) {
 
-       //     builder.where({ 'pages.isPrivate': true, 'pages.privateNS': opts.privateNS })
 
-       //   } else {
 
-       //     builder.where({ 'pages.isPrivate': false })
 
-       //   }
 
-       // })
 
-       .first()
 
-   }
 
-   static async savePageToCache(page) {
 
-     const cachePath = path.join(process.cwd(), `data/cache/${page.hash}.bin`)
 
-     await fs.outputFile(cachePath, WIKI.models.pages.cacheSchema.encode({
 
-       id: page.id,
 
-       authorId: page.authorId,
 
-       authorName: page.authorName,
 
-       createdAt: page.createdAt,
 
-       creatorId: page.creatorId,
 
-       creatorName: page.creatorName,
 
-       description: page.description,
 
-       isPrivate: page.isPrivate === 1 || page.isPrivate === true,
 
-       isPublished: page.isPublished === 1 || page.isPublished === true,
 
-       publishEndDate: page.publishEndDate,
 
-       publishStartDate: page.publishStartDate,
 
-       render: page.render,
 
-       title: page.title,
 
-       toc: _.isString(page.toc) ? page.toc : JSON.stringify(page.toc),
 
-       updatedAt: page.updatedAt
 
-     }))
 
-   }
 
-   static async getPageFromCache(opts) {
 
-     const pageHash = pageHelper.generateHash({ path: opts.path, locale: opts.locale, privateNS: opts.isPrivate ? 'TODO' : '' })
 
-     const cachePath = path.join(process.cwd(), `data/cache/${pageHash}.bin`)
 
-     try {
 
-       const pageBuffer = await fs.readFile(cachePath)
 
-       let page = WIKI.models.pages.cacheSchema.decode(pageBuffer)
 
-       return {
 
-         ...page,
 
-         path: opts.path,
 
-         localeCode: opts.locale,
 
-         isPrivate: opts.isPrivate
 
-       }
 
-     } catch (err) {
 
-       if (err.code === 'ENOENT') {
 
-         return false
 
-       }
 
-       WIKI.logger.error(err)
 
-       throw err
 
-     }
 
-   }
 
-   static async deletePageFromCache(page) {
 
-     return fs.remove(path.join(process.cwd(), `data/cache/${page.hash}.bin`))
 
-   }
 
-   static async flushCache() {
 
-     return fs.emptyDir(path.join(process.cwd(), `data/cache`))
 
-   }
 
-   static async migrateToLocale({ sourceLocale, targetLocale }) {
 
-     return WIKI.models.pages.query()
 
-       .patch({
 
-         localeCode: targetLocale
 
-       })
 
-       .where({
 
-         localeCode: sourceLocale
 
-       })
 
-       .whereNotExists(function() {
 
-         this.select('id').from('pages AS pagesm').where('pagesm.localeCode', targetLocale).andWhereRaw('pagesm.path = pages.path')
 
-       })
 
-   }
 
-   static cleanHTML(rawHTML = '') {
 
-     return striptags(rawHTML || '')
 
-       .replace(emojiRegex(), '')
 
-       .replace(htmlEntitiesRegex, '')
 
-       .replace(punctuationRegex, ' ')
 
-       .replace(/(\r\n|\n|\r)/gm, ' ')
 
-       .replace(/\s\s+/g, ' ')
 
-       .split(' ').filter(w => w.length > 1).join(' ').toLowerCase()
 
-   }
 
- }
 
 
  |