* API-query: normalization
[lhc/web/wiklou.git] / includes / api / ApiMain.php
1 <?php
2
3
4 /*
5 * Created on Sep 4, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
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.
15 *
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.
20 *
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
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ("ApiBase.php");
30 }
31
32 /**
33 * @desc This exception will be thrown when DieUsage is called to stop module execution.
34 */
35 class UsageException extends Exception {
36 var $codestr;
37
38 public function __construct($message, $codestr) {
39 parent :: __construct($message);
40 $this->codestr = $codestr;
41 }
42 public function __toString() {
43 return "{$this->codestr}: {$this->message}";
44 }
45 }
46
47 class ApiMain extends ApiBase {
48
49 private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames, $mApiStartTime, $mResult;
50
51 /**
52 * Constructor
53 * $apiStartTime - time of the originating call for profiling purposes
54 * $modules - an array of actions (keys) and classes that handle them (values)
55 */
56 public function __construct($apiStartTime, $modules, $formats) {
57 // Special handling for the main module: $parent === $this
58 parent :: __construct($this);
59
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);
66 }
67
68 public function GetResult() {
69 return $this->mResult;
70 }
71
72 protected function GetAllowedParams() {
73 return array (
74 'format' => array (
75 GN_ENUM_DFLT => API_DEFAULT_FORMAT,
76 GN_ENUM_CHOICES => $this->mFormatNames
77 ),
78 'action' => array (
79 GN_ENUM_DFLT => 'help',
80 GN_ENUM_CHOICES => $this->mModuleNames
81 )
82 );
83 }
84
85 protected function GetParamDescription() {
86 return array (
87 'format' => 'The format of the output',
88 'action' => 'What action you would like to perform'
89 );
90 }
91
92 public function Execute() {
93 $action = $format = null;
94 try {
95 extract($this->ExtractRequestParams());
96
97 // Create an appropriate printer
98 $this->mPrinter = new $this->mFormats[$format] ($this, $format);
99
100 // Instantiate and execute module requested by the user
101 $module = new $this->mModules[$action] ($this, $action);
102 $module->Execute();
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);
109 }
110 }
111
112 /**
113 * Internal printer
114 */
115 private function PrintResult($isError) {
116 $this->mPrinter->InitPrinter($isError);
117 $this->mPrinter->Execute();
118 $this->mPrinter->ClosePrinter();
119 }
120
121 protected function GetDescription() {
122 return array (
123 '',
124 'This API allows programs to access various functions of MediaWiki software.',
125 ''
126 );
127 }
128
129 public function MainDieUsage($description, $errorCode, $httpRespCode = 0) {
130 $this->mResult->Reset();
131 $this->mResult->addMessage('error', null, $errorCode);
132 if ($httpRespCode === 0)
133 header($errorCode, true);
134 else
135 header($errorCode, true, $httpRespCode);
136
137 $this->mResult->addMessage('usage', null, $this->MakeHelpMsg());
138
139 throw new UsageException($description, $errorCode);
140 }
141
142 /**
143 * Override the parent to generate help messages for all available modules.
144 */
145 public function MakeHelpMsg() {
146
147 // Use parent to make default message for the main module
148 $msg = parent :: MakeHelpMsg();
149
150 $astriks = str_repeat('*** ', 10);
151 $msg .= "\n\n$astriks Modules $astriks\n\n";
152 foreach ($this->mModules as $moduleName => $moduleClass) {
153 $msg .= "* action=$moduleName *";
154 $module = new $this->mModules[$moduleName] ($this, $moduleName);
155 $msg2 = $module->MakeHelpMsg();
156 if ($msg2 !== false)
157 $msg .= $msg2;
158 $msg .= "\n";
159 }
160
161 $msg .= "\n$astriks Formats $astriks\n\n";
162 foreach ($this->mFormats as $moduleName => $moduleClass) {
163 $msg .= "* format=$moduleName *";
164 $module = new $this->mFormats[$moduleName] ($this, $moduleName);
165 $msg2 = $module->MakeHelpMsg();
166 if ($msg2 !== false)
167 $msg .= $msg2;
168 $msg .= "\n";
169 }
170
171 return $msg;
172 }
173 }
174 ?>