* API: (bug 17007) Add export functionality to the API
[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 parameters given,
33 * it will create a list of titles to work on (an instance of the ApiPageSet object)
34 * instantiate and execute various property/list/meta modules,
35 * and assemble all resulting data into a single ApiResult object.
36 *
37 * In the generator mode, a generator will be first executed to populate a second ApiPageSet object,
38 * 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 'usercontribs' => 'ApiQueryContributions',
78 'watchlist' => 'ApiQueryWatchlist',
79 'watchlistraw' => 'ApiQueryWatchlistRaw',
80 'exturlusage' => 'ApiQueryExtLinksUsage',
81 'users' => 'ApiQueryUsers',
82 'random' => 'ApiQueryRandom',
83 );
84
85 private $mQueryMetaModules = array (
86 'siteinfo' => 'ApiQuerySiteinfo',
87 'userinfo' => 'ApiQueryUserInfo',
88 'allmessages' => 'ApiQueryAllmessages',
89 );
90
91 private $mSlaveDB = null;
92 private $mNamedDB = array();
93
94 public function __construct($main, $action) {
95 parent :: __construct($main, $action);
96
97 // Allow custom modules to be added in LocalSettings.php
98 global $wgAPIPropModules, $wgAPIListModules, $wgAPIMetaModules;
99 self :: appendUserModules($this->mQueryPropModules, $wgAPIPropModules);
100 self :: appendUserModules($this->mQueryListModules, $wgAPIListModules);
101 self :: appendUserModules($this->mQueryMetaModules, $wgAPIMetaModules);
102
103 $this->mPropModuleNames = array_keys($this->mQueryPropModules);
104 $this->mListModuleNames = array_keys($this->mQueryListModules);
105 $this->mMetaModuleNames = array_keys($this->mQueryMetaModules);
106
107 // Allow the entire list of modules at first,
108 // but during module instantiation check if it can be used as a generator.
109 $this->mAllowedGenerators = array_merge($this->mListModuleNames, $this->mPropModuleNames);
110 }
111
112 /**
113 * Helper function to append any add-in modules to the list
114 */
115 private static function appendUserModules(&$modules, $newModules) {
116 if (is_array( $newModules )) {
117 foreach ( $newModules as $moduleName => $moduleClass) {
118 $modules[$moduleName] = $moduleClass;
119 }
120 }
121 }
122
123 /**
124 * Gets a default slave database connection object
125 */
126 public function getDB() {
127 if (!isset ($this->mSlaveDB)) {
128 $this->profileDBIn();
129 $this->mSlaveDB = wfGetDB(DB_SLAVE,'api');
130 $this->profileDBOut();
131 }
132 return $this->mSlaveDB;
133 }
134
135 /**
136 * Get the query database connection with the given name.
137 * If no such connection has been requested before, it will be created.
138 * Subsequent calls with the same $name will return the same connection
139 * as the first, regardless of $db or $groups new values.
140 */
141 public function getNamedDB($name, $db, $groups) {
142 if (!array_key_exists($name, $this->mNamedDB)) {
143 $this->profileDBIn();
144 $this->mNamedDB[$name] = wfGetDB($db, $groups);
145 $this->profileDBOut();
146 }
147 return $this->mNamedDB[$name];
148 }
149
150 /**
151 * Gets the set of pages the user has requested (or generated)
152 */
153 public function getPageSet() {
154 return $this->mPageSet;
155 }
156
157 /**
158 * Get the array mapping module names to class names
159 */
160 function getModules() {
161 return array_merge($this->mQueryPropModules, $this->mQueryListModules, $this->mQueryMetaModules);
162 }
163
164 public function getCustomPrinter() {
165 // If &exportnowrap is set, use the raw formatter
166 if ($this->getParameter('exportnowrap'))
167 return new ApiFormatRaw($this->getMain());
168 else
169 return null;
170 }
171
172 /**
173 * Query execution happens in the following steps:
174 * #1 Create a PageSet object with any pages requested by the user
175 * #2 If using generator, execute it to get a new PageSet object
176 * #3 Instantiate all requested modules.
177 * This way the PageSet object will know what shared data is required,
178 * and minimize DB calls.
179 * #4 Output all normalization and redirect resolution information
180 * #5 Execute all requested modules
181 */
182 public function execute() {
183
184 $this->params = $this->extractRequestParams();
185 $this->redirects = $this->params['redirects'];
186
187 //
188 // Create PageSet
189 //
190 $this->mPageSet = new ApiPageSet($this, $this->redirects);
191
192 //
193 // Instantiate requested modules
194 //
195 $modules = array ();
196 $this->InstantiateModules($modules, 'prop', $this->mQueryPropModules);
197 $this->InstantiateModules($modules, 'list', $this->mQueryListModules);
198 $this->InstantiateModules($modules, 'meta', $this->mQueryMetaModules);
199
200 //
201 // If given, execute generator to substitute user supplied data with generated data.
202 //
203 if (isset ($this->params['generator'])) {
204 $this->executeGeneratorModule($this->params['generator'], $modules);
205 } else {
206 // Append custom fields and populate page/revision information
207 $this->addCustomFldsToPageSet($modules, $this->mPageSet);
208 $this->mPageSet->execute();
209 }
210
211 //
212 // Record page information (title, namespace, if exists, etc)
213 //
214 $this->outputGeneralPageInfo();
215
216 //
217 // Execute all requested modules.
218 //
219 foreach ($modules as $module) {
220 $module->profileIn();
221 $module->execute();
222 wfRunHooks('APIQueryAfterExecute', array(&$module));
223 $module->profileOut();
224 }
225 }
226
227 /**
228 * Query modules may optimize data requests through the $this->getPageSet() object
229 * by adding extra fields from the page table.
230 * This function will gather all the extra request fields from the modules.
231 */
232 private function addCustomFldsToPageSet($modules, $pageSet) {
233 // Query all requested modules.
234 foreach ($modules as $module) {
235 $module->requestExtraData($pageSet);
236 }
237 }
238
239 /**
240 * Create instances of all modules requested by the client
241 */
242 private function InstantiateModules(&$modules, $param, $moduleList) {
243 $list = @$this->params[$param];
244 if (!is_null ($list))
245 foreach ($list as $moduleName)
246 $modules[] = new $moduleList[$moduleName] ($this, $moduleName);
247 }
248
249 /**
250 * Appends an element for each page in the current pageSet with the most general
251 * information (id, title), plus any title normalizations and missing or invalid title/pageids/revids.
252 */
253 private function outputGeneralPageInfo() {
254
255 $pageSet = $this->getPageSet();
256 $result = $this->getResult();
257
258 // Title normalizations
259 $normValues = array ();
260 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
261 $normValues[] = array (
262 'from' => $rawTitleStr,
263 'to' => $titleStr
264 );
265 }
266
267 if (count($normValues)) {
268 $result->setIndexedTagName($normValues, 'n');
269 $result->addValue('query', 'normalized', $normValues);
270 }
271
272 // Interwiki titles
273 $intrwValues = array ();
274 foreach ($pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr) {
275 $intrwValues[] = array (
276 'title' => $rawTitleStr,
277 'iw' => $interwikiStr
278 );
279 }
280
281 if (count($intrwValues)) {
282 $result->setIndexedTagName($intrwValues, 'i');
283 $result->addValue('query', 'interwiki', $intrwValues);
284 }
285
286 // Show redirect information
287 $redirValues = array ();
288 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
289 $redirValues[] = array (
290 'from' => strval($titleStrFrom),
291 'to' => $titleStrTo
292 );
293 }
294
295 if (count($redirValues)) {
296 $result->setIndexedTagName($redirValues, 'r');
297 $result->addValue('query', 'redirects', $redirValues);
298 }
299
300 //
301 // Missing revision elements
302 //
303 $missingRevIDs = $pageSet->getMissingRevisionIDs();
304 if (count($missingRevIDs)) {
305 $revids = array ();
306 foreach ($missingRevIDs as $revid) {
307 $revids[$revid] = array (
308 'revid' => $revid
309 );
310 }
311 $result->setIndexedTagName($revids, 'rev');
312 $result->addValue('query', 'badrevids', $revids);
313 }
314
315 //
316 // Page elements
317 //
318 $pages = array ();
319
320 // Report any missing titles
321 foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
322 $vals = array();
323 ApiQueryBase :: addTitleInfo($vals, $title);
324 $vals['missing'] = '';
325 $pages[$fakeId] = $vals;
326 }
327 // Report any invalid titles
328 foreach ($pageSet->getInvalidTitles() as $fakeId => $title)
329 $pages[$fakeId] = array('title' => $title, 'invalid' => '');
330 // Report any missing page ids
331 foreach ($pageSet->getMissingPageIDs() as $pageid) {
332 $pages[$pageid] = array (
333 'pageid' => $pageid,
334 'missing' => ''
335 );
336 }
337
338 // Output general page information for found titles
339 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
340 $vals = array();
341 $vals['pageid'] = $pageid;
342 ApiQueryBase :: addTitleInfo($vals, $title);
343 $pages[$pageid] = $vals;
344 }
345
346 if (count($pages)) {
347
348 if ($this->params['indexpageids']) {
349 $pageIDs = array_keys($pages);
350 // json treats all map keys as strings - converting to match
351 $pageIDs = array_map('strval', $pageIDs);
352 $result->setIndexedTagName($pageIDs, 'id');
353 $result->addValue('query', 'pageids', $pageIDs);
354 }
355
356 $result->setIndexedTagName($pages, 'page');
357 $result->addValue('query', 'pages', $pages);
358
359 if ($this->params['export']) {
360 $exporter = new WikiExporter($this->getDB());
361 // WikiExporter writes to stdout, so catch its
362 // output with an ob
363 ob_start();
364 $exporter->openStream();
365 foreach ($pageSet->getGoodTitles() as $title)
366 if ($title->userCanRead())
367 $exporter->pageByTitle($title);
368 $exporter->closeStream();
369 $exportxml = ob_get_contents();
370 ob_end_clean();
371 if ($this->params['exportnowrap']) {
372 $result->reset();
373 // Raw formatter will handle this
374 $result->addValue(null, 'text', $exportxml);
375 $result->addValue(null, 'mime', 'text/xml');
376 } else
377 $result->addValue('query', 'export', $exportxml);
378 }
379 }
380 }
381
382 /**
383 * For generator mode, execute generator, and use its output as new pageSet
384 */
385 protected function executeGeneratorModule($generatorName, $modules) {
386
387 // Find class that implements requested generator
388 if (isset ($this->mQueryListModules[$generatorName])) {
389 $className = $this->mQueryListModules[$generatorName];
390 } elseif (isset ($this->mQueryPropModules[$generatorName])) {
391 $className = $this->mQueryPropModules[$generatorName];
392 } else {
393 ApiBase :: dieDebug(__METHOD__, "Unknown generator=$generatorName");
394 }
395
396 // Generator results
397 $resultPageSet = new ApiPageSet($this, $this->redirects);
398
399 // Create and execute the generator
400 $generator = new $className ($this, $generatorName);
401 if (!$generator instanceof ApiQueryGeneratorBase)
402 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
403
404 $generator->setGeneratorMode();
405
406 // Add any additional fields modules may need
407 $generator->requestExtraData($this->mPageSet);
408 $this->addCustomFldsToPageSet($modules, $resultPageSet);
409
410 // Populate page information with the original user input
411 $this->mPageSet->execute();
412
413 // populate resultPageSet with the generator output
414 $generator->profileIn();
415 $generator->executeGenerator($resultPageSet);
416 wfRunHooks('APIQueryGeneratorAfterExecute', array(&$generator, &$resultPageSet));
417 $resultPageSet->finishPageSetGeneration();
418 $generator->profileOut();
419
420 // Swap the resulting pageset back in
421 $this->mPageSet = $resultPageSet;
422 }
423
424 /**
425 * Returns the list of allowed parameters for this module.
426 * Qurey module also lists all ApiPageSet parameters as its own.
427 */
428 public function getAllowedParams() {
429 return array (
430 'prop' => array (
431 ApiBase :: PARAM_ISMULTI => true,
432 ApiBase :: PARAM_TYPE => $this->mPropModuleNames
433 ),
434 'list' => array (
435 ApiBase :: PARAM_ISMULTI => true,
436 ApiBase :: PARAM_TYPE => $this->mListModuleNames
437 ),
438 'meta' => array (
439 ApiBase :: PARAM_ISMULTI => true,
440 ApiBase :: PARAM_TYPE => $this->mMetaModuleNames
441 ),
442 'generator' => array (
443 ApiBase :: PARAM_TYPE => $this->mAllowedGenerators
444 ),
445 'redirects' => false,
446 'indexpageids' => false,
447 'export' => false,
448 'exportnowrap' => false,
449 );
450 }
451
452 /**
453 * Override the parent to generate help messages for all available query modules.
454 */
455 public function makeHelpMsg() {
456
457 $msg = '';
458
459 // Make sure the internal object is empty
460 // (just in case a sub-module decides to optimize during instantiation)
461 $this->mPageSet = null;
462 $this->mAllowedGenerators = array(); // Will be repopulated
463
464 $astriks = str_repeat('--- ', 8);
465 $astriks2 = str_repeat('*** ', 10);
466 $msg .= "\n$astriks Query: Prop $astriks\n\n";
467 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules, 'prop');
468 $msg .= "\n$astriks Query: List $astriks\n\n";
469 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules, 'list');
470 $msg .= "\n$astriks Query: Meta $astriks\n\n";
471 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules, 'meta');
472 $msg .= "\n\n$astriks2 Modules: continuation $astriks2\n\n";
473
474 // Perform the base call last because the $this->mAllowedGenerators
475 // will be updated inside makeHelpMsgHelper()
476 // Use parent to make default message for the query module
477 $msg = parent :: makeHelpMsg() . $msg;
478
479 return $msg;
480 }
481
482 /**
483 * For all modules in $moduleList, generate help messages and join them together
484 */
485 private function makeHelpMsgHelper($moduleList, $paramName) {
486
487 $moduleDscriptions = array ();
488
489 foreach ($moduleList as $moduleName => $moduleClass) {
490 $module = new $moduleClass ($this, $moduleName, null);
491
492 $msg = ApiMain::makeHelpMsgHeader($module, $paramName);
493 $msg2 = $module->makeHelpMsg();
494 if ($msg2 !== false)
495 $msg .= $msg2;
496 if ($module instanceof ApiQueryGeneratorBase) {
497 $this->mAllowedGenerators[] = $moduleName;
498 $msg .= "Generator:\n This module may be used as a generator\n";
499 }
500 $moduleDscriptions[] = $msg;
501 }
502
503 return implode("\n", $moduleDscriptions);
504 }
505
506 /**
507 * Override to add extra parameters from PageSet
508 */
509 public function makeHelpMsgParameters() {
510 $psModule = new ApiPageSet($this);
511 return $psModule->makeHelpMsgParameters() . parent :: makeHelpMsgParameters();
512 }
513
514 public function shouldCheckMaxlag() {
515 return true;
516 }
517
518 public function getParamDescription() {
519 return array (
520 'prop' => 'Which properties to get for the titles/revisions/pageids',
521 'list' => 'Which lists to get',
522 'meta' => 'Which meta data to get about the site',
523 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
524 'redirects' => 'Automatically resolve redirects',
525 'indexpageids' => 'Include an additional pageids section listing all returned page IDs.',
526 'export' => 'Export the current revisions of all given or generated pages',
527 'exportnowrap' => 'Return the export XML without wrapping it in an XML result',
528 );
529 }
530
531 public function getDescription() {
532 return array (
533 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
534 'and is loosely based on the old query.php interface.',
535 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
536 );
537 }
538
539 protected function getExamples() {
540 return array (
541 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
542 );
543 }
544
545 public function getVersion() {
546 $psModule = new ApiPageSet($this);
547 $vers = array ();
548 $vers[] = __CLASS__ . ': $Id$';
549 $vers[] = $psModule->getVersion();
550 return $vers;
551 }
552 }