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 $mModules, $mModuleNames, $mApiStartTime, $mResult;
38 * $apiStartTime - time of the originating call for profiling purposes
39 * $modules - an array of actions (keys) and classes that handle them (values)
41 public function __construct($apiStartTime, $modules) {
42 // Special handling for the main module: $parent === $this
43 parent
:: __construct($this);
45 $this->mModules
= $modules;
46 $this->mModuleNames
= array_keys($modules);
47 $this->mApiStartTime
= $apiStartTime;
48 $this->mResult
= new ApiResult($this);
51 public function GetResult() {
52 return $this->mResult
;
55 protected function GetAllowedParams() {
59 GN_ENUM_DFLT
=> 'help',
60 GN_ENUM_ISMULTI
=> false,
61 GN_ENUM_CHOICES
=> $this->mModuleNames
66 public function Execute() {
67 $action = $format = null;
68 extract($this->ExtractRequestParams());
70 // Instantiate and execute module requested by the user
71 $module = new $this->mModules
[$action] ($this, $action);
75 protected function GetDescription() {
76 return "This API allows programs to access various functions of MediaWiki software.";
79 protected function GetParamDescription($paramName) {
81 case 'format': return "The format of the output";
82 case 'action': return "What action you would like to perform";
83 default: return parent
:: GetParamDescription($paramName);
87 public function MainDieUsage($description, $errorCode, $httpRespCode = 0) {
88 $this->mResult
->Reset();
89 $this->mResult
->addMessage('error', null, $errorCode);
90 if ($httpRespCode === 0)
91 header($errorCode, true);
93 header($errorCode, true, $httpRespCode);
95 $this->mResult
->addMessage('usage', null, $this->MakeHelpMsg());
97 var_export($this->mResult
->GetData());