* (bug 23460) Parse action should have a section option
[lhc/web/wiklou.git] / includes / api / ApiQuery.php
1 <?php
2
3 /**
4 * Created on Sep 7, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiBase.php' );
29 }
30
31 /**
32 * This is the main query class. It behaves similar to ApiMain: based on the
33 * parameters given, it will create a list of titles to work on (an ApiPageSet
34 * object), instantiate and execute various property/list/meta modules, and
35 * assemble all resulting data into a single ApiResult object.
36 *
37 * In generator mode, a generator will be executed first to populate a second
38 * ApiPageSet object, and that object will be used for all subsequent modules.
39 *
40 * @ingroup API
41 */
42 class ApiQuery extends ApiBase {
43
44 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
45 private $mPageSet;
46 private $params, $redirect;
47
48 private $mQueryPropModules = array(
49 'info' => 'ApiQueryInfo',
50 'revisions' => 'ApiQueryRevisions',
51 'links' => 'ApiQueryLinks',
52 'langlinks' => 'ApiQueryLangLinks',
53 'images' => 'ApiQueryImages',
54 'imageinfo' => 'ApiQueryImageInfo',
55 'templates' => 'ApiQueryLinks',
56 'categories' => 'ApiQueryCategories',
57 'extlinks' => 'ApiQueryExternalLinks',
58 'categoryinfo' => 'ApiQueryCategoryInfo',
59 'duplicatefiles' => 'ApiQueryDuplicateFiles',
60 );
61
62 private $mQueryListModules = array(
63 'allimages' => 'ApiQueryAllimages',
64 'allpages' => 'ApiQueryAllpages',
65 'alllinks' => 'ApiQueryAllLinks',
66 'allcategories' => 'ApiQueryAllCategories',
67 'allusers' => 'ApiQueryAllUsers',
68 'backlinks' => 'ApiQueryBacklinks',
69 'blocks' => 'ApiQueryBlocks',
70 'categorymembers' => 'ApiQueryCategoryMembers',
71 'deletedrevs' => 'ApiQueryDeletedrevs',
72 'embeddedin' => 'ApiQueryBacklinks',
73 'imageusage' => 'ApiQueryBacklinks',
74 'logevents' => 'ApiQueryLogEvents',
75 'recentchanges' => 'ApiQueryRecentChanges',
76 'search' => 'ApiQuerySearch',
77 'tags' => 'ApiQueryTags',
78 'usercontribs' => 'ApiQueryContributions',
79 'watchlist' => 'ApiQueryWatchlist',
80 'watchlistraw' => 'ApiQueryWatchlistRaw',
81 'exturlusage' => 'ApiQueryExtLinksUsage',
82 'users' => 'ApiQueryUsers',
83 'random' => 'ApiQueryRandom',
84 'protectedtitles' => 'ApiQueryProtectedTitles',
85 );
86
87 private $mQueryMetaModules = array(
88 'siteinfo' => 'ApiQuerySiteinfo',
89 'userinfo' => 'ApiQueryUserInfo',
90 'allmessages' => 'ApiQueryAllmessages',
91 );
92
93 private $mSlaveDB = null;
94 private $mNamedDB = array();
95
96 public function __construct( $main, $action ) {
97 parent::__construct( $main, $action );
98
99 // Allow custom modules to be added in LocalSettings.php
100 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
101 self::appendUserModules( $this->mQueryPropModules, $wgAPIPropModules );
102 self::appendUserModules( $this->mQueryListModules, $wgAPIListModules );
103 self::appendUserModules( $this->mQueryMetaModules, $wgAPIMetaModules );
104
105 $this->mPropModuleNames = array_keys( $this->mQueryPropModules );
106 $this->mListModuleNames = array_keys( $this->mQueryListModules );
107 $this->mMetaModuleNames = array_keys( $this->mQueryMetaModules );
108
109 // Allow the entire list of modules at first,
110 // but during module instantiation check if it can be used as a generator.
111 $this->mAllowedGenerators = array_merge( $this->mListModuleNames, $this->mPropModuleNames );
112 }
113
114 /**
115 * Helper function to append any add-in modules to the list
116 * @param $modules array Module array
117 * @param $newModules array Module array to add to $modules
118 */
119 private static function appendUserModules( &$modules, $newModules ) {
120 if ( is_array( $newModules ) ) {
121 foreach ( $newModules as $moduleName => $moduleClass ) {
122 $modules[$moduleName] = $moduleClass;
123 }
124 }
125 }
126
127 /**
128 * Gets a default slave database connection object
129 * @return Database
130 */
131 public function getDB() {
132 if ( !isset( $this->mSlaveDB ) ) {
133 $this->profileDBIn();
134 $this->mSlaveDB = wfGetDB( DB_SLAVE, 'api' );
135 $this->profileDBOut();
136 }
137 return $this->mSlaveDB;
138 }
139
140 /**
141 * Get the query database connection with the given name.
142 * If no such connection has been requested before, it will be created.
143 * Subsequent calls with the same $name will return the same connection
144 * as the first, regardless of the values of $db and $groups
145 * @param $name string Name to assign to the database connection
146 * @param $db int One of the DB_* constants
147 * @param $groups array Query groups
148 * @return Database
149 */
150 public function getNamedDB( $name, $db, $groups ) {
151 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
152 $this->profileDBIn();
153 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
154 $this->profileDBOut();
155 }
156 return $this->mNamedDB[$name];
157 }
158
159 /**
160 * Gets the set of pages the user has requested (or generated)
161 * @return ApiPageSet
162 */
163 public function getPageSet() {
164 return $this->mPageSet;
165 }
166
167 /**
168 * Get the array mapping module names to class names
169 * @return array(modulename => classname)
170 */
171 function getModules() {
172 return array_merge( $this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules );
173 }
174
175 /**
176 * Get whether the specified module is a prop, list or a meta query module
177 * @param $moduleName string Name of the module to find type for
178 * @return mixed string or null
179 */
180 function getModuleType( $moduleName ) {
181 if ( array_key_exists ( $moduleName, $this->mQueryPropModules ) ) {
182 return 'prop';
183 }
184
185 if ( array_key_exists ( $moduleName, $this->mQueryListModules ) ) {
186 return 'list';
187 }
188
189 if ( array_key_exists ( $moduleName, $this->mQueryMetaModules ) ) {
190 return 'meta';
191 }
192
193 return null;
194 }
195
196 public function getCustomPrinter() {
197 // If &exportnowrap is set, use the raw formatter
198 if ( $this->getParameter( 'export' ) &&
199 $this->getParameter( 'exportnowrap' ) )
200 {
201 return new ApiFormatRaw( $this->getMain(),
202 $this->getMain()->createPrinterByName( 'xml' ) );
203 } else {
204 return null;
205 }
206 }
207
208 /**
209 * Query execution happens in the following steps:
210 * #1 Create a PageSet object with any pages requested by the user
211 * #2 If using a generator, execute it to get a new ApiPageSet object
212 * #3 Instantiate all requested modules.
213 * This way the PageSet object will know what shared data is required,
214 * and minimize DB calls.
215 * #4 Output all normalization and redirect resolution information
216 * #5 Execute all requested modules
217 */
218 public function execute() {
219 $this->params = $this->extractRequestParams();
220 $this->redirects = $this->params['redirects'];
221
222 // Create PageSet
223 $this->mPageSet = new ApiPageSet( $this, $this->redirects );
224
225 // Instantiate requested modules
226 $modules = array();
227 $this->InstantiateModules( $modules, 'prop', $this->mQueryPropModules );
228 $this->InstantiateModules( $modules, 'list', $this->mQueryListModules );
229 $this->InstantiateModules( $modules, 'meta', $this->mQueryMetaModules );
230
231 // If given, execute generator to substitute user supplied data with generated data.
232 if ( isset( $this->params['generator'] ) ) {
233 $this->executeGeneratorModule( $this->params['generator'], $modules );
234 } else {
235 // Append custom fields and populate page/revision information
236 $this->addCustomFldsToPageSet( $modules, $this->mPageSet );
237 $this->mPageSet->execute();
238 }
239
240 // Record page information (title, namespace, if exists, etc)
241 $this->outputGeneralPageInfo();
242
243 // Execute all requested modules.
244 foreach ( $modules as $module ) {
245 $module->profileIn();
246 $module->execute();
247 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
248 $module->profileOut();
249 }
250 }
251
252 /**
253 * Query modules may optimize data requests through the $this->getPageSet() object
254 * by adding extra fields from the page table.
255 * This function will gather all the extra request fields from the modules.
256 * @param $modules array of module objects
257 * @param $pageSet ApiPageSet
258 */
259 private function addCustomFldsToPageSet( $modules, $pageSet ) {
260 // Query all requested modules.
261 foreach ( $modules as $module ) {
262 $module->requestExtraData( $pageSet );
263 }
264 }
265
266 /**
267 * Create instances of all modules requested by the client
268 * @param $modules array to append instatiated modules to
269 * @param $param string Parameter name to read modules from
270 * @param $moduleList array(modulename => classname)
271 */
272 private function InstantiateModules( &$modules, $param, $moduleList ) {
273 $list = @$this->params[$param];
274 if ( !is_null ( $list ) ) {
275 foreach ( $list as $moduleName ) {
276 $modules[] = new $moduleList[$moduleName] ( $this, $moduleName );
277 }
278 }
279 }
280
281 /**
282 * Appends an element for each page in the current pageSet with the
283 * most general information (id, title), plus any title normalizations
284 * and missing or invalid title/pageids/revids.
285 */
286 private function outputGeneralPageInfo() {
287 $pageSet = $this->getPageSet();
288 $result = $this->getResult();
289
290 // We don't check for a full result set here because we can't be adding
291 // more than 380K. The maximum revision size is in the megabyte range,
292 // and the maximum result size must be even higher than that.
293
294 // Title normalizations
295 $normValues = array();
296 foreach ( $pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr ) {
297 $normValues[] = array(
298 'from' => $rawTitleStr,
299 'to' => $titleStr
300 );
301 }
302
303 if ( count( $normValues ) ) {
304 $result->setIndexedTagName( $normValues, 'n' );
305 $result->addValue( 'query', 'normalized', $normValues );
306 }
307
308 // Interwiki titles
309 $intrwValues = array();
310 foreach ( $pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr ) {
311 $intrwValues[] = array(
312 'title' => $rawTitleStr,
313 'iw' => $interwikiStr
314 );
315 }
316
317 if ( count( $intrwValues ) ) {
318 $result->setIndexedTagName( $intrwValues, 'i' );
319 $result->addValue( 'query', 'interwiki', $intrwValues );
320 }
321
322 // Show redirect information
323 $redirValues = array();
324 foreach ( $pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo ) {
325 $redirValues[] = array(
326 'from' => strval( $titleStrFrom ),
327 'to' => $titleStrTo
328 );
329 }
330
331 if ( count( $redirValues ) ) {
332 $result->setIndexedTagName( $redirValues, 'r' );
333 $result->addValue( 'query', 'redirects', $redirValues );
334 }
335
336 //
337 // Missing revision elements
338 //
339 $missingRevIDs = $pageSet->getMissingRevisionIDs();
340 if ( count( $missingRevIDs ) ) {
341 $revids = array();
342 foreach ( $missingRevIDs as $revid ) {
343 $revids[$revid] = array(
344 'revid' => $revid
345 );
346 }
347 $result->setIndexedTagName( $revids, 'rev' );
348 $result->addValue( 'query', 'badrevids', $revids );
349 }
350
351 //
352 // Page elements
353 //
354 $pages = array();
355
356 // Report any missing titles
357 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
358 $vals = array();
359 ApiQueryBase::addTitleInfo( $vals, $title );
360 $vals['missing'] = '';
361 $pages[$fakeId] = $vals;
362 }
363 // Report any invalid titles
364 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
365 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
366 }
367 // Report any missing page ids
368 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
369 $pages[$pageid] = array(
370 'pageid' => $pageid,
371 'missing' => ''
372 );
373 }
374
375 // Output general page information for found titles
376 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
377 $vals = array();
378 $vals['pageid'] = $pageid;
379 ApiQueryBase::addTitleInfo( $vals, $title );
380 $pages[$pageid] = $vals;
381 }
382
383 if ( count( $pages ) ) {
384 if ( $this->params['indexpageids'] ) {
385 $pageIDs = array_keys( $pages );
386 // json treats all map keys as strings - converting to match
387 $pageIDs = array_map( 'strval', $pageIDs );
388 $result->setIndexedTagName( $pageIDs, 'id' );
389 $result->addValue( 'query', 'pageids', $pageIDs );
390 }
391
392 $result->setIndexedTagName( $pages, 'page' );
393 $result->addValue( 'query', 'pages', $pages );
394 }
395 if ( $this->params['export'] ) {
396 $exporter = new WikiExporter( $this->getDB() );
397 // WikiExporter writes to stdout, so catch its
398 // output with an ob
399 ob_start();
400 $exporter->openStream();
401 foreach ( @$pageSet->getGoodTitles() as $title ) {
402 if ( $title->userCanRead() ) {
403 $exporter->pageByTitle( $title );
404 }
405 }
406 $exporter->closeStream();
407 $exportxml = ob_get_contents();
408 ob_end_clean();
409
410 // Don't check the size of exported stuff
411 // It's not continuable, so it would cause more
412 // problems than it'd solve
413 $result->disableSizeCheck();
414 if ( $this->params['exportnowrap'] ) {
415 $result->reset();
416 // Raw formatter will handle this
417 $result->addValue( null, 'text', $exportxml );
418 $result->addValue( null, 'mime', 'text/xml' );
419 } else {
420 $r = array();
421 ApiResult::setContent( $r, $exportxml );
422 $result->addValue( 'query', 'export', $r );
423 }
424 $result->enableSizeCheck();
425 }
426 }
427
428 /**
429 * For generator mode, execute generator, and use its output as new
430 * ApiPageSet
431 * @param $generatorName string Module name
432 * @param $modules array of module objects
433 */
434 protected function executeGeneratorModule( $generatorName, $modules ) {
435 // Find class that implements requested generator
436 if ( isset( $this->mQueryListModules[$generatorName] ) ) {
437 $className = $this->mQueryListModules[$generatorName];
438 } elseif ( isset( $this->mQueryPropModules[$generatorName] ) ) {
439 $className = $this->mQueryPropModules[$generatorName];
440 } else {
441 ApiBase::dieDebug( __METHOD__, "Unknown generator=$generatorName" );
442 }
443
444 // Generator results
445 $resultPageSet = new ApiPageSet( $this, $this->redirects );
446
447 // Create and execute the generator
448 $generator = new $className ( $this, $generatorName );
449 if ( !$generator instanceof ApiQueryGeneratorBase ) {
450 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
451 }
452
453 $generator->setGeneratorMode();
454
455 // Add any additional fields modules may need
456 $generator->requestExtraData( $this->mPageSet );
457 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
458
459 // Populate page information with the original user input
460 $this->mPageSet->execute();
461
462 // populate resultPageSet with the generator output
463 $generator->profileIn();
464 $generator->executeGenerator( $resultPageSet );
465 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
466 $resultPageSet->finishPageSetGeneration();
467 $generator->profileOut();
468
469 // Swap the resulting pageset back in
470 $this->mPageSet = $resultPageSet;
471 }
472
473 public function getAllowedParams() {
474 return array(
475 'prop' => array(
476 ApiBase::PARAM_ISMULTI => true,
477 ApiBase::PARAM_TYPE => $this->mPropModuleNames
478 ),
479 'list' => array(
480 ApiBase::PARAM_ISMULTI => true,
481 ApiBase::PARAM_TYPE => $this->mListModuleNames
482 ),
483 'meta' => array(
484 ApiBase::PARAM_ISMULTI => true,
485 ApiBase::PARAM_TYPE => $this->mMetaModuleNames
486 ),
487 'generator' => array(
488 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
489 ),
490 'redirects' => false,
491 'indexpageids' => false,
492 'export' => false,
493 'exportnowrap' => false,
494 );
495 }
496
497 /**
498 * Override the parent to generate help messages for all available query modules.
499 * @return string
500 */
501 public function makeHelpMsg() {
502 $msg = '';
503
504 // Make sure the internal object is empty
505 // (just in case a sub-module decides to optimize during instantiation)
506 $this->mPageSet = null;
507 $this->mAllowedGenerators = array(); // Will be repopulated
508
509 $astriks = str_repeat( '--- ', 8 );
510 $astriks2 = str_repeat( '*** ', 10 );
511 $msg .= "\n$astriks Query: Prop $astriks\n\n";
512 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules, 'prop' );
513 $msg .= "\n$astriks Query: List $astriks\n\n";
514 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules, 'list' );
515 $msg .= "\n$astriks Query: Meta $astriks\n\n";
516 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules, 'meta' );
517 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
518
519 // Perform the base call last because the $this->mAllowedGenerators
520 // will be updated inside makeHelpMsgHelper()
521 // Use parent to make default message for the query module
522 $msg = parent::makeHelpMsg() . $msg;
523
524 return $msg;
525 }
526
527 /**
528 * For all modules in $moduleList, generate help messages and join them together
529 * @param $moduleList array(modulename => classname)
530 * @param $paramName string Parameter name
531 * @return string
532 */
533 private function makeHelpMsgHelper( $moduleList, $paramName ) {
534 $moduleDescriptions = array();
535
536 foreach ( $moduleList as $moduleName => $moduleClass ) {
537 $module = new $moduleClass ( $this, $moduleName, null );
538
539 $msg = ApiMain::makeHelpMsgHeader( $module, $paramName );
540 $msg2 = $module->makeHelpMsg();
541 if ( $msg2 !== false ) {
542 $msg .= $msg2;
543 }
544 if ( $module instanceof ApiQueryGeneratorBase ) {
545 $this->mAllowedGenerators[] = $moduleName;
546 $msg .= "Generator:\n This module may be used as a generator\n";
547 }
548 $moduleDescriptions[] = $msg;
549 }
550
551 return implode( "\n", $moduleDescriptions );
552 }
553
554 /**
555 * Override to add extra parameters from PageSet
556 * @return string
557 */
558 public function makeHelpMsgParameters() {
559 $psModule = new ApiPageSet( $this );
560 return $psModule->makeHelpMsgParameters() . parent::makeHelpMsgParameters();
561 }
562
563 public function shouldCheckMaxlag() {
564 return true;
565 }
566
567 public function getParamDescription() {
568 return array(
569 'prop' => 'Which properties to get for the titles/revisions/pageids',
570 'list' => 'Which lists to get',
571 'meta' => 'Which meta data to get about the site',
572 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
573 'NOTE: generator parameter names must be prefixed with a \'g\', see examples.' ),
574 'redirects' => 'Automatically resolve redirects',
575 'indexpageids' => 'Include an additional pageids section listing all returned page IDs.',
576 'export' => 'Export the current revisions of all given or generated pages',
577 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
578 );
579 }
580
581 public function getDescription() {
582 return array(
583 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
584 'and is loosely based on the old query.php interface.',
585 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
586 );
587 }
588
589 public function getPossibleErrors() {
590 return array_merge( parent::getPossibleErrors(), array(
591 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
592 ) );
593 }
594
595 protected function getExamples() {
596 return array(
597 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
598 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
599 );
600 }
601
602 public function getVersion() {
603 $psModule = new ApiPageSet( $this );
604 $vers = array();
605 $vers[] = __CLASS__ . ': $Id$';
606 $vers[] = $psModule->getVersion();
607 return $vers;
608 }
609 }