MediaWiki
REL1_24
|
00001 <?php 00030 class ApiParamInfo extends ApiBase { 00031 00035 protected $queryObj; 00036 00037 public function __construct( ApiMain $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 ( $n === 'token' && $obj->needsToken() ) { 00202 $a['tokentype'] = $obj->needsToken(); 00203 } 00204 00205 if ( isset( $p[ApiBase::PARAM_DFLT] ) ) { 00206 $type = $p[ApiBase::PARAM_TYPE]; 00207 if ( $type === 'boolean' ) { 00208 $a['default'] = ( $p[ApiBase::PARAM_DFLT] ? 'true' : 'false' ); 00209 } elseif ( $type === 'string' ) { 00210 $a['default'] = strval( $p[ApiBase::PARAM_DFLT] ); 00211 } elseif ( $type === 'integer' ) { 00212 $a['default'] = intval( $p[ApiBase::PARAM_DFLT] ); 00213 } else { 00214 $a['default'] = $p[ApiBase::PARAM_DFLT]; 00215 } 00216 } 00217 if ( isset( $p[ApiBase::PARAM_ISMULTI] ) && $p[ApiBase::PARAM_ISMULTI] ) { 00218 $a['multi'] = ''; 00219 $a['limit'] = $this->getMain()->canApiHighLimits() ? 00220 ApiBase::LIMIT_SML2 : 00221 ApiBase::LIMIT_SML1; 00222 $a['lowlimit'] = ApiBase::LIMIT_SML1; 00223 $a['highlimit'] = ApiBase::LIMIT_SML2; 00224 } 00225 00226 if ( isset( $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) && $p[ApiBase::PARAM_ALLOW_DUPLICATES] ) { 00227 $a['allowsduplicates'] = ''; 00228 } 00229 00230 if ( isset( $p[ApiBase::PARAM_TYPE] ) ) { 00231 if ( $p[ApiBase::PARAM_TYPE] === 'submodule' ) { 00232 $a['type'] = $obj->getModuleManager()->getNames( $n ); 00233 sort( $a['type'] ); 00234 $a['submodules'] = ''; 00235 } else { 00236 $a['type'] = $p[ApiBase::PARAM_TYPE]; 00237 } 00238 if ( is_array( $a['type'] ) ) { 00239 // To prevent sparse arrays from being serialized to JSON as objects 00240 $a['type'] = array_values( $a['type'] ); 00241 $result->setIndexedTagName( $a['type'], 't' ); 00242 } 00243 } 00244 if ( isset( $p[ApiBase::PARAM_MAX] ) ) { 00245 $a['max'] = $p[ApiBase::PARAM_MAX]; 00246 } 00247 if ( isset( $p[ApiBase::PARAM_MAX2] ) ) { 00248 $a['highmax'] = $p[ApiBase::PARAM_MAX2]; 00249 } 00250 if ( isset( $p[ApiBase::PARAM_MIN] ) ) { 00251 $a['min'] = $p[ApiBase::PARAM_MIN]; 00252 } 00253 $retval['parameters'][] = $a; 00254 } 00255 $result->setIndexedTagName( $retval['parameters'], 'param' ); 00256 00257 return $retval; 00258 } 00259 00260 public function isReadMode() { 00261 return false; 00262 } 00263 00264 public function getAllowedParams() { 00265 $modules = $this->getMain()->getModuleManager()->getNames( 'action' ); 00266 sort( $modules ); 00267 $querymodules = $this->queryObj->getModuleManager()->getNames(); 00268 sort( $querymodules ); 00269 $formatmodules = $this->getMain()->getModuleManager()->getNames( 'format' ); 00270 sort( $formatmodules ); 00271 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 ' . 00296 '(providing titles= and friends) as well', 00297 'formatmodules' => 'List of format module names (value of format= parameter)', 00298 ); 00299 } 00300 00301 public function getDescription() { 00302 return 'Obtain information about certain API parameters and errors.'; 00303 } 00304 00305 public function getExamples() { 00306 return array( 00307 'api.php?action=paraminfo&modules=parse&querymodules=allpages|siteinfo' 00308 ); 00309 } 00310 00311 public function getHelpUrls() { 00312 return 'https://www.mediawiki.org/wiki/API:Parameter_information'; 00313 } 00314 }