(bug 12718) Added action=paraminfo module that provides information about API modules...
[lhc/web/wiklou.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 * @addtogroup 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 $r = array();
45 if(is_array($params['modules']))
46 foreach($params['modules'] as $m)
47 {
48 $className = "Api$m";
49 if(!class_exists($className))
50 {
51 $mods[$m] = array('missing' => '');
52 continue;
53 }
54 $obj = new $className($this->getMain(), $m);
55 $r['modules'][$m] = $this->getClassInfo($obj);
56 }
57 if(is_array($params['querymodules']))
58 foreach($params['querymodules'] as $qm)
59 {
60 $className = "ApiQuery$qm";
61 if(!class_exists($className))
62 {
63 $qmods[$qm] = array('missing' => '');
64 continue;
65 }
66 $obj = new $className($this, 'query');
67 $r['querymodules'][$qm] = $this->getClassInfo($obj);
68 }
69 $result->addValue( null, $this->getModuleName(), $r );
70 }
71
72 function getClassInfo($obj)
73 {
74 $result = $this->getResult();
75 $retval['classname'] = get_class($obj);
76 $retval['description'] = (is_array($obj->getDescription()) ? implode("\n", $obj->getDescription()) : $obj->getDescription());
77 $allowedParams = $obj->getAllowedParams();
78 if(!is_array($allowedParams))
79 return $retval;
80 $retval['parameters'] = array();
81 foreach($obj->getAllowedParams() as $n => $p)
82 {
83 $a = array('name' => $n);
84 if(!is_array($p))
85 {
86 if(is_bool($p))
87 {
88 $a['type'] = 'bool';
89 $a['default'] = ($p ? 'true' : 'false');
90 }
91 if(is_string($p))
92 $a['default'] = $p;
93 $retval['parameters'][] = $a;
94 continue;
95 }
96
97 if(isset($p[ApiBase::PARAM_DFLT]))
98 $a['default'] = $p[ApiBase::PARAM_DFLT];
99 if(isset($p[ApiBase::PARAM_ISMULTI]))
100 if($p[ApiBase::PARAM_ISMULTI])
101 $a['multi'] = '';
102 if(isset($p[ApiBase::PARAM_TYPE]))
103 {
104 $a['type'] = $p[ApiBase::PARAM_TYPE];
105 if(is_array($a['type']))
106 $result->setIndexedTagName($a['type'], 't');
107 }
108 if(isset($p[ApiBase::PARAM_MAX]))
109 $a['max'] = $p[ApiBase::PARAM_MAX];
110 if(isset($p[ApiBase::PARAM_MAX2]))
111 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
112 if(isset($p[ApiBase::PARAM_MIN]))
113 $a['min'] = $p[ApiBase::PARAM_MIN];
114 $retval['parameters'][] = $a;
115 }
116 $result->setIndexedTagName($retval['parameters'], 'param');
117 return $retval;
118 }
119
120 protected function getAllowedParams() {
121 return array (
122 'modules' => array(
123 ApiBase :: PARAM_ISMULTI => true
124 ),
125 'querymodules' => array(
126 ApiBase :: PARAM_ISMULTI => true
127 )
128 );
129 }
130
131 protected function getParamDescription() {
132 return array (
133 'modules' => 'List of module names (value of the action= parameter)',
134 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
135 );
136 }
137
138 protected function getDescription() {
139 return 'Obtain information about certain API parameters';
140 }
141
142 protected function getExamples() {
143 return array (
144 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
145 );
146 }
147
148 public function getVersion() {
149 return __CLASS__ . ': $Id: ApiParse.php 29810 2008-01-15 21:33:08Z catrope $';
150 }
151 }
152