Yuki Takei 9 лет назад
Родитель
Сommit
cc6565598e
1 измененных файлов с 40 добавлено и 0 удалено
  1. 40 0
      lib/models/page.js

+ 40 - 0
lib/models/page.js

@@ -536,6 +536,20 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
+  pageSchema.statics.findPageByRedirectTo = function(path) {
+    var Page = this;
+
+    return new Promise(function(resolve, reject) {
+      Page.findOne({redirectTo: path}, function(err, pageData) {
+        if (err || pageData === null) {
+          return reject(err);
+        }
+
+        return resolve(pageData);
+      });
+    });
+  };
+
   pageSchema.statics.findListByCreator = function(user, option, currentUser) {
   pageSchema.statics.findListByCreator = function(user, option, currentUser) {
     var Page = this;
     var Page = this;
     var User = crowi.model('User');
     var User = crowi.model('User');
@@ -919,6 +933,32 @@ module.exports = function(crowi) {
     });
     });
   };
   };
 
 
+  pageSchema.statics.removePageByPath = function(pagePath) {
+    var Page = this;
+
+    return Page.findPageByPath(redirectPath)
+      .then(function(pageData) {
+        return Page.removePageById(pageData.id);
+      });
+  };
+
+  /**
+   * remove the page that is redirecting to specified `pagePath`
+   * @param {string} pagePath
+   */
+  pageSchema.statics.removeRedirectOriginPageByPath = function(pagePath) {
+    var Page = this;
+
+    return Page.findPageByRedirectTo(pagePath)
+      .then((redirectOriginPageData) => {
+        return Page.removePageById(redirectOriginPageData.id);
+      })
+      .catch((err) => {
+        // do nothing if origin page doesn't exist
+        return Promise.resolve();
+      })
+  };
+
   pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
   pageSchema.statics.rename = function(pageData, newPagePath, user, options) {
     var Page = this
     var Page = this
       , Revision = crowi.model('Revision')
       , Revision = crowi.model('Revision')