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);
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;
56 public function & getResult() {
57 return $this->mResult
;
60 public function getShowVersions() {
61 return $this->mShowVersions
;
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');
70 protected function getAllowedParams() {
73 ApiBase
:: PARAM_DFLT
=> API_DEFAULT_FORMAT
,
74 ApiBase
:: PARAM_TYPE
=> $this->mFormatNames
77 ApiBase
:: PARAM_DFLT
=> 'help',
78 ApiBase
:: PARAM_TYPE
=> $this->mModuleNames
84 protected function getParamDescription() {
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'
92 public function execute() {
94 $action = $format = $version = null;
96 extract($this->extractRequestParams());
97 $this->mShowVersions
= $version;
99 // Create an appropriate printer
100 $this->mPrinter
= new $this->mFormats
[$format] ($this, $format);
102 // Instantiate and execute module requested by the user
103 $module = new $this->mModules
[$action] ($this, $action);
104 $module->profileIn();
106 $module->profileOut();
107 $this->printResult(false);
109 } catch (UsageException
$e) {
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);
123 private function printResult($isError) {
124 $printer = $this->mPrinter
;
125 $printer->profileIn();
126 $printer->initPrinter($isError);
127 if (!$printer->getNeedsRawData())
128 $this->getResult()->SanitizeData();
130 $printer->closePrinter();
131 $printer->profileOut();
134 protected function getDescription() {
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',
143 public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
144 $this->mResult
->Reset();
145 if ($httpRespCode === 0)
146 header($errorCode, true);
148 header($errorCode, true, $httpRespCode);
153 ApiResult
:: setContent($data, $this->makeHelpMsg());
154 $this->mResult
->addValue(null, 'error', $data);
156 throw new UsageException($description, $errorCode);
160 * Override the parent to generate help messages for all available modules.
162 public function makeHelpMsg() {
164 // Use parent to make default message for the main module
165 $msg = parent
:: makeHelpMsg();
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();
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();
191 private $mIsBot = null;
192 public function isBot() {
193 if (!isset ($this->mIsBot
)) {
195 $this->mIsBot
= $wgUser->isAllowed('bot');
197 return $this->mIsBot
;
200 public function getVersion() {
202 $vers[] = __CLASS__
. ': $Id$';
203 $vers[] = ApiBase
:: getBaseVersion();
204 $vers[] = ApiFormatBase
:: getBaseVersion();
205 $vers[] = ApiQueryBase
:: getBaseVersion();
211 * @desc This exception will be thrown when dieUsage is called to stop module execution.
213 class UsageException
extends Exception
{
217 public function __construct($message, $codestr) {
218 parent
:: __construct($message);
219 $this->codestr
= $codestr;
221 public function __toString() {
222 return "{$this->codestr}: {$this->message}";