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
{
38 public function __construct($message, $codestr) {
39 parent
:: __construct($message);
40 $this->codestr
= $codestr;
42 public function __toString() {
43 return "{$this->codestr}: {$this->message}";
47 class ApiMain
extends ApiBase
{
49 private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames, $mApiStartTime, $mResult;
53 * $apiStartTime - time of the originating call for profiling purposes
54 * $modules - an array of actions (keys) and classes that handle them (values)
56 public function __construct($apiStartTime, $modules, $formats) {
57 // Special handling for the main module: $parent === $this
58 parent
:: __construct($this);
60 $this->mModules
= $modules;
61 $this->mModuleNames
= array_keys($modules);
62 $this->mFormats
= $formats;
63 $this->mFormatNames
= array_keys($formats);
64 $this->mApiStartTime
= $apiStartTime;
65 $this->mResult
= new ApiResult($this);
68 public function GetResult() {
69 return $this->mResult
;
72 protected function GetAllowedParams() {
75 GN_ENUM_DFLT
=> API_DEFAULT_FORMAT
,
76 GN_ENUM_CHOICES
=> $this->mFormatNames
79 GN_ENUM_DFLT
=> 'help',
80 GN_ENUM_CHOICES
=> $this->mModuleNames
85 protected function GetParamDescription() {
87 'format' => 'The format of the output',
88 'action' => 'What action you would like to perform'
92 public function Execute() {
93 $action = $format = null;
95 extract($this->ExtractRequestParams());
97 // Create an appropriate printer
98 $this->mPrinter
= new $this->mFormats
[$format] ($this, $format);
100 // Instantiate and execute module requested by the user
101 $module = new $this->mModules
[$action] ($this, $action);
103 $this->PrintResult(false);
104 } catch (UsageException
$e) {
105 // Printer may not be initialized if the ExtractRequestParams() fails for the main module
106 if (!isset ($this->mPrinter
))
107 $this->mPrinter
= new $this->mFormats
[API_DEFAULT_FORMAT
] ($this, API_DEFAULT_FORMAT
);
108 $this->PrintResult(true);
115 private function PrintResult($isError) {
116 $this->mPrinter
->InitPrinter($isError);
117 $this->mPrinter
->Execute();
118 $this->mPrinter
->ClosePrinter();
121 protected function GetDescription() {
122 return "This API allows programs to access various functions of MediaWiki software.";
125 public function MainDieUsage($description, $errorCode, $httpRespCode = 0) {
126 $this->mResult
->Reset();
127 $this->mResult
->addMessage('error', null, $errorCode);
128 if ($httpRespCode === 0)
129 header($errorCode, true);
131 header($errorCode, true, $httpRespCode);
133 $this->mResult
->addMessage('usage', null, $this->MakeHelpMsg());
135 throw new UsageException($description, $errorCode);
139 * Override the parent to generate help messages for all available modules.
141 public function MakeHelpMsg() {
143 // Use parent to make default message for the main module
144 $msg = parent
:: MakeHelpMsg();
146 $msg .= "\n\n*Modules*\n";
147 foreach ($this->mModules
as $moduleName => $moduleClass) {
148 $module = new $this->mModules
[$moduleName] ($this, $moduleName);
149 $msg2 = $module->MakeHelpMsg();
151 $msg .= $msg2 . "\n";
154 $msg .= "\n*Formats*\n";
155 foreach ($this->mFormats
as $moduleName => $moduleClass) {
156 $module = new $this->mFormats
[$moduleName] ($this, $moduleName);
157 $msg2 = $module->MakeHelpMsg();
159 $msg .= $msg2 . "\n";