Florianschmidtwelzow has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/298040

Change subject: OutputPage::getCategories(): Add a possibility to distinguish 
"normal" and "hidden" categories
......................................................................

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
---
M includes/OutputPage.php
M tests/phpunit/includes/OutputPageTest.php
2 files changed, 84 insertions(+), 30 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/40/298040/1

diff --git a/includes/OutputPage.php b/includes/OutputPage.php
index 15b70c8..72148af 100644
--- a/includes/OutputPage.php
+++ b/includes/OutputPage.php
@@ -1256,32 +1256,7 @@
                if ( !is_array( $categories ) || count( $categories ) == 0 ) {
                        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_SLAVE );
-               $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' );
@@ -1311,10 +1286,44 @@
                                        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
+        */
+       public 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_SLAVE );
+               $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;
        }
 
        /**
@@ -1340,12 +1349,26 @@
        }
 
        /**
-        * 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 $type => $categories ) {
+                               $allCategories = array_merge( $allCategories, 
$categories );
+                       }
+                       return $allCategories;
+               }
+               if ( isset( $this->mCategories[$type] ) ) {
+                       return $this->mCategories[$type];
+               }
+               return [];
        }
 
        /**
diff --git a/tests/phpunit/includes/OutputPageTest.php 
b/tests/phpunit/includes/OutputPageTest.php
index 9934749..54b6838 100644
--- a/tests/phpunit/includes/OutputPageTest.php
+++ b/tests/phpunit/includes/OutputPageTest.php
@@ -358,6 +358,37 @@
                $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' ) );
+       }
 }
 
 /**

-- 
To view, visit https://gerrit.wikimedia.org/r/298040
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iadda9ae362a21fbee770240234b8f55326219932
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Florianschmidtwelzow <florian.schmidt.stargatewis...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to