*API: rewired generator (more work needed)
[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 );
153 ApiResult :: setContent($data, $this->makeHelpMsg());
154 $this->mResult->addValue(null, 'error', $data);
155
156 throw new UsageException($description, $errorCode);
157 }
158
159 /**
160 * Override the parent to generate help messages for all available modules.
161 */
162 public function makeHelpMsg() {
163
164 // Use parent to make default message for the main module
165 $msg = parent :: makeHelpMsg();
166
167 $astriks = str_repeat('*** ', 10);
168 $msg .= "\n\n$astriks Modules $astriks\n\n";
169 foreach ($this->mModules as $moduleName => $moduleClass) {
170 $msg .= "* action=$moduleName *";
171 $module = new $this->mModules[$moduleName] ($this, $moduleName);
172 $msg2 = $module->makeHelpMsg();
173 if ($msg2 !== false)
174 $msg .= $msg2;
175 $msg .= "\n";
176 }
177
178 $msg .= "\n$astriks Formats $astriks\n\n";
179 foreach ($this->mFormats as $moduleName => $moduleClass) {
180 $msg .= "* format=$moduleName *";
181 $module = new $this->mFormats[$moduleName] ($this, $moduleName);
182 $msg2 = $module->makeHelpMsg();
183 if ($msg2 !== false)
184 $msg .= $msg2;
185 $msg .= "\n";
186 }
187
188 return $msg;
189 }
190
191 private $mIsBot = null;
192 public function isBot() {
193 if (!isset ($this->mIsBot)) {
194 global $wgUser;
195 $this->mIsBot = $wgUser->isAllowed('bot');
196 }
197 return $this->mIsBot;
198 }
199
200 public function getVersion() {
201 $vers = array ();
202 $vers[] = __CLASS__ . ': $Id$';
203 $vers[] = ApiBase :: getBaseVersion();
204 $vers[] = ApiFormatBase :: getBaseVersion();
205 $vers[] = ApiQueryBase :: getBaseVersion();
206 return $vers;
207 }
208 }
209
210 /**
211 * @desc This exception will be thrown when dieUsage is called to stop module execution.
212 */
213 class UsageException extends Exception {
214
215 private $codestr;
216
217 public function __construct($message, $codestr) {
218 parent :: __construct($message);
219 $this->codestr = $codestr;
220 }
221 public function __toString() {
222 return "{$this->codestr}: {$this->message}";
223 }
224 }
225 ?>