4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiBase.php');
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.
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.
42 class ApiQuery
extends ApiBase
{
44 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
46 private $params, $redirect;
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',
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',
85 private $mQueryMetaModules = array (
86 'siteinfo' => 'ApiQuerySiteinfo',
87 'userinfo' => 'ApiQueryUserInfo',
88 'allmessages' => 'ApiQueryAllmessages',
91 private $mSlaveDB = null;
92 private $mNamedDB = array();
94 public function __construct($main, $action) {
95 parent
:: __construct($main, $action);
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);
103 $this->mPropModuleNames
= array_keys($this->mQueryPropModules
);
104 $this->mListModuleNames
= array_keys($this->mQueryListModules
);
105 $this->mMetaModuleNames
= array_keys($this->mQueryMetaModules
);
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
);
113 * Helper function to append any add-in modules to the list
115 private static function appendUserModules(&$modules, $newModules) {
116 if (is_array( $newModules )) {
117 foreach ( $newModules as $moduleName => $moduleClass) {
118 $modules[$moduleName] = $moduleClass;
124 * Gets a default slave database connection object
126 public function getDB() {
127 if (!isset ($this->mSlaveDB
)) {
128 $this->profileDBIn();
129 $this->mSlaveDB
= wfGetDB(DB_SLAVE
,'api');
130 $this->profileDBOut();
132 return $this->mSlaveDB
;
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.
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();
147 return $this->mNamedDB
[$name];
151 * Gets the set of pages the user has requested (or generated)
153 public function getPageSet() {
154 return $this->mPageSet
;
158 * Get the array mapping module names to class names
160 function getModules() {
161 return array_merge($this->mQueryPropModules
, $this->mQueryListModules
, $this->mQueryMetaModules
);
164 public function getCustomPrinter() {
165 // If &exportnowrap is set, use the raw formatter
166 if ($this->getParameter('exportnowrap'))
167 return new ApiFormatRaw($this->getMain());
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
182 public function execute() {
184 $this->params
= $this->extractRequestParams();
185 $this->redirects
= $this->params
['redirects'];
190 $this->mPageSet
= new ApiPageSet($this, $this->redirects
);
193 // Instantiate requested modules
196 $this->InstantiateModules($modules, 'prop', $this->mQueryPropModules
);
197 $this->InstantiateModules($modules, 'list', $this->mQueryListModules
);
198 $this->InstantiateModules($modules, 'meta', $this->mQueryMetaModules
);
201 // If given, execute generator to substitute user supplied data with generated data.
203 if (isset ($this->params
['generator'])) {
204 $this->executeGeneratorModule($this->params
['generator'], $modules);
206 // Append custom fields and populate page/revision information
207 $this->addCustomFldsToPageSet($modules, $this->mPageSet
);
208 $this->mPageSet
->execute();
212 // Record page information (title, namespace, if exists, etc)
214 $this->outputGeneralPageInfo();
217 // Execute all requested modules.
219 foreach ($modules as $module) {
220 $module->profileIn();
222 wfRunHooks('APIQueryAfterExecute', array(&$module));
223 $module->profileOut();
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.
232 private function addCustomFldsToPageSet($modules, $pageSet) {
233 // Query all requested modules.
234 foreach ($modules as $module) {
235 $module->requestExtraData($pageSet);
240 * Create instances of all modules requested by the client
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);
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.
253 private function outputGeneralPageInfo() {
255 $pageSet = $this->getPageSet();
256 $result = $this->getResult();
258 // Title normalizations
259 $normValues = array ();
260 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
261 $normValues[] = array (
262 'from' => $rawTitleStr,
267 if (count($normValues)) {
268 $result->setIndexedTagName($normValues, 'n');
269 $result->addValue('query', 'normalized', $normValues);
273 $intrwValues = array ();
274 foreach ($pageSet->getInterwikiTitles() as $rawTitleStr => $interwikiStr) {
275 $intrwValues[] = array (
276 'title' => $rawTitleStr,
277 'iw' => $interwikiStr
281 if (count($intrwValues)) {
282 $result->setIndexedTagName($intrwValues, 'i');
283 $result->addValue('query', 'interwiki', $intrwValues);
286 // Show redirect information
287 $redirValues = array ();
288 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
289 $redirValues[] = array (
290 'from' => strval($titleStrFrom),
295 if (count($redirValues)) {
296 $result->setIndexedTagName($redirValues, 'r');
297 $result->addValue('query', 'redirects', $redirValues);
301 // Missing revision elements
303 $missingRevIDs = $pageSet->getMissingRevisionIDs();
304 if (count($missingRevIDs)) {
306 foreach ($missingRevIDs as $revid) {
307 $revids[$revid] = array (
311 $result->setIndexedTagName($revids, 'rev');
312 $result->addValue('query', 'badrevids', $revids);
320 // Report any missing titles
321 foreach ($pageSet->getMissingTitles() as $fakeId => $title) {
323 ApiQueryBase
:: addTitleInfo($vals, $title);
324 $vals['missing'] = '';
325 $pages[$fakeId] = $vals;
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 (
338 // Output general page information for found titles
339 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
341 $vals['pageid'] = $pageid;
342 ApiQueryBase
:: addTitleInfo($vals, $title);
343 $pages[$pageid] = $vals;
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);
356 $result->setIndexedTagName($pages, 'page');
357 $result->addValue('query', 'pages', $pages);
359 if ($this->params
['export']) {
360 $exporter = new WikiExporter($this->getDB());
361 // WikiExporter writes to stdout, so catch its
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();
371 if ($this->params
['exportnowrap']) {
373 // Raw formatter will handle this
374 $result->addValue(null, 'text', $exportxml);
375 $result->addValue(null, 'mime', 'text/xml');
377 $result->addValue('query', 'export', $exportxml);
383 * For generator mode, execute generator, and use its output as new pageSet
385 protected function executeGeneratorModule($generatorName, $modules) {
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];
393 ApiBase
:: dieDebug(__METHOD__
, "Unknown generator=$generatorName");
397 $resultPageSet = new ApiPageSet($this, $this->redirects
);
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");
404 $generator->setGeneratorMode();
406 // Add any additional fields modules may need
407 $generator->requestExtraData($this->mPageSet
);
408 $this->addCustomFldsToPageSet($modules, $resultPageSet);
410 // Populate page information with the original user input
411 $this->mPageSet
->execute();
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();
420 // Swap the resulting pageset back in
421 $this->mPageSet
= $resultPageSet;
425 * Returns the list of allowed parameters for this module.
426 * Qurey module also lists all ApiPageSet parameters as its own.
428 public function getAllowedParams() {
431 ApiBase
:: PARAM_ISMULTI
=> true,
432 ApiBase
:: PARAM_TYPE
=> $this->mPropModuleNames
435 ApiBase
:: PARAM_ISMULTI
=> true,
436 ApiBase
:: PARAM_TYPE
=> $this->mListModuleNames
439 ApiBase
:: PARAM_ISMULTI
=> true,
440 ApiBase
:: PARAM_TYPE
=> $this->mMetaModuleNames
442 'generator' => array (
443 ApiBase
:: PARAM_TYPE
=> $this->mAllowedGenerators
445 'redirects' => false,
446 'indexpageids' => false,
448 'exportnowrap' => false,
453 * Override the parent to generate help messages for all available query modules.
455 public function makeHelpMsg() {
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
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";
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;
483 * For all modules in $moduleList, generate help messages and join them together
485 private function makeHelpMsgHelper($moduleList, $paramName) {
487 $moduleDscriptions = array ();
489 foreach ($moduleList as $moduleName => $moduleClass) {
490 $module = new $moduleClass ($this, $moduleName, null);
492 $msg = ApiMain
::makeHelpMsgHeader($module, $paramName);
493 $msg2 = $module->makeHelpMsg();
496 if ($module instanceof ApiQueryGeneratorBase
) {
497 $this->mAllowedGenerators
[] = $moduleName;
498 $msg .= "Generator:\n This module may be used as a generator\n";
500 $moduleDscriptions[] = $msg;
503 return implode("\n", $moduleDscriptions);
507 * Override to add extra parameters from PageSet
509 public function makeHelpMsgParameters() {
510 $psModule = new ApiPageSet($this);
511 return $psModule->makeHelpMsgParameters() . parent
:: makeHelpMsgParameters();
514 public function shouldCheckMaxlag() {
518 public function getParamDescription() {
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',
531 public function getDescription() {
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.'
539 protected function getExamples() {
541 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
545 public function getVersion() {
546 $psModule = new ApiPageSet($this);
548 $vers[] = __CLASS__
. ': $Id$';
549 $vers[] = $psModule->getVersion();