Merge "Add support for PHP7 random_bytes in favor of mcrypt_create_iv"
[lhc/web/wiklou.git] / includes / api / ApiPageSet.php
index 1a509c5..7d16af8 100644 (file)
@@ -24,6 +24,7 @@
  * @file
  */
 use MediaWiki\MediaWikiServices;
+use Wikimedia\Rdbms\ResultWrapper;
 
 /**
  * This class contains a list of pages that the client has requested.
@@ -64,6 +65,7 @@ class ApiPageSet extends ApiBase {
        private $mMissingPageIDs = [];
        private $mRedirectTitles = [];
        private $mSpecialTitles = [];
+       private $mAllSpecials = []; // separate from mAllPages to avoid breaking getAllTitlesByNamespace()
        private $mNormalizedTitles = [];
        private $mInterwikiTitles = [];
        /** @var Title[] */
@@ -155,10 +157,10 @@ class ApiPageSet extends ApiBase {
                        }
                        $generator = $dbSource->getModuleManager()->getModule( $generatorName, null, true );
                        if ( $generator === null ) {
-                               $this->dieUsage( 'Unknown generator=' . $generatorName, 'badgenerator' );
+                               $this->dieWithError( [ 'apierror-badgenerator-unknown', $generatorName ], 'badgenerator' );
                        }
                        if ( !$generator instanceof ApiQueryGeneratorBase ) {
-                               $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
+                               $this->dieWithError( [ 'apierror-badgenerator-notgenerator', $generatorName ], 'badgenerator' );
                        }
                        // Create a temporary pageset to store generator's output,
                        // add any additional fields generator may need, and execute pageset to populate titles/pageids
@@ -174,7 +176,10 @@ class ApiPageSet extends ApiBase {
                        // populate this pageset with the generator output
                        if ( !$isDryRun ) {
                                $generator->executeGenerator( $this );
-                               Hooks::run( 'APIQueryGeneratorAfterExecute', [ &$generator, &$this ] );
+
+                               // Avoid PHP 7.1 warning of passing $this by reference
+                               $apiModule = $this;
+                               Hooks::run( 'APIQueryGeneratorAfterExecute', [ &$generator, &$apiModule ] );
                        } else {
                                // Prevent warnings from being reported on these parameters
                                $main = $this->getMain();
@@ -194,13 +199,27 @@ class ApiPageSet extends ApiBase {
                        }
                        if ( isset( $this->mParams['pageids'] ) ) {
                                if ( isset( $dataSource ) ) {
-                                       $this->dieUsage( "Cannot use 'pageids' at the same time as '$dataSource'", 'multisource' );
+                                       $this->dieWithError(
+                                               [
+                                                       'apierror-invalidparammix-cannotusewith',
+                                                       $this->encodeParamName( 'pageids' ),
+                                                       $this->encodeParamName( $dataSource )
+                                               ],
+                                               'multisource'
+                                       );
                                }
                                $dataSource = 'pageids';
                        }
                        if ( isset( $this->mParams['revids'] ) ) {
                                if ( isset( $dataSource ) ) {
-                                       $this->dieUsage( "Cannot use 'revids' at the same time as '$dataSource'", 'multisource' );
+                                       $this->dieWithError(
+                                               [
+                                                       'apierror-invalidparammix-cannotusewith',
+                                                       $this->encodeParamName( 'revids' ),
+                                                       $this->encodeParamName( $dataSource )
+                                               ],
+                                               'multisource'
+                                       );
                                }
                                $dataSource = 'revids';
                        }
@@ -216,9 +235,7 @@ class ApiPageSet extends ApiBase {
                                                break;
                                        case 'revids':
                                                if ( $this->mResolveRedirects ) {
-                                                       $this->setWarning( 'Redirect resolution cannot be used ' .
-                                                               'together with the revids= parameter. Any redirects ' .
-                                                               'the revids= point to have not been resolved.' );
+                                                       $this->addWarning( 'apiwarn-redirectsandrevids' );
                                                }
                                                $this->mResolveRedirects = false;
                                                $this->initFromRevIDs( $this->mParams['revids'] );
@@ -1046,7 +1063,7 @@ class ApiPageSet extends ApiBase {
         * @return LinkBatch
         */
        private function getRedirectTargets() {
-               $lb = new LinkBatch();
+               $titlesToResolve = [];
                $db = $this->getDB();
 
                $res = $db->select(
@@ -1073,8 +1090,8 @@ class ApiPageSet extends ApiBase {
                        unset( $this->mPendingRedirectIDs[$rdfrom] );
                        if ( $to->isExternal() ) {
                                $this->mInterwikiTitles[$to->getPrefixedText()] = $to->getInterwiki();
-                       } elseif ( !isset( $this->mAllPages[$row->rd_namespace][$row->rd_title] ) ) {
-                               $lb->add( $row->rd_namespace, $row->rd_title );
+                       } elseif ( !isset( $this->mAllPages[$to->getNamespace()][$to->getDBkey()] ) ) {
+                               $titlesToResolve[] = $to;
                        }
                        $this->mRedirectTitles[$from] = $to;
                }
@@ -1089,7 +1106,11 @@ class ApiPageSet extends ApiBase {
                                        // What the hell. Let's just ignore this
                                        continue;
                                }
-                               $lb->addObj( $rt );
+                               if ( $rt->isExternal() ) {
+                                       $this->mInterwikiTitles[$rt->getPrefixedText()] = $rt->getInterwiki();
+                               } elseif ( !isset( $this->mAllPages[$rt->getNamespace()][$rt->getDBkey()] ) ) {
+                                       $titlesToResolve[] = $rt;
+                               }
                                $from = $title->getPrefixedText();
                                $this->mResolvedRedirectTitles[$from] = $title;
                                $this->mRedirectTitles[$from] = $rt;
@@ -1097,7 +1118,7 @@ class ApiPageSet extends ApiBase {
                        }
                }
 
-               return $lb;
+               return $this->processTitlesArray( $titlesToResolve );
        }
 
        /**
@@ -1136,12 +1157,14 @@ class ApiPageSet extends ApiBase {
                                        $titleObj = Title::newFromTextThrow( $title, $this->mDefaultNamespace );
                                } catch ( MalformedTitleException $ex ) {
                                        // Handle invalid titles gracefully
-                                       $this->mAllPages[0][$title] = $this->mFakePageId;
-                                       $this->mInvalidTitles[$this->mFakePageId] = [
-                                               'title' => $title,
-                                               'invalidreason' => $ex->getMessage(),
-                                       ];
-                                       $this->mFakePageId--;
+                                       if ( !isset( $this->mAllPages[0][$title] ) ) {
+                                               $this->mAllPages[0][$title] = $this->mFakePageId;
+                                               $this->mInvalidTitles[$this->mFakePageId] = [
+                                                       'title' => $title,
+                                                       'invalidreason' => $this->getErrorFormatter()->formatException( $ex, [ 'bc' => true ] ),
+                                               ];
+                                               $this->mFakePageId--;
+                                       }
                                        continue; // There's nothing else we can do
                                }
                        } else {
@@ -1169,8 +1192,13 @@ class ApiPageSet extends ApiBase {
                                if ( $titleObj->getNamespace() < 0 ) {
                                        // Handle Special and Media pages
                                        $titleObj = $titleObj->fixSpecialName();
-                                       $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
-                                       $this->mFakePageId--;
+                                       $ns = $titleObj->getNamespace();
+                                       $dbkey = $titleObj->getDBkey();
+                                       if ( !isset( $this->mAllSpecials[$ns][$dbkey] ) ) {
+                                               $this->mAllSpecials[$ns][$dbkey] = $this->mFakePageId;
+                                               $this->mSpecialTitles[$this->mFakePageId] = $titleObj;
+                                               $this->mFakePageId--;
+                                       }
                                } else {
                                        // Regular page
                                        $linkBatch->addObj( $titleObj );
@@ -1369,7 +1397,7 @@ class ApiPageSet extends ApiBase {
         * @return array
         */
        private static function getPositiveIntegers( $array ) {
-               // bug 25734 API: possible issue with revids validation
+               // T27734 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 ) {
@@ -1412,13 +1440,7 @@ class ApiPageSet extends ApiBase {
                                ApiBase::PARAM_DFLT => false,
                                ApiBase::PARAM_HELP_MSG => [
                                        'api-pageset-param-converttitles',
-                                       new DeferredStringifier(
-                                               function ( IContextSource $context ) {
-                                                       return $context->getLanguage()
-                                                               ->commaList( LanguageConverter::$languagesWithVariants );
-                                               },
-                                               $this
-                                       )
+                                       [ Message::listParam( LanguageConverter::$languagesWithVariants, 'text' ) ],
                                ],
                        ],
                ];