|
|
@@ -192,7 +192,7 @@ class PageService {
|
|
|
.cursor({ batchSize: BULK_REINDEX_SIZE });
|
|
|
}
|
|
|
|
|
|
- // TODO: implement recursive rename
|
|
|
+ // TODO: rewrite recursive rename
|
|
|
async renamePage(page, newPagePath, user, options) {
|
|
|
// v4 compatible process
|
|
|
const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
|
|
|
@@ -204,7 +204,6 @@ class PageService {
|
|
|
const {
|
|
|
path, grant, grantedUsers: grantedUserIds, grantedGroup: grantUserGroupId,
|
|
|
} = page;
|
|
|
- const createRedirectPage = options.createRedirectPage || false;
|
|
|
const updateMetadata = options.updateMetadata || false;
|
|
|
|
|
|
// sanitize path
|
|
|
@@ -233,14 +232,9 @@ class PageService {
|
|
|
await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
|
|
|
/*
|
|
|
- * replace target
|
|
|
+ * TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ * bulkWrite PageRedirectDocument if createRedirectPage is true
|
|
|
*/
|
|
|
- let pageToReplaceWith = null;
|
|
|
- if (createRedirectPage) {
|
|
|
- const body = `redirect ${newPagePath}`;
|
|
|
- pageToReplaceWith = await Page.create(path, body, user, { redirectTo: newPagePath });
|
|
|
- }
|
|
|
- await Page.replaceTargetWithPage(page, pageToReplaceWith);
|
|
|
|
|
|
/*
|
|
|
* update target
|
|
|
@@ -248,7 +242,6 @@ class PageService {
|
|
|
const update: Partial<IPage> = {};
|
|
|
// find or create parent
|
|
|
const newParent = await Page.getParentAndFillAncestors(newPagePath);
|
|
|
-
|
|
|
// update Page
|
|
|
update.path = newPagePath;
|
|
|
update.parent = newParent._id;
|
|
|
@@ -263,20 +256,19 @@ class PageService {
|
|
|
return renamedPage;
|
|
|
}
|
|
|
|
|
|
- private async renamePageV4(page, newPagePath, user, options, isRecursively = false) {
|
|
|
+ // !!renaming always include descendant pages!!
|
|
|
+ private async renamePageV4(page, newPagePath, user, options) {
|
|
|
const Page = this.crowi.model('Page');
|
|
|
const Revision = this.crowi.model('Revision');
|
|
|
const path = page.path;
|
|
|
- const createRedirectPage = options.createRedirectPage || false;
|
|
|
const updateMetadata = options.updateMetadata || false;
|
|
|
|
|
|
// sanitize path
|
|
|
newPagePath = this.crowi.xss.process(newPagePath); // eslint-disable-line no-param-reassign
|
|
|
|
|
|
// create descendants first
|
|
|
- if (isRecursively) {
|
|
|
- await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
- }
|
|
|
+ await this.renameDescendantsWithStream(page, newPagePath, user, options);
|
|
|
+
|
|
|
|
|
|
const update: any = {};
|
|
|
// update Page
|
|
|
@@ -290,10 +282,10 @@ class PageService {
|
|
|
// update Rivisions
|
|
|
await Revision.updateRevisionListByPath(path, { path: newPagePath }, {});
|
|
|
|
|
|
- if (createRedirectPage) {
|
|
|
- const body = `redirect ${newPagePath}`;
|
|
|
- await Page.create(path, body, user, { redirectTo: newPagePath });
|
|
|
- }
|
|
|
+ /*
|
|
|
+ * TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ * bulkWrite PageRedirectDocument if createRedirectPage is true
|
|
|
+ */
|
|
|
|
|
|
this.pageEvent.emit('rename', page, user);
|
|
|
|
|
|
@@ -302,20 +294,13 @@ class PageService {
|
|
|
|
|
|
|
|
|
private async renameDescendants(pages, user, options, oldPagePathPrefix, newPagePathPrefix) {
|
|
|
- const Page = this.crowi.model('Page');
|
|
|
-
|
|
|
const pageCollection = mongoose.connection.collection('pages');
|
|
|
- const revisionCollection = mongoose.connection.collection('revisions');
|
|
|
- const { updateMetadata, createRedirectPage } = options;
|
|
|
+ const { updateMetadata } = options;
|
|
|
|
|
|
const unorderedBulkOp = pageCollection.initializeUnorderedBulkOp();
|
|
|
- const createRediectPageBulkOp = pageCollection.initializeUnorderedBulkOp();
|
|
|
- const revisionUnorderedBulkOp = revisionCollection.initializeUnorderedBulkOp();
|
|
|
- const createRediectRevisionBulkOp = revisionCollection.initializeUnorderedBulkOp();
|
|
|
|
|
|
pages.forEach((page) => {
|
|
|
const newPagePath = page.path.replace(oldPagePathPrefix, newPagePathPrefix);
|
|
|
- const revisionId = new mongoose.Types.ObjectId();
|
|
|
|
|
|
if (updateMetadata) {
|
|
|
unorderedBulkOp
|
|
|
@@ -325,25 +310,10 @@ class PageService {
|
|
|
else {
|
|
|
unorderedBulkOp.find({ _id: page._id }).update({ $set: { path: newPagePath } });
|
|
|
}
|
|
|
- if (createRedirectPage) {
|
|
|
- createRediectPageBulkOp.insert({
|
|
|
- path: page.path, revision: revisionId, creator: user._id, lastUpdateUser: user._id, status: Page.STATUS_PUBLISHED, redirectTo: newPagePath,
|
|
|
- });
|
|
|
- createRediectRevisionBulkOp.insert({
|
|
|
- _id: revisionId, path: page.path, body: `redirect ${newPagePath}`, author: user._id, format: 'markdown',
|
|
|
- });
|
|
|
- }
|
|
|
- revisionUnorderedBulkOp.find({ path: page.path }).update({ $set: { path: newPagePath } });
|
|
|
});
|
|
|
|
|
|
try {
|
|
|
await unorderedBulkOp.execute();
|
|
|
- await revisionUnorderedBulkOp.execute();
|
|
|
- // Execute after unorderedBulkOp to prevent duplication
|
|
|
- if (createRedirectPage) {
|
|
|
- await createRediectPageBulkOp.execute();
|
|
|
- await createRediectRevisionBulkOp.execute();
|
|
|
- }
|
|
|
}
|
|
|
catch (err) {
|
|
|
if (err.code !== 11000) {
|