* API: Optimized PageSet object to avoid executing queries against page table twice.
[lhc/web/wiklou.git] / includes / api / ApiMain.php
1 <?php
2
3
4 /*
5 * Created on Sep 4, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiBase.php');
30 }
31
32 class ApiMain extends ApiBase {
33
34 private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
35 private $mApiStartTime, $mResult, $mShowVersions, $mEnableWrite;
36
37 /**
38 * Constructor
39 * $apiStartTime - time of the originating call for profiling purposes
40 * $modules - an array of actions (keys) and classes that handle them (values)
41 */
42 public function __construct($apiStartTime, $modules, $formats, $enableWrite) {
43 // Special handling for the main module: $parent === $this
44 parent :: __construct($this, 'main');
45
46 $this->mModules = $modules;
47 $this->mModuleNames = array_keys($modules);
48 $this->mFormats = $formats;
49 $this->mFormatNames = array_keys($formats);
50 $this->mApiStartTime = $apiStartTime;
51 $this->mResult = new ApiResult($this);
52 $this->mShowVersions = false;
53 $this->mEnableWrite = $enableWrite;
54 }
55
56 public function & getResult() {
57 return $this->mResult;
58 }
59
60 public function getShowVersions() {
61 return $this->mShowVersions;
62 }
63
64 public function requestWriteMode() {
65 if (!$this->mEnableWrite)
66 $this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
67 'statement is included in the site\'s LocalSettings.php file', 'readonly');
68 }
69
70 protected function getAllowedParams() {
71 return array (
72 'format' => array (
73 ApiBase :: PARAM_DFLT => API_DEFAULT_FORMAT,
74 ApiBase :: PARAM_TYPE => $this->mFormatNames
75 ),
76 'action' => array (
77 ApiBase :: PARAM_DFLT => 'help',
78 ApiBase :: PARAM_TYPE => $this->mModuleNames
79 ),
80 'version' => false
81 );
82 }
83
84 protected function getParamDescription() {
85 return array (
86 'format' => 'The format of the output',
87 'action' => 'What action you would like to perform',
88 'version' => 'When showing help, include version for each module'
89 );
90 }
91
92 public function execute() {
93 $this->profileIn();
94 $action = $format = $version = null;
95 try {
96 extract($this->extractRequestParams());
97 $this->mShowVersions = $version;
98
99 // Create an appropriate printer
100 $this->mPrinter = new $this->mFormats[$format] ($this, $format);
101
102 // Instantiate and execute module requested by the user
103 $module = new $this->mModules[$action] ($this, $action);
104 $module->profileIn();
105 $module->execute();
106 $module->profileOut();
107 $this->printResult(false);
108
109 } catch (UsageException $e) {
110
111 // Printer may not be initialized if the extractRequestParams() fails for the main module
112 if (!isset ($this->mPrinter))
113 $this->mPrinter = new $this->mFormats[API_DEFAULT_FORMAT] ($this, API_DEFAULT_FORMAT);
114 $this->printResult(true);
115
116 }
117 $this->profileOut();
118 }
119
120 /**
121 * Internal printer
122 */
123 private function printResult($isError) {
124 $printer = $this->mPrinter;
125 $printer->profileIn();
126 $printer->initPrinter($isError);
127 if (!$printer->getNeedsRawData())
128 $this->getResult()->SanitizeData();
129 $printer->execute();
130 $printer->closePrinter();
131 $printer->profileOut();
132 }
133
134 protected function getDescription() {
135 return array (
136 '',
137 'This API allows programs to access various functions of MediaWiki software.',
138 'For more details see API Home Page @ http://meta.wikimedia.org/wiki/API',
139 ''
140 );
141 }
142
143 public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
144 $this->mResult->Reset();
145 if ($httpRespCode === 0)
146 header($errorCode, true);
147 else
148 header($errorCode, true, $httpRespCode);
149
150 $data = array (
151 'code' => $errorCode,
152 'info' => $description
153 );
154 ApiResult :: setContent($data, $this->makeHelpMsg());
155 $this->mResult->addValue(null, 'error', $data);
156
157 throw new UsageException($description, $errorCode);
158 }
159
160 /**
161 * Override the parent to generate help messages for all available modules.
162 */
163 public function makeHelpMsg() {
164
165 // Use parent to make default message for the main module
166 $msg = parent :: makeHelpMsg();
167
168 $astriks = str_repeat('*** ', 10);
169 $msg .= "\n\n$astriks Modules $astriks\n\n";
170 foreach ($this->mModules as $moduleName => $moduleClass) {
171 $msg .= "* action=$moduleName *";
172 $module = new $this->mModules[$moduleName] ($this, $moduleName);
173 $msg2 = $module->makeHelpMsg();
174 if ($msg2 !== false)
175 $msg .= $msg2;
176 $msg .= "\n";
177 }
178
179 $msg .= "\n$astriks Formats $astriks\n\n";
180 foreach ($this->mFormats as $moduleName => $moduleClass) {
181 $msg .= "* format=$moduleName *";
182 $module = new $this->mFormats[$moduleName] ($this, $moduleName);
183 $msg2 = $module->makeHelpMsg();
184 if ($msg2 !== false)
185 $msg .= $msg2;
186 $msg .= "\n";
187 }
188
189 return $msg;
190 }
191
192 private $mIsBot = null;
193 public function isBot() {
194 if (!isset ($this->mIsBot)) {
195 global $wgUser;
196 $this->mIsBot = $wgUser->isAllowed('bot');
197 }
198 return $this->mIsBot;
199 }
200
201 public function getVersion() {
202 $vers = array ();
203 $vers[] = __CLASS__ . ': $Id$';
204 $vers[] = ApiBase :: getBaseVersion();
205 $vers[] = ApiFormatBase :: getBaseVersion();
206 $vers[] = ApiQueryBase :: getBaseVersion();
207 return $vers;
208 }
209 }
210
211 /**
212 * @desc This exception will be thrown when dieUsage is called to stop module execution.
213 */
214 class UsageException extends Exception {
215
216 private $codestr;
217
218 public function __construct($message, $codestr) {
219 parent :: __construct($message);
220 $this->codestr = $codestr;
221 }
222 public function __toString() {
223 return "{$this->codestr}: {$this->message}";
224 }
225 }
226 ?>