* Removed inconsistent hard coding of colons
[mediawiki.git] / includes / api / ApiQuery.php
bloba42dec23498aab58b2e69e695e57c35779ea8754
1 <?php
3 /*
4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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');
31 /**
32 * @addtogroup API
34 class ApiQuery extends ApiBase {
36 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
37 private $mPageSet;
39 private $mQueryPropModules = array (
40 'info' => 'ApiQueryInfo',
41 'revisions' => 'ApiQueryRevisions'
43 // 'categories' => 'ApiQueryCategories',
44 // 'imageinfo' => 'ApiQueryImageinfo',
45 // 'langlinks' => 'ApiQueryLanglinks',
46 // 'links' => 'ApiQueryLinks',
47 // 'templates' => 'ApiQueryTemplates',
49 private $mQueryListModules = array (
50 'allpages' => 'ApiQueryAllpages',
51 'logevents' => 'ApiQueryLogEvents',
52 'watchlist' => 'ApiQueryWatchlist',
53 'recentchanges' => 'ApiQueryRecentChanges',
54 'backlinks' => 'ApiQueryBacklinks',
55 'embeddedin' => 'ApiQueryBacklinks',
56 'imagelinks' => 'ApiQueryBacklinks',
57 'usercontribs' => 'ApiQueryContributions'
59 // 'categorymembers' => 'ApiQueryCategorymembers',
60 // 'embeddedin' => 'ApiQueryEmbeddedin',
61 // 'imagelinks' => 'ApiQueryImagelinks',
62 // 'recentchanges' => 'ApiQueryRecentchanges',
63 // 'users' => 'ApiQueryUsers',
64 // 'watchlist' => 'ApiQueryWatchlist',
66 private $mQueryMetaModules = array (
67 'siteinfo' => 'ApiQuerySiteinfo'
69 // 'userinfo' => 'ApiQueryUserinfo',
71 private $mSlaveDB = null;
73 public function __construct($main, $action) {
74 parent :: __construct($main, $action);
75 $this->mPropModuleNames = array_keys($this->mQueryPropModules);
76 $this->mListModuleNames = array_keys($this->mQueryListModules);
77 $this->mMetaModuleNames = array_keys($this->mQueryMetaModules);
79 // Allow the entire list of modules at first,
80 // but during module instantiation check if it can be used as a generator.
81 $this->mAllowedGenerators = array_merge($this->mListModuleNames, $this->mPropModuleNames);
84 public function getDB() {
85 if (!isset ($this->mSlaveDB)) {
86 $this->profileDBIn();
87 $this->mSlaveDB = wfGetDB(DB_SLAVE);
88 $this->profileDBOut();
90 return $this->mSlaveDB;
93 public function getPageSet() {
94 return $this->mPageSet;
97 /**
98 * Query execution happens in the following steps:
99 * #1 Create a PageSet object with any pages requested by the user
100 * #2 If using generator, execute it to get a new PageSet object
101 * #3 Instantiate all requested modules.
102 * This way the PageSet object will know what shared data is required,
103 * and minimize DB calls.
104 * #4 Output all normalization and redirect resolution information
105 * #5 Execute all requested modules
107 public function execute() {
108 $prop = $list = $meta = $generator = $redirects = null;
109 extract($this->extractRequestParams());
112 // Create PageSet
114 $this->mPageSet = new ApiPageSet($this, $redirects);
116 // Instantiate required modules
117 $modules = array ();
118 if (isset ($prop))
119 foreach ($prop as $moduleName)
120 $modules[] = new $this->mQueryPropModules[$moduleName] ($this, $moduleName);
121 if (isset ($list))
122 foreach ($list as $moduleName)
123 $modules[] = new $this->mQueryListModules[$moduleName] ($this, $moduleName);
124 if (isset ($meta))
125 foreach ($meta as $moduleName)
126 $modules[] = new $this->mQueryMetaModules[$moduleName] ($this, $moduleName);
128 // Modules may optimize data requests through the $this->getPageSet() object
129 // Execute all requested modules.
130 foreach ($modules as $module) {
131 $module->requestExtraData();
135 // If given, execute generator to substitute user supplied data with generated data.
137 if (isset ($generator))
138 $this->executeGeneratorModule($generator, $redirects);
141 // Populate page information for the given pageSet
143 $this->mPageSet->execute();
146 // Record page information (title, namespace, if exists, etc)
148 $this->outputGeneralPageInfo();
151 // Execute all requested modules.
153 foreach ($modules as $module) {
154 $module->profileIn();
155 $module->execute();
156 $module->profileOut();
160 private function outputGeneralPageInfo() {
162 $pageSet = $this->getPageSet();
163 $result = $this->getResult();
165 // Title normalizations
166 $normValues = array ();
167 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
168 $normValues[] = array (
169 'from' => $rawTitleStr,
170 'to' => $titleStr
174 if (!empty ($normValues)) {
175 $result->setIndexedTagName($normValues, 'n');
176 $result->addValue('query', 'normalized', $normValues);
179 // Show redirect information
180 $redirValues = array ();
181 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
182 $redirValues[] = array (
183 'from' => $titleStrFrom,
184 'to' => $titleStrTo
188 if (!empty ($redirValues)) {
189 $result->setIndexedTagName($redirValues, 'r');
190 $result->addValue('query', 'redirects', $redirValues);
194 // Missing revision elements
196 $missingRevIDs = $pageSet->getMissingRevisionIDs();
197 if (!empty ($missingRevIDs)) {
198 $revids = array ();
199 foreach ($missingRevIDs as $revid) {
200 $revids[$revid] = array (
201 'revid' => $revid
204 $result->setIndexedTagName($revids, 'rev');
205 $result->addValue('query', 'badrevids', $revids);
209 // Page elements
211 $pages = array ();
213 // Report any missing titles
214 $fakepageid = -1;
215 foreach ($pageSet->getMissingTitles() as $title) {
216 $pages[$fakepageid--] = array (
217 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'missing' => '');
220 // Report any missing page ids
221 foreach ($pageSet->getMissingPageIDs() as $pageid) {
222 $pages[$pageid] = array (
223 'pageid' => $pageid,
224 'missing' => ''
228 // Output general page information for found titles
229 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
230 $pages[$pageid] = array (
231 'pageid' => $pageid,
232 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText());
235 if (!empty ($pages)) {
236 $result->setIndexedTagName($pages, 'page');
237 $result->addValue('query', 'pages', $pages);
241 protected function executeGeneratorModule($generatorName, $redirects) {
243 // Find class that implements requested generator
244 if (isset ($this->mQueryListModules[$generatorName])) {
245 $className = $this->mQueryListModules[$generatorName];
247 elseif (isset ($this->mQueryPropModules[$generatorName])) {
248 $className = $this->mQueryPropModules[$generatorName];
249 } else {
250 ApiBase :: dieDebug(__METHOD__, "Unknown generator=$generatorName");
253 // Use current pageset as the result, and create a new one just for the generator
254 $resultPageSet = $this->mPageSet;
255 $this->mPageSet = new ApiPageSet($this, $redirects);
257 // Create and execute the generator
258 $generator = new $className ($this, $generatorName);
259 if (!$generator instanceof ApiQueryGeneratorBase)
260 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
262 $generator->setGeneratorMode();
263 $generator->requestExtraData();
265 // execute current pageSet to get the data for the generator module
266 $this->mPageSet->execute();
268 // populate resultPageSet with the generator output
269 $generator->profileIn();
270 $generator->executeGenerator($resultPageSet);
271 $resultPageSet->finishPageSetGeneration();
272 $generator->profileOut();
274 // Swap the resulting pageset back in
275 $this->mPageSet = $resultPageSet;
278 protected function getAllowedParams() {
279 return array (
280 'prop' => array (
281 ApiBase :: PARAM_ISMULTI => true,
282 ApiBase :: PARAM_TYPE => $this->mPropModuleNames
284 'list' => array (
285 ApiBase :: PARAM_ISMULTI => true,
286 ApiBase :: PARAM_TYPE => $this->mListModuleNames
288 'meta' => array (
289 ApiBase :: PARAM_ISMULTI => true,
290 ApiBase :: PARAM_TYPE => $this->mMetaModuleNames
292 'generator' => array (
293 ApiBase :: PARAM_TYPE => $this->mAllowedGenerators
295 'redirects' => false
300 * Override the parent to generate help messages for all available query modules.
302 public function makeHelpMsg() {
304 // Use parent to make default message for the query module
305 $msg = parent :: makeHelpMsg();
307 // Make sure the internal object is empty
308 // (just in case a sub-module decides to optimize during instantiation)
309 $this->mPageSet = null;
311 $astriks = str_repeat('--- ', 8);
312 $msg .= "\n$astriks Query: Prop $astriks\n\n";
313 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules, 'prop');
314 $msg .= "\n$astriks Query: List $astriks\n\n";
315 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules, 'list');
316 $msg .= "\n$astriks Query: Meta $astriks\n\n";
317 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules, 'meta');
319 return $msg;
322 private function makeHelpMsgHelper($moduleList, $paramName) {
324 $moduleDscriptions = array ();
326 foreach ($moduleList as $moduleName => $moduleClass) {
327 $msg = "* $paramName=$moduleName *";
328 $module = new $moduleClass ($this, $moduleName, null);
329 $msg2 = $module->makeHelpMsg();
330 if ($msg2 !== false)
331 $msg .= $msg2;
332 if ($module instanceof ApiQueryGeneratorBase)
333 $msg .= "Generator:\n This module may be used as a generator\n";
334 $moduleDscriptions[] = $msg;
337 return implode("\n", $moduleDscriptions);
341 * Override to add extra parameters from PageSet
343 public function makeHelpMsgParameters() {
344 $psModule = new ApiPageSet($this);
345 return $psModule->makeHelpMsgParameters() . parent :: makeHelpMsgParameters();
348 protected function getParamDescription() {
349 return array (
350 'prop' => 'Which properties to get for the titles/revisions/pageids',
351 'list' => 'Which lists to get',
352 'meta' => 'Which meta data to get about the site',
353 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
354 'redirects' => 'Automatically resolve redirects'
358 protected function getDescription() {
359 return array (
360 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
361 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
362 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
366 protected function getExamples() {
367 return array (
368 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
372 public function getVersion() {
373 $psModule = new ApiPageSet($this);
374 $vers = array ();
375 $vers[] = __CLASS__ . ': $Id$';
376 $vers[] = $psModule->getVersion();
377 return $vers;