2016-10-06 15:27:35 +03:00
|
|
|
var _ = require('lodash'),
|
|
|
|
Promise = require('bluebird'),
|
|
|
|
db = require('../../data/db'),
|
|
|
|
commands = require('../schema').commands,
|
2017-12-15 00:14:55 +03:00
|
|
|
ghostVersion = require('../../lib/ghost-version'),
|
2017-12-12 00:47:46 +03:00
|
|
|
common = require('../../lib/common'),
|
2017-12-14 15:26:48 +03:00
|
|
|
security = require('../../lib/security'),
|
2017-12-11 21:14:05 +03:00
|
|
|
models = require('../../models'),
|
2018-10-17 14:29:24 +03:00
|
|
|
EXCLUDED_TABLES = ['accesstokens', 'refreshtokens', 'clients', 'client_trusted_domains', 'sessions'],
|
2018-08-06 18:18:59 +03:00
|
|
|
EXCLUDED_FIELDS_CONDITIONS = {
|
|
|
|
settings: [{
|
|
|
|
operator: 'whereNot',
|
|
|
|
key: 'key',
|
|
|
|
value: 'permalinks'
|
|
|
|
}]
|
|
|
|
},
|
2016-03-12 21:54:06 +03:00
|
|
|
modelOptions = {context: {internal: true}},
|
|
|
|
|
|
|
|
// private
|
|
|
|
getVersionAndTables,
|
|
|
|
exportTable,
|
|
|
|
|
|
|
|
// public
|
|
|
|
doExport,
|
2014-07-19 16:20:16 +04:00
|
|
|
exportFileName;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
exportFileName = function exportFileName(options) {
|
2019-03-13 23:06:05 +03:00
|
|
|
var datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss'),
|
2014-07-19 16:20:16 +04:00
|
|
|
title = '';
|
|
|
|
|
2018-01-11 18:03:21 +03:00
|
|
|
options = options || {};
|
|
|
|
|
|
|
|
// custom filename
|
|
|
|
if (options.filename) {
|
|
|
|
return Promise.resolve(options.filename + '.json');
|
|
|
|
}
|
|
|
|
|
2018-08-10 16:31:54 +03:00
|
|
|
return models.Settings.findOne({key: 'title'}, _.merge({}, modelOptions, _.pick(options, 'transacting'))).then(function (result) {
|
2014-07-19 16:20:16 +04:00
|
|
|
if (result) {
|
2017-12-14 15:26:48 +03:00
|
|
|
title = security.string.safe(result.get('value')) + '.';
|
2014-07-19 16:20:16 +04:00
|
|
|
}
|
2017-03-08 13:26:57 +03:00
|
|
|
|
2014-07-19 16:20:16 +04:00
|
|
|
return title + 'ghost.' + datetime + '.json';
|
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
common.logging.error(new common.errors.GhostError({err: err}));
|
2014-07-19 16:20:16 +04:00
|
|
|
return 'ghost.' + datetime + '.json';
|
|
|
|
});
|
|
|
|
};
|
2013-06-16 00:52:03 +04:00
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
getVersionAndTables = function getVersionAndTables(options) {
|
2016-03-12 21:54:06 +03:00
|
|
|
var props = {
|
2017-01-26 15:12:00 +03:00
|
|
|
version: ghostVersion.full,
|
2017-08-01 16:27:13 +03:00
|
|
|
tables: commands.getTables(options.transacting)
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
return Promise.props(props);
|
|
|
|
};
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
exportTable = function exportTable(tableName, options) {
|
2018-07-30 18:21:52 +03:00
|
|
|
if (EXCLUDED_TABLES.indexOf(tableName) < 0 ||
|
|
|
|
(options.include && _.isArray(options.include) && options.include.indexOf(tableName) !== -1)) {
|
2018-08-06 18:18:59 +03:00
|
|
|
const query = (options.transacting || db.knex)(tableName);
|
|
|
|
|
|
|
|
if (EXCLUDED_FIELDS_CONDITIONS[tableName]) {
|
|
|
|
EXCLUDED_FIELDS_CONDITIONS[tableName].forEach((condition) => {
|
|
|
|
query[condition.operator](condition.key, condition.value);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
return query.select();
|
2016-03-12 21:54:06 +03:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
doExport = function doExport(options) {
|
2018-07-30 18:21:52 +03:00
|
|
|
options = options || {include: []};
|
2017-08-01 16:27:13 +03:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
var tables, version;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return getVersionAndTables(options).then(function exportAllTables(result) {
|
2016-03-12 21:54:06 +03:00
|
|
|
tables = result.tables;
|
|
|
|
version = result.version;
|
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
return Promise.mapSeries(tables, function (tableName) {
|
|
|
|
return exportTable(tableName, options);
|
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
}).then(function formatData(tableData) {
|
|
|
|
var exportData = {
|
|
|
|
meta: {
|
|
|
|
exported_on: new Date().getTime(),
|
|
|
|
version: version
|
|
|
|
},
|
|
|
|
data: {
|
|
|
|
// Filled below
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
_.each(tables, function (name, i) {
|
|
|
|
exportData.data[name] = tableData[i];
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2016-03-12 21:54:06 +03:00
|
|
|
|
|
|
|
return exportData;
|
|
|
|
}).catch(function (err) {
|
2017-12-12 00:47:46 +03:00
|
|
|
return Promise.reject(new common.errors.DataExportError({
|
2016-10-06 15:27:35 +03:00
|
|
|
err: err,
|
2017-12-12 00:47:46 +03:00
|
|
|
context: common.i18n.t('errors.data.export.errorExportingData')
|
2016-10-06 15:27:35 +03:00
|
|
|
}));
|
2013-09-15 20:04:42 +04:00
|
|
|
});
|
2013-06-25 15:43:15 +04:00
|
|
|
};
|
2013-09-15 20:04:42 +04:00
|
|
|
|
2016-03-12 21:54:06 +03:00
|
|
|
module.exports = {
|
|
|
|
doExport: doExport,
|
2018-07-30 18:21:52 +03:00
|
|
|
fileName: exportFileName,
|
|
|
|
EXCLUDED_TABLES: EXCLUDED_TABLES
|
2016-03-12 21:54:06 +03:00
|
|
|
};
|