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