Merge "Revert "Adding sanity check to Title::isRedirect().""
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
index fdee6fe..598f9ad 100644 (file)
  * @file
  */
 
-if ( !defined( 'MEDIAWIKI' ) ) {
-       // Eclipse helper - will be ignored in production
-       require_once( 'ApiQueryBase.php' );
-}
-
 /**
  * This class contains a list of pages that the client has requested.
  * Initially, when the client passes in titles=, pageids=, or revisions=
@@ -57,8 +52,9 @@ class ApiPageSet extends ApiQueryBase {
 
        /**
         * Constructor
-        * @param $query ApiQuery
+        * @param $query ApiQueryBase
         * @param $resolveRedirects bool Whether redirects should be resolved
+        * @param $convertTitles bool
         */
        public function __construct( $query, $resolveRedirects = false, $convertTitles = false ) {
                parent::__construct( $query, 'query' );
@@ -346,7 +342,7 @@ class ApiPageSet extends ApiQueryBase {
 
        /**
         * Populate this PageSet from a rowset returned from the database
-        * @param $db Database object
+        * @param $db DatabaseBase object
         * @param $queryResult ResultWrapper Query result object
         */
        public function populateFromQueryResult( $db, $queryResult ) {
@@ -446,18 +442,24 @@ class ApiPageSet extends ApiQueryBase {
                }
 
                $pageids = array_map( 'intval', $pageids ); // paranoia
-               $set = array(
-                       'page_id' => $pageids
-               );
-               $db = $this->getDB();
+               $remaining = array_flip( $pageids );
 
-               // Get pageIDs data from the `page` table
-               $this->profileDBIn();
-               $res = $db->select( 'page', $this->getPageTableFields(), $set,
-                                       __METHOD__ );
-               $this->profileDBOut();
+               $pageids = self::getPositiveIntegers( $pageids );
+
+               $res = null;
+               if ( count( $pageids ) ) {
+                       $set = array(
+                               'page_id' => $pageids
+                       );
+                       $db = $this->getDB();
+
+                       // Get pageIDs data from the `page` table
+                       $this->profileDBIn();
+                       $res = $db->select( 'page', $this->getPageTableFields(), $set,
+                                               __METHOD__ );
+                       $this->profileDBOut();
+               }
 
-               $remaining = array_flip( $pageids );
                $this->initFromQueryResult( $res, $remaining, false );  // process PageIDs
 
                // Resolve any found redirects
@@ -479,20 +481,28 @@ class ApiPageSet extends ApiQueryBase {
                        ApiBase::dieDebug( __METHOD__, 'Missing $processTitles parameter when $remaining is provided' );
                }
 
-               foreach ( $res as $row ) {
-                       $pageId = intval( $row->page_id );
+               $usernames = array();
+               if ( $res ) {
+                       foreach ( $res as $row ) {
+                               $pageId = intval( $row->page_id );
+
+                               // Remove found page from the list of remaining items
+                               if ( isset( $remaining ) ) {
+                                       if ( $processTitles ) {
+                                               unset( $remaining[$row->page_namespace][$row->page_title] );
+                                       } else {
+                                               unset( $remaining[$pageId] );
+                                       }
+                               }
 
-                       // Remove found page from the list of remaining items
-                       if ( isset( $remaining ) ) {
-                               if ( $processTitles ) {
-                                       unset( $remaining[$row->page_namespace][$row->page_title] );
-                               } else {
-                                       unset( $remaining[$pageId] );
+                               // Store any extra fields requested by modules
+                               $this->processDbRow( $row );
+
+                               // Need gender information
+                               if( MWNamespace::hasGenderDistinction( $row->page_namespace ) ) {
+                                       $usernames[] = $row->page_title;
                                }
                        }
-
-                       // Store any extra fields requested by modules
-                       $this->processDbRow( $row );
                }
 
                if ( isset( $remaining ) ) {
@@ -506,6 +516,11 @@ class ApiPageSet extends ApiQueryBase {
                                                $this->mMissingTitles[$this->mFakePageId] = $title;
                                                $this->mFakePageId--;
                                                $this->mTitles[] = $title;
+
+                                               // need gender information
+                                               if( MWNamespace::hasGenderDistinction( $ns ) ) {
+                                                       $usernames[] = $dbkey;
+                                               }
                                        }
                                }
                        } else {
@@ -517,6 +532,10 @@ class ApiPageSet extends ApiQueryBase {
                                }
                        }
                }
+
+               // Get gender information
+               $genderCache = GenderCache::singleton();
+               $genderCache->doQuery( $usernames, __METHOD__ );
        }
 
        /**
@@ -534,21 +553,25 @@ class ApiPageSet extends ApiQueryBase {
                $pageids = array();
                $remaining = array_flip( $revids );
 
-               $tables = array( 'revision', 'page' );
-               $fields = array( 'rev_id', 'rev_page' );
-               $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
-
-               // Get pageIDs data from the `page` table
-               $this->profileDBIn();
-               $res = $db->select( $tables, $fields, $where,  __METHOD__ );
-               foreach ( $res as $row ) {
-                       $revid = intval( $row->rev_id );
-                       $pageid = intval( $row->rev_page );
-                       $this->mGoodRevIDs[$revid] = $pageid;
-                       $pageids[$pageid] = '';
-                       unset( $remaining[$revid] );
+               $revids = self::getPositiveIntegers( $revids );
+
+               if ( count( $revids ) ) {
+                       $tables = array( 'revision', 'page' );
+                       $fields = array( 'rev_id', 'rev_page' );
+                       $where = array( 'rev_id' => $revids, 'rev_page = page_id' );
+
+                       // Get pageIDs data from the `page` table
+                       $this->profileDBIn();
+                       $res = $db->select( $tables, $fields, $where,  __METHOD__ );
+                       foreach ( $res as $row ) {
+                               $revid = intval( $row->rev_id );
+                               $pageid = intval( $row->rev_page );
+                               $this->mGoodRevIDs[$revid] = $pageid;
+                               $pageids[$pageid] = '';
+                               unset( $remaining[$revid] );
+                       }
+                       $this->profileDBOut();
                }
-               $this->profileDBOut();
 
                $this->mMissingRevIDs = array_keys( $remaining );
 
@@ -632,8 +655,8 @@ class ApiPageSet extends ApiQueryBase {
                        // We found pages that aren't in the redirect table
                        // Add them
                        foreach ( $this->mPendingRedirectIDs as $id => $title ) {
-                               $article = new Article( $title );
-                               $rt = $article->insertRedirect();
+                               $page = WikiPage::factory( $title );
+                               $rt = $page->insertRedirect();
                                if ( !$rt ) {
                                        // What the hell. Let's just ignore this
                                        continue;
@@ -656,6 +679,9 @@ class ApiPageSet extends ApiQueryBase {
         * @return LinkBatch
         */
        private function processTitlesArray( $titles ) {
+               $genderCache = GenderCache::singleton();
+               $genderCache->doTitlesArray( $titles, __METHOD__ );
+
                $linkBatch = new LinkBatch();
 
                foreach ( $titles as $title ) {
@@ -711,7 +737,26 @@ class ApiPageSet extends ApiQueryBase {
                return $linkBatch;
        }
 
-       protected function getAllowedParams() {
+       /**
+        * Returns the input array of integers with all values < 0 removed
+        *
+        * @param $array array
+        * @return array
+        */
+       private static function getPositiveIntegers( $array ) {
+               // bug 25734 API: possible issue with revids validation
+               // It seems with a load of revision rows, MySQL gets upset
+               // Remove any < 0 integers, as they can't be valid
+               foreach( $array as $i => $int ) {
+                       if ( $int < 0 ) {
+                               unset( $array[$i] );
+                       }
+               }
+
+               return $array;
+       }
+
+       public function getAllowedParams() {
                return array(
                        'titles' => array(
                                ApiBase::PARAM_ISMULTI => true
@@ -727,7 +772,7 @@ class ApiPageSet extends ApiQueryBase {
                );
        }
 
-       protected function getParamDescription() {
+       public function getParamDescription() {
                return array(
                        'titles' => 'A list of titles to work on',
                        'pageids' => 'A list of page IDs to work on',