83949913a841ef60f1c7a20f8a504c6928284ad2
5 * Created on Sep 4, 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 ApiMain
extends ApiBase
{
34 private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
35 private $mApiStartTime, $mResult, $mShowVersions, $mEnableWrite;
39 * $apiStartTime - time of the originating call for profiling purposes
40 * $modules - an array of actions (keys) and classes that handle them (values)
42 public function __construct($apiStartTime, $modules, $formats, $enableWrite) {
43 // Special handling for the main module: $parent === $this
44 parent
:: __construct($this, 'main');
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;
55 // Initialize Error handler
56 set_exception_handler( array($this, 'exceptionHandler') );
59 public function & getResult() {
60 return $this->mResult
;
63 public function getShowVersions() {
64 return $this->mShowVersions
;
67 public function requestWriteMode() {
68 if (!$this->mEnableWrite
)
69 $this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
70 'statement is included in the site\'s LocalSettings.php file', 'readonly');
73 protected function getAllowedParams() {
76 ApiBase
:: PARAM_DFLT
=> API_DEFAULT_FORMAT
,
77 ApiBase
:: PARAM_TYPE
=> $this->mFormatNames
80 ApiBase
:: PARAM_DFLT
=> 'help',
81 ApiBase
:: PARAM_TYPE
=> $this->mModuleNames
87 protected function getParamDescription() {
89 'format' => 'The format of the output',
90 'action' => 'What action you would like to perform',
91 'version' => 'When showing help, include version for each module'
95 public function execute() {
97 $action = $format = $version = null;
99 extract($this->extractRequestParams());
100 $this->mShowVersions
= $version;
102 // Create an appropriate printer
103 $this->mPrinter
= new $this->mFormats
[$format] ($this, $format);
105 // Instantiate and execute module requested by the user
106 $module = new $this->mModules
[$action] ($this, $action);
107 $module->profileIn();
109 $module->profileOut();
110 $this->printResult(false);
112 } catch (UsageException
$e) {
121 private function printResult($isError) {
122 $printer = $this->mPrinter
;
123 $printer->profileIn();
124 $printer->initPrinter($isError);
125 if (!$printer->getNeedsRawData())
126 $this->getResult()->SanitizeData();
128 $printer->closePrinter();
129 $printer->profileOut();
132 private function printError() {
133 // Printer may not be initialized if the extractRequestParams() fails for the main module
134 if (!isset ($this->mPrinter
))
135 $this->mPrinter
= new $this->mFormats
[API_DEFAULT_FORMAT
] ($this, API_DEFAULT_FORMAT
);
136 $this->printResult(true);
139 protected function getDescription() {
142 'This API allows programs to access various functions of MediaWiki software.',
143 'For more details see API Home Page @ http://meta.wikimedia.org/wiki/API',
148 public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
149 if ($httpRespCode === 0)
150 header($errorCode, true);
152 header($errorCode, true, $httpRespCode);
154 $this->makeErrorMessage($description, $errorCode);
156 throw new UsageException($description, $errorCode);
159 public function makeErrorMessage($description, $errorCode, $customContent = null) {
160 $this->mResult
->Reset();
162 'code' => $errorCode,
163 'info' => $description
166 ApiResult
:: setContent($data,
167 is_null($customContent) ?
$this->makeHelpMsg() : $customContent);
169 $this->mResult
->addValue(null, 'error', $data);
173 * Override the parent to generate help messages for all available modules.
175 public function makeHelpMsg() {
177 // Use parent to make default message for the main module
178 $msg = parent
:: makeHelpMsg();
180 $astriks = str_repeat('*** ', 10);
181 $msg .= "\n\n$astriks Modules $astriks\n\n";
182 foreach ($this->mModules
as $moduleName => $moduleClass) {
183 $msg .= "* action=$moduleName *";
184 $module = new $this->mModules
[$moduleName] ($this, $moduleName);
185 $msg2 = $module->makeHelpMsg();
191 $msg .= "\n$astriks Formats $astriks\n\n";
192 foreach ($this->mFormats
as $moduleName => $moduleClass) {
193 $msg .= "* format=$moduleName *";
194 $module = new $this->mFormats
[$moduleName] ($this, $moduleName);
195 $msg2 = $module->makeHelpMsg();
205 * Exception handler which simulates the appropriate catch() handling:
209 * } catch ( MWException $e ) {
211 * } catch ( Exception $e ) {
212 * echo $e->__toString();
218 * !!!!!!!!!!!!! REVIEW needed !!!!!!!!!!!!!!!!!!
220 * this method needs to be reviewed/cleaned up
225 public function exceptionHandler( $e ) {
226 global $wgFullyInitialised;
227 if ( is_a( $e, 'MWException' ) ) {
229 $msg = "Exception Caught: {$e->getMessage()}";
230 $this->makeErrorMessage($msg, 'internal_error', "\n\n{$e->getTraceAsString()}\n\n");
232 } catch (Exception
$e2) {
233 echo $e->__toString();
236 echo $e->__toString();
239 // Final cleanup, similar to wfErrorExit()
240 if ( $wgFullyInitialised ) {
242 wfLogProfilingData(); // uses $wgRequest, hence the $wgFullyInitialised condition
243 } catch ( Exception
$e ) {}
246 // Exit value should be nonzero for the benefit of shell jobs
251 private $mIsBot = null;
252 public function isBot() {
253 if (!isset ($this->mIsBot
)) {
255 $this->mIsBot
= $wgUser->isAllowed('bot');
257 return $this->mIsBot
;
260 public function getVersion() {
262 $vers[] = __CLASS__
. ': $Id$';
263 $vers[] = ApiBase
:: getBaseVersion();
264 $vers[] = ApiFormatBase
:: getBaseVersion();
265 $vers[] = ApiQueryBase
:: getBaseVersion();
271 * @desc This exception will be thrown when dieUsage is called to stop module execution.
273 class UsageException
extends Exception
{
277 public function __construct($message, $codestr) {
278 parent
:: __construct($message);
279 $this->codestr
= $codestr;
281 public function __toString() {
282 return "{$this->codestr}: {$this->message}";