MediaWiki  REL1_23
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                     ApiResult::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                     ApiResult::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                     // To prevent sparse arrays from being serialized to JSON as objects
00230                     $a['type'] = array_values( $a['type'] );
00231                     $result->setIndexedTagName( $a['type'], 't' );
00232                 }
00233             }
00234             if ( isset( $p[ApiBase::PARAM_MAX] ) ) {
00235                 $a['max'] = $p[ApiBase::PARAM_MAX];
00236             }
00237             if ( isset( $p[ApiBase::PARAM_MAX2] ) ) {
00238                 $a['highmax'] = $p[ApiBase::PARAM_MAX2];
00239             }
00240             if ( isset( $p[ApiBase::PARAM_MIN] ) ) {
00241                 $a['min'] = $p[ApiBase::PARAM_MIN];
00242             }
00243             $retval['parameters'][] = $a;
00244         }
00245         $result->setIndexedTagName( $retval['parameters'], 'param' );
00246 
00247         $props = $obj->getFinalResultProperties();
00248         $listResult = null;
00249         if ( $props !== false ) {
00250             $retval['props'] = array();
00251 
00252             foreach ( $props as $prop => $properties ) {
00253                 $propResult = array();
00254                 if ( $prop == ApiBase::PROP_LIST ) {
00255                     $listResult = $properties;
00256                     continue;
00257                 }
00258                 if ( $prop != ApiBase::PROP_ROOT ) {
00259                     $propResult['name'] = $prop;
00260                 }
00261                 $propResult['properties'] = array();
00262 
00263                 foreach ( $properties as $name => $p ) {
00264                     $propertyResult = array();
00265 
00266                     $propertyResult['name'] = $name;
00267 
00268                     if ( !is_array( $p ) ) {
00269                         $p = array( ApiBase::PROP_TYPE => $p );
00270                     }
00271 
00272                     $propertyResult['type'] = $p[ApiBase::PROP_TYPE];
00273 
00274                     if ( is_array( $propertyResult['type'] ) ) {
00275                         $propertyResult['type'] = array_values( $propertyResult['type'] );
00276                         $result->setIndexedTagName( $propertyResult['type'], 't' );
00277                     }
00278 
00279                     $nullable = null;
00280                     if ( isset( $p[ApiBase::PROP_NULLABLE] ) ) {
00281                         $nullable = $p[ApiBase::PROP_NULLABLE];
00282                     }
00283 
00284                     if ( $nullable === true ) {
00285                         $propertyResult['nullable'] = '';
00286                     }
00287 
00288                     $propResult['properties'][] = $propertyResult;
00289                 }
00290 
00291                 $result->setIndexedTagName( $propResult['properties'], 'property' );
00292                 $retval['props'][] = $propResult;
00293             }
00294 
00295             // default is true for query modules, false for other modules, overridden by ApiBase::PROP_LIST
00296             if ( $listResult === true || ( $listResult !== false && $obj instanceof ApiQueryBase ) ) {
00297                 $retval['listresult'] = '';
00298             }
00299 
00300             $result->setIndexedTagName( $retval['props'], 'prop' );
00301         }
00302 
00303         // Errors
00304         $retval['errors'] = $this->parseErrors( $obj->getFinalPossibleErrors() );
00305         $result->setIndexedTagName( $retval['errors'], 'error' );
00306 
00307         return $retval;
00308     }
00309 
00310     public function isReadMode() {
00311         return false;
00312     }
00313 
00314     public function getAllowedParams() {
00315         $modules = $this->getMain()->getModuleManager()->getNames( 'action' );
00316         sort( $modules );
00317         $querymodules = $this->queryObj->getModuleManager()->getNames();
00318         sort( $querymodules );
00319         $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' );
00320         sort( $formatmodules );
00321 
00322         return array(
00323             'modules' => array(
00324                 ApiBase::PARAM_ISMULTI => true,
00325                 ApiBase::PARAM_TYPE => $modules,
00326             ),
00327             'querymodules' => array(
00328                 ApiBase::PARAM_ISMULTI => true,
00329                 ApiBase::PARAM_TYPE => $querymodules,
00330             ),
00331             'mainmodule' => false,
00332             'pagesetmodule' => false,
00333             'formatmodules' => array(
00334                 ApiBase::PARAM_ISMULTI => true,
00335                 ApiBase::PARAM_TYPE => $formatmodules,
00336             )
00337         );
00338     }
00339 
00340     public function getParamDescription() {
00341         return array(
00342             'modules' => 'List of module names (value of the action= parameter)',
00343             'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
00344             'mainmodule' => 'Get information about the main (top-level) module as well',
00345             'pagesetmodule' => 'Get information about the pageset module ' .
00346                 '(providing titles= and friends) as well',
00347             'formatmodules' => 'List of format module names (value of format= parameter)',
00348         );
00349     }
00350 
00351     public function getDescription() {
00352         return 'Obtain information about certain API parameters and errors.';
00353     }
00354 
00355     public function getExamples() {
00356         return array(
00357             'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
00358         );
00359     }
00360 
00361     public function getHelpUrls() {
00362         return 'https://www.mediawiki.org/wiki/API:Parameter_information';
00363     }
00364 }