00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if ( !defined( 'MEDIAWIKI' ) ) {
00027
00028 require_once( 'ApiQueryBase.php' );
00029 }
00030
00036 class ApiQuerySiteinfo extends ApiQueryBase {
00037
00038 public function __construct( $query, $moduleName ) {
00039 parent :: __construct( $query, $moduleName, 'si' );
00040 }
00041
00042 public function execute() {
00043 $params = $this->extractRequestParams();
00044 $done = array();
00045 foreach ( $params['prop'] as $p )
00046 {
00047 switch ( $p )
00048 {
00049 case 'general':
00050 $fit = $this->appendGeneralInfo( $p );
00051 break;
00052 case 'namespaces':
00053 $fit = $this->appendNamespaces( $p );
00054 break;
00055 case 'namespacealiases':
00056 $fit = $this->appendNamespaceAliases( $p );
00057 break;
00058 case 'specialpagealiases':
00059 $fit = $this->appendSpecialPageAliases( $p );
00060 break;
00061 case 'magicwords':
00062 $fit = $this->appendMagicWords( $p );
00063 break;
00064 case 'interwikimap':
00065 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
00066 $fit = $this->appendInterwikiMap( $p, $filteriw );
00067 break;
00068 case 'dbrepllag':
00069 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
00070 break;
00071 case 'statistics':
00072 $fit = $this->appendStatistics( $p );
00073 break;
00074 case 'usergroups':
00075 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
00076 break;
00077 case 'extensions':
00078 $fit = $this->appendExtensions( $p );
00079 break;
00080 case 'fileextensions':
00081 $fit = $this->appendFileExtensions( $p );
00082 break;
00083 case 'rightsinfo':
00084 $fit = $this->appendRightsInfo( $p );
00085 break;
00086 case 'languages':
00087 $fit = $this->appendLanguages( $p );
00088 break;
00089 default :
00090 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
00091 }
00092 if ( !$fit )
00093 {
00094
00095
00096 $this->setContinueEnumParameter( 'prop', implode( '|',
00097 array_diff( $params['prop'], $done ) ) );
00098 break;
00099 }
00100 $done[] = $p;
00101 }
00102 }
00103
00104 protected function appendGeneralInfo( $property ) {
00105 global $wgContLang;
00106 global $wgLang;
00107
00108 $data = array();
00109 $mainPage = Title :: newFromText( wfMsgForContent( 'mainpage' ) );
00110 $data['mainpage'] = $mainPage->getPrefixedText();
00111 $data['base'] = $mainPage->getFullUrl();
00112 $data['sitename'] = $GLOBALS['wgSitename'];
00113 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
00114 $data['phpversion'] = phpversion();
00115 $data['phpsapi'] = php_sapi_name();
00116 $data['dbtype'] = $GLOBALS['wgDBtype'];
00117 $data['dbversion'] = $this->getDB()->getServerVersion();
00118
00119 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
00120 if ( $svn )
00121 $data['rev'] = $svn;
00122
00123
00124 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
00125
00126 if ( isset( $GLOBALS['wgRightsCode'] ) )
00127 $data['rightscode'] = $GLOBALS['wgRightsCode'];
00128 $data['rights'] = $GLOBALS['wgRightsText'];
00129 $data['lang'] = $GLOBALS['wgLanguageCode'];
00130 if ( $wgContLang->isRTL() )
00131 $data['rtl'] = '';
00132 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
00133
00134 if ( wfReadOnly() ) {
00135 $data['readonly'] = '';
00136 $data['readonlyreason'] = wfReadOnlyReason();
00137 }
00138 if ( $GLOBALS['wgEnableWriteAPI'] )
00139 $data['writeapi'] = '';
00140
00141 $tz = $GLOBALS['wgLocaltimezone'];
00142 $offset = $GLOBALS['wgLocalTZoffset'];
00143 if ( is_null( $tz ) ) {
00144 $tz = 'UTC';
00145 $offset = 0;
00146 } elseif ( is_null( $offset ) ) {
00147 $offset = 0;
00148 }
00149 $data['timezone'] = $tz;
00150 $data['timeoffset'] = intval( $offset );
00151 $data['articlepath'] = $GLOBALS['wgArticlePath'];
00152 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
00153 $data['script'] = $GLOBALS['wgScript'];
00154 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
00155 $data['server'] = $GLOBALS['wgServer'];
00156 $data['wikiid'] = wfWikiID();
00157 $data['time'] = wfTimestamp( TS_ISO_8601, time() );
00158
00159 return $this->getResult()->addValue( 'query', $property, $data );
00160 }
00161
00162 protected function appendNamespaces( $property ) {
00163 global $wgContLang;
00164 $data = array();
00165 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title )
00166 {
00167 $data[$ns] = array(
00168 'id' => intval( $ns ),
00169 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
00170 );
00171 ApiResult :: setContent( $data[$ns], $title );
00172 $canonical = MWNamespace::getCanonicalName( $ns );
00173
00174 if ( MWNamespace::hasSubpages( $ns ) )
00175 $data[$ns]['subpages'] = '';
00176
00177 if ( $canonical )
00178 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
00179
00180 if ( MWNamespace::isContent( $ns ) )
00181 $data[$ns]['content'] = '';
00182 }
00183
00184 $this->getResult()->setIndexedTagName( $data, 'ns' );
00185 return $this->getResult()->addValue( 'query', $property, $data );
00186 }
00187
00188 protected function appendNamespaceAliases( $property ) {
00189 global $wgNamespaceAliases, $wgContLang;
00190 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
00191 $namespaces = $wgContLang->getNamespaces();
00192 $data = array();
00193 foreach ( $aliases as $title => $ns ) {
00194 if ( $namespaces[$ns] == $title ) {
00195
00196 continue;
00197 }
00198 $item = array(
00199 'id' => intval( $ns )
00200 );
00201 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
00202 $data[] = $item;
00203 }
00204
00205 $this->getResult()->setIndexedTagName( $data, 'ns' );
00206 return $this->getResult()->addValue( 'query', $property, $data );
00207 }
00208
00209 protected function appendSpecialPageAliases( $property ) {
00210 global $wgLang;
00211 $data = array();
00212 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
00213 {
00214 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
00215 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
00216 $data[] = $arr;
00217 }
00218 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
00219 return $this->getResult()->addValue( 'query', $property, $data );
00220 }
00221
00222 protected function appendMagicWords( $property ) {
00223 global $wgContLang;
00224 $data = array();
00225 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
00226 {
00227 $caseSensitive = array_shift( $aliases );
00228 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
00229 if ( $caseSensitive )
00230 $arr['case-sensitive'] = '';
00231 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
00232 $data[] = $arr;
00233 }
00234 $this->getResult()->setIndexedTagName( $data, 'magicword' );
00235 return $this->getResult()->addValue( 'query', $property, $data );
00236 }
00237
00238 protected function appendInterwikiMap( $property, $filter ) {
00239 $this->resetQueryParams();
00240 $this->addTables( 'interwiki' );
00241 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
00242
00243 if ( $filter === 'local' )
00244 $this->addWhere( 'iw_local = 1' );
00245 elseif ( $filter === '!local' )
00246 $this->addWhere( 'iw_local = 0' );
00247 elseif ( $filter )
00248 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
00249
00250 $this->addOption( 'ORDER BY', 'iw_prefix' );
00251
00252 $db = $this->getDB();
00253 $res = $this->select( __METHOD__ );
00254
00255 $data = array();
00256 $langNames = Language::getLanguageNames();
00257 while ( $row = $db->fetchObject( $res ) )
00258 {
00259 $val = array();
00260 $val['prefix'] = $row->iw_prefix;
00261 if ( $row->iw_local == '1' )
00262 $val['local'] = '';
00263
00264 if ( isset( $langNames[$row->iw_prefix] ) )
00265 $val['language'] = $langNames[$row->iw_prefix];
00266 $val['url'] = $row->iw_url;
00267
00268 $data[] = $val;
00269 }
00270 $db->freeResult( $res );
00271
00272 $this->getResult()->setIndexedTagName( $data, 'iw' );
00273 return $this->getResult()->addValue( 'query', $property, $data );
00274 }
00275
00276 protected function appendDbReplLagInfo( $property, $includeAll ) {
00277 global $wgShowHostnames;
00278 $data = array();
00279 if ( $includeAll ) {
00280 if ( !$wgShowHostnames )
00281 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
00282
00283 $lb = wfGetLB();
00284 $lags = $lb->getLagTimes();
00285 foreach ( $lags as $i => $lag ) {
00286 $data[] = array(
00287 'host' => $lb->getServerName( $i ),
00288 'lag' => $lag
00289 );
00290 }
00291 } else {
00292 list( $host, $lag ) = wfGetLB()->getMaxLag();
00293 $data[] = array(
00294 'host' => $wgShowHostnames ? $host : '',
00295 'lag' => intval( $lag )
00296 );
00297 }
00298
00299 $result = $this->getResult();
00300 $result->setIndexedTagName( $data, 'db' );
00301 return $this->getResult()->addValue( 'query', $property, $data );
00302 }
00303
00304 protected function appendStatistics( $property ) {
00305 global $wgDisableCounters;
00306 $data = array();
00307 $data['pages'] = intval( SiteStats::pages() );
00308 $data['articles'] = intval( SiteStats::articles() );
00309 if ( !$wgDisableCounters ) {
00310 $data['views'] = intval( SiteStats::views() );
00311 }
00312 $data['edits'] = intval( SiteStats::edits() );
00313 $data['images'] = intval( SiteStats::images() );
00314 $data['users'] = intval( SiteStats::users() );
00315 $data['activeusers'] = intval( SiteStats::activeUsers() );
00316 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
00317 $data['jobs'] = intval( SiteStats::jobs() );
00318 return $this->getResult()->addValue( 'query', $property, $data );
00319 }
00320
00321 protected function appendUserGroups( $property, $numberInGroup ) {
00322 global $wgGroupPermissions;
00323 $data = array();
00324 foreach ( $wgGroupPermissions as $group => $permissions ) {
00325 $arr = array(
00326 'name' => $group,
00327 'rights' => array_keys( $permissions, true ),
00328 );
00329 if ( $numberInGroup )
00330 $arr['number'] = SiteStats::numberInGroup( $group );
00331
00332 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
00333 $data[] = $arr;
00334 }
00335
00336 $this->getResult()->setIndexedTagName( $data, 'group' );
00337 return $this->getResult()->addValue( 'query', $property, $data );
00338 }
00339
00340 protected function appendFileExtensions( $property ) {
00341 global $wgFileExtensions;
00342
00343 $data = array();
00344 foreach ( $wgFileExtensions as $ext ) {
00345 $data[] = array( 'ext' => $ext );
00346 }
00347 $this->getResult()->setIndexedTagName( $data, 'fe' );
00348 return $this->getResult()->addValue( 'query', $property, $data );
00349 }
00350
00351 protected function appendExtensions( $property ) {
00352 global $wgExtensionCredits;
00353 $data = array();
00354 foreach ( $wgExtensionCredits as $type => $extensions ) {
00355 foreach ( $extensions as $ext ) {
00356 $ret = array();
00357 $ret['type'] = $type;
00358 if ( isset( $ext['name'] ) )
00359 $ret['name'] = $ext['name'];
00360 if ( isset( $ext['description'] ) )
00361 $ret['description'] = $ext['description'];
00362 if ( isset( $ext['descriptionmsg'] ) ) {
00363
00364 if ( is_array( $ext['descriptionmsg'] ) ) {
00365 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
00366 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
00367 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
00368 } else {
00369 $ret['descriptionmsg'] = $ext['descriptionmsg'];
00370 }
00371 }
00372 if ( isset( $ext['author'] ) ) {
00373 $ret['author'] = is_array( $ext['author'] ) ?
00374 implode( ', ', $ext['author' ] ) : $ext['author'];
00375 }
00376 if ( isset( $ext['url'] ) ) {
00377 $ret['url'] = $ext['url'];
00378 }
00379 if ( isset( $ext['version'] ) ) {
00380 $ret['version'] = $ext['version'];
00381 } elseif ( isset( $ext['svn-revision'] ) &&
00382 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
00383 $ext['svn-revision'], $m ) )
00384 {
00385 $ret['version'] = 'r' . $m[1];
00386 }
00387 $data[] = $ret;
00388 }
00389 }
00390
00391 $this->getResult()->setIndexedTagName( $data, 'ext' );
00392 return $this->getResult()->addValue( 'query', $property, $data );
00393 }
00394
00395
00396 protected function appendRightsInfo( $property ) {
00397 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
00398 $title = Title::newFromText( $wgRightsPage );
00399 $url = $title ? $title->getFullURL() : $wgRightsUrl;
00400 $text = $wgRightsText;
00401 if ( !$text && $title ) {
00402 $text = $title->getPrefixedText();
00403 }
00404
00405 $data = array(
00406 'url' => $url ? $url : '',
00407 'text' => $text ? $text : ''
00408 );
00409
00410 return $this->getResult()->addValue( 'query', $property, $data );
00411 }
00412
00413 public function appendLanguages( $property ) {
00414 $data = array();
00415 foreach ( Language::getLanguageNames() as $code => $name ) {
00416 $lang = array( 'code' => $code );
00417 ApiResult::setContent( $lang, $name );
00418 $data[] = $lang;
00419 }
00420 $this->getResult()->setIndexedTagName( $data, 'lang' );
00421 return $this->getResult()->addValue( 'query', $property, $data );
00422 }
00423
00424 public function getCacheMode( $params ) {
00425 return 'public';
00426 }
00427
00428 public function getAllowedParams() {
00429 return array(
00430 'prop' => array(
00431 ApiBase :: PARAM_DFLT => 'general',
00432 ApiBase :: PARAM_ISMULTI => true,
00433 ApiBase :: PARAM_TYPE => array(
00434 'general',
00435 'namespaces',
00436 'namespacealiases',
00437 'specialpagealiases',
00438 'magicwords',
00439 'interwikimap',
00440 'dbrepllag',
00441 'statistics',
00442 'usergroups',
00443 'extensions',
00444 'fileextensions',
00445 'rightsinfo',
00446 'languages',
00447 )
00448 ),
00449 'filteriw' => array(
00450 ApiBase :: PARAM_TYPE => array(
00451 'local',
00452 '!local',
00453 )
00454 ),
00455 'showalldb' => false,
00456 'numberingroup' => false,
00457 );
00458 }
00459
00460 public function getParamDescription() {
00461 return array(
00462 'prop' => array(
00463 'Which sysinfo properties to get:',
00464 ' general - Overall system information',
00465 ' namespaces - List of registered namespaces and their canonical names',
00466 ' namespacealiases - List of registered namespace aliases',
00467 ' specialpagealiases - List of special page aliases',
00468 ' magicwords - List of magic words and their aliases',
00469 ' statistics - Returns site statistics',
00470 ' interwikimap - Returns interwiki map (optionally filtered)',
00471 ' dbrepllag - Returns database server with the highest replication lag',
00472 ' usergroups - Returns user groups and the associated permissions',
00473 ' extensions - Returns extensions installed on the wiki',
00474 ' fileextensions - Returns list of file extensions allowed to be uploaded',
00475 ' rightsinfo - Returns wiki rights (license) information if available',
00476 ' languages - Returns a list of languages MediaWiki supports',
00477 ),
00478 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
00479 'showalldb' => 'List all database servers, not just the one lagging the most',
00480 'numberingroup' => 'Lists the number of users in user groups',
00481 );
00482 }
00483
00484 public function getDescription() {
00485 return 'Return general information about the site.';
00486 }
00487
00488 public function getPossibleErrors() {
00489 return array_merge( parent::getPossibleErrors(), array(
00490 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
00491 ) );
00492 }
00493
00494 protected function getExamples() {
00495 return array(
00496 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
00497 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
00498 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
00499 );
00500 }
00501
00502 public function getVersion() {
00503 return __CLASS__ . ': $Id: ApiQuerySiteinfo.php 69932 2010-07-26 08:03:21Z tstarling $';
00504 }
00505 }