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