619d1cae517180bb2f0feec490ec70e0a0b6abf9
[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 /**
41 * List of Api Query prop modules
42 * @var array
43 */
44 private static $QueryPropModules = array(
45 'categories' => 'ApiQueryCategories',
46 'categoryinfo' => 'ApiQueryCategoryInfo',
47 'duplicatefiles' => 'ApiQueryDuplicateFiles',
48 'extlinks' => 'ApiQueryExternalLinks',
49 'images' => 'ApiQueryImages',
50 'imageinfo' => 'ApiQueryImageInfo',
51 'info' => 'ApiQueryInfo',
52 'links' => 'ApiQueryLinks',
53 'iwlinks' => 'ApiQueryIWLinks',
54 'langlinks' => 'ApiQueryLangLinks',
55 'pageprops' => 'ApiQueryPageProps',
56 'revisions' => 'ApiQueryRevisions',
57 'stashimageinfo' => 'ApiQueryStashImageInfo',
58 'templates' => 'ApiQueryLinks',
59 );
60
61 /**
62 * List of Api Query list modules
63 * @var array
64 */
65 private static $QueryListModules = array(
66 'allcategories' => 'ApiQueryAllCategories',
67 'allimages' => 'ApiQueryAllImages',
68 'alllinks' => 'ApiQueryAllLinks',
69 'allpages' => 'ApiQueryAllPages',
70 'alltransclusions' => 'ApiQueryAllLinks',
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 /**
96 * List of Api Query meta modules
97 * @var array
98 */
99 private static $QueryMetaModules = array(
100 'allmessages' => 'ApiQueryAllMessages',
101 'siteinfo' => 'ApiQuerySiteinfo',
102 'userinfo' => 'ApiQueryUserInfo',
103 );
104
105 /**
106 * List of Api Query generator modules
107 * Defined in code, rather than being derived at runtime,
108 * due to performance reasons
109 * @var array
110 */
111 private $mQueryGenerators = array(
112 'allcategories' => 'ApiQueryAllCategories',
113 'allimages' => 'ApiQueryAllImages',
114 'alllinks' => 'ApiQueryAllLinks',
115 'allpages' => 'ApiQueryAllPages',
116 'alltransclusions' => 'ApiQueryAllLinks',
117 'backlinks' => 'ApiQueryBacklinks',
118 'categories' => 'ApiQueryCategories',
119 'categorymembers' => 'ApiQueryCategoryMembers',
120 'duplicatefiles' => 'ApiQueryDuplicateFiles',
121 'embeddedin' => 'ApiQueryBacklinks',
122 'exturlusage' => 'ApiQueryExtLinksUsage',
123 'images' => 'ApiQueryImages',
124 'imageusage' => 'ApiQueryBacklinks',
125 'iwbacklinks' => 'ApiQueryIWBacklinks',
126 'langbacklinks' => 'ApiQueryLangBacklinks',
127 'links' => 'ApiQueryLinks',
128 'protectedtitles' => 'ApiQueryProtectedTitles',
129 'querypage' => 'ApiQueryQueryPage',
130 'random' => 'ApiQueryRandom',
131 'recentchanges' => 'ApiQueryRecentChanges',
132 'search' => 'ApiQuerySearch',
133 'templates' => 'ApiQueryLinks',
134 'watchlist' => 'ApiQueryWatchlist',
135 'watchlistraw' => 'ApiQueryWatchlistRaw',
136 );
137
138 /**
139 * @var ApiPageSet
140 */
141 private $mPageSet;
142
143 private $params;
144 private $iwUrl;
145 private $mNamedDB = array();
146 private $mModuleMgr;
147
148 /**
149 * @param $main ApiMain
150 * @param $action string
151 */
152 public function __construct( $main, $action ) {
153 parent::__construct( $main, $action );
154
155 $this->mModuleMgr = new ApiModuleManager( $this );
156
157 // Allow custom modules to be added in LocalSettings.php
158 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
159 $this->mModuleMgr->addModules( self::$QueryPropModules, 'prop' );
160 $this->mModuleMgr->addModules( $wgAPIPropModules, 'prop' );
161 $this->mModuleMgr->addModules( self::$QueryListModules, 'list' );
162 $this->mModuleMgr->addModules( $wgAPIListModules, 'list' );
163 $this->mModuleMgr->addModules( self::$QueryMetaModules, 'meta' );
164 $this->mModuleMgr->addModules( $wgAPIMetaModules, 'meta' );
165
166 global $wgAPIGeneratorModules;
167 if ( is_array( $wgAPIGeneratorModules ) ) {
168 foreach ( $wgAPIGeneratorModules as $moduleName => $moduleClass ) {
169 $this->mQueryGenerators[$moduleName] = $moduleClass;
170 }
171 }
172
173 // Create PageSet that will process titles/pageids/revids/generator
174 $this->mPageSet = new ApiPageSet( $this );
175 }
176
177 /**
178 * Overrides to return this instance's module manager.
179 * @return ApiModuleManager
180 */
181 public function getModuleManager() {
182 return $this->mModuleMgr;
183 }
184
185 /**
186 * Get the query database connection with the given name.
187 * If no such connection has been requested before, it will be created.
188 * Subsequent calls with the same $name will return the same connection
189 * as the first, regardless of the values of $db and $groups
190 * @param $name string Name to assign to the database connection
191 * @param $db int One of the DB_* constants
192 * @param $groups array Query groups
193 * @return DatabaseBase
194 */
195 public function getNamedDB( $name, $db, $groups ) {
196 if ( !array_key_exists( $name, $this->mNamedDB ) ) {
197 $this->profileDBIn();
198 $this->mNamedDB[$name] = wfGetDB( $db, $groups );
199 $this->profileDBOut();
200 }
201 return $this->mNamedDB[$name];
202 }
203
204 /**
205 * Gets the set of pages the user has requested (or generated)
206 * @return ApiPageSet
207 */
208 public function getPageSet() {
209 return $this->mPageSet;
210 }
211
212 /**
213 * Get the array mapping module names to class names
214 * @deprecated since 1.21, use getModuleManager()'s methods instead
215 * @return array array(modulename => classname)
216 */
217 public function getModules() {
218 wfDeprecated( __METHOD__, '1.21' );
219 return $this->getModuleManager()->getNamesWithClasses();
220 }
221
222 /**
223 * Get the generators array mapping module names to class names
224 * @return array array(modulename => classname)
225 */
226 public function getGenerators() {
227 return $this->mQueryGenerators;
228 }
229
230 /**
231 * Get whether the specified module is a prop, list or a meta query module
232 * @deprecated since 1.21, use getModuleManager()->getModuleGroup()
233 * @param $moduleName string Name of the module to find type for
234 * @return mixed string or null
235 */
236 function getModuleType( $moduleName ) {
237 return $this->getModuleManager()->getModuleGroup( $moduleName );
238 }
239
240 /**
241 * @return ApiFormatRaw|null
242 */
243 public function getCustomPrinter() {
244 // If &exportnowrap is set, use the raw formatter
245 if ( $this->getParameter( 'export' ) &&
246 $this->getParameter( 'exportnowrap' ) )
247 {
248 return new ApiFormatRaw( $this->getMain(),
249 $this->getMain()->createPrinterByName( 'xml' ) );
250 } else {
251 return null;
252 }
253 }
254
255 /**
256 * Query execution happens in the following steps:
257 * #1 Create a PageSet object with any pages requested by the user
258 * #2 If using a generator, execute it to get a new ApiPageSet object
259 * #3 Instantiate all requested modules.
260 * This way the PageSet object will know what shared data is required,
261 * and minimize DB calls.
262 * #4 Output all normalization and redirect resolution information
263 * #5 Execute all requested modules
264 */
265 public function execute() {
266 $this->params = $this->extractRequestParams();
267 $this->iwUrl = $this->params['iwurl'];
268
269 // Instantiate requested modules
270 $modules = array();
271 $this->instantiateModules( $modules, 'prop' );
272 $this->instantiateModules( $modules, 'list' );
273 $this->instantiateModules( $modules, 'meta' );
274
275 // Query modules may optimize data requests through the $this->getPageSet()
276 // object by adding extra fields from the page table.
277 // This function will gather all the extra request fields from the modules.
278 foreach ( $modules as $module ) {
279 $module->requestExtraData( $this->mPageSet );
280 }
281
282 // Populate page/revision information
283 $this->mPageSet->execute();
284 $cacheMode = $this->mPageSet->getCacheMode();
285
286 // Record page information (title, namespace, if exists, etc)
287 $this->outputGeneralPageInfo();
288
289 // Execute all requested modules.
290 /**
291 * @var $module ApiQueryBase
292 */
293 foreach ( $modules as $module ) {
294 $params = $module->extractRequestParams();
295 $cacheMode = $this->mergeCacheMode(
296 $cacheMode, $module->getCacheMode( $params ) );
297 $module->profileIn();
298 $module->execute();
299 wfRunHooks( 'APIQueryAfterExecute', array( &$module ) );
300 $module->profileOut();
301 }
302
303 // Set the cache mode
304 $this->getMain()->setCacheMode( $cacheMode );
305 }
306
307 /**
308 * Update a cache mode string, applying the cache mode of a new module to it.
309 * The cache mode may increase in the level of privacy, but public modules
310 * added to private data do not decrease the level of privacy.
311 *
312 * @param $cacheMode string
313 * @param $modCacheMode string
314 * @return string
315 */
316 protected function mergeCacheMode( $cacheMode, $modCacheMode ) {
317 if ( $modCacheMode === 'anon-public-user-private' ) {
318 if ( $cacheMode !== 'private' ) {
319 $cacheMode = 'anon-public-user-private';
320 }
321 } elseif ( $modCacheMode === 'public' ) {
322 // do nothing, if it's public already it will stay public
323 } else { // private
324 $cacheMode = 'private';
325 }
326 return $cacheMode;
327 }
328
329 /**
330 * Create instances of all modules requested by the client
331 * @param $modules Array to append instantiated modules to
332 * @param $param string Parameter name to read modules from
333 */
334 private function instantiateModules( &$modules, $param ) {
335 if ( isset( $this->params[$param] ) ) {
336 foreach ( $this->params[$param] as $moduleName ) {
337 $modules[] = $this->mModuleMgr->getModule( $moduleName );
338 }
339 }
340 }
341
342 /**
343 * Appends an element for each page in the current pageSet with the
344 * most general information (id, title), plus any title normalizations
345 * and missing or invalid title/pageids/revids.
346 */
347 private function outputGeneralPageInfo() {
348 $pageSet = $this->getPageSet();
349 $result = $this->getResult();
350
351 // We don't check for a full result set here because we can't be adding
352 // more than 380K. The maximum revision size is in the megabyte range,
353 // and the maximum result size must be even higher than that.
354
355 $values = $pageSet->getNormalizedTitlesAsResult( $result );
356 if ( $values ) {
357 $result->addValue( 'query', 'normalized', $values );
358 }
359 $values = $pageSet->getConvertedTitlesAsResult( $result );
360 if ( $values ) {
361 $result->addValue( 'query', 'converted', $values );
362 }
363 $values = $pageSet->getInterwikiTitlesAsResult( $result, $this->iwUrl );
364 if ( $values ) {
365 $result->addValue( 'query', 'interwiki', $values );
366 }
367 $values = $pageSet->getRedirectTitlesAsResult( $result );
368 if ( $values ) {
369 $result->addValue( 'query', 'redirects', $values );
370 }
371 $values = $pageSet->getMissingRevisionIDsAsResult( $result );
372 if ( $values ) {
373 $result->addValue( 'query', 'badrevids', $values );
374 }
375
376 // Page elements
377 $pages = array();
378
379 // Report any missing titles
380 foreach ( $pageSet->getMissingTitles() as $fakeId => $title ) {
381 $vals = array();
382 ApiQueryBase::addTitleInfo( $vals, $title );
383 $vals['missing'] = '';
384 $pages[$fakeId] = $vals;
385 }
386 // Report any invalid titles
387 foreach ( $pageSet->getInvalidTitles() as $fakeId => $title ) {
388 $pages[$fakeId] = array( 'title' => $title, 'invalid' => '' );
389 }
390 // Report any missing page ids
391 foreach ( $pageSet->getMissingPageIDs() as $pageid ) {
392 $pages[$pageid] = array(
393 'pageid' => $pageid,
394 'missing' => ''
395 );
396 }
397 // Report special pages
398 foreach ( $pageSet->getSpecialTitles() as $fakeId => $title ) {
399 $vals = array();
400 ApiQueryBase::addTitleInfo( $vals, $title );
401 $vals['special'] = '';
402 if ( $title->isSpecialPage() &&
403 !SpecialPageFactory::exists( $title->getDbKey() ) ) {
404 $vals['missing'] = '';
405 } elseif ( $title->getNamespace() == NS_MEDIA &&
406 !wfFindFile( $title ) ) {
407 $vals['missing'] = '';
408 }
409 $pages[$fakeId] = $vals;
410 }
411
412 // Output general page information for found titles
413 foreach ( $pageSet->getGoodTitles() as $pageid => $title ) {
414 $vals = array();
415 $vals['pageid'] = $pageid;
416 ApiQueryBase::addTitleInfo( $vals, $title );
417 $pages[$pageid] = $vals;
418 }
419
420 if ( count( $pages ) ) {
421 if ( $this->params['indexpageids'] ) {
422 $pageIDs = array_keys( $pages );
423 // json treats all map keys as strings - converting to match
424 $pageIDs = array_map( 'strval', $pageIDs );
425 $result->setIndexedTagName( $pageIDs, 'id' );
426 $result->addValue( 'query', 'pageids', $pageIDs );
427 }
428
429 $result->setIndexedTagName( $pages, 'page' );
430 $result->addValue( 'query', 'pages', $pages );
431 }
432 if ( $this->params['export'] ) {
433 $this->doExport( $pageSet, $result );
434 }
435 }
436
437 /**
438 * @param $pageSet ApiPageSet Pages to be exported
439 * @param $result ApiResult Result to output to
440 */
441 private function doExport( $pageSet, $result ) {
442 $exportTitles = array();
443 $titles = $pageSet->getGoodTitles();
444 if ( count( $titles ) ) {
445 $user = $this->getUser();
446 foreach ( $titles as $title ) {
447 if ( $title->userCan( 'read', $user ) ) {
448 $exportTitles[] = $title;
449 }
450 }
451 }
452
453 $exporter = new WikiExporter( $this->getDB() );
454 // WikiExporter writes to stdout, so catch its
455 // output with an ob
456 ob_start();
457 $exporter->openStream();
458 foreach ( $exportTitles as $title ) {
459 $exporter->pageByTitle( $title );
460 }
461 $exporter->closeStream();
462 $exportxml = ob_get_contents();
463 ob_end_clean();
464
465 // Don't check the size of exported stuff
466 // It's not continuable, so it would cause more
467 // problems than it'd solve
468 $result->disableSizeCheck();
469 if ( $this->params['exportnowrap'] ) {
470 $result->reset();
471 // Raw formatter will handle this
472 $result->addValue( null, 'text', $exportxml );
473 $result->addValue( null, 'mime', 'text/xml' );
474 } else {
475 $r = array();
476 ApiResult::setContent( $r, $exportxml );
477 $result->addValue( 'query', 'export', $r );
478 }
479 $result->enableSizeCheck();
480 }
481
482 public function getAllowedParams( $flags = 0 ) {
483 $result = array(
484 'prop' => array(
485 ApiBase::PARAM_ISMULTI => true,
486 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'prop' )
487 ),
488 'list' => array(
489 ApiBase::PARAM_ISMULTI => true,
490 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'list' )
491 ),
492 'meta' => array(
493 ApiBase::PARAM_ISMULTI => true,
494 ApiBase::PARAM_TYPE => $this->mModuleMgr->getNames( 'meta' )
495 ),
496 'indexpageids' => false,
497 'export' => false,
498 'exportnowrap' => false,
499 'iwurl' => false,
500 );
501 if( $flags ) {
502 $result += $this->getPageSet()->getFinalParams( $flags );
503 }
504 return $result;
505 }
506
507 /**
508 * Override the parent to generate help messages for all available query modules.
509 * @return string
510 */
511 public function makeHelpMsg() {
512
513 // Use parent to make default message for the query module
514 $msg = parent::makeHelpMsg();
515
516 $querySeparator = str_repeat( '--- ', 12 );
517 $moduleSeparator = str_repeat( '*** ', 14 );
518 $msg .= "\n$querySeparator Query: Prop $querySeparator\n\n";
519 $msg .= $this->makeHelpMsgHelper( 'prop' );
520 $msg .= "\n$querySeparator Query: List $querySeparator\n\n";
521 $msg .= $this->makeHelpMsgHelper( 'list' );
522 $msg .= "\n$querySeparator Query: Meta $querySeparator\n\n";
523 $msg .= $this->makeHelpMsgHelper( 'meta' );
524 $msg .= "\n\n$moduleSeparator Modules: continuation $moduleSeparator\n\n";
525
526 return $msg;
527 }
528
529 /**
530 * For all modules of a given group, generate help messages and join them together
531 * @param $group string Module group
532 * @return string
533 */
534 private function makeHelpMsgHelper( $group ) {
535 $moduleDescriptions = array();
536
537 $moduleNames = $this->mModuleMgr->getNames( $group );
538 sort( $moduleNames );
539 foreach ( $moduleNames as $name ) {
540 /**
541 * @var $module ApiQueryBase
542 */
543 $module = $this->mModuleMgr->getModule( $name );
544
545 $msg = ApiMain::makeHelpMsgHeader( $module, $group );
546 $msg2 = $module->makeHelpMsg();
547 if ( $msg2 !== false ) {
548 $msg .= $msg2;
549 }
550 if ( $module instanceof ApiQueryGeneratorBase ) {
551 $msg .= "Generator:\n This module may be used as a generator\n";
552 }
553 $moduleDescriptions[] = $msg;
554 }
555
556 return implode( "\n", $moduleDescriptions );
557 }
558
559 public function shouldCheckMaxlag() {
560 return true;
561 }
562
563 public function getParamDescription() {
564 return $this->getPageSet()->getParamDescription() + array(
565 'prop' => 'Which properties to get for the titles/revisions/pageids. Module help is available below',
566 'list' => 'Which lists to get. Module help is available below',
567 'meta' => 'Which metadata to get about the site. Module help is available below',
568 'indexpageids' => 'Include an additional pageids section listing all returned page IDs',
569 'export' => 'Export the current revisions of all given or generated pages',
570 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
571 'iwurl' => 'Whether to get the full URL if the title is an interwiki link',
572 );
573 }
574
575 public function getDescription() {
576 return array(
577 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
578 'and is loosely based on the old query.php interface.',
579 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites'
580 );
581 }
582
583 public function getPossibleErrors() {
584 return array_merge(
585 parent::getPossibleErrors(),
586 $this->getPageSet()->getPossibleErrors()
587 );
588 }
589
590 public function getExamples() {
591 return array(
592 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
593 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
594 );
595 }
596
597 public function getHelpUrls() {
598 return array(
599 'https://www.mediawiki.org/wiki/API:Meta',
600 'https://www.mediawiki.org/wiki/API:Properties',
601 'https://www.mediawiki.org/wiki/API:Lists',
602 );
603 }
604 }