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
28 * This is the main query class. It behaves similar to ApiMain: based on the
29 * parameters given, it will create a list of titles to work on (an ApiPageSet
30 * object), instantiate and execute various property/list/meta modules, and
31 * assemble all resulting data into a single ApiResult object.
33 * In generator mode, a generator will be executed first to populate a second
34 * ApiPageSet object, and that object will be used for all subsequent modules.
38 class ApiQuery
extends ApiBase
{
40 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
47 private $params, $redirects, $convertTitles, $iwUrl;
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 'stashimageinfo' => 'ApiQueryStashImageInfo',
58 'templates' => 'ApiQueryLinks',
59 'categories' => 'ApiQueryCategories',
60 'extlinks' => 'ApiQueryExternalLinks',
61 'categoryinfo' => 'ApiQueryCategoryInfo',
62 'duplicatefiles' => 'ApiQueryDuplicateFiles',
63 'pageprops' => 'ApiQueryPageProps',
66 private $mQueryListModules = array(
67 'allimages' => 'ApiQueryAllImages',
68 'allpages' => 'ApiQueryAllPages',
69 'alllinks' => 'ApiQueryAllLinks',
70 'allcategories' => 'ApiQueryAllCategories',
71 'allusers' => 'ApiQueryAllUsers',
72 'backlinks' => 'ApiQueryBacklinks',
73 'blocks' => 'ApiQueryBlocks',
74 'categorymembers' => 'ApiQueryCategoryMembers',
75 'deletedrevs' => 'ApiQueryDeletedrevs',
76 'embeddedin' => 'ApiQueryBacklinks',
77 'filearchive' => 'ApiQueryFilearchive',
78 'imageusage' => 'ApiQueryBacklinks',
79 'iwbacklinks' => 'ApiQueryIWBacklinks',
80 'langbacklinks' => 'ApiQueryLangBacklinks',
81 'logevents' => 'ApiQueryLogEvents',
82 'recentchanges' => 'ApiQueryRecentChanges',
83 'search' => 'ApiQuerySearch',
84 'tags' => 'ApiQueryTags',
85 'usercontribs' => 'ApiQueryContributions',
86 'watchlist' => 'ApiQueryWatchlist',
87 'watchlistraw' => 'ApiQueryWatchlistRaw',
88 'exturlusage' => 'ApiQueryExtLinksUsage',
89 'users' => 'ApiQueryUsers',
90 'random' => 'ApiQueryRandom',
91 'protectedtitles' => 'ApiQueryProtectedTitles',
92 'querypage' => 'ApiQueryQueryPage',
95 private $mQueryMetaModules = array(
96 'siteinfo' => 'ApiQuerySiteinfo',
97 'userinfo' => 'ApiQueryUserInfo',
98 'allmessages' => 'ApiQueryAllMessages',
101 private $mSlaveDB = null;
102 private $mNamedDB = array();
104 protected $mAllowedGenerators = array();
107 * @param $main ApiMain
108 * @param $action string
110 public function __construct( $main, $action ) {
111 parent
::__construct( $main, $action );
113 // Allow custom modules to be added in LocalSettings.php
114 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
115 self
::appendUserModules( $this->mQueryPropModules
, $wgAPIPropModules );
116 self
::appendUserModules( $this->mQueryListModules
, $wgAPIListModules );
117 self
::appendUserModules( $this->mQueryMetaModules
, $wgAPIMetaModules );
119 $this->mPropModuleNames
= array_keys( $this->mQueryPropModules
);
120 $this->mListModuleNames
= array_keys( $this->mQueryListModules
);
121 $this->mMetaModuleNames
= array_keys( $this->mQueryMetaModules
);
123 $this->makeGeneratorList( $this->mQueryPropModules
);
124 $this->makeGeneratorList( $this->mQueryListModules
);
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 DatabaseBase
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 DatabaseBase
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)
176 public function getPageSet() {
177 return $this->mPageSet
;
181 * Get the array mapping module names to class names
182 * @return array 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] ) ) {
198 if ( isset( $this->mQueryListModules
[$moduleName] ) ) {
202 if ( isset( $this->mQueryMetaModules
[$moduleName] ) ) {
210 * @return ApiFormatRaw|null
212 public function getCustomPrinter() {
213 // If &exportnowrap is set, use the raw formatter
214 if ( $this->getParameter( 'export' ) &&
215 $this->getParameter( 'exportnowrap' ) )
217 return new ApiFormatRaw( $this->getMain(),
218 $this->getMain()->createPrinterByName( 'xml' ) );
225 * Query execution happens in the following steps:
226 * #1 Create a PageSet object with any pages requested by the user
227 * #2 If using a generator, execute it to get a new ApiPageSet object
228 * #3 Instantiate all requested modules.
229 * This way the PageSet object will know what shared data is required,
230 * and minimize DB calls.
231 * #4 Output all normalization and redirect resolution information
232 * #5 Execute all requested modules
234 public function execute() {
235 $this->params
= $this->extractRequestParams();
236 $this->redirects
= $this->params
['redirects'];
237 $this->convertTitles
= $this->params
['converttitles'];
238 $this->iwUrl
= $this->params
['iwurl'];
241 $this->mPageSet
= new ApiPageSet( $this, $this->redirects
, $this->convertTitles
);
243 // Instantiate requested modules
245 $this->instantiateModules( $modules, 'prop', $this->mQueryPropModules
);
246 $this->instantiateModules( $modules, 'list', $this->mQueryListModules
);
247 $this->instantiateModules( $modules, 'meta', $this->mQueryMetaModules
);
249 $cacheMode = 'public';
251 // If given, execute generator to substitute user supplied data with generated data.
252 if ( isset( $this->params
['generator'] ) ) {
253 $generator = $this->newGenerator( $this->params
['generator'] );
254 $params = $generator->extractRequestParams();
255 $cacheMode = $this->mergeCacheMode( $cacheMode,
256 $generator->getCacheMode( $params ) );
257 $this->executeGeneratorModule( $generator, $modules );
259 // Append custom fields and populate page/revision information
260 $this->addCustomFldsToPageSet( $modules, $this->mPageSet
);
261 $this->mPageSet
->execute();
264 // Record page information (title, namespace, if exists, etc)
265 $this->outputGeneralPageInfo();
267 // Execute all requested modules.
269 * @var $module ApiQueryBase
271 foreach ( $modules as $module ) {
272 $params = $module->extractRequestParams();
273 $cacheMode = $this->mergeCacheMode(
274 $cacheMode, $module->getCacheMode( $params ) );
275 $module->profileIn();
277 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
278 $module->profileOut();
281 // Set the cache mode
282 $this->getMain()->setCacheMode( $cacheMode );
286 * Update a cache mode string, applying the cache mode of a new module to it.
287 * The cache mode may increase in the level of privacy, but public modules
288 * added to private data do not decrease the level of privacy.
290 * @param $cacheMode string
291 * @param $modCacheMode string
294 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
295 if ( $modCacheMode === 'anon-public-user-private' ) {
296 if ( $cacheMode !== 'private' ) {
297 $cacheMode = 'anon-public-user-private';
299 } elseif ( $modCacheMode === 'public' ) {
300 // do nothing, if it's public already it will stay public
302 $cacheMode = 'private';
308 * Query modules may optimize data requests through the $this->getPageSet() object
309 * by adding extra fields from the page table.
310 * This function will gather all the extra request fields from the modules.
311 * @param $modules array of module objects
312 * @param $pageSet ApiPageSet
314 private function addCustomFldsToPageSet( $modules, $pageSet ) {
315 // Query all requested modules.
317 * @var $module ApiQueryBase
319 foreach ( $modules as $module ) {
320 $module->requestExtraData( $pageSet );
325 * Create instances of all modules requested by the client
326 * @param $modules Array to append instantiated modules to
327 * @param $param string Parameter name to read modules from
328 * @param $moduleList Array array(modulename => classname)
330 private function instantiateModules( &$modules, $param, $moduleList ) {
331 if ( isset( $this->params
[$param] ) ) {
332 foreach ( $this->params
[$param] as $moduleName ) {
333 $modules[] = new $moduleList[$moduleName] ( $this, $moduleName );
339 * Appends an element for each page in the current pageSet with the
340 * most general information (id, title), plus any title normalizations
341 * and missing or invalid title/pageids/revids.
343 private function outputGeneralPageInfo() {
344 $pageSet = $this->getPageSet();
345 $result = $this->getResult();
347 // We don't check for a full result set here because we can't be adding
348 // more than 380K. The maximum revision size is in the megabyte range,
349 // and the maximum result size must be even higher than that.
351 // Title normalizations
352 $normValues = array();
353 foreach ( $pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
354 $normValues[] = array(
355 'from' => $rawTitleStr,
360 if ( count( $normValues ) ) {
361 $result->setIndexedTagName( $normValues, 'n' );
362 $result->addValue( 'query', 'normalized', $normValues );
366 $convValues = array();
367 foreach ( $pageSet->getConvertedTitles() as $rawTitleStr => $titleStr ) {
368 $convValues[] = array(
369 'from' => $rawTitleStr,
374 if ( count( $convValues ) ) {
375 $result->setIndexedTagName( $convValues, 'c' );
376 $result->addValue( 'query', 'converted', $convValues );
380 $intrwValues = array();
381 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
383 'title' => $rawTitleStr,
384 'iw' => $interwikiStr,
386 if ( $this->iwUrl
) {
387 $title = Title
::newFromText( $rawTitleStr );
388 $item['url'] = wfExpandUrl( $title->getFullURL(), PROTO_CURRENT
);
390 $intrwValues[] = $item;
393 if ( count( $intrwValues ) ) {
394 $result->setIndexedTagName( $intrwValues, 'i' );
395 $result->addValue( 'query', 'interwiki', $intrwValues );
398 // Show redirect information
399 $redirValues = array();
401 * @var $titleTo Title
403 foreach ( $pageSet->getRedirectTitles() as $titleStrFrom => $titleTo ) {
405 'from' => strval( $titleStrFrom ),
406 'to' => $titleTo->getPrefixedText(),
408 if ( $titleTo->getFragment() !== '' ) {
409 $r['tofragment'] = $titleTo->getFragment();
414 if ( count( $redirValues ) ) {
415 $result->setIndexedTagName( $redirValues, 'r' );
416 $result->addValue( 'query', 'redirects', $redirValues );
419 // Missing revision elements
420 $missingRevIDs = $pageSet->getMissingRevisionIDs();
421 if ( count( $missingRevIDs ) ) {
423 foreach ( $missingRevIDs as $revid ) {
424 $revids[$revid] = array(
428 $result->setIndexedTagName( $revids, 'rev' );
429 $result->addValue( 'query', 'badrevids', $revids );
435 // Report any missing titles
436 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
438 ApiQueryBase
::addTitleInfo( $vals, $title );
439 $vals['missing'] = '';
440 $pages[$fakeId] = $vals;
442 // Report any invalid titles
443 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
444 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
446 // Report any missing page ids
447 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
448 $pages[$pageid] = array(
453 // Report special pages
454 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
456 ApiQueryBase
::addTitleInfo( $vals, $title );
457 $vals['special'] = '';
458 if ( $title->isSpecialPage() &&
459 !SpecialPageFactory
::exists( $title->getDbKey() ) ) {
460 $vals['missing'] = '';
461 } elseif ( $title->getNamespace() == NS_MEDIA
&&
462 !wfFindFile( $title ) ) {
463 $vals['missing'] = '';
465 $pages[$fakeId] = $vals;
468 // Output general page information for found titles
469 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
471 $vals['pageid'] = $pageid;
472 ApiQueryBase
::addTitleInfo( $vals, $title );
473 $pages[$pageid] = $vals;
476 if ( count( $pages ) ) {
477 if ( $this->params
['indexpageids'] ) {
478 $pageIDs = array_keys( $pages );
479 // json treats all map keys as strings - converting to match
480 $pageIDs = array_map( 'strval', $pageIDs );
481 $result->setIndexedTagName( $pageIDs, 'id' );
482 $result->addValue( 'query', 'pageids', $pageIDs );
485 $result->setIndexedTagName( $pages, 'page' );
486 $result->addValue( 'query', 'pages', $pages );
488 if ( $this->params
['export'] ) {
489 $this->doExport( $pageSet, $result );
494 * @param $pageSet ApiPageSet Pages to be exported
495 * @param $result ApiResult Result to output to
497 private function doExport( $pageSet, $result ) {
498 $exportTitles = array();
499 $titles = $pageSet->getGoodTitles();
500 if ( count( $titles ) ) {
501 foreach ( $titles as $title ) {
502 if ( $title->userCan( 'read' ) ) {
503 $exportTitles[] = $title;
508 $exporter = new WikiExporter( $this->getDB() );
509 // WikiExporter writes to stdout, so catch its
512 $exporter->openStream();
513 foreach ( $exportTitles as $title ) {
514 $exporter->pageByTitle( $title );
516 $exporter->closeStream();
517 $exportxml = ob_get_contents();
520 // Don't check the size of exported stuff
521 // It's not continuable, so it would cause more
522 // problems than it'd solve
523 $result->disableSizeCheck();
524 if ( $this->params
['exportnowrap'] ) {
526 // Raw formatter will handle this
527 $result->addValue( null, 'text', $exportxml );
528 $result->addValue( null, 'mime', 'text/xml' );
531 ApiResult
::setContent( $r, $exportxml );
532 $result->addValue( 'query', 'export', $r );
534 $result->enableSizeCheck();
538 * Create a generator object of the given type and return it
539 * @param $generatorName string Module name
540 * @return ApiQueryGeneratorBase
542 public function newGenerator( $generatorName ) {
543 // Find class that implements requested generator
544 if ( isset( $this->mQueryListModules
[$generatorName] ) ) {
545 $className = $this->mQueryListModules
[$generatorName];
546 } elseif ( isset( $this->mQueryPropModules
[$generatorName] ) ) {
547 $className = $this->mQueryPropModules
[$generatorName];
549 ApiBase
::dieDebug( __METHOD__
, "Unknown generator=$generatorName" );
551 $generator = new $className ( $this, $generatorName );
552 if ( !$generator instanceof ApiQueryGeneratorBase
) {
553 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
555 $generator->setGeneratorMode();
560 * For generator mode, execute generator, and use its output as new
562 * @param $generator ApiQueryGeneratorBase Generator Module
563 * @param $modules array of module objects
565 protected function executeGeneratorModule( $generator, $modules ) {
567 $resultPageSet = new ApiPageSet( $this, $this->redirects
, $this->convertTitles
);
569 // Add any additional fields modules may need
570 $generator->requestExtraData( $this->mPageSet
);
571 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
573 // Populate page information with the original user input
574 $this->mPageSet
->execute();
576 // populate resultPageSet with the generator output
577 $generator->profileIn();
578 $generator->executeGenerator( $resultPageSet );
579 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
580 $resultPageSet->finishPageSetGeneration();
581 $generator->profileOut();
583 // Swap the resulting pageset back in
584 $this->mPageSet
= $resultPageSet;
587 public function getAllowedParams() {
590 ApiBase
::PARAM_ISMULTI
=> true,
591 ApiBase
::PARAM_TYPE
=> $this->mPropModuleNames
594 ApiBase
::PARAM_ISMULTI
=> true,
595 ApiBase
::PARAM_TYPE
=> $this->mListModuleNames
598 ApiBase
::PARAM_ISMULTI
=> true,
599 ApiBase
::PARAM_TYPE
=> $this->mMetaModuleNames
601 'generator' => array(
602 ApiBase
::PARAM_TYPE
=> $this->mAllowedGenerators
604 'redirects' => false,
605 'converttitles' => false,
606 'indexpageids' => false,
608 'exportnowrap' => false,
614 * Override the parent to generate help messages for all available query modules.
617 public function makeHelpMsg() {
618 // Make sure the internal object is empty
619 // (just in case a sub-module decides to optimize during instantiation)
620 $this->mPageSet
= null;
622 $querySeparator = str_repeat( '--- ', 12 );
623 $moduleSeparator = str_repeat( '*** ', 14 );
624 $msg = "\n$querySeparator Query: Prop $querySeparator\n\n";
625 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules
, 'prop' );
626 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
627 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules
, 'list' );
628 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
629 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules
, 'meta' );
630 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
632 // Use parent to make default message for the query module
633 $msg = parent
::makeHelpMsg() . $msg;
639 * For all modules in $moduleList, generate help messages and join them together
640 * @param $moduleList Array array(modulename => classname)
641 * @param $paramName string Parameter name
644 private function makeHelpMsgHelper( $moduleList, $paramName ) {
645 $moduleDescriptions = array();
647 foreach ( $moduleList as $moduleName => $moduleClass ) {
649 * @var $module ApiQueryBase
651 $module = new $moduleClass( $this, $moduleName, null );
653 $msg = ApiMain
::makeHelpMsgHeader( $module, $paramName );
654 $msg2 = $module->makeHelpMsg();
655 if ( $msg2 !== false ) {
658 if ( $module instanceof ApiQueryGeneratorBase
) {
659 $msg .= "Generator:\n This module may be used as a generator\n";
661 $moduleDescriptions[] = $msg;
664 return implode( "\n", $moduleDescriptions );
668 * Adds any classes that are a subclass of ApiQueryGeneratorBase
669 * to the allowed generator list
670 * @param $moduleList array()
672 private function makeGeneratorList( $moduleList ) {
673 foreach( $moduleList as $moduleName => $moduleClass ) {
674 if ( is_subclass_of( $moduleClass, 'ApiQueryGeneratorBase' ) ) {
675 $this->mAllowedGenerators
[] = $moduleName;
681 * Override to add extra parameters from PageSet
684 public function makeHelpMsgParameters() {
685 $psModule = new ApiPageSet( $this );
686 return $psModule->makeHelpMsgParameters() . parent
::makeHelpMsgParameters();
689 public function shouldCheckMaxlag() {
693 public function getParamDescription() {
695 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
696 'list' => 'Which lists to get. Module help is available below',
697 'meta' => 'Which metadata to get about the site. Module help is available below',
698 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
699 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
700 'redirects' => 'Automatically resolve redirects',
701 'converttitles' => array( "Convert titles to other variants if necessary. Only works if the wiki's content language supports variant conversion.",
702 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter
::$languagesWithVariants ) ),
703 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
704 'export' => 'Export the current revisions of all given or generated pages',
705 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
706 'iwurl' => 'Whether to get the full URL if the title is an interwiki link',
710 public function getDescription() {
712 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
713 'and is loosely based on the old query.php interface.',
714 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
718 public function getPossibleErrors() {
719 return array_merge( parent
::getPossibleErrors(), array(
720 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
724 public function getExamples() {
726 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
727 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
731 public function getHelpUrls() {
733 'https://www.mediawiki.org/wiki/API:Meta',
734 'https://www.mediawiki.org/wiki/API:Properties',
735 'https://www.mediawiki.org/wiki/API:Lists',
739 public function getVersion() {
740 $psModule = new ApiPageSet( $this );
742 $vers[] = __CLASS__
. ': $Id$';
743 $vers[] = $psModule->getVersion();