2014-08-17 10:17:23 +04:00
|
|
|
var Promise = require('bluebird'),
|
2014-08-09 23:16:54 +04:00
|
|
|
_ = require('lodash'),
|
|
|
|
models = require('../../models'),
|
|
|
|
errors = require('../../errors'),
|
|
|
|
globalUtils = require('../../utils'),
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n = require('../../i18n'),
|
2014-07-31 23:53:55 +04:00
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
internal = {context: {internal: true}},
|
2014-07-31 23:53:55 +04:00
|
|
|
utils,
|
|
|
|
areEmpty,
|
|
|
|
updatedSettingKeys,
|
|
|
|
stripProperties;
|
|
|
|
|
|
|
|
updatedSettingKeys = {
|
|
|
|
activePlugins: 'activeApps',
|
|
|
|
installedPlugins: 'installedApps'
|
|
|
|
};
|
|
|
|
|
|
|
|
areEmpty = function (object) {
|
|
|
|
var fields = _.toArray(arguments).slice(1),
|
2016-06-11 21:23:27 +03:00
|
|
|
areEmpty = _.every(fields, function (field) {
|
2014-07-31 23:53:55 +04:00
|
|
|
return _.isEmpty(object[field]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return areEmpty;
|
|
|
|
};
|
|
|
|
|
|
|
|
stripProperties = function stripProperties(properties, data) {
|
2016-06-11 21:23:27 +03:00
|
|
|
data = _.cloneDeep(data);
|
2014-07-31 23:53:55 +04:00
|
|
|
_.each(data, function (obj) {
|
|
|
|
_.each(properties, function (property) {
|
|
|
|
delete obj[property];
|
|
|
|
});
|
|
|
|
});
|
|
|
|
return data;
|
|
|
|
};
|
|
|
|
|
|
|
|
utils = {
|
2015-04-18 00:27:04 +03:00
|
|
|
internal: internal,
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
processUsers: function preProcessUsers(tableData, owner, existingUsers, objs) {
|
|
|
|
// We need to:
|
|
|
|
// 1. figure out who the owner of the blog is
|
|
|
|
// 2. figure out what users we have
|
|
|
|
// 3. figure out what users the import data refers to in foreign keys
|
|
|
|
// 4. try to map each one to a user
|
|
|
|
var userKeys = ['created_by', 'updated_by', 'published_by', 'author_id'],
|
|
|
|
userMap = {};
|
|
|
|
|
|
|
|
// Search the passed in objects for any user foreign keys
|
|
|
|
_.each(objs, function (obj) {
|
|
|
|
if (tableData[obj]) {
|
|
|
|
// For each object in the tableData that matches
|
|
|
|
_.each(tableData[obj], function (data) {
|
|
|
|
// For each possible user foreign key
|
|
|
|
_.each(userKeys, function (key) {
|
|
|
|
if (_.has(data, key) && data[key] !== null) {
|
|
|
|
userMap[data[key]] = {};
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// We now have a list of users we need to figure out what their email addresses are
|
|
|
|
_.each(_.keys(userMap), function (userToMap) {
|
|
|
|
userToMap = parseInt(userToMap, 10);
|
|
|
|
var foundUser = _.find(tableData.users, function (tableDataUser) {
|
|
|
|
return tableDataUser.id === userToMap;
|
|
|
|
});
|
|
|
|
|
|
|
|
// we now know that userToMap's email is foundUser.email - look them up in existing users
|
|
|
|
if (foundUser && _.has(foundUser, 'email') && _.has(existingUsers, foundUser.email)) {
|
|
|
|
existingUsers[foundUser.email].importId = userToMap;
|
|
|
|
userMap[userToMap] = existingUsers[foundUser.email].realId;
|
|
|
|
} else if (userToMap === 1) {
|
|
|
|
// if we don't have user data and the id is 1, we assume this means the owner
|
|
|
|
existingUsers[owner.email].importId = userToMap;
|
|
|
|
userMap[userToMap] = existingUsers[owner.email].realId;
|
2016-05-16 21:47:44 +03:00
|
|
|
} else if (userToMap === 0) {
|
|
|
|
// CASE: external context
|
|
|
|
userMap[userToMap] = '0';
|
2014-08-09 23:16:54 +04:00
|
|
|
} else {
|
|
|
|
throw new errors.DataImportError(
|
2015-11-12 15:29:45 +03:00
|
|
|
i18n.t('errors.data.import.utils.dataLinkedToUnknownUser', {userToMap: userToMap}), 'user.id', userToMap
|
2014-08-09 23:16:54 +04:00
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// now replace any user foreign keys
|
|
|
|
_.each(objs, function (obj) {
|
|
|
|
if (tableData[obj]) {
|
|
|
|
// For each object in the tableData that matches
|
|
|
|
_.each(tableData[obj], function (data) {
|
|
|
|
// For each possible user foreign key
|
|
|
|
_.each(userKeys, function (key) {
|
|
|
|
if (_.has(data, key) && data[key] !== null) {
|
|
|
|
data[key] = userMap[data[key]];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return tableData;
|
|
|
|
},
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
preProcessPostTags: function preProcessPostTags(tableData) {
|
|
|
|
var postTags,
|
|
|
|
postsWithTags = {};
|
|
|
|
|
|
|
|
postTags = tableData.posts_tags;
|
2014-09-10 08:06:24 +04:00
|
|
|
_.each(postTags, function (postTag) {
|
|
|
|
if (!postsWithTags.hasOwnProperty(postTag.post_id)) {
|
|
|
|
postsWithTags[postTag.post_id] = [];
|
2014-07-31 23:53:55 +04:00
|
|
|
}
|
2014-09-10 08:06:24 +04:00
|
|
|
postsWithTags[postTag.post_id].push(postTag.tag_id);
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
_.each(postsWithTags, function (tagIds, postId) {
|
2014-07-31 23:53:55 +04:00
|
|
|
var post, tags;
|
|
|
|
post = _.find(tableData.posts, function (post) {
|
2014-09-10 08:06:24 +04:00
|
|
|
return post.id === parseInt(postId, 10);
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
|
|
|
if (post) {
|
|
|
|
tags = _.filter(tableData.tags, function (tag) {
|
2014-09-10 08:06:24 +04:00
|
|
|
return _.indexOf(tagIds, tag.id) !== -1;
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
|
|
|
post.tags = [];
|
|
|
|
_.each(tags, function (tag) {
|
|
|
|
// names are unique.. this should get the right tags added
|
|
|
|
// as long as tags are added first;
|
|
|
|
post.tags.push({name: tag.name});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return tableData;
|
|
|
|
},
|
|
|
|
|
2015-05-12 22:40:01 +03:00
|
|
|
preProcessRolesUsers: function preProcessRolesUsers(tableData, owner, roles) {
|
2016-06-11 21:23:27 +03:00
|
|
|
var validRoles = _.map(roles, 'name');
|
2015-05-12 22:40:01 +03:00
|
|
|
if (!tableData.roles || !tableData.roles.length) {
|
|
|
|
tableData.roles = roles;
|
|
|
|
}
|
|
|
|
|
|
|
|
_.each(tableData.roles, function (_role) {
|
|
|
|
var match = false;
|
|
|
|
// Check import data does not contain unknown roles
|
|
|
|
_.each(validRoles, function (validRole) {
|
|
|
|
if (_role.name === validRole) {
|
|
|
|
match = true;
|
|
|
|
_role.oldId = _role.id;
|
|
|
|
_role.id = _.find(roles, {name: validRole}).id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
// If unknown role is found then remove role to force down to Author
|
|
|
|
if (!match) {
|
|
|
|
_role.oldId = _role.id;
|
|
|
|
_role.id = _.find(roles, {name: 'Author'}).id;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2014-09-10 08:06:24 +04:00
|
|
|
_.each(tableData.roles_users, function (roleUser) {
|
2014-07-31 23:53:55 +04:00
|
|
|
var user = _.find(tableData.users, function (user) {
|
2014-09-10 08:06:24 +04:00
|
|
|
return user.id === parseInt(roleUser.user_id, 10);
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
2015-05-12 22:40:01 +03:00
|
|
|
|
|
|
|
// Map role_id to updated roles id
|
|
|
|
roleUser.role_id = _.find(tableData.roles, {oldId: roleUser.role_id}).id;
|
|
|
|
|
|
|
|
// Check for owner users that do not match current owner and change role to administrator
|
|
|
|
if (roleUser.role_id === owner.roles[0].id && user && user.email && user.email !== owner.email) {
|
|
|
|
roleUser.role_id = _.find(roles, {name: 'Administrator'}).id;
|
|
|
|
user.roles = [roleUser.role_id];
|
|
|
|
}
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
// just the one role for now
|
|
|
|
if (user && !user.roles) {
|
2014-09-10 08:06:24 +04:00
|
|
|
user.roles = [roleUser.role_id];
|
2014-07-31 23:53:55 +04:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return tableData;
|
|
|
|
},
|
|
|
|
|
2014-08-27 00:41:11 +04:00
|
|
|
importTags: function importTags(tableData, transaction) {
|
|
|
|
if (!tableData) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
var ops = [];
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
tableData = stripProperties(['id'], tableData);
|
|
|
|
_.each(tableData, function (tag) {
|
2015-05-01 00:14:19 +03:00
|
|
|
// Validate minimum tag fields
|
2014-07-31 23:53:55 +04:00
|
|
|
if (areEmpty(tag, 'name', 'slug')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
ops.push(models.Tag.findOne({name: tag.name}, {transacting: transaction}).then(function (_tag) {
|
|
|
|
if (!_tag) {
|
2015-04-18 00:27:04 +03:00
|
|
|
return models.Tag.add(tag, _.extend({}, internal, {transacting: transaction}))
|
2014-07-31 23:53:55 +04:00
|
|
|
.catch(function (error) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject({raw: error, model: 'tag', data: tag});
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return _tag;
|
2016-01-24 22:30:32 +03:00
|
|
|
}).reflect());
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
2014-08-27 00:41:11 +04:00
|
|
|
|
2016-01-24 22:30:32 +03:00
|
|
|
return Promise.all(ops);
|
2014-07-31 23:53:55 +04:00
|
|
|
},
|
|
|
|
|
2014-08-27 00:41:11 +04:00
|
|
|
importPosts: function importPosts(tableData, transaction) {
|
|
|
|
if (!tableData) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
var ops = [];
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
tableData = stripProperties(['id'], tableData);
|
|
|
|
_.each(tableData, function (post) {
|
2015-05-01 00:14:19 +03:00
|
|
|
// Validate minimum post fields
|
2014-07-31 23:53:55 +04:00
|
|
|
if (areEmpty(post, 'title', 'slug', 'markdown')) {
|
|
|
|
return;
|
|
|
|
}
|
2014-08-27 00:41:11 +04:00
|
|
|
|
2015-05-01 00:14:19 +03:00
|
|
|
// The post importer has auto-timestamping disabled
|
2014-12-11 18:50:10 +03:00
|
|
|
if (!post.created_at) {
|
|
|
|
post.created_at = Date.now();
|
|
|
|
}
|
|
|
|
|
2015-04-18 00:27:04 +03:00
|
|
|
ops.push(models.Post.add(post, _.extend({}, internal, {transacting: transaction, importing: true}))
|
2014-08-27 00:41:11 +04:00
|
|
|
.catch(function (error) {
|
|
|
|
return Promise.reject({raw: error, model: 'post', data: post});
|
2016-01-24 22:30:32 +03:00
|
|
|
}).reflect()
|
2014-12-11 18:50:10 +03:00
|
|
|
);
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
2014-08-27 00:41:11 +04:00
|
|
|
|
2016-01-24 22:30:32 +03:00
|
|
|
return Promise.all(ops);
|
2014-07-31 23:53:55 +04:00
|
|
|
},
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
importUsers: function importUsers(tableData, existingUsers, transaction) {
|
|
|
|
var ops = [];
|
2014-07-31 23:53:55 +04:00
|
|
|
tableData = stripProperties(['id'], tableData);
|
|
|
|
_.each(tableData, function (user) {
|
2015-05-01 00:14:19 +03:00
|
|
|
// Validate minimum user fields
|
2014-07-31 23:53:55 +04:00
|
|
|
if (areEmpty(user, 'name', 'slug', 'email')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
if (_.has(existingUsers, user.email)) {
|
|
|
|
// User is already present, ignore
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set password to a random password, and lock the account
|
|
|
|
user.password = globalUtils.uid(50);
|
|
|
|
user.status = 'locked';
|
|
|
|
|
2015-04-18 00:27:04 +03:00
|
|
|
ops.push(models.User.add(user, _.extend({}, internal, {transacting: transaction}))
|
2014-07-31 23:53:55 +04:00
|
|
|
.catch(function (error) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject({raw: error, model: 'user', data: user});
|
2014-07-31 23:53:55 +04:00
|
|
|
}));
|
|
|
|
});
|
|
|
|
|
2014-08-09 23:16:54 +04:00
|
|
|
return ops;
|
2014-07-31 23:53:55 +04:00
|
|
|
},
|
|
|
|
|
2014-08-27 00:41:11 +04:00
|
|
|
importSettings: function importSettings(tableData, transaction) {
|
|
|
|
if (!tableData) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
// for settings we need to update individual settings, and insert any missing ones
|
|
|
|
// settings we MUST NOT update are 'core' and 'theme' settings
|
|
|
|
// as all of these will cause side effects which don't make sense for an import
|
2014-08-27 00:41:11 +04:00
|
|
|
var blackList = ['core', 'theme'],
|
|
|
|
ops = [];
|
2014-07-31 23:53:55 +04:00
|
|
|
|
|
|
|
tableData = stripProperties(['id'], tableData);
|
|
|
|
tableData = _.filter(tableData, function (data) {
|
|
|
|
return blackList.indexOf(data.type) === -1;
|
|
|
|
});
|
|
|
|
|
|
|
|
// Clean up legacy plugin setting references
|
|
|
|
_.each(tableData, function (datum) {
|
|
|
|
datum.key = updatedSettingKeys[datum.key] || datum.key;
|
|
|
|
});
|
|
|
|
|
2015-04-18 00:27:04 +03:00
|
|
|
ops.push(models.Settings.edit(tableData, _.extend({}, internal, {transacting: transaction})).catch(function (error) {
|
2014-09-18 16:48:18 +04:00
|
|
|
// Ignore NotFound errors
|
|
|
|
if (!(error instanceof errors.NotFoundError)) {
|
|
|
|
return Promise.reject({raw: error, model: 'setting', data: tableData});
|
|
|
|
}
|
2016-01-24 22:30:32 +03:00
|
|
|
}).reflect());
|
2014-08-27 00:41:11 +04:00
|
|
|
|
2016-01-24 22:30:32 +03:00
|
|
|
return Promise.all(ops);
|
2014-07-31 23:53:55 +04:00
|
|
|
},
|
|
|
|
|
|
|
|
/** For later **/
|
2014-08-27 00:41:11 +04:00
|
|
|
importApps: function importApps(tableData, transaction) {
|
|
|
|
if (!tableData) {
|
|
|
|
return Promise.resolve();
|
|
|
|
}
|
|
|
|
|
|
|
|
var ops = [];
|
|
|
|
|
2014-07-31 23:53:55 +04:00
|
|
|
tableData = stripProperties(['id'], tableData);
|
|
|
|
_.each(tableData, function (app) {
|
|
|
|
// Avoid duplicates
|
|
|
|
ops.push(models.App.findOne({name: app.name}, {transacting: transaction}).then(function (_app) {
|
|
|
|
if (!_app) {
|
2015-04-18 00:27:04 +03:00
|
|
|
return models.App.add(app, _.extend({}, internal, {transacting: transaction}))
|
2014-07-31 23:53:55 +04:00
|
|
|
.catch(function (error) {
|
2014-08-17 10:17:23 +04:00
|
|
|
return Promise.reject({raw: error, model: 'app', data: app});
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
|
|
|
}
|
2014-08-17 10:17:23 +04:00
|
|
|
|
|
|
|
return _app;
|
2016-01-24 22:30:32 +03:00
|
|
|
}).reflect());
|
2014-07-31 23:53:55 +04:00
|
|
|
});
|
2014-08-27 00:41:11 +04:00
|
|
|
|
2016-01-24 22:30:32 +03:00
|
|
|
return Promise.all(ops);
|
2014-07-31 23:53:55 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-08-17 10:17:23 +04:00
|
|
|
module.exports = utils;
|