Checking permissions for $wgUser while doing an edit with another user is not a good...
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
index fee4750..d13df81 100644 (file)
@@ -1,10 +1,9 @@
 <?php
-
 /**
- * Created on Sep 24, 2006
- *
  * API for MediaWiki 1.8+
  *
+ * Created on Sep 24, 2006
+ *
  * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
  *
  * This program is free software; you can redistribute it and/or modify
  *
  * You should have received a copy of the GNU General Public License along
  * with this program; if not, write to the Free Software Foundation, Inc.,
- * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @file
  */
 
 if ( !defined( 'MEDIAWIKI' ) ) {
@@ -45,9 +46,10 @@ class ApiPageSet extends ApiQueryBase {
 
        private $mAllPages; // [ns][dbkey] => page_id or negative when missing
        private $mTitles, $mGoodTitles, $mMissingTitles, $mInvalidTitles;
-       private $mMissingPageIDs, $mRedirectTitles;
+       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();
@@ -72,6 +74,7 @@ class ApiPageSet extends ApiQueryBase {
                $this->mInterwikiTitles = array();
                $this->mGoodRevIDs = array();
                $this->mMissingRevIDs = array();
+               $this->mSpecialTitles = array();
 
                $this->mRequestedPageFields = array();
                $this->mResolveRedirects = $resolveRedirects;
@@ -79,7 +82,10 @@ class ApiPageSet extends ApiQueryBase {
                        $this->mPendingRedirectIDs = array();
                }
 
-               $this->mFakePageId = -1;
+               $this->mConvertTitles = $convertTitles;
+               $this->mConvertedTitles = array();
+
+               $this->mFakePageId = - 1;
        }
 
        /**
@@ -221,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,6 +261,14 @@ class ApiPageSet extends ApiQueryBase {
                return $this->mMissingRevIDs;
        }
 
+       /**
+        * Get the list of titles with negative namespace
+        * @return array Title
+        */
+       public function getSpecialTitles() {
+               return $this->mSpecialTitles;
+       }
+
        /**
         * Returns the number of revisions (requested with revids= parameter)\
         * @return int
@@ -324,7 +347,7 @@ 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();
@@ -445,7 +468,7 @@ 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.
@@ -457,7 +480,7 @@ class ApiPageSet extends ApiQueryBase {
                        ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
                }
 
-               while ( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $pageId = intval( $row->page_id );
 
                        // Remove found page from the list of remaining items
@@ -472,14 +495,13 @@ class ApiPageSet extends ApiQueryBase {
                        // Store any extra fields requested by modules
                        $this->processDbRow( $row );
                }
-               $db->freeResult( $res );
 
                if ( isset( $remaining ) ) {
                        // Any items left in the $remaining list are added as missing
                        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;
@@ -520,14 +542,13 @@ class ApiPageSet extends ApiQueryBase {
                // Get pageIDs data from the `page` table
                $this->profileDBIn();
                $res = $db->select( $tables, $fields, $where,  __METHOD__ );
-               while ( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $revid = intval( $row->rev_id );
                        $pageid = intval( $row->rev_page );
                        $this->mGoodRevIDs[$revid] = $pageid;
                        $pageids[$pageid] = '';
                        unset( $remaining[$revid] );
                }
-               $db->freeResult( $res );
                $this->profileDBOut();
 
                $this->mMissingRevIDs = array_keys( $remaining );
@@ -596,7 +617,7 @@ class ApiPageSet extends ApiQueryBase {
                );
                $this->profileDBOut();
 
-               while ( $row = $db->fetchObject( $res ) ) {
+               foreach ( $res as $row ) {
                        $rdfrom = intval( $row->rd_from );
                        $from = $this->mPendingRedirectIDs[$rdfrom]->getPrefixedText();
                        $to = Title::makeTitle( $row->rd_namespace, $row->rd_title )->getPrefixedText();
@@ -606,7 +627,7 @@ class ApiPageSet extends ApiQueryBase {
                        }
                        $this->mRedirectTitles[$from] = $to;
                }
-               $db->freeResult( $res );
+
                if ( $this->mPendingRedirectIDs ) {
                        // We found pages that aren't in the redirect table
                        // Add them
@@ -646,15 +667,32 @@ 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 {
-                               // Validation
+                               // 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 ) {
-                                       $this->setWarning( 'No support for special pages has been implemented' );
+                                       // Handle Special and Media pages
+                                       $titleObj = $titleObj->fixSpecialName();
+                                       $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
+                                       $this->mFakePageId--;
                                } else {
+                                       // Regular page
                                        $linkBatch->addObj( $titleObj );
                                }
                        }
@@ -664,7 +702,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();
                        }
                }