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