New hooks ApiMain::moduleManager and ApiQuery::moduleManager
[lhc/web/wiklou.git] / includes / api / ApiQuery.php
1 <?php
2 /**
3 *
4 *
5 * Created on Sep 7, 2006
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
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.
32 *
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.
35 *
36 * @ingroup API
37 */
38 class ApiQuery extends ApiBase {
39
40 /**
41 * List of Api Query prop modules
42 * @var array
43 */
44 private static $QueryPropModules = array(
45 'categories' => 'ApiQueryCategories',
46 'categoryinfo' => 'ApiQueryCategoryInfo',
47 'contributors' => 'ApiQueryContributors',
48 'deletedrevisions' => 'ApiQueryDeletedRevisions',
49 'duplicatefiles' => 'ApiQueryDuplicateFiles',
50 'extlinks' => 'ApiQueryExternalLinks',
51 'fileusage' => 'ApiQueryBacklinksprop',
52 'images' => 'ApiQueryImages',
53 'imageinfo' => 'ApiQueryImageInfo',
54 'info' => 'ApiQueryInfo',
55 'links' => 'ApiQueryLinks',
56 'linkshere' => 'ApiQueryBacklinksprop',
57 'iwlinks' => 'ApiQueryIWLinks',
58 'langlinks' => 'ApiQueryLangLinks',
59 'pageprops' => 'ApiQueryPageProps',
60 'redirects' => 'ApiQueryBacklinksprop',
61 'revisions' => 'ApiQueryRevisions',
62 'stashimageinfo' => 'ApiQueryStashImageInfo',
63 'templates' => 'ApiQueryLinks',
64 'transcludedin' => 'ApiQueryBacklinksprop',
65 );
66
67 /**
68 * List of Api Query list modules
69 * @var array
70 */
71 private static $QueryListModules = array(
72 'allcategories' => 'ApiQueryAllCategories',
73 'alldeletedrevisions' => 'ApiQueryAllDeletedRevisions',
74 'allfileusages' => 'ApiQueryAllLinks',
75 'allimages' => 'ApiQueryAllImages',
76 'alllinks' => 'ApiQueryAllLinks',
77 'allpages' => 'ApiQueryAllPages',
78 'allredirects' => 'ApiQueryAllLinks',
79 'alltransclusions' => 'ApiQueryAllLinks',
80 'allusers' => 'ApiQueryAllUsers',
81 'backlinks' => 'ApiQueryBacklinks',
82 'blocks' => 'ApiQueryBlocks',
83 'categorymembers' => 'ApiQueryCategoryMembers',
84 'deletedrevs' => 'ApiQueryDeletedrevs',
85 'embeddedin' => 'ApiQueryBacklinks',
86 'exturlusage' => 'ApiQueryExtLinksUsage',
87 'filearchive' => 'ApiQueryFilearchive',
88 'imageusage' => 'ApiQueryBacklinks',
89 'iwbacklinks' => 'ApiQueryIWBacklinks',
90 'langbacklinks' => 'ApiQueryLangBacklinks',
91 'logevents' => 'ApiQueryLogEvents',
92 'pageswithprop' => 'ApiQueryPagesWithProp',
93 'pagepropnames' => 'ApiQueryPagePropNames',
94 'prefixsearch' => 'ApiQueryPrefixSearch',
95 'protectedtitles' => 'ApiQueryProtectedTitles',
96 'querypage' => 'ApiQueryQueryPage',
97 'random' => 'ApiQueryRandom',
98 'recentchanges' => 'ApiQueryRecentChanges',
99 'search' => 'ApiQuerySearch',
100 'tags' => 'ApiQueryTags',
101 'usercontribs' => 'ApiQueryContributions',
102 'users' => 'ApiQueryUsers',
103 'watchlist' => 'ApiQueryWatchlist',
104 'watchlistraw' => 'ApiQueryWatchlistRaw',
105 );
106
107 /**
108 * List of Api Query meta modules
109 * @var array
110 */
111 private static $QueryMetaModules = array(
112 'allmessages' => 'ApiQueryAllMessages',
113 'siteinfo' => 'ApiQuerySiteinfo',
114 'userinfo' => 'ApiQueryUserInfo',
115 'filerepoinfo' => 'ApiQueryFileRepoInfo',
116 'tokens' => 'ApiQueryTokens',
117 );
118
119 /**
120 * @var ApiPageSet
121 */
122 private $mPageSet;
123
124 private $mParams;
125 private $mNamedDB = array();
126 private $mModuleMgr;
127
128 /**
129 * @param ApiMain $main
130 * @param string $action
131 */
132 public function __construct( ApiMain $main, $action ) {
133 parent::__construct( $main, $action );
134
135 $this->mModuleMgr = new ApiModuleManager( $this );
136
137 // Allow custom modules to be added in LocalSettings.php
138 $config = $this->getConfig();
139 $this->mModuleMgr->addModules( self::$QueryPropModules, 'prop' );
140 $this->mModuleMgr->addModules( $config->get( 'APIPropModules' ), 'prop' );
141 $this->mModuleMgr->addModules( self::$QueryListModules, 'list' );
142 $this->mModuleMgr->addModules( $config->get( 'APIListModules' ), 'list' );
143 $this->mModuleMgr->addModules( self::$QueryMetaModules, 'meta' );
144 $this->mModuleMgr->addModules( $config->get( 'APIMetaModules' ), 'meta' );
145
146 Hooks::run( 'ApiQuery::moduleManager', array( $this->mModuleMgr ) );
147
148 // Create PageSet that will process titles/pageids/revids/generator
149 $this->mPageSet = new ApiPageSet( $this );
150 }
151
152 /**
153 * Overrides to return this instance's module manager.
154 * @return ApiModuleManager
155 */
156 public function getModuleManager() {
157 return $this->mModuleMgr;
158 }
159
160 /**
161 * Get the query database connection with the given name.
162 * If no such connection has been requested before, it will be created.
163 * Subsequent calls with the same $name will return the same connection
164 * as the first, regardless of the values of $db and $groups
165 * @param string $name Name to assign to the database connection
166 * @param int $db One of the DB_* constants
167 * @param array $groups Query groups
168 * @return DatabaseBase
169 */
170 public function getNamedDB( $name, $db, $groups ) {
171 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
172 $this->profileDBIn();
173 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
174 $this->profileDBOut();
175 }
176
177 return $this->mNamedDB[$name];
178 }
179
180 /**
181 * Gets the set of pages the user has requested (or generated)
182 * @return ApiPageSet
183 */
184 public function getPageSet() {
185 return $this->mPageSet;
186 }
187
188 /**
189 * Get the array mapping module names to class names
190 * @deprecated since 1.21, use getModuleManager()'s methods instead
191 * @return array Array(modulename => classname)
192 */
193 public function getModules() {
194 wfDeprecated( __METHOD__, '1.21' );
195
196 return $this->getModuleManager()->getNamesWithClasses();
197 }
198
199 /**
200 * Get the generators array mapping module names to class names
201 * @deprecated since 1.21, list of generators is maintained by ApiPageSet
202 * @return array Array(modulename => classname)
203 */
204 public function getGenerators() {
205 wfDeprecated( __METHOD__, '1.21' );
206 $gens = array();
207 foreach ( $this->mModuleMgr->getNamesWithClasses() as $name => $class ) {
208 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
209 $gens[$name] = $class;
210 }
211 }
212
213 return $gens;
214 }
215
216 /**
217 * Get whether the specified module is a prop, list or a meta query module
218 * @deprecated since 1.21, use getModuleManager()->getModuleGroup()
219 * @param string $moduleName Name of the module to find type for
220 * @return string|null
221 */
222 function getModuleType( $moduleName ) {
223 return $this->getModuleManager()->getModuleGroup( $moduleName );
224 }
225
226 /**
227 * @return ApiFormatRaw|null
228 */
229 public function getCustomPrinter() {
230 // If &exportnowrap is set, use the raw formatter
231 if ( $this->getParameter( 'export' ) &&
232 $this->getParameter( 'exportnowrap' )
233 ) {
234 return new ApiFormatRaw( $this->getMain(),
235 $this->getMain()->createPrinterByName( 'xml' ) );
236 } else {
237 return null;
238 }
239 }
240
241 /**
242 * Query execution happens in the following steps:
243 * #1 Create a PageSet object with any pages requested by the user
244 * #2 If using a generator, execute it to get a new ApiPageSet object
245 * #3 Instantiate all requested modules.
246 * This way the PageSet object will know what shared data is required,
247 * and minimize DB calls.
248 * #4 Output all normalization and redirect resolution information
249 * #5 Execute all requested modules
250 */
251 public function execute() {
252 $this->mParams = $this->extractRequestParams();
253
254 if ( $this->mParams['continue'] === null && !$this->mParams['rawcontinue'] ) {
255 $this->logFeatureUsage( 'action=query&!rawcontinue&!continue' );
256 $this->setWarning(
257 'Formatting of continuation data will be changing soon. ' .
258 'To continue using the current formatting, use the \'rawcontinue\' parameter. ' .
259 'To begin using the new format, pass an empty string for \'continue\' ' .
260 'in the initial query.'
261 );
262 }
263
264 // Instantiate requested modules
265 $allModules = array();
266 $this->instantiateModules( $allModules, 'prop' );
267 $propModules = array_keys( $allModules );
268 $this->instantiateModules( $allModules, 'list' );
269 $this->instantiateModules( $allModules, 'meta' );
270
271 // Filter modules based on continue parameter
272 list( $generatorDone, $modules ) = $this->getResult()->beginContinuation(
273 $this->mParams['continue'], $allModules, $propModules
274 );
275
276 if ( !$generatorDone ) {
277 // Query modules may optimize data requests through the $this->getPageSet()
278 // object by adding extra fields from the page table.
279 foreach ( $modules as $module ) {
280 $module->requestExtraData( $this->mPageSet );
281 }
282 // Populate page/revision information
283 $this->mPageSet->execute();
284 // Record page information (title, namespace, if exists, etc)
285 $this->outputGeneralPageInfo();
286 } else {
287 $this->mPageSet->executeDryRun();
288 }
289
290 $cacheMode = $this->mPageSet->getCacheMode();
291
292 // Execute all unfinished modules
293 /** @var $module ApiQueryBase */
294 foreach ( $modules as $module ) {
295 $params = $module->extractRequestParams();
296 $cacheMode = $this->mergeCacheMode(
297 $cacheMode, $module->getCacheMode( $params ) );
298 $module->profileIn();
299 $module->execute();
300 Hooks::run( 'APIQueryAfterExecute', array( &$module ) );
301 $module->profileOut();
302 }
303
304 // Set the cache mode
305 $this->getMain()->setCacheMode( $cacheMode );
306
307 // Write the continuation data into the result
308 $this->getResult()->endContinuation(
309 $this->mParams['continue'] === null ? 'raw' : 'standard'
310 );
311 }
312
313 /**
314 * Update a cache mode string, applying the cache mode of a new module to it.
315 * The cache mode may increase in the level of privacy, but public modules
316 * added to private data do not decrease the level of privacy.
317 *
318 * @param string $cacheMode
319 * @param string $modCacheMode
320 * @return string
321 */
322 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
323 if ( $modCacheMode === 'anon-public-user-private' ) {
324 if ( $cacheMode !== 'private' ) {
325 $cacheMode = 'anon-public-user-private';
326 }
327 } elseif ( $modCacheMode === 'public' ) {
328 // do nothing, if it's public already it will stay public
329 } else { // private
330 $cacheMode = 'private';
331 }
332
333 return $cacheMode;
334 }
335
336 /**
337 * Create instances of all modules requested by the client
338 * @param array $modules To append instantiated modules to
339 * @param string $param Parameter name to read modules from
340 */
341 private function instantiateModules( &$modules, $param ) {
342 $wasPosted = $this->getRequest()->wasPosted();
343 if ( isset( $this->mParams[$param] ) ) {
344 foreach ( $this->mParams[$param] as $moduleName ) {
345 $instance = $this->mModuleMgr->getModule( $moduleName, $param );
346 if ( $instance === null ) {
347 ApiBase::dieDebug( __METHOD__, 'Error instantiating module' );
348 }
349 if ( !$wasPosted && $instance->mustBePosted() ) {
350 $this->dieUsageMsgOrDebug( array( 'mustbeposted', $moduleName ) );
351 }
352 // Ignore duplicates. TODO 2.0: die()?
353 if ( !array_key_exists( $moduleName, $modules ) ) {
354 $modules[$moduleName] = $instance;
355 }
356 }
357 }
358 }
359
360 /**
361 * Appends an element for each page in the current pageSet with the
362 * most general information (id, title), plus any title normalizations
363 * and missing or invalid title/pageids/revids.
364 */
365 private function outputGeneralPageInfo() {
366 $pageSet = $this->getPageSet();
367 $result = $this->getResult();
368
369 // We can't really handle max-result-size failure here, but we need to
370 // check anyway in case someone set the limit stupidly low.
371 $fit = true;
372
373 $values = $pageSet->getNormalizedTitlesAsResult( $result );
374 if ( $values ) {
375 $fit = $fit && $result->addValue( 'query', 'normalized', $values );
376 }
377 $values = $pageSet->getConvertedTitlesAsResult( $result );
378 if ( $values ) {
379 $fit = $fit && $result->addValue( 'query', 'converted', $values );
380 }
381 $values = $pageSet->getInterwikiTitlesAsResult( $result, $this->mParams['iwurl'] );
382 if ( $values ) {
383 $fit = $fit && $result->addValue( 'query', 'interwiki', $values );
384 }
385 $values = $pageSet->getRedirectTitlesAsResult( $result );
386 if ( $values ) {
387 $fit = $fit && $result->addValue( 'query', 'redirects', $values );
388 }
389 $values = $pageSet->getMissingRevisionIDsAsResult( $result );
390 if ( $values ) {
391 $fit = $fit && $result->addValue( 'query', 'badrevids', $values );
392 }
393
394 // Page elements
395 $pages = array();
396
397 // Report any missing titles
398 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
399 $vals = array();
400 ApiQueryBase::addTitleInfo( $vals, $title );
401 $vals['missing'] = '';
402 $pages[$fakeId] = $vals;
403 }
404 // Report any invalid titles
405 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
406 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
407 }
408 // Report any missing page ids
409 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
410 $pages[$pageid] = array(
411 'pageid' => $pageid,
412 'missing' => ''
413 );
414 }
415 // Report special pages
416 /** @var $title Title */
417 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
418 $vals = array();
419 ApiQueryBase::addTitleInfo( $vals, $title );
420 $vals['special'] = '';
421 if ( $title->isSpecialPage() &&
422 !SpecialPageFactory::exists( $title->getDBkey() )
423 ) {
424 $vals['missing'] = '';
425 } elseif ( $title->getNamespace() == NS_MEDIA &&
426 !wfFindFile( $title )
427 ) {
428 $vals['missing'] = '';
429 }
430 $pages[$fakeId] = $vals;
431 }
432
433 // Output general page information for found titles
434 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
435 $vals = array();
436 $vals['pageid'] = $pageid;
437 ApiQueryBase::addTitleInfo( $vals, $title );
438 $pages[$pageid] = $vals;
439 }
440
441 if ( count( $pages ) ) {
442 $pageSet->populateGeneratorData( $pages );
443
444 if ( $this->mParams['indexpageids'] ) {
445 $pageIDs = array_keys( $pages );
446 // json treats all map keys as strings - converting to match
447 $pageIDs = array_map( 'strval', $pageIDs );
448 $result->setIndexedTagName( $pageIDs, 'id' );
449 $fit = $fit && $result->addValue( 'query', 'pageids', $pageIDs );
450 }
451
452 $result->setIndexedTagName( $pages, 'page' );
453 $fit = $fit && $result->addValue( 'query', 'pages', $pages );
454 }
455
456 if ( !$fit ) {
457 $this->dieUsage(
458 'The value of $wgAPIMaxResultSize on this wiki is ' .
459 'too small to hold basic result information',
460 'badconfig'
461 );
462 }
463
464 if ( $this->mParams['export'] ) {
465 $this->doExport( $pageSet, $result );
466 }
467 }
468
469 /**
470 * This method is called by the generator base when generator in the smart-continue
471 * mode tries to set 'query-continue' value. ApiQuery stores those values separately
472 * until the post-processing when it is known if the generation should continue or repeat.
473 * @deprecated since 1.24
474 * @param ApiQueryGeneratorBase $module Generator module
475 * @param string $paramName
476 * @param mixed $paramValue
477 * @return bool True if processed, false if this is a legacy continue
478 */
479 public function setGeneratorContinue( $module, $paramName, $paramValue ) {
480 wfDeprecated( __METHOD__, '1.24' );
481 $this->getResult()->setGeneratorContinueParam( $module, $paramName, $paramValue );
482 return $this->getParameter( 'continue' ) !== null;
483 }
484
485 /**
486 * @param ApiPageSet $pageSet Pages to be exported
487 * @param ApiResult $result Result to output to
488 */
489 private function doExport( $pageSet, $result ) {
490 $exportTitles = array();
491 $titles = $pageSet->getGoodTitles();
492 if ( count( $titles ) ) {
493 $user = $this->getUser();
494 /** @var $title Title */
495 foreach ( $titles as $title ) {
496 if ( $title->userCan( 'read', $user ) ) {
497 $exportTitles[] = $title;
498 }
499 }
500 }
501
502 $exporter = new WikiExporter( $this->getDB() );
503 // WikiExporter writes to stdout, so catch its
504 // output with an ob
505 ob_start();
506 $exporter->openStream();
507 foreach ( $exportTitles as $title ) {
508 $exporter->pageByTitle( $title );
509 }
510 $exporter->closeStream();
511 $exportxml = ob_get_contents();
512 ob_end_clean();
513
514 // Don't check the size of exported stuff
515 // It's not continuable, so it would cause more
516 // problems than it'd solve
517 if ( $this->mParams['exportnowrap'] ) {
518 $result->reset();
519 // Raw formatter will handle this
520 $result->addValue( null, 'text', $exportxml, ApiResult::NO_SIZE_CHECK );
521 $result->addValue( null, 'mime', 'text/xml', ApiResult::NO_SIZE_CHECK );
522 } else {
523 $r = array();
524 ApiResult::setContent( $r, $exportxml );
525 $result->addValue( 'query', 'export', $r, ApiResult::NO_SIZE_CHECK );
526 }
527 }
528
529 public function getAllowedParams( $flags = 0 ) {
530 $result = array(
531 'prop' => array(
532 ApiBase::PARAM_ISMULTI => true,
533 ApiBase::PARAM_TYPE => 'submodule',
534 ),
535 'list' => array(
536 ApiBase::PARAM_ISMULTI => true,
537 ApiBase::PARAM_TYPE => 'submodule',
538 ),
539 'meta' => array(
540 ApiBase::PARAM_ISMULTI => true,
541 ApiBase::PARAM_TYPE => 'submodule',
542 ),
543 'indexpageids' => false,
544 'export' => false,
545 'exportnowrap' => false,
546 'iwurl' => false,
547 'continue' => null,
548 'rawcontinue' => false,
549 );
550 if ( $flags ) {
551 $result += $this->getPageSet()->getFinalParams( $flags );
552 }
553
554 return $result;
555 }
556
557 /**
558 * Override the parent to generate help messages for all available query modules.
559 * @deprecated since 1.25
560 * @return string
561 */
562 public function makeHelpMsg() {
563 wfDeprecated( __METHOD__, '1.25' );
564
565 // Use parent to make default message for the query module
566 $msg = parent::makeHelpMsg();
567
568 $querySeparator = str_repeat( '--- ', 12 );
569 $moduleSeparator = str_repeat( '*** ', 14 );
570 $msg .= "\n$querySeparator Query: Prop $querySeparator\n\n";
571 $msg .= $this->makeHelpMsgHelper( 'prop' );
572 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
573 $msg .= $this->makeHelpMsgHelper( 'list' );
574 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
575 $msg .= $this->makeHelpMsgHelper( 'meta' );
576 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
577
578 return $msg;
579 }
580
581 /**
582 * For all modules of a given group, generate help messages and join them together
583 * @deprecated since 1.25
584 * @param string $group Module group
585 * @return string
586 */
587 private function makeHelpMsgHelper( $group ) {
588 $moduleDescriptions = array();
589
590 $moduleNames = $this->mModuleMgr->getNames( $group );
591 sort( $moduleNames );
592 foreach ( $moduleNames as $name ) {
593 /**
594 * @var $module ApiQueryBase
595 */
596 $module = $this->mModuleMgr->getModule( $name );
597
598 $msg = ApiMain::makeHelpMsgHeader( $module, $group );
599 $msg2 = $module->makeHelpMsg();
600 if ( $msg2 !== false ) {
601 $msg .= $msg2;
602 }
603 if ( $module instanceof ApiQueryGeneratorBase ) {
604 $msg .= "Generator:\n This module may be used as a generator\n";
605 }
606 $moduleDescriptions[] = $msg;
607 }
608
609 return implode( "\n", $moduleDescriptions );
610 }
611
612 public function shouldCheckMaxlag() {
613 return true;
614 }
615
616 protected function getExamplesMessages() {
617 return array(
618 'action=query&prop=revisions&meta=siteinfo&' .
619 'titles=Main%20Page&rvprop=user|comment&continue='
620 => 'apihelp-query-example-revisions',
621 'action=query&generator=allpages&gapprefix=API/&prop=revisions&continue='
622 => 'apihelp-query-example-allpages',
623 );
624 }
625
626 public function getHelpUrls() {
627 return array(
628 'https://www.mediawiki.org/wiki/API:Query',
629 'https://www.mediawiki.org/wiki/API:Meta',
630 'https://www.mediawiki.org/wiki/API:Properties',
631 'https://www.mediawiki.org/wiki/API:Lists',
632 );
633 }
634 }