Fix the last assignment in conditional, bit of code duplication, as the next isn...
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
index 96be5ef..dc7ea95 100644 (file)
@@ -1,9 +1,8 @@
 <?php
-
 /**
- * Created on Sep 24, 2006
  *
- * API for MediaWiki 1.8+
+ *
+ * Created on Sep 24, 2006
  *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
@@ -21,6 +20,8 @@
  * with this program; if not, write to the Free Software Foundation, Inc.,
  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -48,6 +49,7 @@ class ApiPageSet extends ApiQueryBase {
        private $mMissingPageIDs, $mRedirectTitles, $mSpecialTitles;
        private $mNormalizedTitles, $mInterwikiTitles;
        private $mResolveRedirects, $mPendingRedirectIDs;
+       private $mConvertTitles, $mConvertedTitles;
        private $mGoodRevIDs, $mMissingRevIDs;
        private $mFakePageId;
 
@@ -58,7 +60,7 @@ class ApiPageSet extends ApiQueryBase {
         * @param $query ApiQuery
         * @param $resolveRedirects bool Whether redirects should be resolved
         */
-       public function __construct( $query, $resolveRedirects = false ) {
+       public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) {
                parent::__construct( $query, 'query' );
 
                $this->mAllPages = array();
@@ -80,6 +82,9 @@ class ApiPageSet extends ApiQueryBase {
                        $this->mPendingRedirectIDs = array();
                }
 
+               $this->mConvertTitles = $convertTitles;
+               $this->mConvertedTitles = array();
+
                $this->mFakePageId = - 1;
        }
 
@@ -222,6 +227,15 @@ class ApiPageSet extends ApiQueryBase {
                return $this->mNormalizedTitles;
        }
 
+       /**
+        * Get a list of title conversions - maps a title to its converted
+        * version.
+        * @return array raw_prefixed_title (string) => prefixed_title (string)
+        */
+       public function getConvertedTitles() {
+               return $this->mConvertedTitles;
+       }
+
        /**
         * Get a list of interwiki titles - maps a title to its interwiki
         * prefix.
@@ -246,7 +260,7 @@ class ApiPageSet extends ApiQueryBase {
        public function getMissingRevisionIDs() {
                return $this->mMissingRevIDs;
        }
-       
+
        /**
         * Get the list of titles with negative namespace
         * @return array Title
@@ -333,11 +347,11 @@ class ApiPageSet extends ApiQueryBase {
        /**
         * Populate this PageSet from a rowset returned from the database
         * @param $db Database object
-        * @param $queryResult Query result object
+        * @param $queryResult ResultWrapper Query result object
         */
        public function populateFromQueryResult( $db, $queryResult ) {
                $this->profileIn();
-               $this->initFromQueryResult( $db, $queryResult );
+               $this->initFromQueryResult( $queryResult );
                $this->profileOut();
        }
 
@@ -416,7 +430,7 @@ class ApiPageSet extends ApiQueryBase {
                $this->profileDBOut();
 
                // Hack: get the ns:titles stored in array(ns => array(titles)) format
-               $this->initFromQueryResult( $db, $res, $linkBatch->data, true ); // process Titles
+               $this->initFromQueryResult( $res, $linkBatch->data, true ); // process Titles
 
                // Resolve any found redirects
                $this->resolvePendingRedirects();
@@ -444,7 +458,7 @@ class ApiPageSet extends ApiQueryBase {
                $this->profileDBOut();
 
                $remaining = array_flip( $pageids );
-               $this->initFromQueryResult( $db, $res, $remaining, false );     // process PageIDs
+               $this->initFromQueryResult( $res, $remaining, false );  // process PageIDs
 
                // Resolve any found redirects
                $this->resolvePendingRedirects();
@@ -453,15 +467,14 @@ class ApiPageSet extends ApiQueryBase {
        /**
         * Iterate through the result of the query on 'page' table,
         * and for each row create and store title object and save any extra fields requested.
-        * @param $db Database
-        * @param $res DB Query result
+        * @param $res ResultWrapper DB Query result
         * @param $remaining array of either pageID or ns/title elements (optional).
         *        If given, any missing items will go to $mMissingPageIDs and $mMissingTitles
         * @param $processTitles bool Must be provided together with $remaining.
         *        If true, treat $remaining as an array of [ns][title]
         *        If false, treat it as an array of [pageIDs]
         */
-       private function initFromQueryResult( $db, $res, &$remaining = null, $processTitles = null ) {
+       private function initFromQueryResult( $res, &$remaining = null, $processTitles = null ) {
                if ( !is_null( $remaining ) && is_null( $processTitles ) ) {
                        ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
                }
@@ -487,7 +500,7 @@ class ApiPageSet extends ApiQueryBase {
                        if ( $processTitles ) {
                                // The remaining titles in $remaining are non-existent pages
                                foreach ( $remaining as $ns => $dbkeys ) {
-                                       foreach ( $dbkeys as $dbkey => $unused ) {
+                                       foreach ( array_keys( $dbkeys ) as $dbkey ) {
                                                $title = Title::makeTitle( $ns, $dbkey );
                                                $this->mAllPages[$ns][$dbkey] = $this->mFakePageId;
                                                $this->mMissingTitles[$this->mFakePageId] = $title;
@@ -575,7 +588,7 @@ class ApiPageSet extends ApiQueryBase {
                                $this->profileDBOut();
 
                                // Hack: get the ns:titles stored in array(ns => array(titles)) format
-                               $this->initFromQueryResult( $db, $res, $linkBatch->data, true );
+                               $this->initFromQueryResult( $res, $linkBatch->data, true );
                        }
                }
        }
@@ -653,16 +666,31 @@ class ApiPageSet extends ApiQueryBase {
                                $this->mFakePageId--;
                                continue; // There's nothing else we can do
                        }
+                       $unconvertedTitle = $titleObj->getPrefixedText();
+                       $titleWasConverted = false;
                        $iw = $titleObj->getInterwiki();
                        if ( strval( $iw ) !== '' ) {
                                // This title is an interwiki link.
                                $this->mInterwikiTitles[$titleObj->getPrefixedText()] = $iw;
                        } else {
+                               // Variants checking
+                               global $wgContLang;
+                               if ( $this->mConvertTitles &&
+                                               count( $wgContLang->getVariants() ) > 1  &&
+                                               !$titleObj->exists() ) {
+                                       // Language::findVariantLink will modify titleObj into
+                                       // the canonical variant if possible
+                                       $wgContLang->findVariantLink( $title, $titleObj );
+                                       $titleWasConverted = $unconvertedTitle !== $titleObj->getPrefixedText();
+                               }
+
                                if ( $titleObj->getNamespace() < 0 ) {
+                                       // Handle Special and Media pages
                                        $titleObj = $titleObj->fixSpecialName();
                                        $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
                                        $this->mFakePageId--;
                                } else {
+                                       // Regular page
                                        $linkBatch->addObj( $titleObj );
                                }
                        }
@@ -672,7 +700,9 @@ class ApiPageSet extends ApiQueryBase {
                        // titles with the originally requested when e.g. the
                        // namespace is localized or the capitalization is
                        // different
-                       if ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
+                       if ( $titleWasConverted ) {
+                               $this->mConvertedTitles[$title] = $titleObj->getPrefixedText();
+                       } elseif ( is_string( $title ) && $title !== $titleObj->getPrefixedText() ) {
                                $this->mNormalizedTitles[$title] = $titleObj->getPrefixedText();
                        }
                }