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