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