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, $mApiStartTime, $mResult, $mShowVersions;
38 * $apiStartTime - time of the originating call for profiling purposes
39 * $modules - an array of actions (keys) and classes that handle them (values)
41 public function __construct($apiStartTime, $modules, $formats) {
42 // Special handling for the main module: $parent === $this
43 parent
:: __construct($this);
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;
54 public function & getResult() {
55 return $this->mResult
;
58 public function getShowVersions() {
59 return $this->mShowVersions
;
62 protected function getAllowedParams() {
65 ApiBase
:: PARAM_DFLT
=> API_DEFAULT_FORMAT
,
66 ApiBase
:: PARAM_TYPE
=> $this->mFormatNames
69 ApiBase
:: PARAM_DFLT
=> 'help',
70 ApiBase
:: PARAM_TYPE
=> $this->mModuleNames
76 protected function getParamDescription() {
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'
84 public function execute() {
86 $action = $format = $version = null;
88 extract($this->extractRequestParams());
89 $this->mShowVersions
= $version;
91 // Create an appropriate printer
92 $this->mPrinter
= new $this->mFormats
[$format] ($this, $format);
94 // Instantiate and execute module requested by the user
95 $module = new $this->mModules
[$action] ($this, $action);
98 $module->profileOut();
99 $this->printResult(false);
101 } catch (UsageException
$e) {
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);
115 private function printResult($isError) {
116 $printer = $this->mPrinter
;
117 $printer->profileIn();
118 $printer->initPrinter($isError);
119 if (!$printer->getNeedsRawData())
120 $this->getResult()->SanitizeData();
122 $printer->closePrinter();
123 $printer->profileOut();
126 protected function getDescription() {
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',
135 public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
136 $this->mResult
->Reset();
137 if ($httpRespCode === 0)
138 header($errorCode, true);
140 header($errorCode, true, $httpRespCode);
145 ApiResult
:: setContent($data, $this->makeHelpMsg());
146 $this->mResult
->addValue(null, 'error', $data);
148 throw new UsageException($description, $errorCode);
152 * Override the parent to generate help messages for all available modules.
154 public function makeHelpMsg() {
156 // Use parent to make default message for the main module
157 $msg = parent
:: makeHelpMsg();
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();
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();
183 private $mIsBot = null;
184 public function isBot() {
185 if (!isset ($this->mIsBot
)) {
187 $this->mIsBot
= $wgUser->isAllowed('bot');
189 return $this->mIsBot
;
192 public function getVersion() {
195 parent
:: getVersion(), __CLASS__
. ': $Id$', ApiFormatBase
:: getBaseVersion());
200 * @desc This exception will be thrown when dieUsage is called to stop module execution.
202 class UsageException
extends Exception
{
206 public function __construct($message, $codestr) {
207 parent
:: __construct($message);
208 $this->codestr
= $codestr;
210 public function __toString() {
211 return "{$this->codestr}: {$this->message}";