summaryrefslogtreecommitdiff
path: root/includes/api/ApiMain.php
blob: 046d7d7cbd6505c33c0622ebc2123487128592a3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
<?php


/*
 * Created on Sep 4, 2006
 *
 * API for MediaWiki 1.8+
 *
 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */

if (!defined('MEDIAWIKI')) {
	// Eclipse helper - will be ignored in production
	require_once ('ApiBase.php');
}

class ApiMain extends ApiBase {

	private $mPrinter, $mModules, $mModuleNames, $mFormats, $mFormatNames;
	private $mApiStartTime, $mResult, $mShowVersions, $mEnableWrite;

	/**
	* Constructor
	* $apiStartTime - time of the originating call for profiling purposes
	* $modules - an array of actions (keys) and classes that handle them (values) 
	*/
	public function __construct($apiStartTime, $modules, $formats, $enableWrite) {
		// Special handling for the main module: $parent === $this
		parent :: __construct($this, 'main');

		$this->mModules = $modules;
		$this->mModuleNames = array_keys($modules);
		$this->mFormats = $formats;
		$this->mFormatNames = array_keys($formats);
		$this->mApiStartTime = $apiStartTime;
		$this->mResult = new ApiResult($this);
		$this->mShowVersions = false;
		$this->mEnableWrite = $enableWrite;
	}

	public function & getResult() {
		return $this->mResult;
	}

	public function getShowVersions() {
		return $this->mShowVersions;
	}

	public function requestWriteMode() {
		if (!$this->mEnableWrite)
			$this->dieUsage('Editing of this site is disabled. Make sure the $wgEnableWriteAPI=true; ' .
			'statement is included in the site\'s LocalSettings.php file', 'readonly');
	}

	protected function getAllowedParams() {
		return array (
			'format' => array (
				ApiBase :: PARAM_DFLT => API_DEFAULT_FORMAT,
				ApiBase :: PARAM_TYPE => $this->mFormatNames
			),
			'action' => array (
				ApiBase :: PARAM_DFLT => 'help',
				ApiBase :: PARAM_TYPE => $this->mModuleNames
			),
			'version' => false
		);
	}

	protected function getParamDescription() {
		return array (
			'format' => 'The format of the output',
			'action' => 'What action you would like to perform',
			'version' => 'When showing help, include version for each module'
		);
	}

	public function execute() {
		$this->profileIn();
		$action = $format = $version = null;
		try {
			extract($this->extractRequestParams());
			$this->mShowVersions = $version;

			// Create an appropriate printer
			$this->mPrinter = new $this->mFormats[$format] ($this, $format);

			// Instantiate and execute module requested by the user
			$module = new $this->mModules[$action] ($this, $action);
			$module->profileIn();
			$module->execute();
			$module->profileOut();
			$this->printResult(false);

		} catch (UsageException $e) {

			// Printer may not be initialized if the extractRequestParams() fails for the main module
			if (!isset ($this->mPrinter))
				$this->mPrinter = new $this->mFormats[API_DEFAULT_FORMAT] ($this, API_DEFAULT_FORMAT);
			$this->printResult(true);

		}
		$this->profileOut();
	}

	/**
	 * Internal printer
	 */
	private function printResult($isError) {
		$printer = $this->mPrinter;
		$printer->profileIn();
		$printer->initPrinter($isError);
		if (!$printer->getNeedsRawData())
			$this->getResult()->SanitizeData();
		$printer->execute();
		$printer->closePrinter();
		$printer->profileOut();
	}

	protected function getDescription() {
		return array (
			'',
			'This API allows programs to access various functions of MediaWiki software.',
			'For more details see API Home Page @ http://meta.wikimedia.org/wiki/API',
			''
		);
	}

	public function mainDieUsage($description, $errorCode, $httpRespCode = 0) {
		$this->mResult->Reset();
		if ($httpRespCode === 0)
			header($errorCode, true);
		else
			header($errorCode, true, $httpRespCode);

		$data = array (
			'code' => $errorCode,
			'info' => $description
		);
		ApiResult :: setContent($data, $this->makeHelpMsg());
		$this->mResult->addValue(null, 'error', $data);

		throw new UsageException($description, $errorCode);
	}

	/**
	 * Override the parent to generate help messages for all available modules.
	 */
	public function makeHelpMsg() {

		// Use parent to make default message for the main module
		$msg = parent :: makeHelpMsg();

		$astriks = str_repeat('*** ', 10);
		$msg .= "\n\n$astriks Modules  $astriks\n\n";
		foreach ($this->mModules as $moduleName => $moduleClass) {
			$msg .= "* action=$moduleName *";
			$module = new $this->mModules[$moduleName] ($this, $moduleName);
			$msg2 = $module->makeHelpMsg();
			if ($msg2 !== false)
				$msg .= $msg2;
			$msg .= "\n";
		}

		$msg .= "\n$astriks Formats  $astriks\n\n";
		foreach ($this->mFormats as $moduleName => $moduleClass) {
			$msg .= "* format=$moduleName *";
			$module = new $this->mFormats[$moduleName] ($this, $moduleName);
			$msg2 = $module->makeHelpMsg();
			if ($msg2 !== false)
				$msg .= $msg2;
			$msg .= "\n";
		}

		return $msg;
	}

	private $mIsBot = null;
	public function isBot() {
		if (!isset ($this->mIsBot)) {
			global $wgUser;
			$this->mIsBot = $wgUser->isAllowed('bot');
		}
		return $this->mIsBot;
	}

	public function getVersion() {
		$vers = array ();
		$vers[] = __CLASS__ . ': $Id: ApiMain.php 16820 2006-10-06 01:02:14Z yurik $';
		$vers[] = ApiBase :: getBaseVersion();
		$vers[] = ApiFormatBase :: getBaseVersion();
		$vers[] = ApiQueryBase :: getBaseVersion();
		return $vers;
	}
}

/**
* @desc This exception will be thrown when dieUsage is called to stop module execution.
*/
class UsageException extends Exception {

	private $codestr;

	public function __construct($message, $codestr) {
		parent :: __construct($message);
		$this->codestr = $codestr;
	}
	public function __toString() {
		return "{$this->codestr}: {$this->message}";
	}
}
?>