From f4b8549f5fad898449f92f0878780dbb6bb9afc0 Mon Sep 17 00:00:00 2001 From: Florian Date: Fri, 8 Jul 2016 21:11:53 +0200 Subject: [PATCH] OutputPage::getCategories(): Add a possibility to distinguish "normal" and "hidden" categories There's currently no way to get, e.g., all categories except the hidden ones just as text. The OutputPage::getCategories() method always returns all categories as an array of strings (titles) and the getCategoryLinks() method returns the result of Linker::link but with the distinction between "normal" and "hidden" categories. This change adds a new parameter to OutputPage::getCategories(), $type, which can be used to define, what categories should be returned. The default value is "all", which means, that all categories are returned (the current result of the method). With the value "normal" and "hidden", the method will return the respective values. This could be used in I97d7de723fe72da26c7dbde0a559a13704c7099a to remove the stupid Linker::link() and isset workaround. Change-Id: Iadda9ae362a21fbee770240234b8f55326219932 --- includes/OutputPage.php | 87 +++++++++++++++-------- tests/phpunit/includes/OutputPageTest.php | 31 ++++++++ 2 files changed, 88 insertions(+), 30 deletions(-) diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 863a426524..5530416205 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -107,7 +107,10 @@ class OutputPage extends ContextSource { protected $mCategoryLinks = []; /** @var array */ - protected $mCategories = []; + protected $mCategories = [ + 'hidden' => [], + 'normal' => [], + ]; /** @var array */ protected $mIndicators = []; @@ -1252,31 +1255,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' ); @@ -1306,12 +1285,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 * @@ -1335,12 +1348,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]; } /** diff --git a/tests/phpunit/includes/OutputPageTest.php b/tests/phpunit/includes/OutputPageTest.php index c637d34a5a..626987209d 100644 --- a/tests/phpunit/includes/OutputPageTest.php +++ b/tests/phpunit/includes/OutputPageTest.php @@ -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' ) ); + } } /** -- 2.20.1