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