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