45848b27252ab021e7b8de9a38f52128d5a42a57
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'
51 // 'backlinks' => 'ApiQueryBacklinks',
52 // 'categorymembers' => 'ApiQueryCategorymembers',
53 // 'embeddedin' => 'ApiQueryEmbeddedin',
54 // 'imagelinks' => 'ApiQueryImagelinks',
55 // 'logevents' => 'ApiQueryLogevents',
56 // 'recentchanges' => 'ApiQueryRecentchanges',
57 // 'usercontribs' => 'ApiQueryUsercontribs',
58 // 'users' => 'ApiQueryUsers',
59 // 'watchlist' => 'ApiQueryWatchlist',
61 private $mQueryMetaModules = array (
62 'siteinfo' => 'ApiQuerySiteinfo'
64 // 'userinfo' => 'ApiQueryUserinfo',
66 private $mSlaveDB = null;
68 public function __construct($main, $action) {
69 parent
:: __construct($main, $action);
70 $this->mPropModuleNames
= array_keys($this->mQueryPropModules
);
71 $this->mListModuleNames
= array_keys($this->mQueryListModules
);
72 $this->mMetaModuleNames
= array_keys($this->mQueryMetaModules
);
73 $this->mValidNamespaces
= null;
75 // Allow the entire list of modules at first,
76 // but during module instantiation check if it can be used as a generator.
77 $this->mAllowedGenerators
= array_merge($this->mListModuleNames
, $this->mPropModuleNames
);
80 public function getDB() {
81 if (!isset ($this->mSlaveDB
))
82 $this->mSlaveDB
= & wfGetDB(DB_SLAVE
);
83 return $this->mSlaveDB
;
86 public function getPageSet() {
87 return $this->mPageSet
;
90 public function getValidNamespaces() {
93 if (is_null($this->mValidNamespaces
)) {
94 $this->mValidNamespaces
= array ();
95 foreach (array_keys($wgContLang->getNamespaces()) as $ns) {
97 $this->mValidNamespaces
[] = $ns; // strval($ns);
100 return $this->mValidNamespaces
;
104 * Query execution happens in the following steps:
105 * #1 Create a PageSet object with any pages requested by the user
106 * #2 If using generator, execute it to get a new PageSet object
107 * #3 Instantiate all requested modules.
108 * This way the PageSet object will know what shared data is required,
109 * and minimize DB calls.
110 * #4 Output all normalization and redirect resolution information
111 * #5 Execute all requested modules
113 public function execute() {
114 $prop = $list = $meta = $generator = $redirects = null;
115 extract($this->extractRequestParams());
120 $this->mPageSet
= new ApiPageSet($this, $redirects);
122 // Instantiate required modules
125 foreach ($prop as $moduleName)
126 $modules[] = new $this->mQueryPropModules
[$moduleName] ($this, $moduleName);
128 foreach ($list as $moduleName)
129 $modules[] = new $this->mQueryListModules
[$moduleName] ($this, $moduleName);
131 foreach ($meta as $moduleName)
132 $modules[] = new $this->mQueryMetaModules
[$moduleName] ($this, $moduleName);
134 // Modules may optimize data requests through the $this->getPageSet() object
135 // Execute all requested modules.
136 foreach ($modules as $module) {
137 $module->requestExtraData();
141 // If given, execute generator to substitute user supplied data with generated data.
143 if (isset ($generator))
144 $this->executeGeneratorModule($generator, $redirects);
147 // Populate page information for the given pageSet
149 $this->mPageSet
->execute();
152 // Record page information (title, namespace, if exists, etc)
154 $this->outputGeneralPageInfo();
157 // Execute all requested modules.
159 foreach ($modules as $module) {
160 $module->profileIn();
162 $module->profileOut();
166 private function outputGeneralPageInfo() {
168 $pageSet = $this->getPageSet();
170 // Title normalizations
171 $normValues = array ();
172 foreach ($pageSet->getNormalizedTitles() as $rawTitleStr => $titleStr) {
173 $normValues[] = array (
174 'from' => $rawTitleStr,
179 if (!empty ($normValues)) {
180 ApiResult
:: setIndexedTagName($normValues, 'n');
181 $this->getResult()->addValue('query', 'normalized', $normValues);
184 // Show redirect information
185 $redirValues = array ();
186 foreach ($pageSet->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
187 $redirValues[] = array (
188 'from' => $titleStrFrom,
193 if (!empty ($redirValues)) {
194 ApiResult
:: setIndexedTagName($redirValues, 'r');
195 $this->getResult()->addValue('query', 'redirects', $redirValues);
200 // Missing revision elements
202 $missingRevIDs = $pageSet->getMissingRevisionIDs();
203 if (!empty($missingRevIDs)) {
205 foreach ($missingRevIDs as $revid) {
206 $revids[$revid] = array (
210 ApiResult
:: setIndexedTagName($revids, 'rev');
211 $this->getResult()->addValue('query', 'badrevids', $revids);
219 // Report any missing titles
221 foreach ($pageSet->getMissingTitles() as $title) {
222 $pages[$fakepageid--] = array (
223 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'missing' => '');
226 // Report any missing page ids
227 foreach ($pageSet->getMissingPageIDs() as $pageid) {
228 $pages[$pageid] = array (
234 // Output general page information for found titles
235 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
236 $pages[$pageid] = array (
237 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'id' => $pageid);
240 if (!empty ($pages)) {
241 ApiResult
:: setIndexedTagName($pages, 'page');
242 $this->getResult()->addValue('query', 'pages', $pages);
246 protected function executeGeneratorModule($generatorName, $redirects) {
248 // Find class that implements requested generator
249 if (isset ($this->mQueryListModules
[$generatorName])) {
250 $className = $this->mQueryListModules
[$generatorName];
252 elseif (isset ($this->mQueryPropModules
[$generatorName])) {
253 $className = $this->mQueryPropModules
[$generatorName];
255 ApiBase
:: dieDebug(__METHOD__
, "Unknown generator=$generatorName");
258 // Use current pageset as the result, and create a new one just for the generator
259 $resultPageSet = $this->mPageSet
;
260 $this->mPageSet
= new ApiPageSet($this, $redirects);
262 // Create and execute the generator
263 $generator = new $className ($this, $generatorName);
264 if (!$generator instanceof ApiQueryGeneratorBase
)
265 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
267 $generator->setGeneratorMode();
268 $generator->requestExtraData();
270 // execute current pageSet to get the data for the generator module
271 $this->mPageSet
->execute();
273 // populate resultPageSet with the generator output
274 $generator->profileIn();
275 $generator->executeGenerator($resultPageSet);
276 $resultPageSet->finishPageSetGeneration();
277 $generator->profileOut();
279 // Swap the resulting pageset back in
280 $this->mPageSet
= $resultPageSet;
283 protected function getAllowedParams() {
286 ApiBase
:: PARAM_ISMULTI
=> true,
287 ApiBase
:: PARAM_TYPE
=> $this->mPropModuleNames
290 ApiBase
:: PARAM_ISMULTI
=> true,
291 ApiBase
:: PARAM_TYPE
=> $this->mListModuleNames
294 ApiBase
:: PARAM_ISMULTI
=> true,
295 ApiBase
:: PARAM_TYPE
=> $this->mMetaModuleNames
297 'generator' => array (
298 ApiBase
:: PARAM_TYPE
=> $this->mAllowedGenerators
305 * Override the parent to generate help messages for all available query modules.
307 public function makeHelpMsg() {
309 // Use parent to make default message for the query module
310 $msg = parent
:: makeHelpMsg();
312 // Make sure the internal object is empty
313 // (just in case a sub-module decides to optimize during instantiation)
314 $this->mPageSet
= null;
316 $astriks = str_repeat('--- ', 8);
317 $msg .= "\n$astriks Query: Prop $astriks\n\n";
318 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules
, 'prop');
319 $msg .= "\n$astriks Query: List $astriks\n\n";
320 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules
, 'list');
321 $msg .= "\n$astriks Query: Meta $astriks\n\n";
322 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules
, 'meta');
327 private function makeHelpMsgHelper($moduleList, $paramName) {
329 $moduleDscriptions = array ();
331 foreach ($moduleList as $moduleName => $moduleClass) {
332 $msg = "* $paramName=$moduleName *";
333 $module = new $moduleClass ($this, $moduleName, null);
334 $msg2 = $module->makeHelpMsg();
337 if ($module instanceof ApiQueryGeneratorBase
)
338 $msg .= "Generator:\n This module may be used as a generator\n";
339 $moduleDscriptions[] = $msg;
342 return implode("\n", $moduleDscriptions);
346 * Override to add extra parameters from PageSet
348 public function makeHelpMsgParameters() {
349 $psModule = new ApiPageSet($this);
350 return $psModule->makeHelpMsgParameters() . parent
:: makeHelpMsgParameters();
353 protected function getParamDescription() {
355 'prop' => 'Which properties to get for the titles/revisions/pageids',
356 'list' => 'Which lists to get',
357 'meta' => 'Which meta data to get about the site',
358 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
359 'redirects' => 'Automatically resolve redirects'
363 protected function getDescription() {
365 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
366 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
367 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
371 protected function getExamples() {
373 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
377 public function getVersion() {
378 $psModule = new ApiPageSet($this);
380 $vers[] = __CLASS__
. ': $Id$';
381 $vers[] = $psModule->getVersion();