03d0b44c84557f3a0d940ec0617f8cbc4a5645e3
[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 // Report special pages
378 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
379 $vals = array();
380 ApiQueryBase::addTitleInfo( $vals, $title );
381 $vals['special'] = '';
382 if ( $title->getNamespace() == NS_SPECIAL &&
383 !SpecialPage::exists( $title->getText() ) ) {
384 $vals['missing'] = '';
385 }
386 $pages[$fakeId] = $vals;
387 }
388
389 // Output general page information for found titles
390 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
391 $vals = array();
392 $vals['pageid'] = $pageid;
393 ApiQueryBase::addTitleInfo( $vals, $title );
394 $pages[$pageid] = $vals;
395 }
396
397 if ( count( $pages ) ) {
398 if ( $this->params['indexpageids'] ) {
399 $pageIDs = array_keys( $pages );
400 // json treats all map keys as strings - converting to match
401 $pageIDs = array_map( 'strval', $pageIDs );
402 $result->setIndexedTagName( $pageIDs, 'id' );
403 $result->addValue( 'query', 'pageids', $pageIDs );
404 }
405
406 $result->setIndexedTagName( $pages, 'page' );
407 $result->addValue( 'query', 'pages', $pages );
408 }
409 if ( $this->params['export'] ) {
410 $exporter = new WikiExporter( $this->getDB() );
411 // WikiExporter writes to stdout, so catch its
412 // output with an ob
413 ob_start();
414 $exporter->openStream();
415 foreach ( @$pageSet->getGoodTitles() as $title ) {
416 if ( $title->userCanRead() ) {
417 $exporter->pageByTitle( $title );
418 }
419 }
420 $exporter->closeStream();
421 $exportxml = ob_get_contents();
422 ob_end_clean();
423
424 // Don't check the size of exported stuff
425 // It's not continuable, so it would cause more
426 // problems than it'd solve
427 $result->disableSizeCheck();
428 if ( $this->params['exportnowrap'] ) {
429 $result->reset();
430 // Raw formatter will handle this
431 $result->addValue( null, 'text', $exportxml );
432 $result->addValue( null, 'mime', 'text/xml' );
433 } else {
434 $r = array();
435 ApiResult::setContent( $r, $exportxml );
436 $result->addValue( 'query', 'export', $r );
437 }
438 $result->enableSizeCheck();
439 }
440 }
441
442 /**
443 * For generator mode, execute generator, and use its output as new
444 * ApiPageSet
445 * @param $generatorName string Module name
446 * @param $modules array of module objects
447 */
448 protected function executeGeneratorModule( $generatorName, $modules ) {
449 // Find class that implements requested generator
450 if ( isset( $this->mQueryListModules[$generatorName] ) ) {
451 $className = $this->mQueryListModules[$generatorName];
452 } elseif ( isset( $this->mQueryPropModules[$generatorName] ) ) {
453 $className = $this->mQueryPropModules[$generatorName];
454 } else {
455 ApiBase::dieDebug( __METHOD__, "Unknown generator=$generatorName" );
456 }
457
458 // Generator results
459 $resultPageSet = new ApiPageSet( $this, $this->redirects );
460
461 // Create and execute the generator
462 $generator = new $className ( $this, $generatorName );
463 if ( !$generator instanceof ApiQueryGeneratorBase ) {
464 $this->dieUsage( "Module $generatorName cannot be used as a generator", 'badgenerator' );
465 }
466
467 $generator->setGeneratorMode();
468
469 // Add any additional fields modules may need
470 $generator->requestExtraData( $this->mPageSet );
471 $this->addCustomFldsToPageSet( $modules, $resultPageSet );
472
473 // Populate page information with the original user input
474 $this->mPageSet->execute();
475
476 // populate resultPageSet with the generator output
477 $generator->profileIn();
478 $generator->executeGenerator( $resultPageSet );
479 wfRunHooks( 'APIQueryGeneratorAfterExecute', array( &$generator, &$resultPageSet ) );
480 $resultPageSet->finishPageSetGeneration();
481 $generator->profileOut();
482
483 // Swap the resulting pageset back in
484 $this->mPageSet = $resultPageSet;
485 }
486
487 public function getAllowedParams() {
488 return array(
489 'prop' => array(
490 ApiBase::PARAM_ISMULTI => true,
491 ApiBase::PARAM_TYPE => $this->mPropModuleNames
492 ),
493 'list' => array(
494 ApiBase::PARAM_ISMULTI => true,
495 ApiBase::PARAM_TYPE => $this->mListModuleNames
496 ),
497 'meta' => array(
498 ApiBase::PARAM_ISMULTI => true,
499 ApiBase::PARAM_TYPE => $this->mMetaModuleNames
500 ),
501 'generator' => array(
502 ApiBase::PARAM_TYPE => $this->mAllowedGenerators
503 ),
504 'redirects' => false,
505 'indexpageids' => false,
506 'export' => false,
507 'exportnowrap' => false,
508 );
509 }
510
511 /**
512 * Override the parent to generate help messages for all available query modules.
513 * @return string
514 */
515 public function makeHelpMsg() {
516 $msg = '';
517
518 // Make sure the internal object is empty
519 // (just in case a sub-module decides to optimize during instantiation)
520 $this->mPageSet = null;
521 $this->mAllowedGenerators = array(); // Will be repopulated
522
523 $astriks = str_repeat( '--- ', 8 );
524 $astriks2 = str_repeat( '*** ', 10 );
525 $msg .= "\n$astriks Query: Prop $astriks\n\n";
526 $msg .= $this->makeHelpMsgHelper( $this->mQueryPropModules, 'prop' );
527 $msg .= "\n$astriks Query: List $astriks\n\n";
528 $msg .= $this->makeHelpMsgHelper( $this->mQueryListModules, 'list' );
529 $msg .= "\n$astriks Query: Meta $astriks\n\n";
530 $msg .= $this->makeHelpMsgHelper( $this->mQueryMetaModules, 'meta' );
531 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
532
533 // Perform the base call last because the $this->mAllowedGenerators
534 // will be updated inside makeHelpMsgHelper()
535 // Use parent to make default message for the query module
536 $msg = parent::makeHelpMsg() . $msg;
537
538 return $msg;
539 }
540
541 /**
542 * For all modules in $moduleList, generate help messages and join them together
543 * @param $moduleList array(modulename => classname)
544 * @param $paramName string Parameter name
545 * @return string
546 */
547 private function makeHelpMsgHelper( $moduleList, $paramName ) {
548 $moduleDescriptions = array();
549
550 foreach ( $moduleList as $moduleName => $moduleClass ) {
551 $module = new $moduleClass ( $this, $moduleName, null );
552
553 $msg = ApiMain::makeHelpMsgHeader( $module, $paramName );
554 $msg2 = $module->makeHelpMsg();
555 if ( $msg2 !== false ) {
556 $msg .= $msg2;
557 }
558 if ( $module instanceof ApiQueryGeneratorBase ) {
559 $this->mAllowedGenerators[] = $moduleName;
560 $msg .= "Generator:\n This module may be used as a generator\n";
561 }
562 $moduleDescriptions[] = $msg;
563 }
564
565 return implode( "\n", $moduleDescriptions );
566 }
567
568 /**
569 * Override to add extra parameters from PageSet
570 * @return string
571 */
572 public function makeHelpMsgParameters() {
573 $psModule = new ApiPageSet( $this );
574 return $psModule->makeHelpMsgParameters() . parent::makeHelpMsgParameters();
575 }
576
577 public function shouldCheckMaxlag() {
578 return true;
579 }
580
581 public function getParamDescription() {
582 return array(
583 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
584 'list' => 'Which lists to get. Module help is available below',
585 'meta' => 'Which metadata to get about the site. Module help is available below',
586 'generator' => array( 'Use the output of a list as the input for other prop/list/meta items',
587 'NOTE: generator parameter names must be prefixed with a \'g\', see examples' ),
588 'redirects' => 'Automatically resolve redirects',
589 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
590 'export' => 'Export the current revisions of all given or generated pages',
591 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
592 );
593 }
594
595 public function getDescription() {
596 return array(
597 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
598 'and is loosely based on the old query.php interface.',
599 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
600 );
601 }
602
603 public function getPossibleErrors() {
604 return array_merge( parent::getPossibleErrors(), array(
605 array( 'code' => 'badgenerator', 'info' => 'Module $generatorName cannot be used as a generator' ),
606 ) );
607 }
608
609 protected function getExamples() {
610 return array(
611 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
612 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
613 );
614 }
615
616 public function getVersion() {
617 $psModule = new ApiPageSet( $this );
618 $vers = array();
619 $vers[] = __CLASS__ . ': $Id$';
620 $vers[] = $psModule->getVersion();
621 return $vers;
622 }
623 }