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