Просмотр исходного кода

fix #155: Bug: Recursively option processes also unexpedted pages

use properly `findListByStartWith` and `findListWithDescendants`
Yuki Takei 8 лет назад
Родитель
Сommit
c4bd9afbfc
2 измененных файлов с 54 добавлено и 10 удалено
  1. 51 7
      lib/models/page.js
  2. 3 3
      lib/routes/page.js

+ 51 - 7
lib/models/page.js

@@ -610,11 +610,24 @@ module.exports = function(crowi) {
   };
 
   /**
-   * findListByStartWith
+   * find the page that is match with `path` and its descendants
+   */
+  pageSchema.statics.findListWithDescendants = function(path, userData, option) {
+    var Page = this;
+
+    // add slash to the last
+    path = Page.addSlashOfEnd(path);
+    // escape
+    path = escapeStringRegexp(path);
+
+    return Page.findListByStartWith(path, userData, option);
+  };
+
+  /**
+   * find pages that start with `path`
    *
    * If `path` has `/` at the end, returns '{path}/*' and '{path}' self.
    * If `path` doesn't have `/` at the end, returns '{path}*'
-   * e.g.
    */
   pageSchema.statics.findListByStartWith = function(path, userData, option) {
     var Page = this;
@@ -657,12 +670,32 @@ module.exports = function(crowi) {
     });
   };
 
+  /**
+   * generate the query to find the page that is match with `path` and its descendants
+   */
+  pageSchema.statics.generateQueryToListWithDescendants = function(path, userData, option) {
+    var Page = this;
+
+    // add slash to the last
+    path = Page.addSlashOfEnd(path);
+    // escape
+    path = escapeStringRegexp(path);
+
+    return Page.generateQueryToListByStartWith(path, userData, option);
+  };
+
+  /**
+   * generate the query to find pages that start with `path`
+   *
+   * If `path` has `/` at the end, returns '{path}/*' and '{path}' self.
+   * If `path` doesn't have `/` at the end, returns '{path}*'
+   */
   pageSchema.statics.generateQueryToListByStartWith = function(path, userData, option) {
     var Page = this;
     var pathCondition = [];
     var includeDeletedPage = option.includeDeletedPage || false;
 
-    var queryReg = new RegExp('^' + escapeStringRegexp(path));
+    var queryReg = new RegExp('^' + path);
     pathCondition.push({path: queryReg});
     if (path.match(/\/$/)) {
       debug('Page list by ending with /, so find also upper level page');
@@ -890,7 +923,7 @@ module.exports = function(crowi) {
 
     return new Promise(function (resolve, reject) {
       Page
-      .generateQueryToListByStartWith(path, user, options)
+      .generateQueryToListWithDescendants(path, user, options)
       .then(function (pages) {
         Promise.all(pages.map(function (page) {
           return Page.deletePage(page, user, options);
@@ -941,7 +974,7 @@ module.exports = function(crowi) {
 
       return new Promise(function (resolve, reject) {
         Page
-        .generateQueryToListByStartWith(path, user, options)
+        .generateQueryToListWithDescendants(path, user, options)
         .then(function (pages) {
           Promise.all(pages.map(function (page) {
             return Page.revertDeletedPage(page, user, options);
@@ -997,7 +1030,7 @@ module.exports = function(crowi) {
 
     return new Promise(function (resolve, reject) {
       Page
-      .generateQueryToListByStartWith(path, user, options)
+      .generateQueryToListWithDescendants(path, user, options)
       .then(function (pages) {
         Promise.all(pages.map(function (page) {
           return Page.completelyDeletePage(page, user, options);
@@ -1095,7 +1128,7 @@ module.exports = function(crowi) {
 
     return new Promise(function(resolve, reject) {
       Page
-      .generateQueryToListByStartWith(path, user, options)
+      .generateQueryToListWithDescendants(path, user, options)
       .then(function(pages) {
         Promise.all(pages.map(function(page) {
           newPagePath = page.path.replace(pathRegExp, newPagePathPrefix);
@@ -1114,6 +1147,17 @@ module.exports = function(crowi) {
     return;
   };
 
+  /**
+   * return path that added slash to the end for specified path
+   */
+  pageSchema.statics.addSlashOfEnd = function(path) {
+    let returnPath = path;
+    if (!path.match(/\/$/)) {
+      returnPath += '/';
+    }
+    return returnPath;
+  }
+
   pageSchema.statics.GRANT_PUBLIC = GRANT_PUBLIC;
   pageSchema.statics.GRANT_RESTRICTED = GRANT_RESTRICTED;
   pageSchema.statics.GRANT_SPECIFIED = GRANT_SPECIFIED;

+ 3 - 3
lib/routes/page.js

@@ -142,7 +142,7 @@ module.exports = function(crowi, app) {
     var limit = 50;
     var offset = parseInt(req.query.offset)  || 0;
     var SEENER_THRESHOLD = 10;
-    // add slash to the last
+    // add slash if root
     path = path + (path == '/' ? '' : '/');
 
     debug('Page list show', path);
@@ -303,7 +303,7 @@ module.exports = function(crowi, app) {
     // get list pages
     .then(function() {
       if (!isRedirect) {
-        Page.findListByStartWith(path, req.user, queryOptions)
+        Page.findListWithDescendants(path, req.user, queryOptions)
         .then(function(pageList) {
           if (pageList.length > limit) {
             pageList.pop();
@@ -355,7 +355,7 @@ module.exports = function(crowi, app) {
       pages: [],
     };
 
-    Page.findListByStartWith(path, req.user, queryOptions)
+    Page.findListWithDescendants(path, req.user, queryOptions)
     .then(function(pageList) {
 
       if (pageList.length > limit) {