Added an optional $maxdepth to CategoryFinder
[lhc/web/wiklou.git] / includes / CategoryFinder.php
index 2a70f5f..3561f7f 100644 (file)
@@ -56,6 +56,9 @@ class CategoryFinder {
        /** @var array Array of article/category IDs */
        protected $next = [];
 
        /** @var array Array of article/category IDs */
        protected $next = [];
 
+       /** @var int Max layer depth **/
+       protected $maxdepth = -1;
+
        /** @var array Array of DBKEY category names */
        protected $targets = [];
 
        /** @var array Array of DBKEY category names */
        protected $targets = [];
 
@@ -73,12 +76,17 @@ class CategoryFinder {
         * @param array $articleIds Array of article IDs
         * @param array $categories FIXME
         * @param string $mode FIXME, default 'AND'.
         * @param array $articleIds Array of article IDs
         * @param array $categories FIXME
         * @param string $mode FIXME, default 'AND'.
+        * @param int $maxdepth Maximum layer depth. Where:
+        *      -1 means deep recursion (default);
+        *       0 means no-parents;
+        *       1 means one parent layer, etc.
         * @todo FIXME: $categories/$mode
         */
         * @todo FIXME: $categories/$mode
         */
-       public function seed( $articleIds, $categories, $mode = 'AND' ) {
+       public function seed( $articleIds, $categories, $mode = 'AND', $maxdepth = -1 ) {
                $this->articles = $articleIds;
                $this->next = $articleIds;
                $this->mode = $mode;
                $this->articles = $articleIds;
                $this->next = $articleIds;
                $this->mode = $mode;
+               $this->maxdepth = $maxdepth;
 
                # Set the list of target categories; convert them to DBKEY form first
                $this->targets = [];
 
                # Set the list of target categories; convert them to DBKEY form first
                $this->targets = [];
@@ -98,8 +106,17 @@ class CategoryFinder {
         */
        public function run() {
                $this->dbr = wfGetDB( DB_REPLICA );
         */
        public function run() {
                $this->dbr = wfGetDB( DB_REPLICA );
-               while ( count( $this->next ) > 0 ) {
+
+               $i = 0;
+               $dig = true;
+               while ( count( $this->next ) && $dig ) {
                        $this->scanNextLayer();
                        $this->scanNextLayer();
+
+                       // Is there any depth limit?
+                       if ( $this->maxdepth !== -1 ) {
+                               $dig = $i < $this->maxdepth;
+                               $i++;
+                       }
                }
 
                # Now check if this applies to the individual articles
                }
 
                # Now check if this applies to the individual articles