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