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 $mMetaModuleNames, $mPropModuleNames, $mListModuleNames;
37 private $mQueryMetaModules = array (
38 'siteinfo' => 'ApiQuerySiteinfo'
40 // 'userinfo' => 'ApiQueryUserinfo',
42 private $mQueryPropModules = array (
43 'info' => 'ApiQueryInfo',
44 'revisions' => 'ApiQueryRevisions'
46 // 'categories' => 'ApiQueryCategories',
47 // 'imageinfo' => 'ApiQueryImageinfo',
48 // 'langlinks' => 'ApiQueryLanglinks',
49 // 'links' => 'ApiQueryLinks',
50 // 'templates' => 'ApiQueryTemplates',
52 private $mQueryListModules = array (
53 'allpages' => 'ApiQueryAllpages'
55 // 'backlinks' => 'ApiQueryBacklinks',
56 // 'categorymembers' => 'ApiQueryCategorymembers',
57 // 'embeddedin' => 'ApiQueryEmbeddedin',
58 // 'imagelinks' => 'ApiQueryImagelinks',
59 // 'logevents' => 'ApiQueryLogevents',
60 // 'recentchanges' => 'ApiQueryRecentchanges',
61 // 'usercontribs' => 'ApiQueryUsercontribs',
62 // 'users' => 'ApiQueryUsers',
63 // 'watchlist' => 'ApiQueryWatchlist',
65 private $mSlaveDB = null;
67 public function __construct($main, $action) {
68 parent
:: __construct($main);
69 $this->mMetaModuleNames
= array_keys($this->mQueryMetaModules
);
70 $this->mPropModuleNames
= array_keys($this->mQueryPropModules
);
71 $this->mListModuleNames
= array_keys($this->mQueryListModules
);
73 // Allow the entire list of modules at first,
74 // but during module instantiation check if it can be used as a generator.
75 $this->mAllowedGenerators
= array_merge($this->mListModuleNames
, $this->mPropModuleNames
);
78 public function getDB() {
79 if (!isset ($this->mSlaveDB
))
80 $this->mSlaveDB
= & wfGetDB(DB_SLAVE
);
81 return $this->mSlaveDB
;
84 public function getData() {
89 * Query execution happens in the following steps:
90 * #1 Create a PageSet object with any pages requested by the user
91 * #2 If using generator, execute it to get a new PageSet object
92 * #3 Instantiate all requested modules.
93 * This way the PageSet object will know what shared data is required,
94 * and minimize DB calls.
95 * #4 Output all normalization and redirect resolution information
96 * #5 Execute all requested modules
98 public function execute() {
99 $meta = $prop = $list = $generator = $titles = $pageids = null;
101 extract($this->extractRequestParams());
104 // Create and initialize PageSet
107 if (isset ($titles) && isset($pageids))
108 $this->dieUsage("At present you may not use titles= and pageids= at the same time", 'multisource');
110 $this->mData
= new ApiPageSet($this, $redirects);
113 $this->mData
->populateTitles($titles);
116 $this->mData
->populatePageIDs($pageids);
119 // If generator is provided, get a new dataset to work on
121 if (isset ($generator))
122 $this->executeGenerator($generator, $redirects);
124 // Instantiate required modules
125 // During instantiation, modules may optimize data requests through the $this->mData object
126 // $this->mData will be lazy loaded when modules begin to request data during execution
129 foreach ($meta as $moduleName)
130 $modules[] = new $this->mQueryMetaModules
[$moduleName] ($this, $moduleName);
132 foreach ($prop as $moduleName)
133 $modules[] = new $this->mQueryPropModules
[$moduleName] ($this, $moduleName);
135 foreach ($list as $moduleName)
136 $modules[] = new $this->mQueryListModules
[$moduleName] ($this, $moduleName);
138 // Title normalizations
139 foreach ($this->mData
->getNormalizedTitles() as $rawTitleStr => $titleStr) {
140 $this->getResult()->addMessage('query', 'normalized', array (
141 'from' => $rawTitleStr,
147 // Show redirect information
149 foreach ($this->mData
->getRedirectTitles() as $titleStrFrom => $titleStrTo) {
150 $this->getResult()->addMessage('query', 'redirects', array (
151 'from' => $titleStrFrom,
158 // Execute all requested modules.
159 foreach ($modules as $module) {
160 $module->profileIn();
162 $module->profileOut();
166 protected function executeGenerator($generator, $redirects) {
168 // Find class that implements requested generator
169 if (isset ($this->mQueryListModules
[$generator]))
170 $className = $this->mQueryListModules
[$generator];
172 if (isset ($this->mQueryPropModules
[$generator]))
173 $className = $this->mQueryPropModules
[$generator];
175 $this->dieDebug("Unknown generator=$generator");
177 $module = new $className ($this, $generator, true);
179 // change $this->mData
182 $this->dieUsage("Generator execution has not been implemented", 'notimplemented');
185 protected function getAllowedParams() {
188 GN_ENUM_ISMULTI
=> true,
189 GN_ENUM_TYPE
=> $this->mMetaModuleNames
192 GN_ENUM_ISMULTI
=> true,
193 GN_ENUM_TYPE
=> $this->mPropModuleNames
196 GN_ENUM_ISMULTI
=> true,
197 GN_ENUM_TYPE
=> $this->mListModuleNames
199 // 'generator' => array (
200 // GN_ENUM_TYPE => $this->mAllowedGenerators
203 GN_ENUM_ISMULTI
=> true
205 // 'pageids' => array (
206 // GN_ENUM_TYPE => 'integer',
207 // GN_ENUM_ISMULTI => true
214 * Override the parent to generate help messages for all available query modules.
216 public function makeHelpMsg() {
218 // Use parent to make default message for the query module
219 $msg = parent
:: makeHelpMsg();
221 // Make sure the internal object is empty
222 // (just in case a sub-module decides to optimize during instantiation)
225 $astriks = str_repeat('--- ', 8);
226 $msg .= "\n$astriks Query: Meta $astriks\n\n";
227 $msg .= $this->makeHelpMsgHelper($this->mQueryMetaModules
, 'meta');
228 $msg .= "\n$astriks Query: Prop $astriks\n\n";
229 $msg .= $this->makeHelpMsgHelper($this->mQueryPropModules
, 'prop');
230 $msg .= "\n$astriks Query: List $astriks\n\n";
231 $msg .= $this->makeHelpMsgHelper($this->mQueryListModules
, 'list');
236 private function makeHelpMsgHelper($moduleList, $paramName) {
239 foreach ($moduleList as $moduleName => $moduleClass) {
240 $msg .= "* $paramName=$moduleName *";
241 $module = new $moduleClass ($this, $moduleName, null);
242 $msg2 = $module->makeHelpMsg();
246 if ($module->getCanGenerate())
247 $msg .= " * Can be used as a generator\n";
253 protected function getParamDescription() {
255 'meta' => 'Which meta data to get about the site',
256 'prop' => 'Which properties to get for the titles/revisions/pageids',
257 'list' => 'Which lists to get',
258 'generator' => 'Use the output of a list as the input for other prop/list/meta items',
259 'titles' => 'A list of titles to work on',
260 'pageids' => 'A list of page IDs to work on',
261 'redirects' => 'Automatically resolve redirects'
265 protected function getDescription() {
267 'Query API module allows applications to get needed pieces of data from the MediaWiki databases,',
268 'and is loosely based on the Query API interface currently available on all MediaWiki servers.',
269 'All data modifications will first have to use query to acquire a token to prevent abuse from malicious sites.'
273 protected function getExamples() {
275 'api.php?action=query&what=content&titles=ArticleA|ArticleB'