Merge "numRows on MySQL no longer propagates unrelated errors"
[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 'duplicatefiles' => 'ApiQueryDuplicateFiles',
48 'extlinks' => 'ApiQueryExternalLinks',
49 'images' => 'ApiQueryImages',
50 'imageinfo' => 'ApiQueryImageInfo',
51 'info' => 'ApiQueryInfo',
52 'links' => 'ApiQueryLinks',
53 'iwlinks' => 'ApiQueryIWLinks',
54 'langlinks' => 'ApiQueryLangLinks',
55 'pageprops' => 'ApiQueryPageProps',
56 'revisions' => 'ApiQueryRevisions',
57 'stashimageinfo' => 'ApiQueryStashImageInfo',
58 'templates' => 'ApiQueryLinks',
59 );
60
61 /**
62 * List of Api Query list modules
63 * @var array
64 */
65 private static $QueryListModules = array(
66 'allcategories' => 'ApiQueryAllCategories',
67 'allimages' => 'ApiQueryAllImages',
68 'alllinks' => 'ApiQueryAllLinks',
69 'allpages' => 'ApiQueryAllPages',
70 'alltransclusions' => 'ApiQueryAllLinks',
71 'allusers' => 'ApiQueryAllUsers',
72 'backlinks' => 'ApiQueryBacklinks',
73 'blocks' => 'ApiQueryBlocks',
74 'categorymembers' => 'ApiQueryCategoryMembers',
75 'deletedrevs' => 'ApiQueryDeletedrevs',
76 'embeddedin' => 'ApiQueryBacklinks',
77 'exturlusage' => 'ApiQueryExtLinksUsage',
78 'filearchive' => 'ApiQueryFilearchive',
79 'imageusage' => 'ApiQueryBacklinks',
80 'iwbacklinks' => 'ApiQueryIWBacklinks',
81 'langbacklinks' => 'ApiQueryLangBacklinks',
82 'logevents' => 'ApiQueryLogEvents',
83 'pageswithprop' => 'ApiQueryPagesWithProp',
84 'pagepropnames' => 'ApiQueryPagePropNames',
85 'protectedtitles' => 'ApiQueryProtectedTitles',
86 'querypage' => 'ApiQueryQueryPage',
87 'random' => 'ApiQueryRandom',
88 'recentchanges' => 'ApiQueryRecentChanges',
89 'search' => 'ApiQuerySearch',
90 'tags' => 'ApiQueryTags',
91 'usercontribs' => 'ApiQueryContributions',
92 'users' => 'ApiQueryUsers',
93 'watchlist' => 'ApiQueryWatchlist',
94 'watchlistraw' => 'ApiQueryWatchlistRaw',
95 );
96
97 /**
98 * List of Api Query meta modules
99 * @var array
100 */
101 private static $QueryMetaModules = array(
102 'allmessages' => 'ApiQueryAllMessages',
103 'siteinfo' => 'ApiQuerySiteinfo',
104 'userinfo' => 'ApiQueryUserInfo',
105 );
106
107 /**
108 * @var ApiPageSet
109 */
110 private $mPageSet;
111
112 private $mParams;
113 private $mNamedDB = array();
114 private $mModuleMgr;
115 private $mGeneratorContinue;
116 private $mUseLegacyContinue;
117
118 /**
119 * @param $main ApiMain
120 * @param $action string
121 */
122 public function __construct( $main, $action ) {
123 parent::__construct( $main, $action );
124
125 $this->mModuleMgr = new ApiModuleManager( $this );
126
127 // Allow custom modules to be added in LocalSettings.php
128 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
129 $this->mModuleMgr->addModules( self::$QueryPropModules, 'prop' );
130 $this->mModuleMgr->addModules( $wgAPIPropModules, 'prop' );
131 $this->mModuleMgr->addModules( self::$QueryListModules, 'list' );
132 $this->mModuleMgr->addModules( $wgAPIListModules, 'list' );
133 $this->mModuleMgr->addModules( self::$QueryMetaModules, 'meta' );
134 $this->mModuleMgr->addModules( $wgAPIMetaModules, 'meta' );
135
136 // Create PageSet that will process titles/pageids/revids/generator
137 $this->mPageSet = new ApiPageSet( $this );
138 }
139
140 /**
141 * Overrides to return this instance's module manager.
142 * @return ApiModuleManager
143 */
144 public function getModuleManager() {
145 return $this->mModuleMgr;
146 }
147
148 /**
149 * Get the query database connection with the given name.
150 * If no such connection has been requested before, it will be created.
151 * Subsequent calls with the same $name will return the same connection
152 * as the first, regardless of the values of $db and $groups
153 * @param string $name Name to assign to the database connection
154 * @param int $db One of the DB_* constants
155 * @param array $groups Query groups
156 * @return DatabaseBase
157 */
158 public function getNamedDB( $name, $db, $groups ) {
159 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
160 $this->profileDBIn();
161 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
162 $this->profileDBOut();
163 }
164 return $this->mNamedDB[$name];
165 }
166
167 /**
168 * Gets the set of pages the user has requested (or generated)
169 * @return ApiPageSet
170 */
171 public function getPageSet() {
172 return $this->mPageSet;
173 }
174
175 /**
176 * Get the array mapping module names to class names
177 * @deprecated since 1.21, use getModuleManager()'s methods instead
178 * @return array array(modulename => classname)
179 */
180 public function getModules() {
181 wfDeprecated( __METHOD__, '1.21' );
182 return $this->getModuleManager()->getNamesWithClasses();
183 }
184
185 /**
186 * Get the generators array mapping module names to class names
187 * @deprecated since 1.21, list of generators is maintained by ApiPageSet
188 * @return array array(modulename => classname)
189 */
190 public function getGenerators() {
191 wfDeprecated( __METHOD__, '1.21' );
192 $gens = array();
193 foreach ( $this->mModuleMgr->getNamesWithClasses() as $name => $class ) {
194 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
195 $gens[$name] = $class;
196 }
197 }
198 return $gens;
199 }
200
201 /**
202 * Get whether the specified module is a prop, list or a meta query module
203 * @deprecated since 1.21, use getModuleManager()->getModuleGroup()
204 * @param string $moduleName Name of the module to find type for
205 * @return mixed string or null
206 */
207 function getModuleType( $moduleName ) {
208 return $this->getModuleManager()->getModuleGroup( $moduleName );
209 }
210
211 /**
212 * @return ApiFormatRaw|null
213 */
214 public function getCustomPrinter() {
215 // If &exportnowrap is set, use the raw formatter
216 if ( $this->getParameter( 'export' ) &&
217 $this->getParameter( 'exportnowrap' ) )
218 {
219 return new ApiFormatRaw( $this->getMain(),
220 $this->getMain()->createPrinterByName( 'xml' ) );
221 } else {
222 return null;
223 }
224 }
225
226 /**
227 * Query execution happens in the following steps:
228 * #1 Create a PageSet object with any pages requested by the user
229 * #2 If using a generator, execute it to get a new ApiPageSet object
230 * #3 Instantiate all requested modules.
231 * This way the PageSet object will know what shared data is required,
232 * and minimize DB calls.
233 * #4 Output all normalization and redirect resolution information
234 * #5 Execute all requested modules
235 */
236 public function execute() {
237 $this->mParams = $this->extractRequestParams();
238
239 // $pagesetParams is a array of parameter names used by the pageset generator
240 // or null if pageset has already finished and is no longer needed
241 // $completeModules is a set of complete modules with the name as key
242 $this->initContinue( $pagesetParams, $completeModules );
243
244 // Instantiate requested modules
245 $allModules = array();
246 $this->instantiateModules( $allModules, 'prop' );
247 $propModules = $allModules; // Keep a copy
248 $this->instantiateModules( $allModules, 'list' );
249 $this->instantiateModules( $allModules, 'meta' );
250
251 // Filter modules based on continue parameter
252 $modules = $this->initModules( $allModules, $completeModules, $pagesetParams !== null );
253
254 // Execute pageset if in legacy mode or if pageset is not done
255 if ( $completeModules === null || $pagesetParams !== null ) {
256 // Populate page/revision information
257 $this->mPageSet->execute();
258 // Record page information (title, namespace, if exists, etc)
259 $this->outputGeneralPageInfo();
260 } else {
261 $this->mPageSet->executeDryRun();
262 }
263
264 $cacheMode = $this->mPageSet->getCacheMode();
265
266 // Execute all unfinished modules
267 /** @var $module ApiQueryBase */
268 foreach ( $modules as $module ) {
269 $params = $module->extractRequestParams();
270 $cacheMode = $this->mergeCacheMode(
271 $cacheMode, $module->getCacheMode( $params ) );
272 $module->profileIn();
273 $module->execute();
274 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
275 $module->profileOut();
276 }
277
278 // Set the cache mode
279 $this->getMain()->setCacheMode( $cacheMode );
280
281 if ( $completeModules === null ) {
282 return; // Legacy continue, we are done
283 }
284
285 // Reformat query-continue result section
286 $result = $this->getResult();
287 $qc = $result->getData();
288 if ( isset( $qc['query-continue'] ) ) {
289 $qc = $qc['query-continue'];
290 $result->unsetValue( null, 'query-continue' );
291 } elseif ( $this->mGeneratorContinue !== null ) {
292 $qc = array();
293 } else {
294 // no more "continue"s, we are done!
295 return;
296 }
297
298 // we are done with all the modules that do not have result in query-continue
299 $completeModules = array_merge( $completeModules, array_diff_key( $modules, $qc ) );
300 if ( $pagesetParams !== null ) {
301 // The pageset is still in use, check if all props have finished
302 $incompleteProps = array_intersect_key( $propModules, $qc );
303 if ( count( $incompleteProps ) > 0 ) {
304 // Properties are not done, continue with the same pageset state - copy current parameters
305 $main = $this->getMain();
306 $contValues = array();
307 foreach ( $pagesetParams as $param ) {
308 // The param name is already prefix-encoded
309 $contValues[$param] = $main->getVal( $param );
310 }
311 } elseif ( $this->mGeneratorContinue !== null ) {
312 // Move to the next set of pages produced by pageset, properties need to be restarted
313 $contValues = $this->mGeneratorContinue;
314 $pagesetParams = array_keys( $contValues );
315 $completeModules = array_diff_key( $completeModules, $propModules );
316 } else {
317 // Done with the pageset, finish up with the the lists and meta modules
318 $pagesetParams = null;
319 }
320 }
321
322 $continue = '||' . implode( '|', array_keys( $completeModules ) );
323 if ( $pagesetParams !== null ) {
324 // list of all pageset parameters to use in the next request
325 $continue = implode( '|', $pagesetParams ) . $continue;
326 } else {
327 // we are done with the pageset
328 $contValues = array();
329 $continue = '-' . $continue;
330 }
331 $contValues['continue'] = $continue;
332 foreach ( $qc as $qcModule ) {
333 foreach ( $qcModule as $qcKey => $qcValue ) {
334 $contValues[$qcKey] = $qcValue;
335 }
336 }
337 $this->getResult()->addValue( null, 'continue', $contValues );
338 }
339
340 /**
341 * Parse 'continue' parameter into the list of complete modules and a list of generator parameters
342 * @param array|null $pagesetParams returns list of generator params or null if pageset is done
343 * @param array|null $completeModules returns list of finished modules (as keys), or null if legacy
344 */
345 private function initContinue( &$pagesetParams, &$completeModules ) {
346 $pagesetParams = array();
347 $continue = $this->mParams['continue'];
348 if ( $continue !== null ) {
349 $this->mUseLegacyContinue = false;
350 if ( $continue !== '' ) {
351 // Format: ' pagesetParam1 | pagesetParam2 || module1 | module2 | module3 | ...
352 // If pageset is done, use '-'
353 $continue = explode( '||', $continue );
354 $this->dieContinueUsageIf( count( $continue ) !== 2 );
355 if ( $continue[0] === '-' ) {
356 $pagesetParams = null; // No need to execute pageset
357 } elseif ( $continue[0] !== '' ) {
358 // list of pageset params that might need to be repeated
359 $pagesetParams = explode( '|', $continue[0] );
360 }
361 $continue = $continue[1];
362 }
363 if ( $continue !== '' ) {
364 $completeModules = array_flip( explode( '|', $continue ) );
365 } else {
366 $completeModules = array();
367 }
368 } else {
369 $this->mUseLegacyContinue = true;
370 $completeModules = null;
371 }
372 }
373
374 /**
375 * Validate sub-modules, filter out completed ones, and do requestExtraData()
376 * @param array $allModules An dict of name=>instance of all modules requested by the client
377 * @param array|null $completeModules list of finished modules, or null if legacy continue
378 * @param bool $usePageset True if pageset will be executed
379 * @return array of modules to be processed during this execution
380 */
381 private function initModules( $allModules, $completeModules, $usePageset ) {
382 $modules = $allModules;
383 $tmp = $completeModules;
384 $wasPosted = $this->getRequest()->wasPosted();
385 $main = $this->getMain();
386
387 /** @var $module ApiQueryBase */
388 foreach ( $allModules as $moduleName => $module ) {
389 if ( !$wasPosted && $module->mustBePosted() ) {
390 $this->dieUsageMsgOrDebug( array( 'mustbeposted', $moduleName ) );
391 }
392 if ( $completeModules !== null && array_key_exists( $moduleName, $completeModules ) ) {
393 // If this module is done, mark all its params as used
394 $module->extractRequestParams();
395 // Make sure this module is not used during execution
396 unset( $modules[$moduleName] );
397 unset( $tmp[$moduleName] );
398 } elseif ( $completeModules === null || $usePageset ) {
399 // Query modules may optimize data requests through the $this->getPageSet()
400 // object by adding extra fields from the page table.
401 // This function will gather all the extra request fields from the modules.
402 $module->requestExtraData( $this->mPageSet );
403 } else {
404 // Error - this prop module must have finished before generator is done
405 $this->dieContinueUsageIf( $this->mModuleMgr->getModuleGroup( $moduleName ) === 'prop' );
406 }
407 }
408 $this->dieContinueUsageIf( $completeModules !== null && count( $tmp ) !== 0 );
409 return $modules;
410 }
411
412 /**
413 * Update a cache mode string, applying the cache mode of a new module to it.
414 * The cache mode may increase in the level of privacy, but public modules
415 * added to private data do not decrease the level of privacy.
416 *
417 * @param $cacheMode string
418 * @param $modCacheMode string
419 * @return string
420 */
421 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
422 if ( $modCacheMode === 'anon-public-user-private' ) {
423 if ( $cacheMode !== 'private' ) {
424 $cacheMode = 'anon-public-user-private';
425 }
426 } elseif ( $modCacheMode === 'public' ) {
427 // do nothing, if it's public already it will stay public
428 } else { // private
429 $cacheMode = 'private';
430 }
431 return $cacheMode;
432 }
433
434 /**
435 * Create instances of all modules requested by the client
436 * @param array $modules to append instantiated modules to
437 * @param string $param Parameter name to read modules from
438 */
439 private function instantiateModules( &$modules, $param ) {
440 if ( isset( $this->mParams[$param] ) ) {
441 foreach ( $this->mParams[$param] as $moduleName ) {
442 $instance = $this->mModuleMgr->getModule( $moduleName, $param );
443 if ( $instance === null ) {
444 ApiBase::dieDebug( __METHOD__, 'Error instantiating module' );
445 }
446 // Ignore duplicates. TODO 2.0: die()?
447 if ( !array_key_exists( $moduleName, $modules ) ) {
448 $modules[$moduleName] = $instance;
449 }
450 }
451 }
452 }
453
454 /**
455 * Appends an element for each page in the current pageSet with the
456 * most general information (id, title), plus any title normalizations
457 * and missing or invalid title/pageids/revids.
458 */
459 private function outputGeneralPageInfo() {
460 $pageSet = $this->getPageSet();
461 $result = $this->getResult();
462
463 // We don't check for a full result set here because we can't be adding
464 // more than 380K. The maximum revision size is in the megabyte range,
465 // and the maximum result size must be even higher than that.
466
467 $values = $pageSet->getNormalizedTitlesAsResult( $result );
468 if ( $values ) {
469 $result->addValue( 'query', 'normalized', $values );
470 }
471 $values = $pageSet->getConvertedTitlesAsResult( $result );
472 if ( $values ) {
473 $result->addValue( 'query', 'converted', $values );
474 }
475 $values = $pageSet->getInterwikiTitlesAsResult( $result, $this->mParams['iwurl'] );
476 if ( $values ) {
477 $result->addValue( 'query', 'interwiki', $values );
478 }
479 $values = $pageSet->getRedirectTitlesAsResult( $result );
480 if ( $values ) {
481 $result->addValue( 'query', 'redirects', $values );
482 }
483 $values = $pageSet->getMissingRevisionIDsAsResult( $result );
484 if ( $values ) {
485 $result->addValue( 'query', 'badrevids', $values );
486 }
487
488 // Page elements
489 $pages = array();
490
491 // Report any missing titles
492 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
493 $vals = array();
494 ApiQueryBase::addTitleInfo( $vals, $title );
495 $vals['missing'] = '';
496 $pages[$fakeId] = $vals;
497 }
498 // Report any invalid titles
499 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
500 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
501 }
502 // Report any missing page ids
503 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
504 $pages[$pageid] = array(
505 'pageid' => $pageid,
506 'missing' => ''
507 );
508 }
509 // Report special pages
510 /** @var $title Title */
511 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
512 $vals = array();
513 ApiQueryBase::addTitleInfo( $vals, $title );
514 $vals['special'] = '';
515 if ( $title->isSpecialPage() &&
516 !SpecialPageFactory::exists( $title->getDbKey() ) ) {
517 $vals['missing'] = '';
518 } elseif ( $title->getNamespace() == NS_MEDIA &&
519 !wfFindFile( $title ) ) {
520 $vals['missing'] = '';
521 }
522 $pages[$fakeId] = $vals;
523 }
524
525 // Output general page information for found titles
526 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
527 $vals = array();
528 $vals['pageid'] = $pageid;
529 ApiQueryBase::addTitleInfo( $vals, $title );
530 $pages[$pageid] = $vals;
531 }
532
533 if ( count( $pages ) ) {
534 if ( $this->mParams['indexpageids'] ) {
535 $pageIDs = array_keys( $pages );
536 // json treats all map keys as strings - converting to match
537 $pageIDs = array_map( 'strval', $pageIDs );
538 $result->setIndexedTagName( $pageIDs, 'id' );
539 $result->addValue( 'query', 'pageids', $pageIDs );
540 }
541
542 $result->setIndexedTagName( $pages, 'page' );
543 $result->addValue( 'query', 'pages', $pages );
544 }
545 if ( $this->mParams['export'] ) {
546 $this->doExport( $pageSet, $result );
547 }
548 }
549
550 /**
551 * This method is called by the generator base when generator in the smart-continue
552 * mode tries to set 'query-continue' value. ApiQuery stores those values separately
553 * until the post-processing when it is known if the generation should continue or repeat.
554 * @param ApiQueryGeneratorBase $module generator module
555 * @param string $paramName
556 * @param mixed $paramValue
557 * @return bool true if processed, false if this is a legacy continue
558 */
559 public function setGeneratorContinue( $module, $paramName, $paramValue ) {
560 if ( $this->mUseLegacyContinue ) {
561 return false;
562 }
563 $paramName = $module->encodeParamName( $paramName );
564 if ( $this->mGeneratorContinue === null ) {
565 $this->mGeneratorContinue = array();
566 }
567 $this->mGeneratorContinue[$paramName] = $paramValue;
568 return true;
569 }
570
571 /**
572 * @param $pageSet ApiPageSet Pages to be exported
573 * @param $result ApiResult Result to output to
574 */
575 private function doExport( $pageSet, $result ) {
576 $exportTitles = array();
577 $titles = $pageSet->getGoodTitles();
578 if ( count( $titles ) ) {
579 $user = $this->getUser();
580 /** @var $title Title */
581 foreach ( $titles as $title ) {
582 if ( $title->userCan( 'read', $user ) ) {
583 $exportTitles[] = $title;
584 }
585 }
586 }
587
588 $exporter = new WikiExporter( $this->getDB() );
589 // WikiExporter writes to stdout, so catch its
590 // output with an ob
591 ob_start();
592 $exporter->openStream();
593 foreach ( $exportTitles as $title ) {
594 $exporter->pageByTitle( $title );
595 }
596 $exporter->closeStream();
597 $exportxml = ob_get_contents();
598 ob_end_clean();
599
600 // Don't check the size of exported stuff
601 // It's not continuable, so it would cause more
602 // problems than it'd solve
603 $result->disableSizeCheck();
604 if ( $this->mParams['exportnowrap'] ) {
605 $result->reset();
606 // Raw formatter will handle this
607 $result->addValue( null, 'text', $exportxml );
608 $result->addValue( null, 'mime', 'text/xml' );
609 } else {
610 $r = array();
611 ApiResult::setContent( $r, $exportxml );
612 $result->addValue( 'query', 'export', $r );
613 }
614 $result->enableSizeCheck();
615 }
616
617 public function getAllowedParams( $flags = 0 ) {
618 $result = array(
619 'prop' => array(
620 ApiBase::PARAM_ISMULTI => true,
621 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'prop' )
622 ),
623 'list' => array(
624 ApiBase::PARAM_ISMULTI => true,
625 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'list' )
626 ),
627 'meta' => array(
628 ApiBase::PARAM_ISMULTI => true,
629 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'meta' )
630 ),
631 'indexpageids' => false,
632 'export' => false,
633 'exportnowrap' => false,
634 'iwurl' => false,
635 'continue' => null,
636 );
637 if ( $flags ) {
638 $result += $this->getPageSet()->getFinalParams( $flags );
639 }
640 return $result;
641 }
642
643 /**
644 * Override the parent to generate help messages for all available query modules.
645 * @return string
646 */
647 public function makeHelpMsg() {
648
649 // Use parent to make default message for the query module
650 $msg = parent::makeHelpMsg();
651
652 $querySeparator = str_repeat( '--- ', 12 );
653 $moduleSeparator = str_repeat( '*** ', 14 );
654 $msg .= "\n$querySeparator Query: Prop $querySeparator\n\n";
655 $msg .= $this->makeHelpMsgHelper( 'prop' );
656 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
657 $msg .= $this->makeHelpMsgHelper( 'list' );
658 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
659 $msg .= $this->makeHelpMsgHelper( 'meta' );
660 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
661
662 return $msg;
663 }
664
665 /**
666 * For all modules of a given group, generate help messages and join them together
667 * @param string $group Module group
668 * @return string
669 */
670 private function makeHelpMsgHelper( $group ) {
671 $moduleDescriptions = array();
672
673 $moduleNames = $this->mModuleMgr->getNames( $group );
674 sort( $moduleNames );
675 foreach ( $moduleNames as $name ) {
676 /**
677 * @var $module ApiQueryBase
678 */
679 $module = $this->mModuleMgr->getModule( $name );
680
681 $msg = ApiMain::makeHelpMsgHeader( $module, $group );
682 $msg2 = $module->makeHelpMsg();
683 if ( $msg2 !== false ) {
684 $msg .= $msg2;
685 }
686 if ( $module instanceof ApiQueryGeneratorBase ) {
687 $msg .= "Generator:\n This module may be used as a generator\n";
688 }
689 $moduleDescriptions[] = $msg;
690 }
691
692 return implode( "\n", $moduleDescriptions );
693 }
694
695 public function shouldCheckMaxlag() {
696 return true;
697 }
698
699 public function getParamDescription() {
700 return $this->getPageSet()->getParamDescription() + array(
701 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
702 'list' => 'Which lists to get. Module help is available below',
703 'meta' => 'Which metadata to get about the site. Module help is available below',
704 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
705 'export' => 'Export the current revisions of all given or generated pages',
706 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
707 'iwurl' => 'Whether to get the full URL if the title is an interwiki link',
708 'continue' => array(
709 'When present, formats query-continue as key-value pairs that should simply be merged into the original request.',
710 'This parameter must be set to an empty string in the initial query.',
711 'This parameter is recommended for all new development, and will be made default in the next API version.' ),
712 );
713 }
714
715 public function getDescription() {
716 return array(
717 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
718 'and is loosely based on the old query.php interface.',
719 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
720 );
721 }
722
723 public function getPossibleErrors() {
724 return array_merge(
725 parent::getPossibleErrors(),
726 $this->getPageSet()->getPossibleErrors()
727 );
728 }
729
730 public function getExamples() {
731 return array(
732 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment&continue=',
733 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions&continue=',
734 );
735 }
736
737 public function getHelpUrls() {
738 return array(
739 'https://www.mediawiki.org/wiki/API:Meta',
740 'https://www.mediawiki.org/wiki/API:Properties',
741 'https://www.mediawiki.org/wiki/API:Lists',
742 );
743 }
744 }