Followup to r69776: cache result of extractRequestParams() because it gets called...
[mediawiki.git] / includes / api / ApiQuery.php
blobcb7080df5c24e44da37f29ff337c9c67a9882ca3
1 <?php
3 /**
4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiBase.php' );
31 /**
32 * This is the main query class. It behaves similar to ApiMain: based on the
33 * parameters given, it will create a list of titles to work on (an ApiPageSet
34 * object), instantiate and execute various property/list/meta modules, and
35 * assemble all resulting data into a single ApiResult object.
37 * In generator mode, a generator will be executed first to populate a second
38 * ApiPageSet object, and that object will be used for all subsequent modules.
40 * @ingroup API
42 class ApiQuery extends ApiBase {
44 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
45 private $mPageSet;
46 private $params;
48 private $mQueryPropModules = array(
49 'info' => 'ApiQueryInfo',
50 'revisions' => 'ApiQueryRevisions',
51 'links' => 'ApiQueryLinks',
52 'iwlinks' => 'ApiQueryIWLinks',
53 'langlinks' => 'ApiQueryLangLinks',
54 'images' => 'ApiQueryImages',
55 'imageinfo' => 'ApiQueryImageInfo',
56 'templates' => 'ApiQueryLinks',
57 'categories' => 'ApiQueryCategories',
58 'extlinks' => 'ApiQueryExternalLinks',
59 'categoryinfo' => 'ApiQueryCategoryInfo',
60 'duplicatefiles' => 'ApiQueryDuplicateFiles',
63 private $mQueryListModules = array(
64 'allimages' => 'ApiQueryAllimages',
65 'allpages' => 'ApiQueryAllpages',
66 'alllinks' => 'ApiQueryAllLinks',
67 'allcategories' => 'ApiQueryAllCategories',
68 'allusers' => 'ApiQueryAllUsers',
69 'backlinks' => 'ApiQueryBacklinks',
70 'blocks' => 'ApiQueryBlocks',
71 'categorymembers' => 'ApiQueryCategoryMembers',
72 'deletedrevs' => 'ApiQueryDeletedrevs',
73 'embeddedin' => 'ApiQueryBacklinks',
74 'filearchive' => 'ApiQueryFilearchive',
75 'imageusage' => 'ApiQueryBacklinks',
76 'iwbacklinks' => 'ApiQueryIWBacklinks',
77 'logevents' => 'ApiQueryLogEvents',
78 'recentchanges' => 'ApiQueryRecentChanges',
79 'search' => 'ApiQuerySearch',
80 'tags' => 'ApiQueryTags',
81 'usercontribs' => 'ApiQueryContributions',
82 'watchlist' => 'ApiQueryWatchlist',
83 'watchlistraw' => 'ApiQueryWatchlistRaw',
84 'exturlusage' => 'ApiQueryExtLinksUsage',
85 'users' => 'ApiQueryUsers',
86 'random' => 'ApiQueryRandom',
87 'protectedtitles' => 'ApiQueryProtectedTitles',
90 private $mQueryMetaModules = array(
91 'siteinfo' => 'ApiQuerySiteinfo',
92 'userinfo' => 'ApiQueryUserInfo',
93 'allmessages' => 'ApiQueryAllmessages',
96 private $mSlaveDB = null;
97 private $mNamedDB = array();
99 public function __construct( $main, $action ) {
100 parent::__construct( $main, $action );
102 // Allow custom modules to be added in LocalSettings.php
103 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
104 self::appendUserModules( $this->mQueryPropModules, $wgAPIPropModules );
105 self::appendUserModules( $this->mQueryListModules, $wgAPIListModules );
106 self::appendUserModules( $this->mQueryMetaModules, $wgAPIMetaModules );
108 $this->mPropModuleNames = array_keys( $this->mQueryPropModules );
109 $this->mListModuleNames = array_keys( $this->mQueryListModules );
110 $this->mMetaModuleNames = array_keys( $this->mQueryMetaModules );
112 // Allow the entire list of modules at first,
113 // but during module instantiation check if it can be used as a generator.
114 $this->mAllowedGenerators = array_merge( $this->mListModuleNames, $this->mPropModuleNames );
118 * Helper function to append any add-in modules to the list
119 * @param $modules array Module array
120 * @param $newModules array Module array to add to $modules
122 private static function appendUserModules( &$modules, $newModules ) {
123 if ( is_array( $newModules ) ) {
124 foreach ( $newModules as $moduleName => $moduleClass ) {
125 $modules[$moduleName] = $moduleClass;
131 * Gets a default slave database connection object
132 * @return Database
134 public function getDB() {
135 if ( !isset( $this->mSlaveDB ) ) {
136 $this->profileDBIn();
137 $this->mSlaveDB = wfGetDB( DB_SLAVE, 'api' );
138 $this->profileDBOut();
140 return $this->mSlaveDB;
144 * Get the query database connection with the given name.
145 * If no such connection has been requested before, it will be created.
146 * Subsequent calls with the same $name will return the same connection
147 * as the first, regardless of the values of $db and $groups
148 * @param $name string Name to assign to the database connection
149 * @param $db int One of the DB_* constants
150 * @param $groups array Query groups
151 * @return Database
153 public function getNamedDB( $name, $db, $groups ) {
154 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
155 $this->profileDBIn();
156 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
157 $this->profileDBOut();
159 return $this->mNamedDB[$name];
163 * Gets the set of pages the user has requested (or generated)
164 * @return ApiPageSet
166 public function getPageSet() {
167 return $this->mPageSet;
171 * Get the array mapping module names to class names
172 * @return array(modulename => classname)
174 function getModules() {
175 return array_merge( $this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules );
179 * Get whether the specified module is a prop, list or a meta query module
180 * @param $moduleName string Name of the module to find type for
181 * @return mixed string or null
183 function getModuleType( $moduleName ) {
184 if ( array_key_exists ( $moduleName, $this->mQueryPropModules ) ) {
185 return 'prop';
188 if ( array_key_exists ( $moduleName, $this->mQueryListModules ) ) {
189 return 'list';
192 if ( array_key_exists ( $moduleName, $this->mQueryMetaModules ) ) {
193 return 'meta';
196 return null;
199 public function getCustomPrinter() {
200 // If &exportnowrap is set, use the raw formatter
201 if ( $this->getParameter( 'export' ) &&
202 $this->getParameter( 'exportnowrap' ) )
204 return new ApiFormatRaw( $this->getMain(),
205 $this->getMain()->createPrinterByName( 'xml' ) );
206 } else {
207 return null;
212 * Query execution happens in the following steps:
213 * #1 Create a PageSet object with any pages requested by the user
214 * #2 If using a generator, execute it to get a new ApiPageSet object
215 * #3 Instantiate all requested modules.
216 * This way the PageSet object will know what shared data is required,
217 * and minimize DB calls.
218 * #4 Output all normalization and redirect resolution information
219 * #5 Execute all requested modules
221 public function execute() {
222 $this->params = $this->extractRequestParams();
223 $this->redirects = $this->params['redirects'];
224 $this->convertTitles = $this->params['converttitles'];
226 // Create PageSet
227 $this->mPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
229 // Instantiate requested modules
230 $modules = array();
231 $this->instantiateModules( $modules, 'prop', $this->mQueryPropModules );
232 $this->instantiateModules( $modules, 'list', $this->mQueryListModules );
233 $this->instantiateModules( $modules, 'meta', $this->mQueryMetaModules );
235 $cacheMode = 'public';
237 // If given, execute generator to substitute user supplied data with generated data.
238 if ( isset( $this->params['generator'] ) ) {
239 $generator = $this->newGenerator( $this->params['generator'] );
240 $params = $generator->extractRequestParams();
241 $cacheMode = $this->mergeCacheMode( $cacheMode,
242 $generator->getCacheMode( $params ) );
243 $this->executeGeneratorModule( $generator, $modules );
244 } else {
245 // Append custom fields and populate page/revision information
246 $this->addCustomFldsToPageSet( $modules, $this->mPageSet );
247 $this->mPageSet->execute();
250 // Record page information (title, namespace, if exists, etc)
251 $this->outputGeneralPageInfo();
253 // Execute all requested modules.
254 foreach ( $modules as $module ) {
255 $params = $module->extractRequestParams();
256 $cacheMode = $this->mergeCacheMode(
257 $cacheMode, $module->getCacheMode( $params ) );
258 $module->profileIn();
259 $module->execute();
260 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
261 $module->profileOut();
264 // Set the cache mode
265 $this->getMain()->setCacheMode( $cacheMode );
269 * Update a cache mode string, applying the cache mode of a new module to it.
270 * The cache mode may increase in the level of privacy, but public modules
271 * added to private data do not decrease the level of privacy.
273 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
274 if ( $modCacheMode === 'anon-public-user-private' ) {
275 if ( $cacheMode !== 'private' ) {
276 $cacheMode = 'anon-public-user-private';
278 } elseif ( $modCacheMode === 'public' ) {
279 // do nothing, if it's public already it will stay public
280 } else { // private
281 $cacheMode = 'private';
283 return $cacheMode;
287 * Query modules may optimize data requests through the $this->getPageSet() object
288 * by adding extra fields from the page table.
289 * This function will gather all the extra request fields from the modules.
290 * @param $modules array of module objects
291 * @param $pageSet ApiPageSet
293 private function addCustomFldsToPageSet( $modules, $pageSet ) {
294 // Query all requested modules.
295 foreach ( $modules as $module ) {
296 $module->requestExtraData( $pageSet );
301 * Create instances of all modules requested by the client
302 * @param $modules array to append instatiated modules to
303 * @param $param string Parameter name to read modules from
304 * @param $moduleList array(modulename => classname)
306 private function instantiateModules( &$modules, $param, $moduleList ) {
307 $list = @$this->params[$param];
308 if ( !is_null ( $list ) ) {
309 foreach ( $list as $moduleName ) {
310 $modules[] = new $moduleList[$moduleName] ( $this, $moduleName );
316 * Appends an element for each page in the current pageSet with the
317 * most general information (id, title), plus any title normalizations
318 * and missing or invalid title/pageids/revids.
320 private function outputGeneralPageInfo() {
321 $pageSet = $this->getPageSet();
322 $result = $this->getResult();
324 // We don't check for a full result set here because we can't be adding
325 // more than 380K. The maximum revision size is in the megabyte range,
326 // and the maximum result size must be even higher than that.
328 // Title normalizations
329 $normValues = array();
330 foreach ( $pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
331 $normValues[] = array(
332 'from' => $rawTitleStr,
333 'to' => $titleStr
337 if ( count( $normValues ) ) {
338 $result->setIndexedTagName( $normValues, 'n' );
339 $result->addValue( 'query', 'normalized', $normValues );
342 // Title conversions
343 $convValues = array();
344 foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) {
345 $convValues[] = array(
346 'from' => $rawTitleStr,
347 'to' => $titleStr
351 if ( count( $convValues ) ) {
352 $result->setIndexedTagName( $convValues, 'c' );
353 $result->addValue( 'query', 'converted', $convValues );
356 // Interwiki titles
357 $intrwValues = array();
358 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
359 $intrwValues[] = array(
360 'title' => $rawTitleStr,
361 'iw' => $interwikiStr
365 if ( count( $intrwValues ) ) {
366 $result->setIndexedTagName( $intrwValues, 'i' );
367 $result->addValue( 'query', 'interwiki', $intrwValues );
370 // Show redirect information
371 $redirValues = array();
372 foreach ( $pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo ) {
373 $redirValues[] = array(
374 'from' => strval( $titleStrFrom ),
375 'to' => $titleStrTo
379 if ( count( $redirValues ) ) {
380 $result->setIndexedTagName( $redirValues, 'r' );
381 $result->addValue( 'query', 'redirects', $redirValues );
385 // Missing revision elements
387 $missingRevIDs = $pageSet->getMissingRevisionIDs();
388 if ( count( $missingRevIDs ) ) {
389 $revids = array();
390 foreach ( $missingRevIDs as $revid ) {
391 $revids[$revid] = array(
392 'revid' => $revid
395 $result->setIndexedTagName( $revids, 'rev' );
396 $result->addValue( 'query', 'badrevids', $revids );
400 // Page elements
402 $pages = array();
404 // Report any missing titles
405 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
406 $vals = array();
407 ApiQueryBase::addTitleInfo( $vals, $title );
408 $vals['missing'] = '';
409 $pages[$fakeId] = $vals;
411 // Report any invalid titles
412 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
413 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
415 // Report any missing page ids
416 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
417 $pages[$pageid] = array(
418 'pageid' => $pageid,
419 'missing' => ''
422 // Report special pages
423 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
424 $vals = array();
425 ApiQueryBase::addTitleInfo( $vals, $title );
426 $vals['special'] = '';
427 if ( $title->getNamespace() == NS_SPECIAL &&
428 !SpecialPage::exists( $title->getText() ) ) {
429 $vals['missing'] = '';
430 } elseif ( $title->getNamespace() == NS_MEDIA &&
431 !wfFindFile( $title ) ) {
432 $vals['missing'] = '';
434 $pages[$fakeId] = $vals;
437 // Output general page information for found titles
438 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
439 $vals = array();
440 $vals['pageid'] = $pageid;
441 ApiQueryBase::addTitleInfo( $vals, $title );
442 $pages[$pageid] = $vals;
445 if ( count( $pages ) ) {
446 if ( $this->params['indexpageids'] ) {
447 $pageIDs = array_keys( $pages );
448 // json treats all map keys as strings - converting to match
449 $pageIDs = array_map( 'strval', $pageIDs );
450 $result->setIndexedTagName( $pageIDs, 'id' );
451 $result->addValue( 'query', 'pageids', $pageIDs );
454 $result->setIndexedTagName( $pages, 'page' );
455 $result->addValue( 'query', 'pages', $pages );
457 if ( $this->params['export'] ) {
458 $exporter = new WikiExporter( $this->getDB() );
459 // WikiExporter writes to stdout, so catch its
460 // output with an ob
461 ob_start();
462 $exporter->openStream();
463 foreach ( @$pageSet->getGoodTitles() as $title ) {
464 if ( $title->userCanRead() ) {
465 $exporter->pageByTitle( $title );
468 $exporter->closeStream();
469 $exportxml = ob_get_contents();
470 ob_end_clean();
472 // Don't check the size of exported stuff
473 // It's not continuable, so it would cause more
474 // problems than it'd solve
475 $result->disableSizeCheck();
476 if ( $this->params['exportnowrap'] ) {
477 $result->reset();
478 // Raw formatter will handle this
479 $result->addValue( null, 'text', $exportxml );
480 $result->addValue( null, 'mime', 'text/xml' );
481 } else {
482 $r = array();
483 ApiResult::setContent( $r, $exportxml );
484 $result->addValue( 'query', 'export', $r );
486 $result->enableSizeCheck();
491 * Create a generator object of the given type and return it
492 * @param $generatorName string Module name
494 public function newGenerator( $generatorName ) {
495 // Find class that implements requested generator
496 if ( isset( $this->mQueryListModules[$generatorName] ) ) {
497 $className = $this->mQueryListModules[$generatorName];
498 } elseif ( isset( $this->mQueryPropModules[$generatorName] ) ) {
499 $className = $this->mQueryPropModules[$generatorName];
500 } else {
501 ApiBase::dieDebug( __METHOD__, "Unknown generator=$generatorName" );
503 $generator = new $className ( $this, $generatorName );
504 if ( !$generator instanceof ApiQueryGeneratorBase ) {
505 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
507 $generator->setGeneratorMode();
508 return $generator;
512 * For generator mode, execute generator, and use its output as new
513 * ApiPageSet
514 * @param $generatorName string Module name
515 * @param $modules array of module objects
517 protected function executeGeneratorModule( $generator, $modules ) {
518 // Generator results
519 $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
521 // Add any additional fields modules may need
522 $generator->requestExtraData( $this->mPageSet );
523 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
525 // Populate page information with the original user input
526 $this->mPageSet->execute();
528 // populate resultPageSet with the generator output
529 $generator->profileIn();
530 $generator->executeGenerator( $resultPageSet );
531 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
532 $resultPageSet->finishPageSetGeneration();
533 $generator->profileOut();
535 // Swap the resulting pageset back in
536 $this->mPageSet = $resultPageSet;
539 public function getAllowedParams() {
540 return array(
541 'prop' => array(
542 ApiBase::PARAM_ISMULTI => true,
543 ApiBase::PARAM_TYPE => $this->mPropModuleNames
545 'list' => array(
546 ApiBase::PARAM_ISMULTI => true,
547 ApiBase::PARAM_TYPE => $this->mListModuleNames
549 'meta' => array(
550 ApiBase::PARAM_ISMULTI => true,
551 ApiBase::PARAM_TYPE => $this->mMetaModuleNames
553 'generator' => array(
554 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
556 'redirects' => false,
557 'converttitles' => false,
558 'indexpageids' => false,
559 'export' => false,
560 'exportnowrap' => false,
565 * Override the parent to generate help messages for all available query modules.
566 * @return string
568 public function makeHelpMsg() {
569 $msg = '';
571 // Make sure the internal object is empty
572 // (just in case a sub-module decides to optimize during instantiation)
573 $this->mPageSet = null;
574 $this->mAllowedGenerators = array(); // Will be repopulated
576 $astriks = str_repeat( '--- ', 8 );
577 $astriks2 = str_repeat( '*** ', 10 );
578 $msg .= "\n$astriks Query: Prop $astriks\n\n";
579 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules, 'prop' );
580 $msg .= "\n$astriks Query: List $astriks\n\n";
581 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules, 'list' );
582 $msg .= "\n$astriks Query: Meta $astriks\n\n";
583 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules, 'meta' );
584 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
586 // Perform the base call last because the $this->mAllowedGenerators
587 // will be updated inside makeHelpMsgHelper()
588 // Use parent to make default message for the query module
589 $msg = parent::makeHelpMsg() . $msg;
591 return $msg;
595 * For all modules in $moduleList, generate help messages and join them together
596 * @param $moduleList array(modulename => classname)
597 * @param $paramName string Parameter name
598 * @return string
600 private function makeHelpMsgHelper( $moduleList, $paramName ) {
601 $moduleDescriptions = array();
603 foreach ( $moduleList as $moduleName => $moduleClass ) {
604 $module = new $moduleClass ( $this, $moduleName, null );
606 $msg = ApiMain::makeHelpMsgHeader( $module, $paramName );
607 $msg2 = $module->makeHelpMsg();
608 if ( $msg2 !== false ) {
609 $msg .= $msg2;
611 if ( $module instanceof ApiQueryGeneratorBase ) {
612 $this->mAllowedGenerators[] = $moduleName;
613 $msg .= "Generator:\n This module may be used as a generator\n";
615 $moduleDescriptions[] = $msg;
618 return implode( "\n", $moduleDescriptions );
622 * Override to add extra parameters from PageSet
623 * @return string
625 public function makeHelpMsgParameters() {
626 $psModule = new ApiPageSet( $this );
627 return $psModule->makeHelpMsgParameters() . parent::makeHelpMsgParameters();
630 public function shouldCheckMaxlag() {
631 return true;
634 public function getParamDescription() {
635 return array(
636 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
637 'list' => 'Which lists to get. Module help is available below',
638 'meta' => 'Which metadata to get about the site. Module help is available below',
639 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
640 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
641 'redirects' => 'Automatically resolve redirects',
642 'converttitles' => "Convert titles to other variants if necessary. Only works if the wiki's content language supports variant conversion.",
643 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
644 'export' => 'Export the current revisions of all given or generated pages',
645 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
649 public function getDescription() {
650 return array(
651 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
652 'and is loosely based on the old query.php interface.',
653 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
657 public function getPossibleErrors() {
658 return array_merge( parent::getPossibleErrors(), array(
659 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
660 ) );
663 protected function getExamples() {
664 return array(
665 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
666 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
670 public function getVersion() {
671 $psModule = new ApiPageSet( $this );
672 $vers = array();
673 $vers[] = __CLASS__ . ': $Id$';
674 $vers[] = $psModule->getVersion();
675 return $vers;