Merge "OutputPage::getCategories(): Add a possibility to distinguish "normal" and...
authorjenkins-bot <jenkins-bot@gerrit.wikimedia.org>
Fri, 25 Nov 2016 00:17:04 +0000 (00:17 +0000)
committerGerrit Code Review <gerrit@wikimedia.org>
Fri, 25 Nov 2016 00:17:04 +0000 (00:17 +0000)
includes/OutputPage.php
tests/phpunit/includes/OutputPageTest.php

index 50629ba..43d71ab 100644 (file)
@@ -107,7 +107,10 @@ class OutputPage extends ContextSource {
        protected $mCategoryLinks = [];
 
        /** @var array */
-       protected $mCategories = [];
+       protected $mCategories = [
+               'hidden' => [],
+               'normal' => [],
+       ];
 
        /** @var array */
        protected $mIndicators = [];
@@ -1249,31 +1252,7 @@ class OutputPage extends ContextSource {
                        return;
                }
 
-               # Add the links to a LinkBatch
-               $arr = [ NS_CATEGORY => $categories ];
-               $lb = new LinkBatch;
-               $lb->setArray( $arr );
-
-               # Fetch existence plus the hiddencat property
-               $dbr = wfGetDB( DB_REPLICA );
-               $fields = array_merge(
-                       LinkCache::getSelectFields(),
-                       [ 'page_namespace', 'page_title', 'pp_value' ]
-               );
-
-               $res = $dbr->select( [ 'page', 'page_props' ],
-                       $fields,
-                       $lb->constructSet( 'page', $dbr ),
-                       __METHOD__,
-                       [],
-                       [ 'page_props' => [ 'LEFT JOIN', [
-                               'pp_propname' => 'hiddencat',
-                               'pp_page = page_id'
-                       ] ] ]
-               );
-
-               # Add the results to the link cache
-               $lb->addResultToCache( LinkCache::singleton(), $res );
+               $res = $this->addCategoryLinksToLBAndGetResult( $categories );
 
                # Set all the values to 'normal'.
                $categories = array_fill_keys( array_keys( $categories ), 'normal' );
@@ -1303,12 +1282,46 @@ class OutputPage extends ContextSource {
                                        continue;
                                }
                                $text = $wgContLang->convertHtml( $title->getText() );
-                               $this->mCategories[] = $title->getText();
+                               $this->mCategories[$type][] = $title->getText();
                                $this->mCategoryLinks[$type][] = Linker::link( $title, $text );
                        }
                }
        }
 
+       /**
+        * @param array $categories
+        * @return bool|ResultWrapper
+        */
+       protected function addCategoryLinksToLBAndGetResult( array $categories ) {
+               # Add the links to a LinkBatch
+               $arr = [ NS_CATEGORY => $categories ];
+               $lb = new LinkBatch;
+               $lb->setArray( $arr );
+
+               # Fetch existence plus the hiddencat property
+               $dbr = wfGetDB( DB_REPLICA );
+               $fields = array_merge(
+                       LinkCache::getSelectFields(),
+                       [ 'page_namespace', 'page_title', 'pp_value' ]
+               );
+
+               $res = $dbr->select( [ 'page', 'page_props' ],
+                       $fields,
+                       $lb->constructSet( 'page', $dbr ),
+                       __METHOD__,
+                       [],
+                       [ 'page_props' => [ 'LEFT JOIN', [
+                               'pp_propname' => 'hiddencat',
+                               'pp_page = page_id'
+                       ] ] ]
+               );
+
+               # Add the results to the link cache
+               $lb->addResultToCache( LinkCache::singleton(), $res );
+
+               return $res;
+       }
+
        /**
         * Reset the category links (but not the category list) and add $categories
         *
@@ -1332,12 +1345,26 @@ class OutputPage extends ContextSource {
        }
 
        /**
-        * Get the list of category names this page belongs to
+        * Get the list of category names this page belongs to.
         *
+        * @param string $type The type of categories which should be returned. Possible values:
+        *  * all: all categories of all types
+        *  * hidden: only the hidden categories
+        *  * normal: all categories, except hidden categories
         * @return array Array of strings
         */
-       public function getCategories() {
-               return $this->mCategories;
+       public function getCategories( $type = 'all' ) {
+               if ( $type === 'all' ) {
+                       $allCategories = [];
+                       foreach ( $this->mCategories as $categories ) {
+                               $allCategories = array_merge( $allCategories, $categories );
+                       }
+                       return $allCategories;
+               }
+               if ( !isset( $this->mCategories[$type] ) ) {
+                       throw new InvalidArgumentException( 'Invalid category type given: ' . $type );
+               }
+               return $this->mCategories[$type];
        }
 
        /**
index c637d34..6269872 100644 (file)
@@ -305,6 +305,37 @@ class OutputPageTest extends MediaWikiTestCase {
                $request->setCookie( 'Token', '123' );
                $this->assertTrue( $outputPage->haveCacheVaryCookies() );
        }
+
+       /*
+        * @covers OutputPage::addCategoryLinks
+        * @covers OutputPage::getCategories
+        */
+       function testGetCategories() {
+               $fakeResultWrapper = new FakeResultWrapper( [
+                       (object) [
+                               'pp_value' => 1,
+                               'page_title' => 'Test'
+                       ],
+                       (object) [
+                               'page_title' => 'Test2'
+                       ]
+               ] );
+               $outputPage = $this->getMockBuilder( 'OutputPage' )
+                       ->setConstructorArgs( [ new RequestContext() ] )
+                       ->setMethods( [ 'addCategoryLinksToLBAndGetResult' ] )
+                       ->getMock();
+               $outputPage->expects( $this->any() )
+                       ->method( 'addCategoryLinksToLBAndGetResult' )
+                       ->will( $this->returnValue( $fakeResultWrapper ) );
+
+               $outputPage->addCategoryLinks( [
+                       'Test' => 'Test',
+                       'Test2' => 'Test2',
+               ] );
+               $this->assertEquals( [ 0 => 'Test', '1' => 'Test2' ], $outputPage->getCategories() );
+               $this->assertEquals( [ 0 => 'Test2' ], $outputPage->getCategories( 'normal' ) );
+               $this->assertEquals( [ 0 => 'Test' ], $outputPage->getCategories( 'hidden' ) );
+       }
 }
 
 /**