mirror of https://github.com/requarks/wiki
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
432 lines
12 KiB
432 lines
12 KiB
8 years ago
|
'use strict'
|
||
8 years ago
|
|
||
7 years ago
|
/* global wiki */
|
||
8 years ago
|
|
||
8 years ago
|
const Promise = require('bluebird')
|
||
|
const path = require('path')
|
||
|
const fs = Promise.promisifyAll(require('fs-extra'))
|
||
|
const _ = require('lodash')
|
||
8 years ago
|
|
||
|
const entryHelper = require('../helpers/entry')
|
||
8 years ago
|
|
||
|
/**
|
||
|
* Entries Model
|
||
|
*/
|
||
|
module.exports = {
|
||
|
|
||
8 years ago
|
_repoPath: 'repo',
|
||
|
_cachePath: 'data/cache',
|
||
|
|
||
|
/**
|
||
|
* Initialize Entries model
|
||
|
*
|
||
|
* @return {Object} Entries model instance
|
||
|
*/
|
||
8 years ago
|
init() {
|
||
8 years ago
|
let self = this
|
||
|
|
||
7 years ago
|
self._repoPath = path.resolve(wiki.ROOTPATH, wiki.config.paths.repo)
|
||
|
self._cachePath = path.resolve(wiki.ROOTPATH, wiki.config.paths.data, 'cache')
|
||
|
wiki.data.repoPath = self._repoPath
|
||
|
wiki.data.cachePath = self._cachePath
|
||
8 years ago
|
|
||
|
return self
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Check if a document already exists
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @return {Promise<Boolean>} True if exists, false otherwise
|
||
|
*/
|
||
8 years ago
|
exists(entryPath) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
|
return self.fetchOriginal(entryPath, {
|
||
|
parseMarkdown: false,
|
||
|
parseMeta: false,
|
||
|
parseTree: false,
|
||
|
includeMarkdown: false,
|
||
|
includeParentInfo: false,
|
||
|
cache: false
|
||
|
}).then(() => {
|
||
|
return true
|
||
|
}).catch((err) => { // eslint-disable-line handle-callback-err
|
||
|
return false
|
||
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Fetch a document from cache, otherwise the original
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @return {Promise<Object>} Page Data
|
||
|
*/
|
||
8 years ago
|
fetch(entryPath) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
8 years ago
|
let cpath = entryHelper.getCachePath(entryPath)
|
||
8 years ago
|
|
||
|
return fs.statAsync(cpath).then((st) => {
|
||
|
return st.isFile()
|
||
|
}).catch((err) => { // eslint-disable-line handle-callback-err
|
||
|
return false
|
||
|
}).then((isCache) => {
|
||
|
if (isCache) {
|
||
|
// Load from cache
|
||
|
|
||
|
return fs.readFileAsync(cpath).then((contents) => {
|
||
|
return JSON.parse(contents)
|
||
|
}).catch((err) => { // eslint-disable-line handle-callback-err
|
||
|
winston.error('Corrupted cache file. Deleting it...')
|
||
|
fs.unlinkSync(cpath)
|
||
|
return false
|
||
|
})
|
||
|
} else {
|
||
|
// Load original
|
||
|
|
||
|
return self.fetchOriginal(entryPath)
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Fetches the original document entry
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @param {Object} options The options
|
||
|
* @return {Promise<Object>} Page data
|
||
|
*/
|
||
8 years ago
|
fetchOriginal(entryPath, options) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
8 years ago
|
let fpath = entryHelper.getFullPath(entryPath)
|
||
|
let cpath = entryHelper.getCachePath(entryPath)
|
||
8 years ago
|
|
||
|
options = _.defaults(options, {
|
||
|
parseMarkdown: true,
|
||
|
parseMeta: true,
|
||
|
parseTree: true,
|
||
|
includeMarkdown: false,
|
||
|
includeParentInfo: true,
|
||
|
cache: true
|
||
|
})
|
||
|
|
||
|
return fs.statAsync(fpath).then((st) => {
|
||
|
if (st.isFile()) {
|
||
|
return fs.readFileAsync(fpath, 'utf8').then((contents) => {
|
||
8 years ago
|
let htmlProcessor = (options.parseMarkdown) ? mark.parseContent(contents) : Promise.resolve('')
|
||
|
|
||
8 years ago
|
// Parse contents
|
||
|
|
||
8 years ago
|
return htmlProcessor.then(html => {
|
||
|
let pageData = {
|
||
|
markdown: (options.includeMarkdown) ? contents : '',
|
||
|
html,
|
||
|
meta: (options.parseMeta) ? mark.parseMeta(contents) : {},
|
||
|
tree: (options.parseTree) ? mark.parseTree(contents) : []
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
if (!pageData.meta.title) {
|
||
|
pageData.meta.title = _.startCase(entryPath)
|
||
|
}
|
||
8 years ago
|
|
||
8 years ago
|
pageData.meta.path = entryPath
|
||
8 years ago
|
|
||
8 years ago
|
// Get parent
|
||
8 years ago
|
|
||
8 years ago
|
let parentPromise = (options.includeParentInfo) ? self.getParentInfo(entryPath).then((parentData) => {
|
||
|
return (pageData.parent = parentData)
|
||
|
}).catch((err) => { // eslint-disable-line handle-callback-err
|
||
|
return (pageData.parent = false)
|
||
|
}) : Promise.resolve(true)
|
||
8 years ago
|
|
||
8 years ago
|
return parentPromise.then(() => {
|
||
|
// Cache to disk
|
||
8 years ago
|
|
||
8 years ago
|
if (options.cache) {
|
||
|
let cacheData = JSON.stringify(_.pick(pageData, ['html', 'meta', 'tree', 'parent']), false, false, false)
|
||
|
return fs.writeFileAsync(cpath, cacheData).catch((err) => {
|
||
|
winston.error('Unable to write to cache! Performance may be affected.')
|
||
|
winston.error(err)
|
||
|
return true
|
||
|
})
|
||
|
} else {
|
||
8 years ago
|
return true
|
||
8 years ago
|
}
|
||
|
}).return(pageData)
|
||
|
})
|
||
8 years ago
|
})
|
||
|
} else {
|
||
|
return false
|
||
|
}
|
||
|
}).catch((err) => { // eslint-disable-line handle-callback-err
|
||
8 years ago
|
throw new Promise.OperationalError(lang.t('errors:notexist', { path: entryPath }))
|
||
8 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Gets the parent information.
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @return {Promise<Object|False>} The parent information.
|
||
|
*/
|
||
8 years ago
|
getParentInfo(entryPath) {
|
||
8 years ago
|
if (_.includes(entryPath, '/')) {
|
||
|
let parentParts = _.initial(_.split(entryPath, '/'))
|
||
|
let parentPath = _.join(parentParts, '/')
|
||
|
let parentFile = _.last(parentParts)
|
||
8 years ago
|
let fpath = entryHelper.getFullPath(parentPath)
|
||
8 years ago
|
|
||
|
return fs.statAsync(fpath).then((st) => {
|
||
|
if (st.isFile()) {
|
||
|
return fs.readFileAsync(fpath, 'utf8').then((contents) => {
|
||
|
let pageMeta = mark.parseMeta(contents)
|
||
|
|
||
|
return {
|
||
|
path: parentPath,
|
||
|
title: (pageMeta.title) ? pageMeta.title : _.startCase(parentFile),
|
||
|
subtitle: (pageMeta.subtitle) ? pageMeta.subtitle : false
|
||
|
}
|
||
|
})
|
||
|
} else {
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:parentinvalid')))
|
||
8 years ago
|
}
|
||
|
})
|
||
|
} else {
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:parentisroot')))
|
||
8 years ago
|
}
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Update an existing document
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @param {String} contents The markdown-formatted contents
|
||
8 years ago
|
* @param {Object} author The author user object
|
||
8 years ago
|
* @return {Promise<Boolean>} True on success, false on failure
|
||
|
*/
|
||
8 years ago
|
update(entryPath, contents, author) {
|
||
8 years ago
|
let self = this
|
||
8 years ago
|
let fpath = entryHelper.getFullPath(entryPath)
|
||
8 years ago
|
|
||
|
return fs.statAsync(fpath).then((st) => {
|
||
|
if (st.isFile()) {
|
||
8 years ago
|
return self.makePersistent(entryPath, contents, author).then(() => {
|
||
8 years ago
|
return self.updateCache(entryPath).then(entry => {
|
||
|
return search.add(entry)
|
||
|
})
|
||
8 years ago
|
})
|
||
|
} else {
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:notexist', { path: entryPath })))
|
||
8 years ago
|
}
|
||
|
}).catch((err) => {
|
||
|
winston.error(err)
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:savefailed')))
|
||
8 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
8 years ago
|
* Update local cache
|
||
8 years ago
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @return {Promise} Promise of the operation
|
||
|
*/
|
||
8 years ago
|
updateCache(entryPath) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
|
return self.fetchOriginal(entryPath, {
|
||
|
parseMarkdown: true,
|
||
|
parseMeta: true,
|
||
|
parseTree: true,
|
||
|
includeMarkdown: true,
|
||
|
includeParentInfo: true,
|
||
|
cache: true
|
||
8 years ago
|
}).catch(err => {
|
||
|
winston.error(err)
|
||
|
return err
|
||
8 years ago
|
}).then((pageData) => {
|
||
|
return {
|
||
|
entryPath,
|
||
|
meta: pageData.meta,
|
||
|
parent: pageData.parent || {},
|
||
|
text: mark.removeMarkdown(pageData.markdown)
|
||
|
}
|
||
8 years ago
|
}).catch(err => {
|
||
|
winston.error(err)
|
||
|
return err
|
||
8 years ago
|
}).then((content) => {
|
||
8 years ago
|
let parentPath = _.chain(content.entryPath).split('/').initial().join('/').value()
|
||
8 years ago
|
return db.Entry.findOneAndUpdate({
|
||
|
_id: content.entryPath
|
||
|
}, {
|
||
8 years ago
|
_id: content.entryPath,
|
||
|
title: content.meta.title || content.entryPath,
|
||
|
subtitle: content.meta.subtitle || '',
|
||
|
parentTitle: content.parent.title || '',
|
||
|
parentPath: parentPath,
|
||
|
isDirectory: false,
|
||
|
isEntry: true
|
||
|
}, {
|
||
|
new: true,
|
||
|
upsert: true
|
||
|
}).then(result => {
|
||
|
let plainResult = result.toObject()
|
||
|
plainResult.text = content.text
|
||
|
return plainResult
|
||
|
})
|
||
8 years ago
|
}).then(result => {
|
||
8 years ago
|
return self.updateTreeInfo().then(() => {
|
||
|
return result
|
||
8 years ago
|
})
|
||
8 years ago
|
}).catch(err => {
|
||
|
winston.error(err)
|
||
|
return err
|
||
8 years ago
|
})
|
||
|
},
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Update tree info for all directory and parent entries
|
||
|
*
|
||
|
* @returns {Promise<Boolean>} Promise of the operation
|
||
|
*/
|
||
8 years ago
|
updateTreeInfo() {
|
||
8 years ago
|
return db.Entry.distinct('parentPath', { parentPath: { $ne: '' } }).then(allPaths => {
|
||
|
if (allPaths.length > 0) {
|
||
|
return Promise.map(allPaths, pathItem => {
|
||
|
let parentPath = _.chain(pathItem).split('/').initial().join('/').value()
|
||
|
let guessedTitle = _.chain(pathItem).split('/').last().startCase().value()
|
||
|
return db.Entry.update({ _id: pathItem }, {
|
||
|
$set: { isDirectory: true },
|
||
|
$setOnInsert: { isEntry: false, title: guessedTitle, parentPath }
|
||
|
}, { upsert: true })
|
||
|
})
|
||
|
} else {
|
||
|
return true
|
||
|
}
|
||
|
})
|
||
|
},
|
||
|
|
||
8 years ago
|
/**
|
||
|
* Create a new document
|
||
|
*
|
||
8 years ago
|
* @param {String} entryPath The entry path
|
||
|
* @param {String} contents The markdown-formatted contents
|
||
8 years ago
|
* @param {Object} author The author user object
|
||
8 years ago
|
* @return {Promise<Boolean>} True on success, false on failure
|
||
8 years ago
|
*/
|
||
8 years ago
|
create(entryPath, contents, author) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
|
return self.exists(entryPath).then((docExists) => {
|
||
|
if (!docExists) {
|
||
8 years ago
|
return self.makePersistent(entryPath, contents, author).then(() => {
|
||
8 years ago
|
return self.updateCache(entryPath).then(entry => {
|
||
|
return search.add(entry)
|
||
|
})
|
||
8 years ago
|
})
|
||
|
} else {
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:alreadyexists')))
|
||
8 years ago
|
}
|
||
|
}).catch((err) => {
|
||
|
winston.error(err)
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:generic')))
|
||
8 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Makes a document persistent to disk and git repository
|
||
|
*
|
||
8 years ago
|
* @param {String} entryPath The entry path
|
||
|
* @param {String} contents The markdown-formatted contents
|
||
8 years ago
|
* @param {Object} author The author user object
|
||
8 years ago
|
* @return {Promise<Boolean>} True on success, false on failure
|
||
8 years ago
|
*/
|
||
8 years ago
|
makePersistent(entryPath, contents, author) {
|
||
8 years ago
|
let fpath = entryHelper.getFullPath(entryPath)
|
||
8 years ago
|
|
||
|
return fs.outputFileAsync(fpath, contents).then(() => {
|
||
8 years ago
|
return git.commitDocument(entryPath, author)
|
||
8 years ago
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Move a document
|
||
|
*
|
||
8 years ago
|
* @param {String} entryPath The current entry path
|
||
|
* @param {String} newEntryPath The new entry path
|
||
8 years ago
|
* @param {Object} author The author user object
|
||
8 years ago
|
* @return {Promise} Promise of the operation
|
||
8 years ago
|
*/
|
||
8 years ago
|
move(entryPath, newEntryPath, author) {
|
||
8 years ago
|
let self = this
|
||
|
|
||
|
if (_.isEmpty(entryPath) || entryPath === 'home') {
|
||
8 years ago
|
return Promise.reject(new Error(lang.t('errors:invalidpath')))
|
||
8 years ago
|
}
|
||
|
|
||
|
return git.moveDocument(entryPath, newEntryPath).then(() => {
|
||
8 years ago
|
return git.commitDocument(newEntryPath, author).then(() => {
|
||
8 years ago
|
// Delete old cache version
|
||
|
|
||
8 years ago
|
let oldEntryCachePath = entryHelper.getCachePath(entryPath)
|
||
8 years ago
|
fs.unlinkAsync(oldEntryCachePath).catch((err) => { return true }) // eslint-disable-line handle-callback-err
|
||
|
|
||
|
// Delete old index entry
|
||
|
|
||
8 years ago
|
search.delete(entryPath)
|
||
8 years ago
|
|
||
|
// Create cache for new entry
|
||
|
|
||
7 years ago
|
return Promise.join(
|
||
|
db.Entry.deleteOne({ _id: entryPath }),
|
||
|
self.updateCache(newEntryPath).then(entry => {
|
||
|
return search.add(entry)
|
||
|
})
|
||
|
)
|
||
8 years ago
|
})
|
||
|
})
|
||
|
},
|
||
|
|
||
|
/**
|
||
|
* Generate a starter page content based on the entry path
|
||
|
*
|
||
|
* @param {String} entryPath The entry path
|
||
|
* @return {Promise<String>} Starter content
|
||
|
*/
|
||
8 years ago
|
getStarter(entryPath) {
|
||
8 years ago
|
let formattedTitle = _.startCase(_.last(_.split(entryPath, '/')))
|
||
|
|
||
8 years ago
|
return fs.readFileAsync(path.join(SERVERPATH, 'app/content/create.md'), 'utf8').then((contents) => {
|
||
8 years ago
|
return _.replace(contents, new RegExp('{TITLE}', 'g'), formattedTitle)
|
||
|
})
|
||
8 years ago
|
},
|
||
|
|
||
|
/**
|
||
|
* Get all entries from base path
|
||
|
*
|
||
|
* @param {String} basePath Path to list from
|
||
8 years ago
|
* @param {Object} usr Current user
|
||
8 years ago
|
* @return {Promise<Array>} List of entries
|
||
|
*/
|
||
8 years ago
|
getFromTree(basePath, usr) {
|
||
8 years ago
|
return db.Entry.find({ parentPath: basePath }, 'title parentPath isDirectory isEntry').sort({ title: 'asc' }).then(results => {
|
||
|
return _.filter(results, r => {
|
||
|
return rights.checkRole('/' + r._id, usr.rights, 'read')
|
||
|
})
|
||
|
})
|
||
8 years ago
|
},
|
||
|
|
||
8 years ago
|
getHistory(entryPath) {
|
||
8 years ago
|
return db.Entry.findOne({ _id: entryPath, isEntry: true }).then(entry => {
|
||
|
if (!entry) { return false }
|
||
|
return git.getHistory(entryPath).then(history => {
|
||
|
return {
|
||
|
meta: entry,
|
||
|
history
|
||
|
}
|
||
|
})
|
||
|
})
|
||
8 years ago
|
}
|
||
|
}
|