3 * API for MediaWiki 1.8+
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiBase.php' );
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.
43 class ApiQuery
extends ApiBase
{
45 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
49 private $mQueryPropModules = array(
50 'info' => 'ApiQueryInfo',
51 'revisions' => 'ApiQueryRevisions',
52 'links' => 'ApiQueryLinks',
53 'iwlinks' => 'ApiQueryIWLinks',
54 'langlinks' => 'ApiQueryLangLinks',
55 'images' => 'ApiQueryImages',
56 'imageinfo' => 'ApiQueryImageInfo',
57 'templates' => 'ApiQueryLinks',
58 'categories' => 'ApiQueryCategories',
59 'extlinks' => 'ApiQueryExternalLinks',
60 'categoryinfo' => 'ApiQueryCategoryInfo',
61 'duplicatefiles' => 'ApiQueryDuplicateFiles',
62 'pageprops' => 'ApiQueryPageProps',
65 private $mQueryListModules = array(
66 'allimages' => 'ApiQueryAllimages',
67 'allpages' => 'ApiQueryAllpages',
68 'alllinks' => 'ApiQueryAllLinks',
69 'allcategories' => 'ApiQueryAllCategories',
70 'allusers' => 'ApiQueryAllUsers',
71 'backlinks' => 'ApiQueryBacklinks',
72 'blocks' => 'ApiQueryBlocks',
73 'categorymembers' => 'ApiQueryCategoryMembers',
74 'deletedrevs' => 'ApiQueryDeletedrevs',
75 'embeddedin' => 'ApiQueryBacklinks',
76 'filearchive' => 'ApiQueryFilearchive',
77 'imageusage' => 'ApiQueryBacklinks',
78 'iwbacklinks' => 'ApiQueryIWBacklinks',
79 'logevents' => 'ApiQueryLogEvents',
80 'recentchanges' => 'ApiQueryRecentChanges',
81 'search' => 'ApiQuerySearch',
82 'tags' => 'ApiQueryTags',
83 'usercontribs' => 'ApiQueryContributions',
84 'watchlist' => 'ApiQueryWatchlist',
85 'watchlistraw' => 'ApiQueryWatchlistRaw',
86 'exturlusage' => 'ApiQueryExtLinksUsage',
87 'users' => 'ApiQueryUsers',
88 'random' => 'ApiQueryRandom',
89 'protectedtitles' => 'ApiQueryProtectedTitles',
92 private $mQueryMetaModules = array(
93 'siteinfo' => 'ApiQuerySiteinfo',
94 'userinfo' => 'ApiQueryUserInfo',
95 'allmessages' => 'ApiQueryAllmessages',
98 private $mSlaveDB = null;
99 private $mNamedDB = array();
101 public function __construct( $main, $action ) {
102 parent
::__construct( $main, $action );
104 // Allow custom modules to be added in LocalSettings.php
105 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
106 self
::appendUserModules( $this->mQueryPropModules
, $wgAPIPropModules );
107 self
::appendUserModules( $this->mQueryListModules
, $wgAPIListModules );
108 self
::appendUserModules( $this->mQueryMetaModules
, $wgAPIMetaModules );
110 $this->mPropModuleNames
= array_keys( $this->mQueryPropModules
);
111 $this->mListModuleNames
= array_keys( $this->mQueryListModules
);
112 $this->mMetaModuleNames
= array_keys( $this->mQueryMetaModules
);
114 // Allow the entire list of modules at first,
115 // but during module instantiation check if it can be used as a generator.
116 $this->mAllowedGenerators
= array_merge( $this->mListModuleNames
, $this->mPropModuleNames
);
120 * Helper function to append any add-in modules to the list
121 * @param $modules array Module array
122 * @param $newModules array Module array to add to $modules
124 private static function appendUserModules( &$modules, $newModules ) {
125 if ( is_array( $newModules ) ) {
126 foreach ( $newModules as $moduleName => $moduleClass ) {
127 $modules[$moduleName] = $moduleClass;
133 * Gets a default slave database connection object
136 public function getDB() {
137 if ( !isset( $this->mSlaveDB
) ) {
138 $this->profileDBIn();
139 $this->mSlaveDB
= wfGetDB( DB_SLAVE
, 'api' );
140 $this->profileDBOut();
142 return $this->mSlaveDB
;
146 * Get the query database connection with the given name.
147 * If no such connection has been requested before, it will be created.
148 * Subsequent calls with the same $name will return the same connection
149 * as the first, regardless of the values of $db and $groups
150 * @param $name string Name to assign to the database connection
151 * @param $db int One of the DB_* constants
152 * @param $groups array Query groups
155 public function getNamedDB( $name, $db, $groups ) {
156 if ( !array_key_exists( $name, $this->mNamedDB
) ) {
157 $this->profileDBIn();
158 $this->mNamedDB
[$name] = wfGetDB( $db, $groups );
159 $this->profileDBOut();
161 return $this->mNamedDB
[$name];
165 * Gets the set of pages the user has requested (or generated)
168 public function getPageSet() {
169 return $this->mPageSet
;
173 * Get the array mapping module names to class names
174 * @return array(modulename => classname)
176 function getModules() {
177 return array_merge( $this->mQueryPropModules
, $this->mQueryListModules
, $this->mQueryMetaModules
);
181 * Get whether the specified module is a prop, list or a meta query module
182 * @param $moduleName string Name of the module to find type for
183 * @return mixed string or null
185 function getModuleType( $moduleName ) {
186 if ( array_key_exists ( $moduleName, $this->mQueryPropModules
) ) {
190 if ( array_key_exists ( $moduleName, $this->mQueryListModules
) ) {
194 if ( array_key_exists ( $moduleName, $this->mQueryMetaModules
) ) {
201 public function getCustomPrinter() {
202 // If &exportnowrap is set, use the raw formatter
203 if ( $this->getParameter( 'export' ) &&
204 $this->getParameter( 'exportnowrap' ) )
206 return new ApiFormatRaw( $this->getMain(),
207 $this->getMain()->createPrinterByName( 'xml' ) );
214 * Query execution happens in the following steps:
215 * #1 Create a PageSet object with any pages requested by the user
216 * #2 If using a generator, execute it to get a new ApiPageSet object
217 * #3 Instantiate all requested modules.
218 * This way the PageSet object will know what shared data is required,
219 * and minimize DB calls.
220 * #4 Output all normalization and redirect resolution information
221 * #5 Execute all requested modules
223 public function execute() {
224 $this->params
= $this->extractRequestParams();
225 $this->redirects
= $this->params
['redirects'];
226 $this->convertTitles
= $this->params
['converttitles'];
229 $this->mPageSet
= new ApiPageSet( $this, $this->redirects
, $this->convertTitles
);
231 // Instantiate requested modules
233 $this->instantiateModules( $modules, 'prop', $this->mQueryPropModules
);
234 $this->instantiateModules( $modules, 'list', $this->mQueryListModules
);
235 $this->instantiateModules( $modules, 'meta', $this->mQueryMetaModules
);
237 $cacheMode = 'public';
239 // If given, execute generator to substitute user supplied data with generated data.
240 if ( isset( $this->params
['generator'] ) ) {
241 $generator = $this->newGenerator( $this->params
['generator'] );
242 $params = $generator->extractRequestParams();
243 $cacheMode = $this->mergeCacheMode( $cacheMode,
244 $generator->getCacheMode( $params ) );
245 $this->executeGeneratorModule( $generator, $modules );
247 // Append custom fields and populate page/revision information
248 $this->addCustomFldsToPageSet( $modules, $this->mPageSet
);
249 $this->mPageSet
->execute();
252 // Record page information (title, namespace, if exists, etc)
253 $this->outputGeneralPageInfo();
255 // Execute all requested modules.
256 foreach ( $modules as $module ) {
257 $params = $module->extractRequestParams();
258 $cacheMode = $this->mergeCacheMode(
259 $cacheMode, $module->getCacheMode( $params ) );
260 $module->profileIn();
262 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
263 $module->profileOut();
266 // Set the cache mode
267 $this->getMain()->setCacheMode( $cacheMode );
271 * Update a cache mode string, applying the cache mode of a new module to it.
272 * The cache mode may increase in the level of privacy, but public modules
273 * added to private data do not decrease the level of privacy.
275 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
276 if ( $modCacheMode === 'anon-public-user-private' ) {
277 if ( $cacheMode !== 'private' ) {
278 $cacheMode = 'anon-public-user-private';
280 } elseif ( $modCacheMode === 'public' ) {
281 // do nothing, if it's public already it will stay public
283 $cacheMode = 'private';
289 * Query modules may optimize data requests through the $this->getPageSet() object
290 * by adding extra fields from the page table.
291 * This function will gather all the extra request fields from the modules.
292 * @param $modules array of module objects
293 * @param $pageSet ApiPageSet
295 private function addCustomFldsToPageSet( $modules, $pageSet ) {
296 // Query all requested modules.
297 foreach ( $modules as $module ) {
298 $module->requestExtraData( $pageSet );
303 * Create instances of all modules requested by the client
304 * @param $modules array to append instatiated modules to
305 * @param $param string Parameter name to read modules from
306 * @param $moduleList array(modulename => classname)
308 private function instantiateModules( &$modules, $param, $moduleList ) {
309 $list = @$this->params
[$param];
310 if ( !is_null ( $list ) ) {
311 foreach ( $list as $moduleName ) {
312 $modules[] = new $moduleList[$moduleName] ( $this, $moduleName );
318 * Appends an element for each page in the current pageSet with the
319 * most general information (id, title), plus any title normalizations
320 * and missing or invalid title/pageids/revids.
322 private function outputGeneralPageInfo() {
323 $pageSet = $this->getPageSet();
324 $result = $this->getResult();
326 // We don't check for a full result set here because we can't be adding
327 // more than 380K. The maximum revision size is in the megabyte range,
328 // and the maximum result size must be even higher than that.
330 // Title normalizations
331 $normValues = array();
332 foreach ( $pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
333 $normValues[] = array(
334 'from' => $rawTitleStr,
339 if ( count( $normValues ) ) {
340 $result->setIndexedTagName( $normValues, 'n' );
341 $result->addValue( 'query', 'normalized', $normValues );
345 $convValues = array();
346 foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) {
347 $convValues[] = array(
348 'from' => $rawTitleStr,
353 if ( count( $convValues ) ) {
354 $result->setIndexedTagName( $convValues, 'c' );
355 $result->addValue( 'query', 'converted', $convValues );
359 $intrwValues = array();
360 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
361 $intrwValues[] = array(
362 'title' => $rawTitleStr,
363 'iw' => $interwikiStr
367 if ( count( $intrwValues ) ) {
368 $result->setIndexedTagName( $intrwValues, 'i' );
369 $result->addValue( 'query', 'interwiki', $intrwValues );
372 // Show redirect information
373 $redirValues = array();
374 foreach ( $pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo ) {
375 $redirValues[] = array(
376 'from' => strval( $titleStrFrom ),
381 if ( count( $redirValues ) ) {
382 $result->setIndexedTagName( $redirValues, 'r' );
383 $result->addValue( 'query', 'redirects', $redirValues );
387 // Missing revision elements
389 $missingRevIDs = $pageSet->getMissingRevisionIDs();
390 if ( count( $missingRevIDs ) ) {
392 foreach ( $missingRevIDs as $revid ) {
393 $revids[$revid] = array(
397 $result->setIndexedTagName( $revids, 'rev' );
398 $result->addValue( 'query', 'badrevids', $revids );
406 // Report any missing titles
407 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
409 ApiQueryBase
::addTitleInfo( $vals, $title );
410 $vals['missing'] = '';
411 $pages[$fakeId] = $vals;
413 // Report any invalid titles
414 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
415 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
417 // Report any missing page ids
418 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
419 $pages[$pageid] = array(
424 // Report special pages
425 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
427 ApiQueryBase
::addTitleInfo( $vals, $title );
428 $vals['special'] = '';
429 if ( $title->getNamespace() == NS_SPECIAL
&&
430 !SpecialPage
::exists( $title->getText() ) ) {
431 $vals['missing'] = '';
432 } elseif ( $title->getNamespace() == NS_MEDIA
&&
433 !wfFindFile( $title ) ) {
434 $vals['missing'] = '';
436 $pages[$fakeId] = $vals;
439 // Output general page information for found titles
440 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
442 $vals['pageid'] = $pageid;
443 ApiQueryBase
::addTitleInfo( $vals, $title );
444 $pages[$pageid] = $vals;
447 if ( count( $pages ) ) {
448 if ( $this->params
['indexpageids'] ) {
449 $pageIDs = array_keys( $pages );
450 // json treats all map keys as strings - converting to match
451 $pageIDs = array_map( 'strval', $pageIDs );
452 $result->setIndexedTagName( $pageIDs, 'id' );
453 $result->addValue( 'query', 'pageids', $pageIDs );
456 $result->setIndexedTagName( $pages, 'page' );
457 $result->addValue( 'query', 'pages', $pages );
459 if ( $this->params
['export'] ) {
460 $exporter = new WikiExporter( $this->getDB() );
461 // WikiExporter writes to stdout, so catch its
464 $exporter->openStream();
465 foreach ( @$pageSet->getGoodTitles() as $title ) {
466 if ( $title->userCanRead() ) {
467 $exporter->pageByTitle( $title );
470 $exporter->closeStream();
471 $exportxml = ob_get_contents();
474 // Don't check the size of exported stuff
475 // It's not continuable, so it would cause more
476 // problems than it'd solve
477 $result->disableSizeCheck();
478 if ( $this->params
['exportnowrap'] ) {
480 // Raw formatter will handle this
481 $result->addValue( null, 'text', $exportxml );
482 $result->addValue( null, 'mime', 'text/xml' );
485 ApiResult
::setContent( $r, $exportxml );
486 $result->addValue( 'query', 'export', $r );
488 $result->enableSizeCheck();
493 * Create a generator object of the given type and return it
494 * @param $generatorName string Module name
496 public function newGenerator( $generatorName ) {
497 // Find class that implements requested generator
498 if ( isset( $this->mQueryListModules
[$generatorName] ) ) {
499 $className = $this->mQueryListModules
[$generatorName];
500 } elseif ( isset( $this->mQueryPropModules
[$generatorName] ) ) {
501 $className = $this->mQueryPropModules
[$generatorName];
503 ApiBase
::dieDebug( __METHOD__
, "Unknown generator=$generatorName" );
505 $generator = new $className ( $this, $generatorName );
506 if ( !$generator instanceof ApiQueryGeneratorBase
) {
507 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
509 $generator->setGeneratorMode();
514 * For generator mode, execute generator, and use its output as new
516 * @param $generatorName string Module name
517 * @param $modules array of module objects
519 protected function executeGeneratorModule( $generator, $modules ) {
521 $resultPageSet = new ApiPageSet( $this, $this->redirects
, $this->convertTitles
);
523 // Add any additional fields modules may need
524 $generator->requestExtraData( $this->mPageSet
);
525 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
527 // Populate page information with the original user input
528 $this->mPageSet
->execute();
530 // populate resultPageSet with the generator output
531 $generator->profileIn();
532 $generator->executeGenerator( $resultPageSet );
533 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
534 $resultPageSet->finishPageSetGeneration();
535 $generator->profileOut();
537 // Swap the resulting pageset back in
538 $this->mPageSet
= $resultPageSet;
541 public function getAllowedParams() {
544 ApiBase
::PARAM_ISMULTI
=> true,
545 ApiBase
::PARAM_TYPE
=> $this->mPropModuleNames
548 ApiBase
::PARAM_ISMULTI
=> true,
549 ApiBase
::PARAM_TYPE
=> $this->mListModuleNames
552 ApiBase
::PARAM_ISMULTI
=> true,
553 ApiBase
::PARAM_TYPE
=> $this->mMetaModuleNames
555 'generator' => array(
556 ApiBase
::PARAM_TYPE
=> $this->mAllowedGenerators
558 'redirects' => false,
559 'converttitles' => false,
560 'indexpageids' => false,
562 'exportnowrap' => false,
567 * Override the parent to generate help messages for all available query modules.
570 public function makeHelpMsg() {
573 // Make sure the internal object is empty
574 // (just in case a sub-module decides to optimize during instantiation)
575 $this->mPageSet
= null;
576 $this->mAllowedGenerators
= array(); // Will be repopulated
578 $astriks = str_repeat( '--- ', 8 );
579 $astriks2 = str_repeat( '*** ', 10 );
580 $msg .= "\n$astriks Query: Prop $astriks\n\n";
581 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules
, 'prop' );
582 $msg .= "\n$astriks Query: List $astriks\n\n";
583 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules
, 'list' );
584 $msg .= "\n$astriks Query: Meta $astriks\n\n";
585 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules
, 'meta' );
586 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
588 // Perform the base call last because the $this->mAllowedGenerators
589 // will be updated inside makeHelpMsgHelper()
590 // Use parent to make default message for the query module
591 $msg = parent
::makeHelpMsg() . $msg;
597 * For all modules in $moduleList, generate help messages and join them together
598 * @param $moduleList array(modulename => classname)
599 * @param $paramName string Parameter name
602 private function makeHelpMsgHelper( $moduleList, $paramName ) {
603 $moduleDescriptions = array();
605 foreach ( $moduleList as $moduleName => $moduleClass ) {
606 $module = new $moduleClass ( $this, $moduleName, null );
608 $msg = ApiMain
::makeHelpMsgHeader( $module, $paramName );
609 $msg2 = $module->makeHelpMsg();
610 if ( $msg2 !== false ) {
613 if ( $module instanceof ApiQueryGeneratorBase
) {
614 $this->mAllowedGenerators
[] = $moduleName;
615 $msg .= "Generator:\n This module may be used as a generator\n";
617 $moduleDescriptions[] = $msg;
620 return implode( "\n", $moduleDescriptions );
624 * Override to add extra parameters from PageSet
627 public function makeHelpMsgParameters() {
628 $psModule = new ApiPageSet( $this );
629 return $psModule->makeHelpMsgParameters() . parent
::makeHelpMsgParameters();
632 public function shouldCheckMaxlag() {
636 public function getParamDescription() {
638 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
639 'list' => 'Which lists to get. Module help is available below',
640 'meta' => 'Which metadata to get about the site. Module help is available below',
641 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
642 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
643 'redirects' => 'Automatically resolve redirects',
644 'converttitles' => "Convert titles to other variants if necessary. Only works if the wiki's content language supports variant conversion.",
645 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
646 'export' => 'Export the current revisions of all given or generated pages',
647 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
651 public function getDescription() {
653 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
654 'and is loosely based on the old query.php interface.',
655 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
659 public function getPossibleErrors() {
660 return array_merge( parent
::getPossibleErrors(), array(
661 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
665 protected function getExamples() {
667 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
668 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
672 public function getVersion() {
673 $psModule = new ApiPageSet( $this );
675 $vers[] = __CLASS__
. ': $Id$';
676 $vers[] = $psModule->getVersion();