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