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