* changed display function for length to Linker::formatRevisionSize
[mediawiki.git] / includes / api / ApiQuery.php
blobdf59704f4645e5947f98779ead7ae5ee04fc23a2
1 <?php
2 /**
5 * Created on Sep 7, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
24 * @file
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
32 /**
33 * This is the main query class. It behaves similar to ApiMain: based on the
34 * parameters given, it will create a list of titles to work on (an ApiPageSet
35 * object), instantiate and execute various property/list/meta modules, and
36 * assemble all resulting data into a single ApiResult object.
38 * In generator mode, a generator will be executed first to populate a second
39 * ApiPageSet object, and that object will be used for all subsequent modules.
41 * @ingroup API
43 class ApiQuery extends ApiBase {
45 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
47 /**
48 * @var ApiPageSet
50 private $mPageSet;
52 private $params, $redirects, $convertTitles;
54 private $mQueryPropModules = array(
55 'info' => 'ApiQueryInfo',
56 'revisions' => 'ApiQueryRevisions',
57 'links' => 'ApiQueryLinks',
58 'iwlinks' => 'ApiQueryIWLinks',
59 'langlinks' => 'ApiQueryLangLinks',
60 'images' => 'ApiQueryImages',
61 'imageinfo' => 'ApiQueryImageInfo',
62 'stashimageinfo' => 'ApiQueryStashImageInfo',
63 'templates' => 'ApiQueryLinks',
64 'categories' => 'ApiQueryCategories',
65 'extlinks' => 'ApiQueryExternalLinks',
66 'categoryinfo' => 'ApiQueryCategoryInfo',
67 'duplicatefiles' => 'ApiQueryDuplicateFiles',
68 'pageprops' => 'ApiQueryPageProps',
71 private $mQueryListModules = array(
72 'allimages' => 'ApiQueryAllimages',
73 'allpages' => 'ApiQueryAllpages',
74 'alllinks' => 'ApiQueryAllLinks',
75 'allcategories' => 'ApiQueryAllCategories',
76 'allusers' => 'ApiQueryAllUsers',
77 'backlinks' => 'ApiQueryBacklinks',
78 'blocks' => 'ApiQueryBlocks',
79 'categorymembers' => 'ApiQueryCategoryMembers',
80 'deletedrevs' => 'ApiQueryDeletedrevs',
81 'embeddedin' => 'ApiQueryBacklinks',
82 'filearchive' => 'ApiQueryFilearchive',
83 'imageusage' => 'ApiQueryBacklinks',
84 'iwbacklinks' => 'ApiQueryIWBacklinks',
85 'iwlanglinks' => 'ApiQueryLangLinks',
86 'logevents' => 'ApiQueryLogEvents',
87 'recentchanges' => 'ApiQueryRecentChanges',
88 'search' => 'ApiQuerySearch',
89 'tags' => 'ApiQueryTags',
90 'usercontribs' => 'ApiQueryContributions',
91 'watchlist' => 'ApiQueryWatchlist',
92 'watchlistraw' => 'ApiQueryWatchlistRaw',
93 'exturlusage' => 'ApiQueryExtLinksUsage',
94 'users' => 'ApiQueryUsers',
95 'random' => 'ApiQueryRandom',
96 'protectedtitles' => 'ApiQueryProtectedTitles',
97 'querypage' => 'ApiQueryQueryPage',
100 private $mQueryMetaModules = array(
101 'siteinfo' => 'ApiQuerySiteinfo',
102 'userinfo' => 'ApiQueryUserInfo',
103 'allmessages' => 'ApiQueryAllmessages',
106 private $mSlaveDB = null;
107 private $mNamedDB = array();
109 public function __construct( $main, $action ) {
110 parent::__construct( $main, $action );
112 // Allow custom modules to be added in LocalSettings.php
113 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
114 self::appendUserModules( $this->mQueryPropModules, $wgAPIPropModules );
115 self::appendUserModules( $this->mQueryListModules, $wgAPIListModules );
116 self::appendUserModules( $this->mQueryMetaModules, $wgAPIMetaModules );
118 $this->mPropModuleNames = array_keys( $this->mQueryPropModules );
119 $this->mListModuleNames = array_keys( $this->mQueryListModules );
120 $this->mMetaModuleNames = array_keys( $this->mQueryMetaModules );
122 // Allow the entire list of modules at first,
123 // but during module instantiation check if it can be used as a generator.
124 $this->mAllowedGenerators = array_merge( $this->mListModuleNames, $this->mPropModuleNames );
128 * Helper function to append any add-in modules to the list
129 * @param $modules array Module array
130 * @param $newModules array Module array to add to $modules
132 private static function appendUserModules( &$modules, $newModules ) {
133 if ( is_array( $newModules ) ) {
134 foreach ( $newModules as $moduleName => $moduleClass ) {
135 $modules[$moduleName] = $moduleClass;
141 * Gets a default slave database connection object
142 * @return Database
144 public function getDB() {
145 if ( !isset( $this->mSlaveDB ) ) {
146 $this->profileDBIn();
147 $this->mSlaveDB = wfGetDB( DB_SLAVE, 'api' );
148 $this->profileDBOut();
150 return $this->mSlaveDB;
154 * Get the query database connection with the given name.
155 * If no such connection has been requested before, it will be created.
156 * Subsequent calls with the same $name will return the same connection
157 * as the first, regardless of the values of $db and $groups
158 * @param $name string Name to assign to the database connection
159 * @param $db int One of the DB_* constants
160 * @param $groups array Query groups
161 * @return Database
163 public function getNamedDB( $name, $db, $groups ) {
164 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
165 $this->profileDBIn();
166 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
167 $this->profileDBOut();
169 return $this->mNamedDB[$name];
173 * Gets the set of pages the user has requested (or generated)
174 * @return ApiPageSet
176 public function getPageSet() {
177 return $this->mPageSet;
181 * Get the array mapping module names to class names
182 * @return array(modulename => classname)
184 function getModules() {
185 return array_merge( $this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules );
189 * Get whether the specified module is a prop, list or a meta query module
190 * @param $moduleName string Name of the module to find type for
191 * @return mixed string or null
193 function getModuleType( $moduleName ) {
194 if ( isset( $this->mQueryPropModules[$moduleName] ) ) {
195 return 'prop';
198 if ( isset( $this->mQueryListModules[$moduleName] ) ) {
199 return 'list';
202 if ( isset( $this->mQueryMetaModules[$moduleName] ) ) {
203 return 'meta';
206 return null;
209 public function getCustomPrinter() {
210 // If &exportnowrap is set, use the raw formatter
211 if ( $this->getParameter( 'export' ) &&
212 $this->getParameter( 'exportnowrap' ) )
214 return new ApiFormatRaw( $this->getMain(),
215 $this->getMain()->createPrinterByName( 'xml' ) );
216 } else {
217 return null;
222 * Query execution happens in the following steps:
223 * #1 Create a PageSet object with any pages requested by the user
224 * #2 If using a generator, execute it to get a new ApiPageSet object
225 * #3 Instantiate all requested modules.
226 * This way the PageSet object will know what shared data is required,
227 * and minimize DB calls.
228 * #4 Output all normalization and redirect resolution information
229 * #5 Execute all requested modules
231 public function execute() {
232 $this->params = $this->extractRequestParams();
233 $this->redirects = $this->params['redirects'];
234 $this->convertTitles = $this->params['converttitles'];
236 // Create PageSet
237 $this->mPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
239 // Instantiate requested modules
240 $modules = array();
241 $this->instantiateModules( $modules, 'prop', $this->mQueryPropModules );
242 $this->instantiateModules( $modules, 'list', $this->mQueryListModules );
243 $this->instantiateModules( $modules, 'meta', $this->mQueryMetaModules );
245 $cacheMode = 'public';
247 // If given, execute generator to substitute user supplied data with generated data.
248 if ( isset( $this->params['generator'] ) ) {
249 $generator = $this->newGenerator( $this->params['generator'] );
250 $params = $generator->extractRequestParams();
251 $cacheMode = $this->mergeCacheMode( $cacheMode,
252 $generator->getCacheMode( $params ) );
253 $this->executeGeneratorModule( $generator, $modules );
254 } else {
255 // Append custom fields and populate page/revision information
256 $this->addCustomFldsToPageSet( $modules, $this->mPageSet );
257 $this->mPageSet->execute();
260 // Record page information (title, namespace, if exists, etc)
261 $this->outputGeneralPageInfo();
263 // Execute all requested modules.
264 foreach ( $modules as $module ) {
265 $params = $module->extractRequestParams();
266 $cacheMode = $this->mergeCacheMode(
267 $cacheMode, $module->getCacheMode( $params ) );
268 $module->profileIn();
269 $module->execute();
270 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
271 $module->profileOut();
274 // Set the cache mode
275 $this->getMain()->setCacheMode( $cacheMode );
279 * Update a cache mode string, applying the cache mode of a new module to it.
280 * The cache mode may increase in the level of privacy, but public modules
281 * added to private data do not decrease the level of privacy.
283 * @return string
285 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
286 if ( $modCacheMode === 'anon-public-user-private' ) {
287 if ( $cacheMode !== 'private' ) {
288 $cacheMode = 'anon-public-user-private';
290 } elseif ( $modCacheMode === 'public' ) {
291 // do nothing, if it's public already it will stay public
292 } else { // private
293 $cacheMode = 'private';
295 return $cacheMode;
299 * Query modules may optimize data requests through the $this->getPageSet() object
300 * by adding extra fields from the page table.
301 * This function will gather all the extra request fields from the modules.
302 * @param $modules array of module objects
303 * @param $pageSet ApiPageSet
305 private function addCustomFldsToPageSet( $modules, $pageSet ) {
306 // Query all requested modules.
307 foreach ( $modules as $module ) {
308 $module->requestExtraData( $pageSet );
313 * Create instances of all modules requested by the client
314 * @param $modules Array to append instantiated modules to
315 * @param $param string Parameter name to read modules from
316 * @param $moduleList Array array(modulename => classname)
318 private function instantiateModules( &$modules, $param, $moduleList ) {
319 $list = @$this->params[$param];
320 if ( !is_null ( $list ) ) {
321 foreach ( $list as $moduleName ) {
322 $modules[] = new $moduleList[$moduleName] ( $this, $moduleName );
328 * Appends an element for each page in the current pageSet with the
329 * most general information (id, title), plus any title normalizations
330 * and missing or invalid title/pageids/revids.
332 private function outputGeneralPageInfo() {
333 $pageSet = $this->getPageSet();
334 $result = $this->getResult();
336 // We don't check for a full result set here because we can't be adding
337 // more than 380K. The maximum revision size is in the megabyte range,
338 // and the maximum result size must be even higher than that.
340 // Title normalizations
341 $normValues = array();
342 foreach ( $pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
343 $normValues[] = array(
344 'from' => $rawTitleStr,
345 'to' => $titleStr
349 if ( count( $normValues ) ) {
350 $result->setIndexedTagName( $normValues, 'n' );
351 $result->addValue( 'query', 'normalized', $normValues );
354 // Title conversions
355 $convValues = array();
356 foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) {
357 $convValues[] = array(
358 'from' => $rawTitleStr,
359 'to' => $titleStr
363 if ( count( $convValues ) ) {
364 $result->setIndexedTagName( $convValues, 'c' );
365 $result->addValue( 'query', 'converted', $convValues );
368 // Interwiki titles
369 $intrwValues = array();
370 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
371 $intrwValues[] = array(
372 'title' => $rawTitleStr,
373 'iw' => $interwikiStr
377 if ( count( $intrwValues ) ) {
378 $result->setIndexedTagName( $intrwValues, 'i' );
379 $result->addValue( 'query', 'interwiki', $intrwValues );
382 // Show redirect information
383 $redirValues = array();
384 foreach ( $pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo ) {
385 $redirValues[] = array(
386 'from' => strval( $titleStrFrom ),
387 'to' => $titleStrTo
391 if ( count( $redirValues ) ) {
392 $result->setIndexedTagName( $redirValues, 'r' );
393 $result->addValue( 'query', 'redirects', $redirValues );
396 // Missing revision elements
397 $missingRevIDs = $pageSet->getMissingRevisionIDs();
398 if ( count( $missingRevIDs ) ) {
399 $revids = array();
400 foreach ( $missingRevIDs as $revid ) {
401 $revids[$revid] = array(
402 'revid' => $revid
405 $result->setIndexedTagName( $revids, 'rev' );
406 $result->addValue( 'query', 'badrevids', $revids );
409 // Page elements
410 $pages = array();
412 // Report any missing titles
413 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
414 $vals = array();
415 ApiQueryBase::addTitleInfo( $vals, $title );
416 $vals['missing'] = '';
417 $pages[$fakeId] = $vals;
419 // Report any invalid titles
420 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
421 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
423 // Report any missing page ids
424 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
425 $pages[$pageid] = array(
426 'pageid' => $pageid,
427 'missing' => ''
430 // Report special pages
431 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
432 $vals = array();
433 ApiQueryBase::addTitleInfo( $vals, $title );
434 $vals['special'] = '';
435 if ( $title->getNamespace() == NS_SPECIAL &&
436 !SpecialPageFactory::exists( $title->getDbKey() ) ) {
437 $vals['missing'] = '';
438 } elseif ( $title->getNamespace() == NS_MEDIA &&
439 !wfFindFile( $title ) ) {
440 $vals['missing'] = '';
442 $pages[$fakeId] = $vals;
445 // Output general page information for found titles
446 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
447 $vals = array();
448 $vals['pageid'] = $pageid;
449 ApiQueryBase::addTitleInfo( $vals, $title );
450 $pages[$pageid] = $vals;
453 if ( count( $pages ) ) {
454 if ( $this->params['indexpageids'] ) {
455 $pageIDs = array_keys( $pages );
456 // json treats all map keys as strings - converting to match
457 $pageIDs = array_map( 'strval', $pageIDs );
458 $result->setIndexedTagName( $pageIDs, 'id' );
459 $result->addValue( 'query', 'pageids', $pageIDs );
462 $result->setIndexedTagName( $pages, 'page' );
463 $result->addValue( 'query', 'pages', $pages );
465 if ( $this->params['export'] ) {
466 $this->doExport( $pageSet, $result );
471 * @param $pageSet ApiPageSet Pages to be exported
472 * @param $result ApiResult Result to output to
474 private function doExport( $pageSet, $result ) {
475 $exportTitles = array();
476 $titles = $pageSet->getGoodTitles();
477 if ( count( $titles ) ) {
478 foreach ( $titles as $title ) {
479 if ( $title->userCanRead() ) {
480 $exportTitles[] = $title;
484 // only export when there are titles
485 if ( !count( $exportTitles ) ) {
486 return;
489 $exporter = new WikiExporter( $this->getDB() );
490 // WikiExporter writes to stdout, so catch its
491 // output with an ob
492 ob_start();
493 $exporter->openStream();
494 foreach ( $exportTitles as $title ) {
495 $exporter->pageByTitle( $title );
497 $exporter->closeStream();
498 $exportxml = ob_get_contents();
499 ob_end_clean();
501 // Don't check the size of exported stuff
502 // It's not continuable, so it would cause more
503 // problems than it'd solve
504 $result->disableSizeCheck();
505 if ( $this->params['exportnowrap'] ) {
506 $result->reset();
507 // Raw formatter will handle this
508 $result->addValue( null, 'text', $exportxml );
509 $result->addValue( null, 'mime', 'text/xml' );
510 } else {
511 $r = array();
512 ApiResult::setContent( $r, $exportxml );
513 $result->addValue( 'query', 'export', $r );
515 $result->enableSizeCheck();
519 * Create a generator object of the given type and return it
520 * @param $generatorName string Module name
521 * @return ApiQueryGeneratorBase
523 public function newGenerator( $generatorName ) {
524 // Find class that implements requested generator
525 if ( isset( $this->mQueryListModules[$generatorName] ) ) {
526 $className = $this->mQueryListModules[$generatorName];
527 } elseif ( isset( $this->mQueryPropModules[$generatorName] ) ) {
528 $className = $this->mQueryPropModules[$generatorName];
529 } else {
530 ApiBase::dieDebug( __METHOD__, "Unknown generator=$generatorName" );
532 $generator = new $className ( $this, $generatorName );
533 if ( !$generator instanceof ApiQueryGeneratorBase ) {
534 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
536 $generator->setGeneratorMode();
537 return $generator;
541 * For generator mode, execute generator, and use its output as new
542 * ApiPageSet
543 * @param $generator ApiQueryGeneratorBase Generator Module
544 * @param $modules array of module objects
546 protected function executeGeneratorModule( $generator, $modules ) {
547 // Generator results
548 $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
550 // Add any additional fields modules may need
551 $generator->requestExtraData( $this->mPageSet );
552 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
554 // Populate page information with the original user input
555 $this->mPageSet->execute();
557 // populate resultPageSet with the generator output
558 $generator->profileIn();
559 $generator->executeGenerator( $resultPageSet );
560 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
561 $resultPageSet->finishPageSetGeneration();
562 $generator->profileOut();
564 // Swap the resulting pageset back in
565 $this->mPageSet = $resultPageSet;
568 public function getAllowedParams() {
569 return array(
570 'prop' => array(
571 ApiBase::PARAM_ISMULTI => true,
572 ApiBase::PARAM_TYPE => $this->mPropModuleNames
574 'list' => array(
575 ApiBase::PARAM_ISMULTI => true,
576 ApiBase::PARAM_TYPE => $this->mListModuleNames
578 'meta' => array(
579 ApiBase::PARAM_ISMULTI => true,
580 ApiBase::PARAM_TYPE => $this->mMetaModuleNames
582 'generator' => array(
583 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
585 'redirects' => false,
586 'converttitles' => false,
587 'indexpageids' => false,
588 'export' => false,
589 'exportnowrap' => false,
594 * Override the parent to generate help messages for all available query modules.
595 * @return string
597 public function makeHelpMsg() {
598 $msg = '';
600 // Make sure the internal object is empty
601 // (just in case a sub-module decides to optimize during instantiation)
602 $this->mPageSet = null;
603 $this->mAllowedGenerators = array(); // Will be repopulated
605 $querySeparator = str_repeat( '--- ', 12 );
606 $moduleSeparator = str_repeat( '*** ', 14 );
607 $msg .= "\n$querySeparator Query: Prop $querySeparator\n\n";
608 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules, 'prop' );
609 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
610 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules, 'list' );
611 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
612 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules, 'meta' );
613 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
615 // Perform the base call last because the $this->mAllowedGenerators
616 // will be updated inside makeHelpMsgHelper()
617 // Use parent to make default message for the query module
618 $msg = parent::makeHelpMsg() . $msg;
620 return $msg;
624 * For all modules in $moduleList, generate help messages and join them together
625 * @param $moduleList Array array(modulename => classname)
626 * @param $paramName string Parameter name
627 * @return string
629 private function makeHelpMsgHelper( $moduleList, $paramName ) {
630 $moduleDescriptions = array();
632 foreach ( $moduleList as $moduleName => $moduleClass ) {
633 $module = new $moduleClass( $this, $moduleName, null );
635 $msg = ApiMain::makeHelpMsgHeader( $module, $paramName );
636 $msg2 = $module->makeHelpMsg();
637 if ( $msg2 !== false ) {
638 $msg .= $msg2;
640 if ( $module instanceof ApiQueryGeneratorBase ) {
641 $this->mAllowedGenerators[] = $moduleName;
642 $msg .= "Generator:\n This module may be used as a generator\n";
644 $moduleDescriptions[] = $msg;
647 return implode( "\n", $moduleDescriptions );
651 * Override to add extra parameters from PageSet
652 * @return string
654 public function makeHelpMsgParameters() {
655 $psModule = new ApiPageSet( $this );
656 return $psModule->makeHelpMsgParameters() . parent::makeHelpMsgParameters();
659 public function shouldCheckMaxlag() {
660 return true;
663 public function getParamDescription() {
664 return array(
665 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
666 'list' => 'Which lists to get. Module help is available below',
667 'meta' => 'Which metadata to get about the site. Module help is available below',
668 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
669 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
670 'redirects' => 'Automatically resolve redirects',
671 'converttitles' => array( "Convert titles to other variants if necessary. Only works if the wiki's content language supports variant conversion.",
672 'Languages that support variant conversion include kk, ku, gan, tg, sr, zh' ),
673 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
674 'export' => 'Export the current revisions of all given or generated pages',
675 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
679 public function getDescription() {
680 return array(
681 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
682 'and is loosely based on the old query.php interface.',
683 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
687 public function getPossibleErrors() {
688 return array_merge( parent::getPossibleErrors(), array(
689 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
690 ) );
693 protected function getExamples() {
694 return array(
695 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
696 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
700 public function getVersion() {
701 $psModule = new ApiPageSet( $this );
702 $vers = array();
703 $vers[] = __CLASS__ . ': $Id$';
704 $vers[] = $psModule->getVersion();
705 return $vers;