MediaWiki  REL1_19
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                 $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                 // Errors
00255                 $retval['errors'] = $this->parseErrors( $obj->getPossibleErrors() );
00256                 $result->setIndexedTagName( $retval['errors'], 'error' );
00257 
00258                 return $retval;
00259         }
00260 
00261         public function isReadMode() {
00262                 return false;
00263         }
00264 
00265         public function getAllowedParams() {
00266                 $modules = array_keys( $this->getMain()->getModules() );
00267                 sort( $modules );
00268                 $querymodules = array_keys( $this->queryObj->getModules() );
00269                 sort( $querymodules );
00270                 $formatmodules = array_keys( $this->getMain()->getFormats() );
00271                 sort( $formatmodules );
00272                 return array(
00273                         'modules' => array(
00274                                 ApiBase::PARAM_ISMULTI => true,
00275                                 ApiBase::PARAM_TYPE => $modules,
00276                         ),
00277                         'querymodules' => array(
00278                                 ApiBase::PARAM_ISMULTI => true,
00279                                 ApiBase::PARAM_TYPE => $querymodules,
00280                         ),
00281                         'mainmodule' => false,
00282                         'pagesetmodule' => false,
00283                         'formatmodules' => array(
00284                                 ApiBase::PARAM_ISMULTI => true,
00285                                 ApiBase::PARAM_TYPE => $formatmodules,
00286                         )
00287                 );
00288         }
00289 
00290         public function getParamDescription() {
00291                 return array(
00292                         'modules' => 'List of module names (value of the action= parameter)',
00293                         'querymodules' => 'List of query module names (value of prop=, meta= or list= parameter)',
00294                         'mainmodule' => 'Get information about the main (top-level) module as well',
00295                         'pagesetmodule' => 'Get information about the pageset module (providing titles= and friends) as well',
00296                         'formatmodules' => 'List of format module names (value of format= parameter)',
00297                 );
00298         }
00299 
00300         public function getDescription() {
00301                 return 'Obtain information about certain API parameters and errors';
00302         }
00303 
00304         public function getExamples() {
00305                 return array(
00306                         'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo'
00307                 );
00308         }
00309 
00310         public function getHelpUrls() {
00311                 return 'https://www.mediawiki.org/wiki/API:Parameter_information';
00312         }
00313 
00314         public function getVersion() {
00315                 return __CLASS__ . ': $Id$';
00316         }
00317 }