Quellcode durchsuchen

Imprv/121 Add recursively option for Move operation

ttaka66 vor 8 Jahren
Ursprung
Commit
4ad4a8b537
1 geänderte Dateien mit 47 neuen und 16 gelöschten Zeilen
  1. 47 16
      lib/models/page.js

+ 47 - 16
lib/models/page.js

@@ -505,6 +505,21 @@ module.exports = function(crowi) {
     });
   };
 
+  // find recursive page by path
+  pageSchema.statics.findRecursivePageByPath = function(path) {
+    var Page = this
+      , pathRegExp = new RegExp('^' + path, 'i')
+
+    return new Promise(function(resolve, reject) {
+      Page
+        .find({ path: pathRegExp })
+        .exec()
+        .then(function(pages) {
+          return resolve(pages);
+        });
+    });
+  };
+
   pageSchema.statics.findListByPageIds = function(ids, options) {
     var Page = this;
     var User = crowi.model('User');
@@ -693,7 +708,7 @@ module.exports = function(crowi) {
     return q;
   }
 
-  pageSchema.statics.updatePageProperty = function(page, updateData) {
+  pageSchema.statics.updatePageProperty = function(page, updateData, isRename) {
     var Page = this;
     return new Promise(function(resolve, reject) {
       // TODO foreach して save
@@ -702,6 +717,10 @@ module.exports = function(crowi) {
           return reject(err);
         }
 
+        if (isRename) {
+          return resolve({ oldPagePath: page.path, newPagePath: updateData.path });
+        }
+
         return resolve(data);
       });
     });
@@ -998,29 +1017,41 @@ module.exports = function(crowi) {
       })
   };
 
-  pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
+  pageSchema.statics.rename = function(pageData, newPagePathPrefix, user, options) {
+
     var Page = this
       , Revision = crowi.model('Revision')
       , path = pageData.path
+      , pathRegExp = new RegExp('^' + path, 'i')
       , createRedirectPage = options.createRedirectPage || 0
       , moveUnderTrees     = options.moveUnderTrees || 0;
 
     return new Promise(function(resolve, reject) {
       // pageData の path を変更
-      Page.updatePageProperty(pageData, {updatedAt: Date.now(), path: newPagePath, lastUpdateUser: user})
-      .then(function(data) {
-        // reivisions の path を変更
-        return Revision.updateRevisionListByPath(path, {path: newPagePath}, {})
-      }).then(function(data) {
-        pageData.path = newPagePath;
-
-        if (createRedirectPage) {
-          var body = 'redirect ' + newPagePath;
-          Page.create(path, body, user, {redirectTo: newPagePath}).then(resolve).catch(reject);
-        } else {
-          resolve(data);
-        }
-        pageEvent.emit('update', pageData, user); // update as renamed page
+      Page
+      .findRecursivePageByPath(path)
+      .then(function(pages) {
+        Promise.all(pages.map(function(page) {
+          newPagePath = page.path.replace(pathRegExp, newPagePathPrefix)
+          return Page.updatePageProperty(page, { updatedAt: Date.now(), path: newPagePath, lastUpdateUser: user });
+        }))
+        .then(function(pagePaths) {
+          // reivisions の path を変更
+          return pagePaths.map(function(pagePaths) {
+            return Revision.updateRevisionListByPath(pagePaths.oldPagePath, { path: pagePaths.newPagePath }, {});
+          });
+        })
+        .then(function(data) {
+          pageData.path = newPagePathPrefix;
+
+          if (createRedirectPage) {
+            var body = 'redirect ' + newPagePath;
+            Page.create(path, body, user, { redirectTo: newPagePath }).then(resolve).catch(reject);
+          } else {
+            resolve(data);
+          }
+          pageEvent.emit('update', pageData, user); // update as renamed page
+        });
       });
     });
   };