itizawa 5 лет назад
Родитель
Сommit
1faff9213f
1 измененных файлов с 12 добавлено и 15 удалено
  1. 12 15
      src/server/service/page.js

+ 12 - 15
src/server/service/page.js

@@ -297,7 +297,7 @@ class PageService {
       .pipe(writeStream);
   }
 
-  async revertDeletedPages(pages, user, pathRedirectToMapping) {
+  async revertDeletedPages(pages, user) {
     const Page = this.crowi.model('Page');
     const pageCollection = mongoose.connection.collection('pages');
     const revisionCollection = mongoose.connection.collection('revisions');
@@ -306,17 +306,24 @@ class PageService {
     const revertPageBulkOp = pageCollection.initializeUnorderedBulkOp();
     const revertRevisionBulkOp = revisionCollection.initializeUnorderedBulkOp();
 
+    const pathToPageMapping = {};
+    const toPaths = pages.map(page => Page.getRevertDeletedPageName(page.path));
+    const toPages = await Page.find({ path: { $in: toPaths } });
+    toPages.forEach((toPage) => {
+      pathToPageMapping[toPage.redirectTo] = toPage;
+    });
+
     pages.forEach((page) => {
 
       // e.g. page.path = /trash/test, toPath = /test
       const toPath = Page.getRevertDeletedPageName(page.path);
 
-      if (pathRedirectToMapping[toPath] != null) {
+      if (pathToPageMapping[toPath] != null) {
       // When the page is deleted, it will always be created with "redirectTo" in the path of the original page.
       // So, it's ok to delete the page
       // However, If a page exists that is not "redirectTo", something is wrong. (Data correction is needed).
-        if (pathRedirectToMapping[toPath] === page.path) {
-          removePageBulkOp.find({ path: toPath, redirectTo: page.path }).remove();
+        if (pathToPageMapping[toPath].redirectTo === page.path) {
+          removePageBulkOp.find({ path: toPath }).remove();
         }
       }
       revertPageBulkOp.find({ _id: page._id }).update({ $set: { path: toPath, status: STATUS_PUBLISHED, lastUpdateUser: user._id } });
@@ -375,16 +382,6 @@ class PageService {
       .lean()
       .cursor();
 
-    const toPath = Page.getRevertDeletedPageName(targetPage.path);
-    const pathRegExp = new RegExp(`^${escapeStringRegexp(toPath)}`, 'i');
-    const pages = await Page.find({ path: pathRegExp });
-
-    // Mapping to set to the body of the new revision
-    const pathRedirectToMapping = {};
-    pages.forEach((page) => {
-      pathRedirectToMapping[page.path] = page.redirectTo;
-    });
-
     const revertDeletedPages = this.revertDeletedPages.bind(this);
     let count = 0;
     const writeStream = new Writable({
@@ -392,7 +389,7 @@ class PageService {
       async write(batch, encoding, callback) {
         try {
           count += batch.length;
-          revertDeletedPages(batch, user, pathRedirectToMapping);
+          revertDeletedPages(batch, user);
           logger.debug(`Reverting pages progressing: (count=${count})`);
         }
         catch (err) {