Merge "(bug 40058) Parse newwindow message."
[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 foreach ( $titles as $title ) {
517 if ( $title->userCan( 'read' ) ) {
518 $exportTitles[] = $title;
519 }
520 }
521 }
522
523 $exporter = new WikiExporter( $this->getDB() );
524 // WikiExporter writes to stdout, so catch its
525 // output with an ob
526 ob_start();
527 $exporter->openStream();
528 foreach ( $exportTitles as $title ) {
529 $exporter->pageByTitle( $title );
530 }
531 $exporter->closeStream();
532 $exportxml = ob_get_contents();
533 ob_end_clean();
534
535 // Don't check the size of exported stuff
536 // It's not continuable, so it would cause more
537 // problems than it'd solve
538 $result->disableSizeCheck();
539 if ( $this->params['exportnowrap'] ) {
540 $result->reset();
541 // Raw formatter will handle this
542 $result->addValue( null, 'text', $exportxml );
543 $result->addValue( null, 'mime', 'text/xml' );
544 } else {
545 $r = array();
546 ApiResult::setContent( $r, $exportxml );
547 $result->addValue( 'query', 'export', $r );
548 }
549 $result->enableSizeCheck();
550 }
551
552 /**
553 * Create a generator object of the given type and return it
554 * @param $generatorName string Module name
555 * @return ApiQueryGeneratorBase
556 */
557 public function newGenerator( $generatorName ) {
558 // Find class that implements requested generator
559 if ( isset( $this->mQueryListModules[$generatorName] ) ) {
560 $className = $this->mQueryListModules[$generatorName];
561 } elseif ( isset( $this->mQueryPropModules[$generatorName] ) ) {
562 $className = $this->mQueryPropModules[$generatorName];
563 } else {
564 ApiBase::dieDebug( __METHOD__, "Unknown generator=$generatorName" );
565 }
566 $generator = new $className ( $this, $generatorName );
567 if ( !$generator instanceof ApiQueryGeneratorBase ) {
568 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
569 }
570 $generator->setGeneratorMode();
571 return $generator;
572 }
573
574 /**
575 * For generator mode, execute generator, and use its output as new
576 * ApiPageSet
577 * @param $generator ApiQueryGeneratorBase Generator Module
578 * @param $modules array of module objects
579 */
580 protected function executeGeneratorModule( $generator, $modules ) {
581 // Generator results
582 $resultPageSet = new ApiPageSet( $this, $this->redirects, $this->convertTitles );
583
584 // Add any additional fields modules may need
585 $generator->requestExtraData( $this->mPageSet );
586 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
587
588 // Populate page information with the original user input
589 $this->mPageSet->execute();
590
591 // populate resultPageSet with the generator output
592 $generator->profileIn();
593 $generator->executeGenerator( $resultPageSet );
594 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
595 $resultPageSet->finishPageSetGeneration();
596 $generator->profileOut();
597
598 // Swap the resulting pageset back in
599 $this->mPageSet = $resultPageSet;
600 }
601
602 public function getAllowedParams() {
603 return array(
604 'prop' => array(
605 ApiBase::PARAM_ISMULTI => true,
606 ApiBase::PARAM_TYPE => $this->mPropModuleNames
607 ),
608 'list' => array(
609 ApiBase::PARAM_ISMULTI => true,
610 ApiBase::PARAM_TYPE => $this->mListModuleNames
611 ),
612 'meta' => array(
613 ApiBase::PARAM_ISMULTI => true,
614 ApiBase::PARAM_TYPE => $this->mMetaModuleNames
615 ),
616 'generator' => array(
617 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
618 ),
619 'redirects' => false,
620 'converttitles' => false,
621 'indexpageids' => false,
622 'export' => false,
623 'exportnowrap' => false,
624 'iwurl' => false,
625 );
626 }
627
628 /**
629 * Override the parent to generate help messages for all available query modules.
630 * @return string
631 */
632 public function makeHelpMsg() {
633 // Make sure the internal object is empty
634 // (just in case a sub-module decides to optimize during instantiation)
635 $this->mPageSet = null;
636
637 $querySeparator = str_repeat( '--- ', 12 );
638 $moduleSeparator = str_repeat( '*** ', 14 );
639 $msg = "\n$querySeparator Query: Prop $querySeparator\n\n";
640 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules, 'prop' );
641 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
642 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules, 'list' );
643 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
644 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules, 'meta' );
645 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
646
647 // Use parent to make default message for the query module
648 $msg = parent::makeHelpMsg() . $msg;
649
650 return $msg;
651 }
652
653 /**
654 * For all modules in $moduleList, generate help messages and join them together
655 * @param $moduleList Array array(modulename => classname)
656 * @param $paramName string Parameter name
657 * @return string
658 */
659 private function makeHelpMsgHelper( $moduleList, $paramName ) {
660 $moduleDescriptions = array();
661
662 foreach ( $moduleList as $moduleName => $moduleClass ) {
663 /**
664 * @var $module ApiQueryBase
665 */
666 $module = new $moduleClass( $this, $moduleName, null );
667
668 $msg = ApiMain::makeHelpMsgHeader( $module, $paramName );
669 $msg2 = $module->makeHelpMsg();
670 if ( $msg2 !== false ) {
671 $msg .= $msg2;
672 }
673 if ( $module instanceof ApiQueryGeneratorBase ) {
674 $msg .= "Generator:\n This module may be used as a generator\n";
675 }
676 $moduleDescriptions[] = $msg;
677 }
678
679 return implode( "\n", $moduleDescriptions );
680 }
681
682 /**
683 * Adds any classes that are a subclass of ApiQueryGeneratorBase
684 * to the allowed generator list
685 * @param $moduleList array()
686 */
687 private function makeGeneratorList( $moduleList ) {
688 foreach( $moduleList as $moduleName => $moduleClass ) {
689 if ( is_subclass_of( $moduleClass, 'ApiQueryGeneratorBase' ) ) {
690 $this->mAllowedGenerators[] = $moduleName;
691 }
692 }
693 }
694
695 /**
696 * Override to add extra parameters from PageSet
697 * @return string
698 */
699 public function makeHelpMsgParameters() {
700 $psModule = new ApiPageSet( $this );
701 return $psModule->makeHelpMsgParameters() . parent::makeHelpMsgParameters();
702 }
703
704 public function shouldCheckMaxlag() {
705 return true;
706 }
707
708 public function getParamDescription() {
709 return array(
710 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
711 'list' => 'Which lists to get. Module help is available below',
712 'meta' => 'Which metadata to get about the site. Module help is available below',
713 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
714 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
715 'redirects' => 'Automatically resolve redirects',
716 'converttitles' => array( "Convert titles to other variants if necessary. Only works if the wiki's content language supports variant conversion.",
717 'Languages that support variant conversion include ' . implode( ', ', LanguageConverter::$languagesWithVariants ) ),
718 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
719 'export' => 'Export the current revisions of all given or generated pages',
720 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
721 'iwurl' => 'Whether to get the full URL if the title is an interwiki link',
722 );
723 }
724
725 public function getDescription() {
726 return array(
727 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
728 'and is loosely based on the old query.php interface.',
729 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
730 );
731 }
732
733 public function getPossibleErrors() {
734 return array_merge( parent::getPossibleErrors(), array(
735 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
736 ) );
737 }
738
739 public function getExamples() {
740 return array(
741 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
742 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
743 );
744 }
745
746 public function getHelpUrls() {
747 return array(
748 'https://www.mediawiki.org/wiki/API:Meta',
749 'https://www.mediawiki.org/wiki/API:Properties',
750 'https://www.mediawiki.org/wiki/API:Lists',
751 );
752 }
753
754 public function getVersion() {
755 $psModule = new ApiPageSet( $this );
756 $vers = array();
757 $vers[] = __CLASS__ . ': $Id$';
758 $vers[] = $psModule->getVersion();
759 return $vers;
760 }
761 }