fca8e30e9c6b11b0af8220de5fc7abf9c08ccb88
[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 (C) 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 public function getCustomPrinter() {
176 // If &exportnowrap is set, use the raw formatter
177 if ($this->getParameter('export') &&
178 $this->getParameter('exportnowrap'))
179 return new ApiFormatRaw($this->getMain(),
180 $this->getMain()->createPrinterByName('xml'));
181 else
182 return null;
183 }
184
185 /**
186 * Query execution happens in the following steps:
187 * #1 Create a PageSet object with any pages requested by the user
188 * #2 If using a generator, execute it to get a new ApiPageSet object
189 * #3 Instantiate all requested modules.
190 * This way the PageSet object will know what shared data is required,
191 * and minimize DB calls.
192 * #4 Output all normalization and redirect resolution information
193 * #5 Execute all requested modules
194 */
195 public function execute() {
196
197 $this->params = $this->extractRequestParams();
198 $this->redirects = $this->params['redirects'];
199
200 //
201 // Create PageSet
202 //
203 $this->mPageSet = new ApiPageSet($this, $this->redirects);
204
205 //
206 // Instantiate requested modules
207 //
208 $modules = array ();
209 $this->InstantiateModules($modules, 'prop', $this->mQueryPropModules);
210 $this->InstantiateModules($modules, 'list', $this->mQueryListModules);
211 $this->InstantiateModules($modules, 'meta', $this->mQueryMetaModules);
212
213 //
214 // If given, execute generator to substitute user supplied data with generated data.
215 //
216 if (isset ($this->params['generator'])) {
217 $this->executeGeneratorModule($this->params['generator'], $modules);
218 } else {
219 // Append custom fields and populate page/revision information
220 $this->addCustomFldsToPageSet($modules, $this->mPageSet);
221 $this->mPageSet->execute();
222 }
223
224 //
225 // Record page information (title, namespace, if exists, etc)
226 //
227 $this->outputGeneralPageInfo();
228
229 //
230 // Execute all requested modules.
231 //
232 foreach ($modules as $module) {
233 $module->profileIn();
234 $module->execute();
235 wfRunHooks('APIQueryAfterExecute', array(&$module));
236 $module->profileOut();
237 }
238 }
239
240 /**
241 * Query modules may optimize data requests through the $this->getPageSet() object
242 * by adding extra fields from the page table.
243 * This function will gather all the extra request fields from the modules.
244 * @param $modules array of module objects
245 * @param $pageSet ApiPageSet
246 */
247 private function addCustomFldsToPageSet($modules, $pageSet) {
248 // Query all requested modules.
249 foreach ($modules as $module) {
250 $module->requestExtraData($pageSet);
251 }
252 }
253
254 /**
255 * Create instances of all modules requested by the client
256 * @param $modules array to append instatiated modules to
257 * @param $param string Parameter name to read modules from
258 * @param $moduleList array(modulename => classname)
259 */
260 private function InstantiateModules(&$modules, $param, $moduleList) {
261 $list = @$this->params[$param];
262 if (!is_null ($list))
263 foreach ($list as $moduleName)
264 $modules[] = new $moduleList[$moduleName] ($this, $moduleName);
265 }
266
267 /**
268 * Appends an element for each page in the current pageSet with the
269 * most general information (id, title), plus any title normalizations
270 * and missing or invalid title/pageids/revids.
271 */
272 private function outputGeneralPageInfo() {
273
274 $pageSet = $this->getPageSet();
275 $result = $this->getResult();
276
277 # We don't check for a full result set here because we can't be adding
278 # more than 380K. The maximum revision size is in the megabyte range,
279 # and the maximum result size must be even higher than that.
280
281 // Title normalizations
282 $normValues = array ();
283 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
284 $normValues[] = array (
285 'from' => $rawTitleStr,
286 'to' => $titleStr
287 );
288 }
289
290 if (count($normValues)) {
291 $result->setIndexedTagName($normValues, 'n');
292 $result->addValue('query', 'normalized', $normValues);
293 }
294
295 // Interwiki titles
296 $intrwValues = array ();
297 foreach ($pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr) {
298 $intrwValues[] = array (
299 'title' => $rawTitleStr,
300 'iw' => $interwikiStr
301 );
302 }
303
304 if (count($intrwValues)) {
305 $result->setIndexedTagName($intrwValues, 'i');
306 $result->addValue('query', 'interwiki', $intrwValues);
307 }
308
309 // Show redirect information
310 $redirValues = array ();
311 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
312 $redirValues[] = array (
313 'from' => strval($titleStrFrom),
314 'to' => $titleStrTo
315 );
316 }
317
318 if (count($redirValues)) {
319 $result->setIndexedTagName($redirValues, 'r');
320 $result->addValue('query', 'redirects', $redirValues);
321 }
322
323 //
324 // Missing revision elements
325 //
326 $missingRevIDs = $pageSet->getMissingRevisionIDs();
327 if (count($missingRevIDs)) {
328 $revids = array ();
329 foreach ($missingRevIDs as $revid) {
330 $revids[$revid] = array (
331 'revid' => $revid
332 );
333 }
334 $result->setIndexedTagName($revids, 'rev');
335 $result->addValue('query', 'badrevids', $revids);
336 }
337
338 //
339 // Page elements
340 //
341 $pages = array ();
342
343 // Report any missing titles
344 foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
345 $vals = array();
346 ApiQueryBase :: addTitleInfo($vals, $title);
347 $vals['missing'] = '';
348 $pages[$fakeId] = $vals;
349 }
350 // Report any invalid titles
351 foreach ($pageSet->getInvalidTitles() as $fakeId => $title)
352 $pages[$fakeId] = array('title' => $title, 'invalid' => '');
353 // Report any missing page ids
354 foreach ($pageSet->getMissingPageIDs() as $pageid) {
355 $pages[$pageid] = array (
356 'pageid' => $pageid,
357 'missing' => ''
358 );
359 }
360
361 // Output general page information for found titles
362 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
363 $vals = array();
364 $vals['pageid'] = $pageid;
365 ApiQueryBase :: addTitleInfo($vals, $title);
366 $pages[$pageid] = $vals;
367 }
368
369 if (count($pages)) {
370
371 if ($this->params['indexpageids']) {
372 $pageIDs = array_keys($pages);
373 // json treats all map keys as strings - converting to match
374 $pageIDs = array_map('strval', $pageIDs);
375 $result->setIndexedTagName($pageIDs, 'id');
376 $result->addValue('query', 'pageids', $pageIDs);
377 }
378
379 $result->setIndexedTagName($pages, 'page');
380 $result->addValue('query', 'pages', $pages);
381 }
382 if ($this->params['export']) {
383 $exporter = new WikiExporter($this->getDB());
384 // WikiExporter writes to stdout, so catch its
385 // output with an ob
386 ob_start();
387 $exporter->openStream();
388 foreach (@$pageSet->getGoodTitles() as $title)
389 if ($title->userCanRead())
390 $exporter->pageByTitle($title);
391 $exporter->closeStream();
392 $exportxml = ob_get_contents();
393 ob_end_clean();
394 // Don't check the size of exported stuff
395 // It's not continuable, so it would cause more
396 // problems than it'd solve
397 $result->disableSizeCheck();
398 if ($this->params['exportnowrap']) {
399 $result->reset();
400 // Raw formatter will handle this
401 $result->addValue(null, 'text', $exportxml);
402 $result->addValue(null, 'mime', 'text/xml');
403 } else {
404 $r = array();
405 ApiResult::setContent($r, $exportxml);
406 $result->addValue('query', 'export', $r);
407 }
408 $result->enableSizeCheck();
409 }
410 }
411
412 /**
413 * For generator mode, execute generator, and use its output as new
414 * ApiPageSet
415 * @param $generatorName string Module name
416 * @param $modules array of module objects
417 */
418 protected function executeGeneratorModule($generatorName, $modules) {
419
420 // Find class that implements requested generator
421 if (isset ($this->mQueryListModules[$generatorName])) {
422 $className = $this->mQueryListModules[$generatorName];
423 } elseif (isset ($this->mQueryPropModules[$generatorName])) {
424 $className = $this->mQueryPropModules[$generatorName];
425 } else {
426 ApiBase :: dieDebug(__METHOD__, "Unknown generator=$generatorName");
427 }
428
429 // Generator results
430 $resultPageSet = new ApiPageSet($this, $this->redirects);
431
432 // Create and execute the generator
433 $generator = new $className ($this, $generatorName);
434 if (!$generator instanceof ApiQueryGeneratorBase)
435 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
436
437 $generator->setGeneratorMode();
438
439 // Add any additional fields modules may need
440 $generator->requestExtraData($this->mPageSet);
441 $this->addCustomFldsToPageSet($modules, $resultPageSet);
442
443 // Populate page information with the original user input
444 $this->mPageSet->execute();
445
446 // populate resultPageSet with the generator output
447 $generator->profileIn();
448 $generator->executeGenerator($resultPageSet);
449 wfRunHooks('APIQueryGeneratorAfterExecute', array(&$generator, &$resultPageSet));
450 $resultPageSet->finishPageSetGeneration();
451 $generator->profileOut();
452
453 // Swap the resulting pageset back in
454 $this->mPageSet = $resultPageSet;
455 }
456
457 public function getAllowedParams() {
458 return array (
459 'prop' => array (
460 ApiBase :: PARAM_ISMULTI => true,
461 ApiBase :: PARAM_TYPE => $this->mPropModuleNames
462 ),
463 'list' => array (
464 ApiBase :: PARAM_ISMULTI => true,
465 ApiBase :: PARAM_TYPE => $this->mListModuleNames
466 ),
467 'meta' => array (
468 ApiBase :: PARAM_ISMULTI => true,
469 ApiBase :: PARAM_TYPE => $this->mMetaModuleNames
470 ),
471 'generator' => array (
472 ApiBase :: PARAM_TYPE => $this->mAllowedGenerators
473 ),
474 'redirects' => false,
475 'indexpageids' => false,
476 'export' => false,
477 'exportnowrap' => false,
478 );
479 }
480
481 /**
482 * Override the parent to generate help messages for all available query modules.
483 * @return string
484 */
485 public function makeHelpMsg() {
486
487 $msg = '';
488
489 // Make sure the internal object is empty
490 // (just in case a sub-module decides to optimize during instantiation)
491 $this->mPageSet = null;
492 $this->mAllowedGenerators = array(); // Will be repopulated
493
494 $astriks = str_repeat('--- ', 8);
495 $astriks2 = str_repeat('*** ', 10);
496 $msg .= "\n$astriks Query: Prop $astriks\n\n";
497 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules, 'prop');
498 $msg .= "\n$astriks Query: List $astriks\n\n";
499 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules, 'list');
500 $msg .= "\n$astriks Query: Meta $astriks\n\n";
501 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules, 'meta');
502 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
503
504 // Perform the base call last because the $this->mAllowedGenerators
505 // will be updated inside makeHelpMsgHelper()
506 // Use parent to make default message for the query module
507 $msg = parent :: makeHelpMsg() . $msg;
508
509 return $msg;
510 }
511
512 /**
513 * For all modules in $moduleList, generate help messages and join them together
514 * @param $moduleList array(modulename => classname)
515 * @param $paramName string Parameter name
516 * @return string
517 */
518 private function makeHelpMsgHelper($moduleList, $paramName) {
519
520 $moduleDescriptions = array ();
521
522 foreach ($moduleList as $moduleName => $moduleClass) {
523 $module = new $moduleClass ($this, $moduleName, null);
524
525 $msg = ApiMain::makeHelpMsgHeader($module, $paramName);
526 $msg2 = $module->makeHelpMsg();
527 if ($msg2 !== false)
528 $msg .= $msg2;
529 if ($module instanceof ApiQueryGeneratorBase) {
530 $this->mAllowedGenerators[] = $moduleName;
531 $msg .= "Generator:\n This module may be used as a generator\n";
532 }
533 $moduleDescriptions[] = $msg;
534 }
535
536 return implode("\n", $moduleDescriptions);
537 }
538
539 /**
540 * Override to add extra parameters from PageSet
541 * @return string
542 */
543 public function makeHelpMsgParameters() {
544 $psModule = new ApiPageSet($this);
545 return $psModule->makeHelpMsgParameters() . parent :: makeHelpMsgParameters();
546 }
547
548 public function shouldCheckMaxlag() {
549 return true;
550 }
551
552 public function getParamDescription() {
553 return array (
554 'prop' => 'Which properties to get for the titles/revisions/pageids',
555 'list' => 'Which lists to get',
556 'meta' => 'Which meta data to get about the site',
557 'generator' => array('Use the output of a list as the input for other prop/list/meta items',
558 'NOTE: generator parameter names must be prefixed with a \'g\', see examples.'),
559 'redirects' => 'Automatically resolve redirects',
560 'indexpageids' => 'Include an additional pageids section listing all returned page IDs.',
561 'export' => 'Export the current revisions of all given or generated pages',
562 'exportnowrap' => 'Return the export XML without wrapping it in an XML result (same format as Special:Export). Can only be used with export',
563 );
564 }
565
566 public function getDescription() {
567 return array (
568 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
569 'and is loosely based on the old query.php interface.',
570 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
571 );
572 }
573
574 protected function getExamples() {
575 return array (
576 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment',
577 'api.php?action=query&generator=allpages&gapprefix=API/&prop=revisions',
578 );
579 }
580
581 public function getVersion() {
582 $psModule = new ApiPageSet($this);
583 $vers = array ();
584 $vers[] = __CLASS__ . ': $Id$';
585 $vers[] = $psModule->getVersion();
586 return $vers;
587 }
588 }