MediaWiki
REL1_20
|
00001 <?php 00030 class ApiParamInfo extends ApiBase { 00031 00035 protected $queryObj; 00036 00037 public function __construct( $main, $action ) { 00038 parent::__construct( $main, $action ); 00039 $this->queryObj = new ApiQuery( $this->getMain(), 'query' ); 00040 } 00041 00042 public function execute() { 00043 // Get parameters 00044 $params = $this->extractRequestParams(); 00045 $result = $this->getResult(); 00046 00047 $res = array(); 00048 if ( is_array( $params['modules'] ) ) { 00049 $modules = $this->getMain()->getModules(); 00050 $res['modules'] = array(); 00051 foreach ( $params['modules'] as $mod ) { 00052 if ( !isset( $modules[$mod] ) ) { 00053 $res['modules'][] = array( 'name' => $mod, 'missing' => '' ); 00054 continue; 00055 } 00056 $obj = new $modules[$mod]( $this->getMain(), $mod ); 00057 00058 $item = $this->getClassInfo( $obj ); 00059 $item['name'] = $mod; 00060 $res['modules'][] = $item; 00061 } 00062 $result->setIndexedTagName( $res['modules'], 'module' ); 00063 } 00064 00065 if ( is_array( $params['querymodules'] ) ) { 00066 $queryModules = $this->queryObj->getModules(); 00067 $res['querymodules'] = array(); 00068 foreach ( $params['querymodules'] as $qm ) { 00069 if ( !isset( $queryModules[$qm] ) ) { 00070 $res['querymodules'][] = array( 'name' => $qm, 'missing' => '' ); 00071 continue; 00072 } 00073 $obj = new $queryModules[$qm]( $this, $qm ); 00074 $item = $this->getClassInfo( $obj ); 00075 $item['name'] = $qm; 00076 $item['querytype'] = $this->queryObj->getModuleType( $qm ); 00077 $res['querymodules'][] = $item; 00078 } 00079 $result->setIndexedTagName( $res['querymodules'], 'module' ); 00080 } 00081 00082 if ( $params['mainmodule'] ) { 00083 $res['mainmodule'] = $this->getClassInfo( $this->getMain() ); 00084 } 00085 00086 if ( $params['pagesetmodule'] ) { 00087 $pageSet = new ApiPageSet( $this->queryObj ); 00088 $res['pagesetmodule'] = $this->getClassInfo( $pageSet ); 00089 } 00090 00091 if ( is_array( $params['formatmodules'] ) ) { 00092 $formats = $this->getMain()->getFormats(); 00093 $res['formatmodules'] = array(); 00094 foreach ( $params['formatmodules'] as $f ) { 00095 if ( !isset( $formats[$f] ) ) { 00096 $res['formatmodules'][] = array( 'name' => $f, 'missing' => '' ); 00097 continue; 00098 } 00099 $obj = new $formats[$f]( $this, $f ); 00100 $item = $this->getClassInfo( $obj ); 00101 $item['name'] = $f; 00102 $res['formatmodules'][] = $item; 00103 } 00104 $result->setIndexedTagName( $res['formatmodules'], 'module' ); 00105 } 00106 $result->addValue( null, $this->getModuleName(), $res ); 00107 } 00108 00113 function getClassInfo( $obj ) { 00114 $result = $this->getResult(); 00115 $retval['classname'] = get_class( $obj ); 00116 $retval['description'] = implode( "\n", (array)$obj->getFinalDescription() ); 00117 00118 $retval['examples'] = ''; 00119 00120 $retval['version'] = implode( "\n", (array)$obj->getVersion() ); 00121 $retval['prefix'] = $obj->getModulePrefix(); 00122 00123 if ( $obj->isReadMode() ) { 00124 $retval['readrights'] = ''; 00125 } 00126 if ( $obj->isWriteMode() ) { 00127 $retval['writerights'] = ''; 00128 } 00129 if ( $obj->mustBePosted() ) { 00130 $retval['mustbeposted'] = ''; 00131 } 00132 if ( $obj instanceof ApiQueryGeneratorBase ) { 00133 $retval['generator'] = ''; 00134 } 00135 00136 $allowedParams = $obj->getFinalParams(); 00137 if ( !is_array( $allowedParams ) ) { 00138 return $retval; 00139 } 00140 00141 $retval['helpurls'] = (array)$obj->getHelpUrls(); 00142 if ( isset( $retval['helpurls'][0] ) && $retval['helpurls'][0] === false ) { 00143 $retval['helpurls'] = array(); 00144 } 00145 $result->setIndexedTagName( $retval['helpurls'], 'helpurl' ); 00146 00147 $examples = $obj->getExamples(); 00148 $retval['allexamples'] = array(); 00149 if ( $examples !== false ) { 00150 if ( is_string( $examples ) ) { 00151 $examples = array( $examples ); 00152 } 00153 foreach( $examples as $k => $v ) { 00154 if ( strlen( $retval['examples'] ) ) { 00155 $retval['examples'] .= ' '; 00156 } 00157 $item = array(); 00158 if ( is_numeric( $k ) ) { 00159 $retval['examples'] .= $v; 00160 $result->setContent( $item, $v ); 00161 } else { 00162 if ( !is_array( $v ) ) { 00163 $item['description'] = $v; 00164 } else { 00165 $item['description'] = implode( $v, "\n" ); 00166 } 00167 $retval['examples'] .= $item['description'] . ' ' . $k; 00168 $result->setContent( $item, $k ); 00169 } 00170 $retval['allexamples'][] = $item; 00171 } 00172 } 00173 $result->setIndexedTagName( $retval['allexamples'], 'example' ); 00174 00175 $retval['parameters'] = array(); 00176 $paramDesc = $obj->getFinalParamDescription(); 00177 foreach ( $allowedParams as $n => $p ) { 00178 $a = array( 'name' => $n ); 00179 if ( isset( $paramDesc[$n] ) ) { 00180 $a['description'] = implode( "\n", (array)$paramDesc[$n] ); 00181 } 00182 00183 //handle shorthand 00184 if( !is_array( $p ) ) { 00185 $p = array( 00186 ApiBase::PARAM_DFLT => $p, 00187 ); 00188 } 00189 00190 //handle missing type 00191 if ( !isset( $p[ApiBase::PARAM_TYPE] ) ) { 00192 $dflt = isset( $p[ApiBase::PARAM_DFLT] ) ? $p[ApiBase::PARAM_DFLT] : null; 00193 if ( is_bool( $dflt ) ) { 00194 $p[ApiBase::PARAM_TYPE] = 'boolean'; 00195 } elseif ( is_string( $dflt ) || is_null( $dflt ) ) { 00196 $p[ApiBase::PARAM_TYPE] = 'string'; 00197 } elseif ( is_int( $dflt ) ) { 00198 $p[ApiBase::PARAM_TYPE] = 'integer'; 00199 } 00200 } 00201 00202 if ( isset( $p[ApiBase::PARAM_DEPRECATED] ) && $p[ApiBase::PARAM_DEPRECATED] ) { 00203 $a['deprecated'] = ''; 00204 } 00205 if ( isset( $p[ApiBase::PARAM_REQUIRED] ) && $p[ApiBase::PARAM_REQUIRED] ) { 00206 $a['required'] = ''; 00207 } 00208 00209 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) { 00210 $type = $p[ApiBase::PARAM_TYPE]; 00211 if( $type === 'boolean' ) { 00212 $a['default'] = ( $p[ApiBase::PARAM_DFLT] ? 'true' : 'false' ); 00213 } elseif( $type === 'string' ) { 00214 $a['default'] = strval( $p[ApiBase::PARAM_DFLT] ); 00215 } elseif( $type === 'integer' ) { 00216 $a['default'] = intval( $p[ApiBase::PARAM_DFLT] ); 00217 } else { 00218 $a['default'] = $p[ApiBase::PARAM_DFLT]; 00219 } 00220 } 00221 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) && $p[ApiBase::PARAM_ISMULTI] ) { 00222 $a['multi'] = ''; 00223 $a['limit'] = $this->getMain()->canApiHighLimits() ? 00224 ApiBase::LIMIT_SML2 : 00225 ApiBase::LIMIT_SML1; 00226 $a['lowlimit'] = ApiBase::LIMIT_SML1; 00227 $a['highlimit'] = ApiBase::LIMIT_SML2; 00228 } 00229 00230 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) && $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) { 00231 $a['allowsduplicates'] = ''; 00232 } 00233 00234 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) { 00235 $a['type'] = $p[ApiBase::PARAM_TYPE]; 00236 if ( is_array( $a['type'] ) ) { 00237 $a['type'] = array_values( $a['type'] ); // to prevent sparse arrays from being serialized to JSON as objects 00238 $result->setIndexedTagName( $a['type'], 't' ); 00239 } 00240 } 00241 if ( isset( $p[ApiBase::PARAM_MAX] ) ) { 00242 $a['max'] = $p[ApiBase::PARAM_MAX]; 00243 } 00244 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) { 00245 $a['highmax'] = $p[ApiBase::PARAM_MAX2]; 00246 } 00247 if ( isset( $p[ApiBase::PARAM_MIN] ) ) { 00248 $a['min'] = $p[ApiBase::PARAM_MIN]; 00249 } 00250 $retval['parameters'][] = $a; 00251 } 00252 $result->setIndexedTagName( $retval['parameters'], 'param' ); 00253 00254 $props = $obj->getFinalResultProperties(); 00255 $listResult = null; 00256 if ( $props !== false ) { 00257 $retval['props'] = array(); 00258 00259 foreach ( $props as $prop => $properties ) { 00260 $propResult = array(); 00261 if ( $prop == ApiBase::PROP_LIST ) { 00262 $listResult = $properties; 00263 continue; 00264 } 00265 if ( $prop != ApiBase::PROP_ROOT ) { 00266 $propResult['name'] = $prop; 00267 } 00268 $propResult['properties'] = array(); 00269 00270 foreach ( $properties as $name => $p ) { 00271 $propertyResult = array(); 00272 00273 $propertyResult['name'] = $name; 00274 00275 if ( !is_array( $p ) ) { 00276 $p = array( ApiBase::PROP_TYPE => $p ); 00277 } 00278 00279 $propertyResult['type'] = $p[ApiBase::PROP_TYPE]; 00280 00281 if ( is_array( $propertyResult['type'] ) ) { 00282 $propertyResult['type'] = array_values( $propertyResult['type'] ); 00283 $result->setIndexedTagName( $propertyResult['type'], 't' ); 00284 } 00285 00286 $nullable = null; 00287 if ( isset( $p[ApiBase::PROP_NULLABLE] ) ) { 00288 $nullable = $p[ApiBase::PROP_NULLABLE]; 00289 } 00290 00291 if ( $nullable === true ) { 00292 $propertyResult['nullable'] = ''; 00293 } 00294 00295 $propResult['properties'][] = $propertyResult; 00296 } 00297 00298 $result->setIndexedTagName( $propResult['properties'], 'property' ); 00299 $retval['props'][] = $propResult; 00300 } 00301 00302 // default is true for query modules, false for other modules, overriden by ApiBase::PROP_LIST 00303 if ( $listResult === true || ( $listResult !== false && $obj instanceof ApiQueryBase ) ) { 00304 $retval['listresult'] = ''; 00305 } 00306 00307 $result->setIndexedTagName( $retval['props'], 'prop' ); 00308 } 00309 00310 // Errors 00311 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() ); 00312 $result->setIndexedTagName( $retval['errors'], 'error' ); 00313 00314 return $retval; 00315 } 00316 00317 public function isReadMode() { 00318 return false; 00319 } 00320 00321 public function getAllowedParams() { 00322 $modules = array_keys( $this->getMain()->getModules() ); 00323 sort( $modules ); 00324 $querymodules = array_keys( $this->queryObj->getModules() ); 00325 sort( $querymodules ); 00326 $formatmodules = array_keys( $this->getMain()->getFormats() ); 00327 sort( $formatmodules ); 00328 return array( 00329 'modules' => array( 00330 ApiBase::PARAM_ISMULTI => true, 00331 ApiBase::PARAM_TYPE => $modules, 00332 ), 00333 'querymodules' => array( 00334 ApiBase::PARAM_ISMULTI => true, 00335 ApiBase::PARAM_TYPE => $querymodules, 00336 ), 00337 'mainmodule' => false, 00338 'pagesetmodule' => false, 00339 'formatmodules' => array( 00340 ApiBase::PARAM_ISMULTI => true, 00341 ApiBase::PARAM_TYPE => $formatmodules, 00342 ) 00343 ); 00344 } 00345 00346 public function getParamDescription() { 00347 return array( 00348 'modules' => 'List of module names (value of the action= parameter)', 00349 'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)', 00350 'mainmodule' => 'Get information about the main (top-level) module as well', 00351 'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well', 00352 'formatmodules' => 'List of format module names (value of format= parameter)', 00353 ); 00354 } 00355 00356 public function getDescription() { 00357 return 'Obtain information about certain API parameters and errors'; 00358 } 00359 00360 public function getExamples() { 00361 return array( 00362 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo' 00363 ); 00364 } 00365 00366 public function getHelpUrls() { 00367 return 'https://www.mediawiki.org/wiki/API:Parameter_information'; 00368 } 00369 00370 public function getVersion() { 00371 return __CLASS__ . ': $Id$'; 00372 } 00373 }