e31c879e1ba9906b522083b01db82efdc58f7bef
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");
33 * @desc This exception will be thrown when DieUsage is called to stop module execution.
35 class UsageException
extends Exception
{
39 public function __construct($message, $codestr) {
40 parent
:: __construct($message);
41 $this->codestr
= $codestr;
43 public function __toString() {
44 return "{$this->codestr}: {$this->message}";
48 class ApiMain
extends ApiBase
{
50 private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames, $mApiStartTime, $mResult;
54 * $apiStartTime - time of the originating call for profiling purposes
55 * $modules - an array of actions (keys) and classes that handle them (values)
57 public function __construct($apiStartTime, $modules, $formats) {
58 // Special handling for the main module: $parent === $this
59 parent
:: __construct($this);
61 $this->mModules
= $modules;
62 $this->mModuleNames
= array_keys($modules);
63 $this->mFormats
= $formats;
64 $this->mFormatNames
= array_keys($formats);
65 $this->mApiStartTime
= $apiStartTime;
66 $this->mResult
= new ApiResult($this);
69 public function GetResult() {
70 return $this->mResult
;
73 protected function GetAllowedParams() {
76 GN_ENUM_DFLT
=> API_DEFAULT_FORMAT
,
77 GN_ENUM_TYPE
=> $this->mFormatNames
80 GN_ENUM_DFLT
=> 'help',
81 GN_ENUM_TYPE
=> $this->mModuleNames
86 protected function GetParamDescription() {
88 'format' => 'The format of the output',
89 'action' => 'What action you would like to perform'
93 public function Execute() {
94 $action = $format = null;
96 extract($this->ExtractRequestParams());
98 // Create an appropriate printer
99 $this->mPrinter
= new $this->mFormats
[$format] ($this, $format);
101 // Instantiate and execute module requested by the user
102 $module = new $this->mModules
[$action] ($this, $action);
104 $this->PrintResult(false);
105 } catch (UsageException
$e) {
106 // Printer may not be initialized if the ExtractRequestParams() fails for the main module
107 if (!isset ($this->mPrinter
))
108 $this->mPrinter
= new $this->mFormats
[API_DEFAULT_FORMAT
] ($this, API_DEFAULT_FORMAT
);
109 $this->PrintResult(true);
116 private function PrintResult($isError) {
117 $this->mPrinter
->InitPrinter($isError);
118 if (!$this->mPrinter
->GetNeedsRawData())
119 $this->GetResult()->SanitizeData();
120 $this->mPrinter
->Execute();
121 $this->mPrinter
->ClosePrinter();
124 protected function GetDescription() {
127 'This API allows programs to access various functions of MediaWiki software.',
132 public function MainDieUsage($description, $errorCode, $httpRespCode = 0) {
133 $this->mResult
->Reset();
134 $this->mResult
->addMessage('error', null, $errorCode);
135 if ($httpRespCode === 0)
136 header($errorCode, true);
138 header($errorCode, true, $httpRespCode);
140 $this->mResult
->addMessage('usage', null, $this->MakeHelpMsg());
142 throw new UsageException($description, $errorCode);
146 * Override the parent to generate help messages for all available modules.
148 public function MakeHelpMsg() {
150 // Use parent to make default message for the main module
151 $msg = parent
:: MakeHelpMsg();
153 $astriks = str_repeat('*** ', 10);
154 $msg .= "\n\n$astriks Modules $astriks\n\n";
155 foreach ($this->mModules
as $moduleName => $moduleClass) {
156 $msg .= "* action=$moduleName *";
157 $module = new $this->mModules
[$moduleName] ($this, $moduleName);
158 $msg2 = $module->MakeHelpMsg();
164 $msg .= "\n$astriks Formats $astriks\n\n";
165 foreach ($this->mFormats
as $moduleName => $moduleClass) {
166 $msg .= "* format=$moduleName *";
167 $module = new $this->mFormats
[$moduleName] ($this, $moduleName);
168 $msg2 = $module->MakeHelpMsg();
177 private $mIsBot = null;
178 public function IsBot() {
179 if (!isset ($this->mIsBot
)) {
181 $this->mIsBot
= $wgUser->isAllowed('bot');
183 return $this->mIsBot
;