2020-04-29 18:44:27 +03:00
|
|
|
const _ = require('lodash');
|
|
|
|
const Promise = require('bluebird');
|
|
|
|
const db = require('../../data/db');
|
|
|
|
const commands = require('../schema').commands;
|
|
|
|
const ghostVersion = require('../../lib/ghost-version');
|
|
|
|
const common = require('../../lib/common');
|
|
|
|
const security = require('../../lib/security');
|
|
|
|
const models = require('../../models');
|
|
|
|
const EXCLUDED_TABLES = ['sessions', 'mobiledoc_revisions'];
|
|
|
|
|
|
|
|
const EXCLUDED_FIELDS_CONDITIONS = {
|
|
|
|
settings: [{
|
|
|
|
operator: 'whereNot',
|
|
|
|
key: 'key',
|
|
|
|
value: 'permalinks'
|
|
|
|
}]
|
|
|
|
};
|
|
|
|
|
|
|
|
const modelOptions = {context: {internal: true}};
|
|
|
|
|
|
|
|
// private
|
|
|
|
let getVersionAndTables;
|
|
|
|
|
|
|
|
let exportTable;
|
|
|
|
|
|
|
|
// public
|
|
|
|
let doExport;
|
|
|
|
|
|
|
|
let exportFileName;
|
2014-07-19 16:20:16 +04:00
|
|
|
|
2017-08-01 16:27:13 +03:00
|
|
|
exportFileName = function exportFileName(options) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const datetime = require('moment')().format('YYYY-MM-DD-HH-mm-ss');
|
|
|
|
let title = '';
|
2014-07-19 16:20:16 +04:00
|
|
|
|
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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const 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
|
|
|
|
2020-04-29 18:44:27 +03:00
|
|
|
let tables;
|
|
|
|
let version;
|
2016-03-12 21:54:06 +03:00
|
|
|
|
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) {
|
2020-04-29 18:44:27 +03:00
|
|
|
const exportData = {
|
2016-03-12 21:54:06 +03:00
|
|
|
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
|
|
|
};
|