2017-11-21 04:08:30 +00:00
|
|
|
const _ = require('lodash')
|
2017-11-19 22:08:23 +00:00
|
|
|
const cfgHelper = require('../helpers/config')
|
2018-02-04 05:53:13 +00:00
|
|
|
const Promise = require('bluebird')
|
2019-01-26 23:35:56 +00:00
|
|
|
const fs = require('fs-extra')
|
|
|
|
const path = require('path')
|
2018-02-04 05:53:13 +00:00
|
|
|
|
2018-03-05 20:49:36 +00:00
|
|
|
/* global WIKI */
|
2017-02-13 05:59:52 +00:00
|
|
|
|
|
|
|
module.exports = {
|
2019-01-13 20:37:45 +00:00
|
|
|
updates: {
|
|
|
|
channel: 'BETA',
|
|
|
|
version: WIKI.version,
|
|
|
|
releaseDate: WIKI.releaseDate,
|
|
|
|
minimumVersionRequired: '2.0.0-beta.0',
|
|
|
|
minimumNodeRequired: '10.12.0'
|
|
|
|
},
|
|
|
|
init() {
|
|
|
|
// Listen for updates events
|
|
|
|
WIKI.events.on('updates', (infoRaw) => {
|
|
|
|
try {
|
|
|
|
this.updates = JSON.parse(infoRaw)
|
|
|
|
} catch (err) {
|
|
|
|
WIKI.logger.warn('Failed to parse updates info.')
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2019-01-26 23:35:56 +00:00
|
|
|
// Clear content cache
|
|
|
|
fs.emptyDir(path.join(WIKI.ROOTPATH, 'data/cache'))
|
|
|
|
|
2019-01-13 20:37:45 +00:00
|
|
|
return this
|
|
|
|
},
|
2017-02-14 04:30:42 +00:00
|
|
|
/**
|
2018-03-05 20:49:36 +00:00
|
|
|
* Upgrade from WIKI.js 1.x - MongoDB database
|
2017-02-14 04:30:42 +00:00
|
|
|
*
|
2017-11-19 22:08:23 +00:00
|
|
|
* @param {Object} opts Options object
|
2017-02-14 04:30:42 +00:00
|
|
|
*/
|
2017-11-19 22:08:23 +00:00
|
|
|
async upgradeFromMongo (opts) {
|
2018-03-05 20:49:36 +00:00
|
|
|
WIKI.logger.info('Upgrading from MongoDB...')
|
2017-12-17 04:41:16 +00:00
|
|
|
|
2017-11-19 22:08:23 +00:00
|
|
|
let mongo = require('mongodb').MongoClient
|
|
|
|
let parsedMongoConStr = cfgHelper.parseConfigValue(opts.mongoCnStr)
|
2017-02-13 05:59:52 +00:00
|
|
|
|
2017-11-19 22:08:23 +00:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
// Connect to MongoDB
|
|
|
|
|
2017-11-21 04:08:30 +00:00
|
|
|
mongo.connect(parsedMongoConStr, {
|
2017-11-19 22:08:23 +00:00
|
|
|
autoReconnect: false,
|
|
|
|
reconnectTries: 2,
|
|
|
|
reconnectInterval: 1000,
|
|
|
|
connectTimeoutMS: 5000,
|
|
|
|
socketTimeoutMS: 5000
|
|
|
|
}, async (err, db) => {
|
|
|
|
try {
|
|
|
|
if (err !== null) { throw err }
|
|
|
|
|
|
|
|
let users = db.collection('users')
|
|
|
|
|
|
|
|
// Check if users table is populated
|
|
|
|
let userCount = await users.count()
|
2017-11-21 04:08:30 +00:00
|
|
|
if (userCount < 2) {
|
|
|
|
throw new Error('MongoDB Upgrade: Users table is empty!')
|
2017-02-13 05:59:52 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 04:08:30 +00:00
|
|
|
// Import all users
|
|
|
|
let userData = await users.find({
|
|
|
|
email: {
|
|
|
|
$not: 'guest'
|
|
|
|
}
|
|
|
|
}).toArray()
|
2018-07-30 02:23:33 +00:00
|
|
|
await WIKI.models.User.bulkCreate(_.map(userData, usr => {
|
2017-11-21 04:08:30 +00:00
|
|
|
return {
|
|
|
|
email: usr.email,
|
|
|
|
name: usr.name || 'Imported User',
|
|
|
|
password: usr.password || '',
|
|
|
|
provider: usr.provider || 'local',
|
|
|
|
providerId: usr.providerId || '',
|
|
|
|
role: 'user',
|
|
|
|
createdAt: usr.createdAt
|
|
|
|
}
|
|
|
|
}))
|
2017-02-14 04:30:42 +00:00
|
|
|
|
2017-11-19 22:08:23 +00:00
|
|
|
resolve(true)
|
|
|
|
} catch (err) {
|
|
|
|
reject(err)
|
|
|
|
}
|
2017-11-21 04:08:30 +00:00
|
|
|
db.close()
|
2017-11-19 22:08:23 +00:00
|
|
|
})
|
2017-02-13 05:59:52 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|