|
|
@@ -229,7 +229,8 @@ class PageService {
|
|
|
const isV5Compatible = this.crowi.configManager.getConfig('crowi', 'app:isV5Compatible');
|
|
|
const isRoot = isTopPage(page.path);
|
|
|
const isPageRestricted = page.grant === Page.GRANT_RESTRICTED;
|
|
|
- const shouldUseV4Process = !isRoot && !isPageRestricted && (!isV5Compatible || !isPageMigrated);
|
|
|
+ const isTrashed = isTrashPage(page.path);
|
|
|
+ const shouldUseV4Process = !isRoot && !isPageRestricted && (!isV5Compatible || !isPageMigrated || !isTrashed);
|
|
|
|
|
|
return shouldUseV4Process;
|
|
|
}
|
|
|
@@ -240,29 +241,6 @@ class PageService {
|
|
|
return page.grant !== Page.GRANT_RESTRICTED && page.grant !== Page.GRANT_SPECIFIED;
|
|
|
}
|
|
|
|
|
|
- /**
|
|
|
- * go back by using redirectTo and return the paths
|
|
|
- * ex: when
|
|
|
- * '/page1' redirects to '/page2' and
|
|
|
- * '/page2' redirects to '/page3'
|
|
|
- * and given '/page3',
|
|
|
- * '/page1' and '/page2' will be return
|
|
|
- *
|
|
|
- * @param {string} redirectTo
|
|
|
- * @param {object} redirectToPagePathMapping
|
|
|
- * @param {array} pagePaths
|
|
|
- */
|
|
|
- private prepareShoudDeletePagesByRedirectTo(redirectTo, redirectToPagePathMapping, pagePaths: any[] = []) {
|
|
|
- const pagePath = redirectToPagePathMapping[redirectTo];
|
|
|
-
|
|
|
- if (pagePath == null) {
|
|
|
- return pagePaths;
|
|
|
- }
|
|
|
-
|
|
|
- pagePaths.push(pagePath);
|
|
|
- return this.prepareShoudDeletePagesByRedirectTo(pagePath, redirectToPagePathMapping, pagePaths);
|
|
|
- }
|
|
|
-
|
|
|
/**
|
|
|
* Generate read stream to operate descendants of the specified page path
|
|
|
* @param {string} targetPagePath
|
|
|
@@ -273,6 +251,7 @@ class PageService {
|
|
|
const { PageQueryBuilder } = Page;
|
|
|
|
|
|
const builder = new PageQueryBuilder(Page.find(), true)
|
|
|
+ .addConditionAsNotMigrated() // to avoid affecting v5 pages
|
|
|
.addConditionToExcludeRedirect()
|
|
|
.addConditionToListOnlyDescendants(targetPagePath);
|
|
|
|
|
|
@@ -286,6 +265,10 @@ class PageService {
|
|
|
async renamePage(page, newPagePath, user, options) {
|
|
|
const Page = this.crowi.model('Page');
|
|
|
|
|
|
+ if (isTopPage(page.path)) {
|
|
|
+ throw Error('It is forbidden to rename the top page');
|
|
|
+ }
|
|
|
+
|
|
|
// v4 compatible process
|
|
|
const shouldUseV4Process = this.shouldUseV4Process(page);
|
|
|
if (shouldUseV4Process) {
|
|
|
@@ -398,7 +381,7 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
|
|
|
- private async renameDescendants(pages, user, options, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = false) {
|
|
|
+ private async renameDescendants(pages, user, options, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = true) {
|
|
|
// v4 compatible process
|
|
|
if (shouldUseV4Process) {
|
|
|
return this.renameDescendantsV4(pages, user, options, oldPagePathPrefix, newPagePathPrefix);
|
|
|
@@ -478,14 +461,14 @@ class PageService {
|
|
|
this.pageEvent.emit('updateMany', pages, user);
|
|
|
}
|
|
|
|
|
|
- private async renameDescendantsWithStream(targetPage, newPagePath, user, options = {}, shouldUseV4Process = false) {
|
|
|
+ private async renameDescendantsWithStream(targetPage, newPagePath, user, options = {}, shouldUseV4Process = true) {
|
|
|
// v4 compatible process
|
|
|
if (shouldUseV4Process) {
|
|
|
return this.renameDescendantsWithStreamV4(targetPage, newPagePath, user, options);
|
|
|
}
|
|
|
|
|
|
- const iterableFactory = new PageCursorsForDescendantsFactory(user, targetPage, true);
|
|
|
- const readStream = await iterableFactory.generateReadable();
|
|
|
+ const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
|
|
|
+ const readStream = await factory.generateReadable();
|
|
|
|
|
|
const newPagePathPrefix = newPagePath;
|
|
|
const pathRegExp = new RegExp(`^${escapeStringRegexp(targetPage.path)}`, 'i');
|
|
|
@@ -726,7 +709,7 @@ class PageService {
|
|
|
return PageTagRelation.insertMany(newPageTagRelation, { ordered: false });
|
|
|
}
|
|
|
|
|
|
- private async duplicateDescendants(pages, user, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = false) {
|
|
|
+ private async duplicateDescendants(pages, user, oldPagePathPrefix, newPagePathPrefix, shouldUseV4Process = true) {
|
|
|
if (shouldUseV4Process) {
|
|
|
return this.duplicateDescendantsV4(pages, user, oldPagePathPrefix, newPagePathPrefix);
|
|
|
}
|
|
|
@@ -840,7 +823,8 @@ class PageService {
|
|
|
return this.duplicateDescendantsWithStreamV4(page, newPagePath, user);
|
|
|
}
|
|
|
|
|
|
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(page.path, user);
|
|
|
+ const iterableFactory = new PageCursorsForDescendantsFactory(user, page, true);
|
|
|
+ const readStream = await iterableFactory.generateReadable();
|
|
|
|
|
|
const newPagePathPrefix = newPagePath;
|
|
|
const pathRegExp = new RegExp(`^${escapeStringRegexp(page.path)}`, 'i');
|
|
|
@@ -959,7 +943,7 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
if (isRecursively) {
|
|
|
- this.deleteDescendantsWithStream(page, user); // use the same process in both version v4 and v5
|
|
|
+ this.deleteDescendantsWithStream(page, user, shouldUseV4Process); // use the same process in both version v4 and v5
|
|
|
}
|
|
|
else {
|
|
|
// replace with an empty page
|
|
|
@@ -1039,41 +1023,6 @@ class PageService {
|
|
|
return deletedPage;
|
|
|
}
|
|
|
|
|
|
- private async deleteCompletelyOperation(pageIds, pagePaths) {
|
|
|
- // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
|
|
|
- const Bookmark = this.crowi.model('Bookmark');
|
|
|
- const Comment = this.crowi.model('Comment');
|
|
|
- const Page = this.crowi.model('Page');
|
|
|
- const PageTagRelation = this.crowi.model('PageTagRelation');
|
|
|
- const ShareLink = this.crowi.model('ShareLink');
|
|
|
- const Revision = this.crowi.model('Revision');
|
|
|
- const Attachment = this.crowi.model('Attachment');
|
|
|
-
|
|
|
- const { attachmentService } = this.crowi;
|
|
|
- const attachments = await Attachment.find({ page: { $in: pageIds } });
|
|
|
-
|
|
|
- const pages = await Page.find({ redirectTo: { $ne: null } });
|
|
|
- const redirectToPagePathMapping = {};
|
|
|
- pages.forEach((page) => {
|
|
|
- redirectToPagePathMapping[page.redirectTo] = page.path;
|
|
|
- });
|
|
|
-
|
|
|
- const redirectedFromPagePaths: any[] = [];
|
|
|
- pagePaths.forEach((pagePath) => {
|
|
|
- redirectedFromPagePaths.push(...this.prepareShoudDeletePagesByRedirectTo(pagePath, redirectToPagePathMapping));
|
|
|
- });
|
|
|
-
|
|
|
- return Promise.all([
|
|
|
- Bookmark.deleteMany({ page: { $in: pageIds } }),
|
|
|
- Comment.deleteMany({ page: { $in: pageIds } }),
|
|
|
- PageTagRelation.deleteMany({ relatedPage: { $in: pageIds } }),
|
|
|
- ShareLink.deleteMany({ relatedPage: { $in: pageIds } }),
|
|
|
- Revision.deleteMany({ path: { $in: pagePaths } }),
|
|
|
- Page.deleteMany({ $or: [{ path: { $in: pagePaths } }, { path: { $in: redirectedFromPagePaths } }, { _id: { $in: pageIds } }] }),
|
|
|
- attachmentService.removeAllAttachments(attachments),
|
|
|
- ]);
|
|
|
- }
|
|
|
-
|
|
|
private async deleteDescendants(pages, user) {
|
|
|
const Page = mongoose.model('Page') as PageModel;
|
|
|
|
|
|
@@ -1124,8 +1073,16 @@ class PageService {
|
|
|
/**
|
|
|
* Create delete stream
|
|
|
*/
|
|
|
- private async deleteDescendantsWithStream(targetPage, user) {
|
|
|
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
|
|
|
+ private async deleteDescendantsWithStream(targetPage, user, shouldUseV4Process = true) {
|
|
|
+ let readStream;
|
|
|
+ if (shouldUseV4Process) {
|
|
|
+ readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
|
|
|
+ readStream = await factory.generateReadable();
|
|
|
+ }
|
|
|
+
|
|
|
|
|
|
const deleteDescendants = this.deleteDescendants.bind(this);
|
|
|
let count = 0;
|
|
|
@@ -1155,6 +1112,47 @@ class PageService {
|
|
|
.pipe(writeStream);
|
|
|
}
|
|
|
|
|
|
+ private async deleteCompletelyOperation(pageIds, pagePaths) {
|
|
|
+ // Delete Bookmarks, Attachments, Revisions, Pages and emit delete
|
|
|
+ const Bookmark = this.crowi.model('Bookmark');
|
|
|
+ const Comment = this.crowi.model('Comment');
|
|
|
+ const Page = this.crowi.model('Page');
|
|
|
+ const PageTagRelation = this.crowi.model('PageTagRelation');
|
|
|
+ const ShareLink = this.crowi.model('ShareLink');
|
|
|
+ const Revision = this.crowi.model('Revision');
|
|
|
+ const Attachment = this.crowi.model('Attachment');
|
|
|
+
|
|
|
+ const { attachmentService } = this.crowi;
|
|
|
+ const attachments = await Attachment.find({ page: { $in: pageIds } });
|
|
|
+
|
|
|
+ /*
|
|
|
+ * TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ * deleteMany related PageRedirect documents
|
|
|
+ */
|
|
|
+ // const pages = await Page.find({ redirectTo: { $ne: null } });
|
|
|
+ // const redirectToPagePathMapping = {};
|
|
|
+ // pages.forEach((page) => {
|
|
|
+ // redirectToPagePathMapping[page.redirectTo] = page.path;
|
|
|
+ // });
|
|
|
+
|
|
|
+ // const redirectedFromPagePaths: any[] = [];
|
|
|
+ // pagePaths.forEach((pagePath) => {
|
|
|
+ // redirectedFromPagePaths.push(...this.prepareShoudDeletePagesByRedirectTo(pagePath, redirectToPagePathMapping));
|
|
|
+ // });
|
|
|
+
|
|
|
+ return Promise.all([
|
|
|
+ Bookmark.deleteMany({ page: { $in: pageIds } }),
|
|
|
+ Comment.deleteMany({ page: { $in: pageIds } }),
|
|
|
+ PageTagRelation.deleteMany({ relatedPage: { $in: pageIds } }),
|
|
|
+ ShareLink.deleteMany({ relatedPage: { $in: pageIds } }),
|
|
|
+ Revision.deleteMany({ path: { $in: pagePaths } }),
|
|
|
+ Page.deleteMany({ $or: [{ path: { $in: pagePaths } }, { _id: { $in: pageIds } }] }),
|
|
|
+ // TODO: https://redmine.weseek.co.jp/issues/86577
|
|
|
+ // Page.deleteMany({ $or: [{ path: { $in: pagePaths } }, { path: { $in: redirectedFromPagePaths } }, { _id: { $in: pageIds } }] }),
|
|
|
+ attachmentService.removeAllAttachments(attachments),
|
|
|
+ ]);
|
|
|
+ }
|
|
|
+
|
|
|
// delete multiple pages
|
|
|
private async deleteMultipleCompletely(pages, user, options = {}) {
|
|
|
const ids = pages.map(page => (page._id));
|
|
|
@@ -1170,6 +1168,43 @@ class PageService {
|
|
|
}
|
|
|
|
|
|
async deleteCompletely(page, user, options = {}, isRecursively = false, preventEmitting = false) {
|
|
|
+ const Page = mongoose.model('Page') as PageModel;
|
|
|
+
|
|
|
+ if (isTopPage(page.path)) {
|
|
|
+ throw Error('It is forbidden to delete the top page');
|
|
|
+ }
|
|
|
+
|
|
|
+ // v4 compatible process
|
|
|
+ const shouldUseV4Process = this.shouldUseV4Process(page);
|
|
|
+ if (shouldUseV4Process) {
|
|
|
+ return this.deleteCompletelyV4(page, user, options, isRecursively, preventEmitting);
|
|
|
+ }
|
|
|
+
|
|
|
+ const ids = [page._id];
|
|
|
+ const paths = [page.path];
|
|
|
+
|
|
|
+ logger.debug('Deleting completely', paths);
|
|
|
+
|
|
|
+ // replace with an empty page
|
|
|
+ const shouldReplace = !isRecursively && !isTrashPage(page.path) && await Page.exists({ parent: page._id });
|
|
|
+ if (shouldReplace) {
|
|
|
+ await Page.replaceTargetWithEmptyPage(page);
|
|
|
+ }
|
|
|
+
|
|
|
+ await this.deleteCompletelyOperation(ids, paths);
|
|
|
+
|
|
|
+ if (isRecursively) {
|
|
|
+ this.deleteCompletelyDescendantsWithStream(page, user, options, shouldUseV4Process);
|
|
|
+ }
|
|
|
+
|
|
|
+ if (!page.isEmpty && !preventEmitting) {
|
|
|
+ this.pageEvent.emit('deleteCompletely', page, user);
|
|
|
+ }
|
|
|
+
|
|
|
+ return;
|
|
|
+ }
|
|
|
+
|
|
|
+ private async deleteCompletelyV4(page, user, options = {}, isRecursively = false, preventEmitting = false) {
|
|
|
const ids = [page._id];
|
|
|
const paths = [page.path];
|
|
|
|
|
|
@@ -1181,7 +1216,7 @@ class PageService {
|
|
|
this.deleteCompletelyDescendantsWithStream(page, user, options);
|
|
|
}
|
|
|
|
|
|
- if (!preventEmitting) {
|
|
|
+ if (!page.isEmpty && !preventEmitting) {
|
|
|
this.pageEvent.emit('deleteCompletely', page, user);
|
|
|
}
|
|
|
|
|
|
@@ -1195,9 +1230,16 @@ class PageService {
|
|
|
/**
|
|
|
* Create delete completely stream
|
|
|
*/
|
|
|
- private async deleteCompletelyDescendantsWithStream(targetPage, user, options = {}) {
|
|
|
+ private async deleteCompletelyDescendantsWithStream(targetPage, user, options = {}, shouldUseV4Process = true) {
|
|
|
+ let readStream;
|
|
|
|
|
|
- const readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
|
|
|
+ if (shouldUseV4Process) { // pages don't have parents
|
|
|
+ readStream = await this.generateReadStreamToOperateOnlyDescendants(targetPage.path, user);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ const factory = new PageCursorsForDescendantsFactory(user, targetPage, true);
|
|
|
+ readStream = await factory.generateReadable();
|
|
|
+ }
|
|
|
|
|
|
const deleteMultipleCompletely = this.deleteMultipleCompletely.bind(this);
|
|
|
let count = 0;
|
|
|
@@ -1561,7 +1603,7 @@ class PageService {
|
|
|
/*
|
|
|
* returns an array of js RegExp instance instead of RE2 instance for mongo filter
|
|
|
*/
|
|
|
- async _generateRegExpsByPageIds(pageIds) {
|
|
|
+ private async _generateRegExpsByPageIds(pageIds) {
|
|
|
const Page = mongoose.model('Page') as unknown as PageModel;
|
|
|
|
|
|
let result;
|
|
|
@@ -1579,7 +1621,7 @@ class PageService {
|
|
|
return regexps;
|
|
|
}
|
|
|
|
|
|
- async _setIsV5CompatibleTrue() {
|
|
|
+ private async _setIsV5CompatibleTrue() {
|
|
|
try {
|
|
|
await this.crowi.configManager.updateConfigsInTheSameNamespace('crowi', {
|
|
|
'app:isV5Compatible': true,
|
|
|
@@ -1733,7 +1775,7 @@ class PageService {
|
|
|
|
|
|
}
|
|
|
|
|
|
- async _v5NormalizeIndex() {
|
|
|
+ private async _v5NormalizeIndex() {
|
|
|
const collection = mongoose.connection.collection('pages');
|
|
|
|
|
|
try {
|