]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiParamInfo.php
MediaWiki 1.15.0-scripts
[autoinstalls/mediawiki.git] / includes / api / ApiParamInfo.php
1 <?php
2
3 /*
4  * Created on Dec 01, 2007
5  *
6  * API for MediaWiki 1.8+
7  *
8  * Copyright (C) 2008 Roan Kattouw <Firstname>.<Lastname>@home.nl
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License along
21  * with this program; if not, write to the Free Software Foundation, Inc.,
22  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23  * http://www.gnu.org/copyleft/gpl.html
24  */
25
26 if (!defined('MEDIAWIKI')) {
27         // Eclipse helper - will be ignored in production
28         require_once ("ApiBase.php");
29 }
30
31 /**
32  * @ingroup API
33  */
34 class ApiParamInfo extends ApiBase {
35
36         public function __construct($main, $action) {
37                 parent :: __construct($main, $action);
38         }
39
40         public function execute() {
41                 // Get parameters
42                 $params = $this->extractRequestParams();
43                 $result = $this->getResult();
44                 $queryObj = new ApiQuery($this->getMain(), 'query');
45                 $r = array();
46                 if(is_array($params['modules']))
47                 {
48                         $modArr = $this->getMain()->getModules();
49                         foreach($params['modules'] as $m)
50                         {
51                                 if(!isset($modArr[$m]))
52                                 {
53                                         $r['modules'][] = array('name' => $m, 'missing' => '');
54                                         continue;
55                                 }
56                                 $obj = new $modArr[$m]($this->getMain(), $m);
57                                 $a = $this->getClassInfo($obj);
58                                 $a['name'] = $m;
59                                 $r['modules'][] = $a;
60                         }
61                         $result->setIndexedTagName($r['modules'], 'module');
62                 }
63                 if(is_array($params['querymodules']))
64                 {
65                         $qmodArr = $queryObj->getModules();
66                         foreach($params['querymodules'] as $qm)
67                         {
68                                 if(!isset($qmodArr[$qm]))
69                                 {
70                                         $r['querymodules'][] = array('name' => $qm, 'missing' => '');
71                                         continue;
72                                 }
73                                 $obj = new $qmodArr[$qm]($this, $qm);
74                                 $a = $this->getClassInfo($obj);
75                                 $a['name'] = $qm;
76                                 $r['querymodules'][] = $a;
77                         }
78                         $result->setIndexedTagName($r['querymodules'], 'module');
79                 }
80                 if($params['mainmodule'])
81                         $r['mainmodule'] = $this->getClassInfo($this->getMain());
82                 if($params['pagesetmodule'])
83                 {
84                         $pageSet = new ApiPageSet($queryObj);
85                         $r['pagesetmodule'] = $this->getClassInfo($pageSet);
86                 }
87                 $result->addValue(null, $this->getModuleName(), $r);
88         }
89
90         function getClassInfo($obj)
91         {
92                 $result = $this->getResult();
93                 $retval['classname'] = get_class($obj);
94                 $retval['description'] = (is_array($obj->getDescription()) ? implode("\n", $obj->getDescription()) : $obj->getDescription());
95                 $retval['prefix'] = $obj->getModulePrefix();
96                 if($obj->isReadMode())
97                         $retval['readrights'] = '';
98                 if($obj->isWriteMode())
99                         $retval['writerights'] = '';
100                 if($obj->mustBePosted())
101                         $retval['mustbeposted'] = '';
102                 $allowedParams = $obj->getFinalParams();
103                 if(!is_array($allowedParams))
104                         return $retval;
105                 $retval['parameters'] = array();
106                 $paramDesc = $obj->getFinalParamDescription();
107                 foreach($allowedParams as $n => $p)
108                 {
109                         $a = array('name' => $n);
110                         if(!is_array($p))
111                         {
112                                 if(is_bool($p))
113                                 {
114                                         $a['type'] = 'bool';
115                                         $a['default'] = ($p ? 'true' : 'false');
116                                 }
117                                 if(is_string($p))
118                                         $a['default'] = $p;
119                                 $retval['parameters'][] = $a;
120                                 continue;
121                         }
122
123                         if(isset($p[ApiBase::PARAM_DFLT]))
124                                 $a['default'] = $p[ApiBase::PARAM_DFLT];
125                         if(isset($p[ApiBase::PARAM_ISMULTI]))
126                                 if($p[ApiBase::PARAM_ISMULTI])
127                                 {
128                                         $a['multi'] = '';
129                                         $a['limit'] = $this->getMain()->canApiHighLimits() ?
130                                                         ApiBase::LIMIT_SML2 :
131                                                         ApiBase::LIMIT_SML1;
132                                 }
133                         if(isset($p[ApiBase::PARAM_ALLOW_DUPLICATES]))
134                                 if($p[ApiBase::PARAM_ALLOW_DUPLICATES])
135                                         $a['allowsduplicates'] = '';
136                         if(isset($p[ApiBase::PARAM_TYPE]))
137                         {
138                                 $a['type'] = $p[ApiBase::PARAM_TYPE];
139                                 if(is_array($a['type']))
140                                         $result->setIndexedTagName($a['type'], 't');
141                         }
142                         if(isset($p[ApiBase::PARAM_MAX]))
143                                 $a['max'] = $p[ApiBase::PARAM_MAX];
144                         if(isset($p[ApiBase::PARAM_MAX2]))
145                                 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
146                         if(isset($p[ApiBase::PARAM_MIN]))
147                                 $a['min'] = $p[ApiBase::PARAM_MIN];
148                         if(isset($paramDesc[$n]))
149                                 $a['description'] = (is_array($paramDesc[$n]) ? implode("\n", $paramDesc[$n]) : $paramDesc[$n]);
150                         $retval['parameters'][] = $a;
151                 }
152                 $result->setIndexedTagName($retval['parameters'], 'param');
153                 return $retval;
154         }
155
156         public function isReadMode() {
157                 return false;
158         }
159
160         public function getAllowedParams() {
161                 return array (
162                         'modules' => array(
163                                 ApiBase :: PARAM_ISMULTI => true
164                         ),
165                         'querymodules' => array(
166                                 ApiBase :: PARAM_ISMULTI => true
167                         ),
168                         'mainmodule' => false,
169                         'pagesetmodule' => false,
170                 );
171         }
172
173         public function getParamDescription() {
174                 return array (
175                         'modules' => 'List of module names (value of the action= parameter)',
176                         'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
177                         'mainmodule' => 'Get information about the main (top-level) module as well',
178                         'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
179                 );
180         }
181
182         public function getDescription() {
183                 return 'Obtain information about certain API parameters';
184         }
185
186         protected function getExamples() {
187                 return array (
188                         'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
189                 );
190         }
191
192         public function getVersion() {
193                 return __CLASS__ . ': $Id: ApiParamInfo.php 48091 2009-03-06 13:49:44Z catrope $';
194         }
195 }