Merge "Accessibility: Don't remove checkbox outline on focus"
[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 'filerepoinfo' => 'ApiQueryFileRepoInfo',
106 );
107
108 /**
109 * @var ApiPageSet
110 */
111 private $mPageSet;
112
113 private $mParams;
114 private $mNamedDB = array();
115 private $mModuleMgr;
116 private $mGeneratorContinue;
117 private $mUseLegacyContinue;
118
119 /**
120 * @param $main ApiMain
121 * @param $action string
122 */
123 public function __construct( $main, $action ) {
124 parent::__construct( $main, $action );
125
126 $this->mModuleMgr = new ApiModuleManager( $this );
127
128 // Allow custom modules to be added in LocalSettings.php
129 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
130 $this->mModuleMgr->addModules( self::$QueryPropModules, 'prop' );
131 $this->mModuleMgr->addModules( $wgAPIPropModules, 'prop' );
132 $this->mModuleMgr->addModules( self::$QueryListModules, 'list' );
133 $this->mModuleMgr->addModules( $wgAPIListModules, 'list' );
134 $this->mModuleMgr->addModules( self::$QueryMetaModules, 'meta' );
135 $this->mModuleMgr->addModules( $wgAPIMetaModules, 'meta' );
136
137 // Create PageSet that will process titles/pageids/revids/generator
138 $this->mPageSet = new ApiPageSet( $this );
139 }
140
141 /**
142 * Overrides to return this instance's module manager.
143 * @return ApiModuleManager
144 */
145 public function getModuleManager() {
146 return $this->mModuleMgr;
147 }
148
149 /**
150 * Get the query database connection with the given name.
151 * If no such connection has been requested before, it will be created.
152 * Subsequent calls with the same $name will return the same connection
153 * as the first, regardless of the values of $db and $groups
154 * @param string $name Name to assign to the database connection
155 * @param int $db One of the DB_* constants
156 * @param array $groups Query groups
157 * @return DatabaseBase
158 */
159 public function getNamedDB( $name, $db, $groups ) {
160 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
161 $this->profileDBIn();
162 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
163 $this->profileDBOut();
164 }
165 return $this->mNamedDB[$name];
166 }
167
168 /**
169 * Gets the set of pages the user has requested (or generated)
170 * @return ApiPageSet
171 */
172 public function getPageSet() {
173 return $this->mPageSet;
174 }
175
176 /**
177 * Get the array mapping module names to class names
178 * @deprecated since 1.21, use getModuleManager()'s methods instead
179 * @return array array(modulename => classname)
180 */
181 public function getModules() {
182 wfDeprecated( __METHOD__, '1.21' );
183 return $this->getModuleManager()->getNamesWithClasses();
184 }
185
186 /**
187 * Get the generators array mapping module names to class names
188 * @deprecated since 1.21, list of generators is maintained by ApiPageSet
189 * @return array array(modulename => classname)
190 */
191 public function getGenerators() {
192 wfDeprecated( __METHOD__, '1.21' );
193 $gens = array();
194 foreach ( $this->mModuleMgr->getNamesWithClasses() as $name => $class ) {
195 if ( is_subclass_of( $class, 'ApiQueryGeneratorBase' ) ) {
196 $gens[$name] = $class;
197 }
198 }
199 return $gens;
200 }
201
202 /**
203 * Get whether the specified module is a prop, list or a meta query module
204 * @deprecated since 1.21, use getModuleManager()->getModuleGroup()
205 * @param string $moduleName Name of the module to find type for
206 * @return mixed string or null
207 */
208 function getModuleType( $moduleName ) {
209 return $this->getModuleManager()->getModuleGroup( $moduleName );
210 }
211
212 /**
213 * @return ApiFormatRaw|null
214 */
215 public function getCustomPrinter() {
216 // If &exportnowrap is set, use the raw formatter
217 if ( $this->getParameter( 'export' ) &&
218 $this->getParameter( 'exportnowrap' ) )
219 {
220 return new ApiFormatRaw( $this->getMain(),
221 $this->getMain()->createPrinterByName( 'xml' ) );
222 } else {
223 return null;
224 }
225 }
226
227 /**
228 * Query execution happens in the following steps:
229 * #1 Create a PageSet object with any pages requested by the user
230 * #2 If using a generator, execute it to get a new ApiPageSet object
231 * #3 Instantiate all requested modules.
232 * This way the PageSet object will know what shared data is required,
233 * and minimize DB calls.
234 * #4 Output all normalization and redirect resolution information
235 * #5 Execute all requested modules
236 */
237 public function execute() {
238 $this->mParams = $this->extractRequestParams();
239
240 // $pagesetParams is a array of parameter names used by the pageset generator
241 // or null if pageset has already finished and is no longer needed
242 // $completeModules is a set of complete modules with the name as key
243 $this->initContinue( $pagesetParams, $completeModules );
244
245 // Instantiate requested modules
246 $allModules = array();
247 $this->instantiateModules( $allModules, 'prop' );
248 $propModules = $allModules; // Keep a copy
249 $this->instantiateModules( $allModules, 'list' );
250 $this->instantiateModules( $allModules, 'meta' );
251
252 // Filter modules based on continue parameter
253 $modules = $this->initModules( $allModules, $completeModules, $pagesetParams !== null );
254
255 // Execute pageset if in legacy mode or if pageset is not done
256 if ( $completeModules === null || $pagesetParams !== null ) {
257 // Populate page/revision information
258 $this->mPageSet->execute();
259 // Record page information (title, namespace, if exists, etc)
260 $this->outputGeneralPageInfo();
261 } else {
262 $this->mPageSet->executeDryRun();
263 }
264
265 $cacheMode = $this->mPageSet->getCacheMode();
266
267 // Execute all unfinished modules
268 /** @var $module ApiQueryBase */
269 foreach ( $modules as $module ) {
270 $params = $module->extractRequestParams();
271 $cacheMode = $this->mergeCacheMode(
272 $cacheMode, $module->getCacheMode( $params ) );
273 $module->profileIn();
274 $module->execute();
275 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
276 $module->profileOut();
277 }
278
279 // Set the cache mode
280 $this->getMain()->setCacheMode( $cacheMode );
281
282 if ( $completeModules === null ) {
283 return; // Legacy continue, we are done
284 }
285
286 // Reformat query-continue result section
287 $result = $this->getResult();
288 $qc = $result->getData();
289 if ( isset( $qc['query-continue'] ) ) {
290 $qc = $qc['query-continue'];
291 $result->unsetValue( null, 'query-continue' );
292 } elseif ( $this->mGeneratorContinue !== null ) {
293 $qc = array();
294 } else {
295 // no more "continue"s, we are done!
296 return;
297 }
298
299 // we are done with all the modules that do not have result in query-continue
300 $completeModules = array_merge( $completeModules, array_diff_key( $modules, $qc ) );
301 if ( $pagesetParams !== null ) {
302 // The pageset is still in use, check if all props have finished
303 $incompleteProps = array_intersect_key( $propModules, $qc );
304 if ( count( $incompleteProps ) > 0 ) {
305 // Properties are not done, continue with the same pageset state - copy current parameters
306 $main = $this->getMain();
307 $contValues = array();
308 foreach ( $pagesetParams as $param ) {
309 // The param name is already prefix-encoded
310 $contValues[$param] = $main->getVal( $param );
311 }
312 } elseif ( $this->mGeneratorContinue !== null ) {
313 // Move to the next set of pages produced by pageset, properties need to be restarted
314 $contValues = $this->mGeneratorContinue;
315 $pagesetParams = array_keys( $contValues );
316 $completeModules = array_diff_key( $completeModules, $propModules );
317 } else {
318 // Done with the pageset, finish up with the the lists and meta modules
319 $pagesetParams = null;
320 }
321 }
322
323 $continue = '||' . implode( '|', array_keys( $completeModules ) );
324 if ( $pagesetParams !== null ) {
325 // list of all pageset parameters to use in the next request
326 $continue = implode( '|', $pagesetParams ) . $continue;
327 } else {
328 // we are done with the pageset
329 $contValues = array();
330 $continue = '-' . $continue;
331 }
332 $contValues['continue'] = $continue;
333 foreach ( $qc as $qcModule ) {
334 foreach ( $qcModule as $qcKey => $qcValue ) {
335 $contValues[$qcKey] = $qcValue;
336 }
337 }
338 $this->getResult()->addValue( null, 'continue', $contValues );
339 }
340
341 /**
342 * Parse 'continue' parameter into the list of complete modules and a list of generator parameters
343 * @param array|null $pagesetParams returns list of generator params or null if pageset is done
344 * @param array|null $completeModules returns list of finished modules (as keys), or null if legacy
345 */
346 private function initContinue( &$pagesetParams, &$completeModules ) {
347 $pagesetParams = array();
348 $continue = $this->mParams['continue'];
349 if ( $continue !== null ) {
350 $this->mUseLegacyContinue = false;
351 if ( $continue !== '' ) {
352 // Format: ' pagesetParam1 | pagesetParam2 || module1 | module2 | module3 | ...
353 // If pageset is done, use '-'
354 $continue = explode( '||', $continue );
355 $this->dieContinueUsageIf( count( $continue ) !== 2 );
356 if ( $continue[0] === '-' ) {
357 $pagesetParams = null; // No need to execute pageset
358 } elseif ( $continue[0] !== '' ) {
359 // list of pageset params that might need to be repeated
360 $pagesetParams = explode( '|', $continue[0] );
361 }
362 $continue = $continue[1];
363 }
364 if ( $continue !== '' ) {
365 $completeModules = array_flip( explode( '|', $continue ) );
366 } else {
367 $completeModules = array();
368 }
369 } else {
370 $this->mUseLegacyContinue = true;
371 $completeModules = null;
372 }
373 }
374
375 /**
376 * Validate sub-modules, filter out completed ones, and do requestExtraData()
377 * @param array $allModules An dict of name=>instance of all modules requested by the client
378 * @param array|null $completeModules list of finished modules, or null if legacy continue
379 * @param bool $usePageset True if pageset will be executed
380 * @return array of modules to be processed during this execution
381 */
382 private function initModules( $allModules, $completeModules, $usePageset ) {
383 $modules = $allModules;
384 $tmp = $completeModules;
385 $wasPosted = $this->getRequest()->wasPosted();
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()->getFinalParamDescription() + 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()->getFinalPossibleErrors()
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:Query',
740 'https://www.mediawiki.org/wiki/API:Meta',
741 'https://www.mediawiki.org/wiki/API:Properties',
742 'https://www.mediawiki.org/wiki/API:Lists',
743 );
744 }
745 }