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);
203 // Report any missing titles
205 foreach ($pageSet->getMissingTitles() as $title) {
206 $pages[$fakepageid--] = array (
207 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'missing' => '');
210 // Report any missing page ids
211 foreach ($pageSet->getMissingPageIDs() as $pageid) {
212 $pages[$pageid] = array (
218 // Output general page information for found titles
219 foreach ($pageSet->getGoodTitles() as $pageid => $title) {
220 $pages[$pageid] = array (
221 'ns' => $title->getNamespace(), 'title' => $title->getPrefixedText(), 'id' => $pageid);
224 if (!empty ($pages)) {
225 ApiResult
:: setIndexedTagName($pages, 'page');
226 $this->getResult()->addValue('query', 'pages', $pages);
230 protected function executeGeneratorModule($generatorName, $redirects) {
232 // Find class that implements requested generator
233 if (isset ($this->mQueryListModules
[$generatorName])) {
234 $className = $this->mQueryListModules
[$generatorName];
236 elseif (isset ($this->mQueryPropModules
[$generatorName])) {
237 $className = $this->mQueryPropModules
[$generatorName];
239 ApiBase
:: dieDebug(__METHOD__
, "Unknown generator=$generatorName");
242 // Use current pageset as the result, and create a new one just for the generator
243 $resultPageSet = $this->mPageSet
;
244 $this->mPageSet
= new ApiPageSet($this, $redirects);
246 // Create and execute the generator
247 $generator = new $className ($this, $generatorName);
248 if (!$generator instanceof ApiQueryGeneratorBase
)
249 $this->dieUsage("Module $generatorName cannot be used as a generator", "badgenerator");
251 $generator->setGeneratorMode();
252 $generator->requestExtraData();
254 // execute current pageSet to get the data for the generator module
255 $this->mPageSet
->execute();
257 // populate resultPageSet with the generator output
258 $generator->profileIn();
259 $generator->executeGenerator($resultPageSet);
260 $resultPageSet->finishPageSetGeneration();
261 $generator->profileOut();
263 // Swap the resulting pageset back in
264 $this->mPageSet
= $resultPageSet;
267 protected function getAllowedParams() {
270 ApiBase
:: PARAM_ISMULTI
=> true,
271 ApiBase
:: PARAM_TYPE
=> $this->mPropModuleNames
274 ApiBase
:: PARAM_ISMULTI
=> true,
275 ApiBase
:: PARAM_TYPE
=> $this->mListModuleNames
278 ApiBase
:: PARAM_ISMULTI
=> true,
279 ApiBase
:: PARAM_TYPE
=> $this->mMetaModuleNames
281 'generator' => array (
282 ApiBase
:: PARAM_TYPE
=> $this->mAllowedGenerators
289 * Override the parent to generate help messages for all available query modules.
291 public function makeHelpMsg() {
293 // Use parent to make default message for the query module
294 $msg = parent
:: makeHelpMsg();
296 // Make sure the internal object is empty
297 // (just in case a sub-module decides to optimize during instantiation)
298 $this->mPageSet
= null;
300 $astriks = str_repeat('--- ', 8);
301 $msg .= "\n$astriks Query: Prop $astriks\n\n";
302 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules
, 'prop');
303 $msg .= "\n$astriks Query: List $astriks\n\n";
304 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules
, 'list');
305 $msg .= "\n$astriks Query: Meta $astriks\n\n";
306 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules
, 'meta');
311 private function makeHelpMsgHelper($moduleList, $paramName) {
313 $moduleDscriptions = array ();
315 foreach ($moduleList as $moduleName => $moduleClass) {
316 $msg = "* $paramName=$moduleName *";
317 $module = new $moduleClass ($this, $moduleName, null);
318 $msg2 = $module->makeHelpMsg();
321 if ($module instanceof ApiQueryGeneratorBase
)
322 $msg .= "Generator:\n This module may be used as a generator\n";
323 $moduleDscriptions[] = $msg;
326 return implode("\n", $moduleDscriptions);
330 * Override to add extra parameters from PageSet
332 public function makeHelpMsgParameters() {
333 $psModule = new ApiPageSet($this);
334 return $psModule->makeHelpMsgParameters() . parent
:: makeHelpMsgParameters();
337 protected function getParamDescription() {
339 'prop' => 'Which properties to get for the titles/revisions/pageids',
340 'list' => 'Which lists to get',
341 'meta' => 'Which meta data to get about the site',
342 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
343 'redirects' => 'Automatically resolve redirects'
347 protected function getDescription() {
349 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
350 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
351 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
355 protected function getExamples() {
357 'api.php?action=query&prop=revisions&meta=siteinfo&titles=Main%20Page&rvprop=user|comment'
361 public function getVersion() {
362 $psModule = new ApiPageSet($this);
364 $vers[] = __CLASS__
. ': $Id$';
365 $vers[] = $psModule->getVersion();