ad3cd28f50ed72e02605e62ee49b5c41e2e8e148
[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 class ApiMain extends ApiBase {
33
34 private $mModules, $mModuleNames, $mApiStartTime, $mResult;
35
36 /**
37 * Constructor
38 * $apiStartTime - time of the originating call for profiling purposes
39 * $modules - an array of actions (keys) and classes that handle them (values)
40 */
41 public function __construct($apiStartTime, $modules) {
42 // Special handling for the main module: $parent === $this
43 parent :: __construct($this);
44
45 $this->mModules = $modules;
46 $this->mModuleNames = array_keys($modules);
47 $this->mApiStartTime = $apiStartTime;
48 $this->mResult = new ApiResult($this);
49 }
50
51 public function GetResult() {
52 return $this->mResult;
53 }
54
55 protected function GetAllowedParams() {
56 return array (
57 'format' => 'xmlfm',
58 'action' => array (
59 GN_ENUM_DFLT => 'help',
60 GN_ENUM_ISMULTI => false,
61 GN_ENUM_CHOICES => $this->mModuleNames
62 )
63 );
64 }
65
66 public function Execute() {
67 $action = $format = null;
68 extract($this->ExtractRequestParams());
69
70 // Instantiate and execute module requested by the user
71 $module = new $this->mModules[$action] ($this, $action);
72 $module->Execute();
73 }
74
75 protected function GetDescription() {
76 return "This API allows programs to access various functions of MediaWiki software.";
77 }
78
79 protected function GetParamDescription($paramName) {
80 switch($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);
84 }
85 }
86
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);
92 else
93 header($errorCode, true, $httpRespCode);
94
95 $this->mResult->addMessage('usage', null, $this->MakeHelpMsg());
96
97 var_export($this->mResult->GetData());
98 }
99 }
100 ?>