(bug 45201) Add option to pass defaultNamespace to ApiPageSet
authorJan Gerber <jgerber@wikimedia.org>
Mon, 4 Mar 2013 21:22:14 +0000 (21:22 +0000)
committerJan Gerber <jgerber@wikimedia.org>
Tue, 5 Mar 2013 03:16:09 +0000 (03:16 +0000)
Change-Id: I47f0ce18fcb877ae88e5eb26eef4c1c74a6d6755

includes/api/ApiPageSet.php

index 1bb7e47..c225b1b 100644 (file)
@@ -69,18 +69,21 @@ class ApiPageSet extends ApiBase {
        private $mFakePageId = -1;
        private $mCacheMode = 'public';
        private $mRequestedPageFields = array();
+       private $mDefaultNamespace = NS_MAIN;
 
        /**
         * Constructor
         * @param $dbSource ApiBase Module implementing getDB().
         *        Allows PageSet to reuse existing db connection from the shared state like ApiQuery.
         * @param $flags int Zero or more flags like DISABLE_GENERATORS
+        * @param $defaultNamespace int the namespace to use if none is specified by a prefix.
         * @since 1.21 accepts $flags instead of two boolean values
         */
-       public function __construct( ApiBase $dbSource, $flags = 0 ) {
+       public function __construct( ApiBase $dbSource, $flags = 0, $defaultNamespace = NS_MAIN ) {
                parent::__construct( $dbSource->getMain(), $dbSource->getModuleName() );
                $this->mDbSource = $dbSource;
                $this->mAllowGenerator = ( $flags & ApiPageSet::DISABLE_GENERATORS ) == 0;
+               $this->mDefaultNamespace = $defaultNamespace;
 
                $this->profileIn();
                $this->mParams = $this->extractRequestParams();
@@ -883,13 +886,15 @@ class ApiPageSet extends ApiBase {
         * @return LinkBatch
         */
        private function processTitlesArray( $titles ) {
-               $genderCache = GenderCache::singleton();
-               $genderCache->doTitlesArray( $titles, __METHOD__ );
-
+               $usernames = array();
                $linkBatch = new LinkBatch();
 
                foreach ( $titles as $title ) {
-                       $titleObj = is_string( $title ) ? Title::newFromText( $title ) : $title;
+                       if ( is_string( $title ) ) {
+                               $titleObj = Title::newFromText( $title, $this->mDefaultNamespace );
+                       } else {
+                               $titleObj = $title;
+                       }
                        if ( !$titleObj ) {
                                // Handle invalid titles gracefully
                                $this->mAllpages[0][$title] = $this->mFakePageId;
@@ -899,10 +904,9 @@ class ApiPageSet extends ApiBase {
                        }
                        $unconvertedTitle = $titleObj->getPrefixedText();
                        $titleWasConverted = false;
-                       $iw = $titleObj->getInterwiki();
-                       if ( strval( $iw ) !== '' ) {
+                       if ( $titleObj->isExternal() ) {
                                // This title is an interwiki link.
-                               $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
+                               $this->mInterwikiTitles[$unconvertedTitle] = $titleObj->getInterwiki();
                        } else {
                                // Variants checking
                                global $wgContLang;
@@ -941,7 +945,15 @@ class ApiPageSet extends ApiBase {
                        } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
                                $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
                        }
+
+                       // Need gender information
+                       if ( MWNamespace::hasGenderDistinction( $titleObj->getNamespace() ) ) {
+                               $usernames[] = $titleObj->getText();
+                       }
                }
+               // Get gender information
+               $genderCache = GenderCache::singleton();
+               $genderCache->doQuery( $usernames, __METHOD__ );
 
                return $linkBatch;
        }