5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
32 class ApiQuery
extends ApiBase
{
34 private $mPropModuleNames, $mListModuleNames, $mMetaModuleNames;
36 private $mValidNamespaces;
38 private $mQueryPropModules = array (
39 'info' => 'ApiQueryInfo',
40 'revisions' => 'ApiQueryRevisions'
42 // 'categories' => 'ApiQueryCategories',
43 // 'imageinfo' => 'ApiQueryImageinfo',
44 // 'langlinks' => 'ApiQueryLanglinks',
45 // 'links' => 'ApiQueryLinks',
46 // 'templates' => 'ApiQueryTemplates',
48 private $mQueryListModules = array (
49 'allpages' => 'ApiQueryAllpages',
50 'watchlist' => 'ApiQueryWatchlist'
52 // 'backlinks' => 'ApiQueryBacklinks',
53 // 'categorymembers' => 'ApiQueryCategorymembers',
54 // 'embeddedin' => 'ApiQueryEmbeddedin',
55 // 'imagelinks' => 'ApiQueryImagelinks',
56 // 'logevents' => 'ApiQueryLogevents',
57 // 'recentchanges' => 'ApiQueryRecentchanges',
58 // 'usercontribs' => 'ApiQueryUsercontribs',
59 // 'users' => 'ApiQueryUsers',
60 // 'watchlist' => 'ApiQueryWatchlist',
62 private $mQueryMetaModules = array (
63 'siteinfo' => 'ApiQuerySiteinfo'
65 // 'userinfo' => 'ApiQueryUserinfo',
67 private $mSlaveDB = null;
69 public function __construct($main, $action) {
70 parent
:: __construct($main, $action);
71 $this->mPropModuleNames
= array_keys($this->mQueryPropModules
);
72 $this->mListModuleNames
= array_keys($this->mQueryListModules
);
73 $this->mMetaModuleNames
= array_keys($this->mQueryMetaModules
);
74 $this->mValidNamespaces
= null;
76 // Allow the entire list of modules at first,
77 // but during module instantiation check if it can be used as a generator.
78 $this->mAllowedGenerators
= array_merge($this->mListModuleNames
, $this->mPropModuleNames
);
81 public function getDB() {
82 if (!isset ($this->mSlaveDB
))
83 $this->mSlaveDB
= & wfGetDB(DB_SLAVE
);
84 return $this->mSlaveDB
;
87 public function getPageSet() {
88 return $this->mPageSet
;
91 public function getValidNamespaces() {
94 if (is_null($this->mValidNamespaces
)) {
95 $this->mValidNamespaces
= array ();
96 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
98 $this->mValidNamespaces
[] = $ns; // strval($ns);
101 return $this->mValidNamespaces
;
105 * Query execution happens in the following steps:
106 * #1 Create a PageSet object with any pages requested by the user
107 * #2 If using generator, execute it to get a new PageSet object
108 * #3 Instantiate all requested modules.
109 * This way the PageSet object will know what shared data is required,
110 * and minimize DB calls.
111 * #4 Output all normalization and redirect resolution information
112 * #5 Execute all requested modules
114 public function execute() {
115 $prop = $list = $meta = $generator = $redirects = null;
116 extract($this->extractRequestParams());
121 $this->mPageSet
= new ApiPageSet($this, $redirects);
123 // Instantiate required modules
126 foreach ($prop as $moduleName)
127 $modules[] = new $this->mQueryPropModules
[$moduleName] ($this, $moduleName);
129 foreach ($list as $moduleName)
130 $modules[] = new $this->mQueryListModules
[$moduleName] ($this, $moduleName);
132 foreach ($meta as $moduleName)
133 $modules[] = new $this->mQueryMetaModules
[$moduleName] ($this, $moduleName);
135 // Modules may optimize data requests through the $this->getPageSet() object
136 // Execute all requested modules.
137 foreach ($modules as $module) {
138 $module->requestExtraData();
142 // If given, execute generator to substitute user supplied data with generated data.
144 if (isset ($generator))
145 $this->executeGeneratorModule($generator, $redirects);
148 // Populate page information for the given pageSet
150 $this->mPageSet
->execute();
153 // Record page information (title, namespace, if exists, etc)
155 $this->outputGeneralPageInfo();
158 // Execute all requested modules.
160 foreach ($modules as $module) {
161 $module->profileIn();
163 $module->profileOut();
167 private function outputGeneralPageInfo() {
169 $pageSet = $this->getPageSet();
171 // Title normalizations
172 $normValues = array ();
173 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
174 $normValues[] = array (
175 'from' => $rawTitleStr,
180 if (!empty ($normValues)) {
181 ApiResult
:: setIndexedTagName($normValues, 'n');
182 $this->getResult()->addValue('query', 'normalized', $normValues);
185 // Show redirect information
186 $redirValues = array ();
187 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
188 $redirValues[] = array (
189 'from' => $titleStrFrom,
194 if (!empty ($redirValues)) {
195 ApiResult
:: setIndexedTagName($redirValues, 'r');
196 $this->getResult()->addValue('query', 'redirects', $redirValues);
201 // Missing revision elements
203 $missingRevIDs = $pageSet->getMissingRevisionIDs();
204 if (!empty($missingRevIDs)) {
206 foreach ($missingRevIDs as $revid) {
207 $revids[$revid] = array (
211 ApiResult
:: setIndexedTagName($revids, 'rev');
212 $this->getResult()->addValue('query', 'badrevids', $revids);
220 // Report any missing titles
222 foreach ($pageSet->getMissingTitles() as $title) {
223 $pages[$fakepageid--] = array (
224 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'missing' => '');
227 // Report any missing page ids
228 foreach ($pageSet->getMissingPageIDs() as $pageid) {
229 $pages[$pageid] = array (
235 // Output general page information for found titles
236 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
237 $pages[$pageid] = array (
238 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'id' => $pageid);
241 if (!empty ($pages)) {
242 ApiResult
:: setIndexedTagName($pages, 'page');
243 $this->getResult()->addValue('query', 'pages', $pages);
247 protected function executeGeneratorModule($generatorName, $redirects) {
249 // Find class that implements requested generator
250 if (isset ($this->mQueryListModules
[$generatorName])) {
251 $className = $this->mQueryListModules
[$generatorName];
253 elseif (isset ($this->mQueryPropModules
[$generatorName])) {
254 $className = $this->mQueryPropModules
[$generatorName];
256 ApiBase
:: dieDebug(__METHOD__
, "Unknown generator=$generatorName");
259 // Use current pageset as the result, and create a new one just for the generator
260 $resultPageSet = $this->mPageSet
;
261 $this->mPageSet
= new ApiPageSet($this, $redirects);
263 // Create and execute the generator
264 $generator = new $className ($this, $generatorName);
265 if (!$generator instanceof ApiQueryGeneratorBase
)
266 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
268 $generator->setGeneratorMode();
269 $generator->requestExtraData();
271 // execute current pageSet to get the data for the generator module
272 $this->mPageSet
->execute();
274 // populate resultPageSet with the generator output
275 $generator->profileIn();
276 $generator->executeGenerator($resultPageSet);
277 $resultPageSet->finishPageSetGeneration();
278 $generator->profileOut();
280 // Swap the resulting pageset back in
281 $this->mPageSet
= $resultPageSet;
284 protected function getAllowedParams() {
287 ApiBase
:: PARAM_ISMULTI
=> true,
288 ApiBase
:: PARAM_TYPE
=> $this->mPropModuleNames
291 ApiBase
:: PARAM_ISMULTI
=> true,
292 ApiBase
:: PARAM_TYPE
=> $this->mListModuleNames
295 ApiBase
:: PARAM_ISMULTI
=> true,
296 ApiBase
:: PARAM_TYPE
=> $this->mMetaModuleNames
298 'generator' => array (
299 ApiBase
:: PARAM_TYPE
=> $this->mAllowedGenerators
306 * Override the parent to generate help messages for all available query modules.
308 public function makeHelpMsg() {
310 // Use parent to make default message for the query module
311 $msg = parent
:: makeHelpMsg();
313 // Make sure the internal object is empty
314 // (just in case a sub-module decides to optimize during instantiation)
315 $this->mPageSet
= null;
317 $astriks = str_repeat('--- ', 8);
318 $msg .= "\n$astriks Query: Prop $astriks\n\n";
319 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules
, 'prop');
320 $msg .= "\n$astriks Query: List $astriks\n\n";
321 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules
, 'list');
322 $msg .= "\n$astriks Query: Meta $astriks\n\n";
323 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules
, 'meta');
328 private function makeHelpMsgHelper($moduleList, $paramName) {
330 $moduleDscriptions = array ();
332 foreach ($moduleList as $moduleName => $moduleClass) {
333 $msg = "* $paramName=$moduleName *";
334 $module = new $moduleClass ($this, $moduleName, null);
335 $msg2 = $module->makeHelpMsg();
338 if ($module instanceof ApiQueryGeneratorBase
)
339 $msg .= "Generator:\n This module may be used as a generator\n";
340 $moduleDscriptions[] = $msg;
343 return implode("\n", $moduleDscriptions);
347 * Override to add extra parameters from PageSet
349 public function makeHelpMsgParameters() {
350 $psModule = new ApiPageSet($this);
351 return $psModule->makeHelpMsgParameters() . parent
:: makeHelpMsgParameters();
354 protected function getParamDescription() {
356 'prop' => 'Which properties to get for the titles/revisions/pageids',
357 'list' => 'Which lists to get',
358 'meta' => 'Which meta data to get about the site',
359 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
360 'redirects' => 'Automatically resolve redirects'
364 protected function getDescription() {
366 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
367 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
368 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
372 protected function getExamples() {
374 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
378 public function getVersion() {
379 $psModule = new ApiPageSet($this);
381 $vers[] = __CLASS__
. ': $Id$';
382 $vers[] = $psModule->getVersion();