protected $mCategoryLinks = [];
/** @var array */
- protected $mCategories = [];
+ protected $mCategories = [
+ 'hidden' => [],
+ 'normal' => [],
+ ];
/** @var array */
protected $mIndicators = [];
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' );
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
*
}
/**
- * 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];
}
/**
$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' ) );
+ }
}
/**