5 * Created on Dec 01, 2007
7 * Copyright © 2008 Roan Kattouw <Firstname>.<Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
35 class ApiParamInfo
extends ApiBase
{
37 public function __construct( $main, $action ) {
38 parent
::__construct( $main, $action );
41 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45 $queryObj = new ApiQuery( $this->getMain(), 'query' );
47 if ( is_array( $params['modules'] ) ) {
48 $modArr = $this->getMain()->getModules();
49 $r['modules'] = array();
50 foreach ( $params['modules'] as $m ) {
51 if ( !isset( $modArr[$m] ) ) {
52 $r['modules'][] = array( 'name' => $m, 'missing' => '' );
55 $obj = new $modArr[$m]( $this->getMain(), $m );
56 $a = $this->getClassInfo( $obj );
60 $result->setIndexedTagName( $r['modules'], 'module' );
62 if ( is_array( $params['querymodules'] ) ) {
63 $qmodArr = $queryObj->getModules();
64 $r['querymodules'] = array();
65 foreach ( $params['querymodules'] as $qm ) {
66 if ( !isset( $qmodArr[$qm] ) ) {
67 $r['querymodules'][] = array( 'name' => $qm, 'missing' => '' );
70 $obj = new $qmodArr[$qm]( $this, $qm );
71 $a = $this->getClassInfo( $obj );
73 $a['querytype'] = $queryObj->getModuleType( $qm );
74 $r['querymodules'][] = $a;
76 $result->setIndexedTagName( $r['querymodules'], 'module' );
78 if ( $params['mainmodule'] ) {
79 $r['mainmodule'] = $this->getClassInfo( $this->getMain() );
81 if ( $params['pagesetmodule'] ) {
82 $pageSet = new ApiPageSet( $queryObj );
83 $r['pagesetmodule'] = $this->getClassInfo( $pageSet );
85 $result->addValue( null, $this->getModuleName(), $r );
92 function getClassInfo( $obj ) {
93 $result = $this->getResult();
94 $retval['classname'] = get_class( $obj );
95 $retval['description'] = implode( "\n", (array)$obj->getDescription() );
96 $examples = (array)$obj->getExamples();
97 $retval['examples'] = implode( "\n", $examples );
98 $retval['version'] = implode( "\n", (array)$obj->getVersion() );
99 $retval['prefix'] = $obj->getModulePrefix();
101 if ( $obj->isReadMode() ) {
102 $retval['readrights'] = '';
104 if ( $obj->isWriteMode() ) {
105 $retval['writerights'] = '';
107 if ( $obj->mustBePosted() ) {
108 $retval['mustbeposted'] = '';
110 if ( $obj instanceof ApiQueryGeneratorBase
) {
111 $retval['generator'] = '';
114 $allowedParams = $obj->getFinalParams();
115 if ( !is_array( $allowedParams ) ) {
119 $retval['helpurls'] = (array)$obj->getHelpUrls();
120 if ( isset( $retval['helpurls'][0] ) && $retval['helpurls'][0] === false ) {
121 $retval['helpurls'] = array();
123 $result->setIndexedTagName( $retval['helpurls'], 'helpurl' );
125 $retval['allexamples'] = $examples;
126 if ( isset( $retval['allexamples'][0] ) && $retval['allexamples'][0] === false ) {
127 $retval['allexamples'] = array();
129 $result->setIndexedTagName( $retval['allexamples'], 'example' );
131 $retval['parameters'] = array();
132 $paramDesc = $obj->getFinalParamDescription();
133 foreach ( $allowedParams as $n => $p ) {
134 $a = array( 'name' => $n );
135 if ( isset( $paramDesc[$n] ) ) {
136 $a['description'] = implode( "\n", (array)$paramDesc[$n] );
140 if( !is_array( $p ) ) {
142 ApiBase
::PARAM_DFLT
=> $p,
146 //handle missing type
147 if ( !isset( $p[ApiBase
::PARAM_TYPE
] ) ) {
148 $dflt = isset( $p[ApiBase
::PARAM_DFLT
] ) ?
$p[ApiBase
::PARAM_DFLT
] : null;
149 if ( is_bool( $dflt ) ) {
150 $p[ApiBase
::PARAM_TYPE
] = 'boolean';
151 } elseif ( is_string( $dflt ) ||
is_null( $dflt ) ) {
152 $p[ApiBase
::PARAM_TYPE
] = 'string';
153 } elseif ( is_int( $dflt ) ) {
154 $p[ApiBase
::PARAM_TYPE
] = 'integer';
158 if ( isset( $p[ApiBase
::PARAM_DEPRECATED
] ) && $p[ApiBase
::PARAM_DEPRECATED
] ) {
159 $a['deprecated'] = '';
161 if ( isset( $p[ApiBase
::PARAM_REQUIRED
] ) && $p[ApiBase
::PARAM_REQUIRED
] ) {
165 if ( isset( $p[ApiBase
::PARAM_DFLT
] ) ) {
166 $type = $p[ApiBase
::PARAM_TYPE
];
167 if( $type === 'boolean' ) {
168 $a['default'] = ( $p[ApiBase
::PARAM_DFLT
] ?
'true' : 'false' );
169 } elseif( $type === 'string' ) {
170 $a['default'] = strval( $p[ApiBase
::PARAM_DFLT
] );
171 } elseif( $type === 'integer' ) {
172 $a['default'] = intval( $p[ApiBase
::PARAM_DFLT
] );
174 $a['default'] = $p[ApiBase
::PARAM_DFLT
];
177 if ( isset( $p[ApiBase
::PARAM_ISMULTI
] ) && $p[ApiBase
::PARAM_ISMULTI
] ) {
179 $a['limit'] = $this->getMain()->canApiHighLimits() ?
180 ApiBase
::LIMIT_SML2
:
182 $a['lowlimit'] = ApiBase
::LIMIT_SML1
;
183 $a['highlimit'] = ApiBase
::LIMIT_SML2
;
186 if ( isset( $p[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) && $p[ApiBase
::PARAM_ALLOW_DUPLICATES
] ) {
187 $a['allowsduplicates'] = '';
190 if ( isset( $p[ApiBase
::PARAM_TYPE
] ) ) {
191 $a['type'] = $p[ApiBase
::PARAM_TYPE
];
192 if ( is_array( $a['type'] ) ) {
193 $a['type'] = array_values( $a['type'] ); // to prevent sparse arrays from being serialized to JSON as objects
194 $result->setIndexedTagName( $a['type'], 't' );
197 if ( isset( $p[ApiBase
::PARAM_MAX
] ) ) {
198 $a['max'] = $p[ApiBase
::PARAM_MAX
];
200 if ( isset( $p[ApiBase
::PARAM_MAX2
] ) ) {
201 $a['highmax'] = $p[ApiBase
::PARAM_MAX2
];
203 if ( isset( $p[ApiBase
::PARAM_MIN
] ) ) {
204 $a['min'] = $p[ApiBase
::PARAM_MIN
];
206 $retval['parameters'][] = $a;
208 $result->setIndexedTagName( $retval['parameters'], 'param' );
211 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
212 $result->setIndexedTagName( $retval['errors'], 'error' );
217 public function isReadMode() {
221 public function getAllowedParams() {
224 ApiBase
::PARAM_ISMULTI
=> true
226 'querymodules' => array(
227 ApiBase
::PARAM_ISMULTI
=> true
229 'mainmodule' => false,
230 'pagesetmodule' => false,
234 public function getParamDescription() {
236 'modules' => 'List of module names (value of the action= parameter)',
237 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
238 'mainmodule' => 'Get information about the main (top-level) module as well',
239 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
243 public function getDescription() {
244 return 'Obtain information about certain API parameters and errors';
247 protected function getExamples() {
249 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
253 public function getHelpUrls() {
254 return 'http://www.mediawiki.org/wiki/API:Parameter_information';
257 public function getVersion() {
258 return __CLASS__
. ': $Id$';