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