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 );
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 default :
00087 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
00088 }
00089 if(!$fit)
00090 {
00091 # Abuse siprop as a query-continue parameter
00092 # and set it to all unprocessed props
00093 $this->setContinueEnumParameter('prop', implode('|',
00094 array_diff($params['prop'], $done)));
00095 break;
00096 }
00097 $done[] = $p;
00098 }
00099 }
00100
00101 protected function appendGeneralInfo( $property ) {
00102 global $wgSitename, $wgVersion, $wgCapitalLinks, $wgRightsCode, $wgRightsText, $wgContLang;
00103 global $wgLanguageCode, $IP, $wgEnableWriteAPI, $wgLang, $wgLocaltimezone, $wgLocalTZoffset;
00104
00105 $data = array();
00106 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
00107 $data['mainpage'] = $mainPage->getPrefixedText();
00108 $data['base'] = $mainPage->getFullUrl();
00109 $data['sitename'] = $wgSitename;
00110 $data['generator'] = "MediaWiki $wgVersion";
00111
00112 $svn = SpecialVersion::getSvnRevision( $IP );
00113 if( $svn )
00114 $data['rev'] = $svn;
00115
00116 $data['case'] = $wgCapitalLinks ? 'first-letter' : 'case-sensitive';
00117
00118 if( isset( $wgRightsCode ) )
00119 $data['rightscode'] = $wgRightsCode;
00120 $data['rights'] = $wgRightsText;
00121 $data['lang'] = $wgLanguageCode;
00122 if( $wgContLang->isRTL() )
00123 $data['rtl'] = '';
00124 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
00125
00126 if( wfReadOnly() )
00127 $data['readonly'] = '';
00128 if( $wgEnableWriteAPI )
00129 $data['writeapi'] = '';
00130
00131 $tz = $wgLocaltimezone;
00132 $offset = $wgLocalTZoffset;
00133 if( is_null( $tz ) ) {
00134 $tz = 'UTC';
00135 $offset = 0;
00136 } elseif( is_null( $offset ) ) {
00137 $offset = 0;
00138 }
00139 $data['timezone'] = $tz;
00140 $data['timeoffset'] = intval($offset);
00141
00142 return $this->getResult()->addValue( 'query', $property, $data );
00143 }
00144
00145 protected function appendNamespaces( $property ) {
00146 global $wgContLang;
00147 $data = array();
00148 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
00149 {
00150 $data[$ns] = array(
00151 'id' => intval($ns)
00152 );
00153 ApiResult :: setContent( $data[$ns], $title );
00154 $canonical = MWNamespace::getCanonicalName( $ns );
00155
00156 if( MWNamespace::hasSubpages( $ns ) )
00157 $data[$ns]['subpages'] = '';
00158
00159 if( $canonical )
00160 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
00161 }
00162
00163 $this->getResult()->setIndexedTagName( $data, 'ns' );
00164 return $this->getResult()->addValue( 'query', $property, $data );
00165 }
00166
00167 protected function appendNamespaceAliases( $property ) {
00168 global $wgNamespaceAliases, $wgContLang;
00169 $wgContLang->load();
00170 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases );
00171 $namespaces = $wgContLang->getNamespaces();
00172 $data = array();
00173 foreach( $aliases as $title => $ns ) {
00174 if( $namespaces[$ns] == $title ) {
00175
00176 continue;
00177 }
00178 $item = array(
00179 'id' => intval($ns)
00180 );
00181 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
00182 $data[] = $item;
00183 }
00184
00185 $this->getResult()->setIndexedTagName( $data, 'ns' );
00186 return $this->getResult()->addValue( 'query', $property, $data );
00187 }
00188
00189 protected function appendSpecialPageAliases( $property ) {
00190 global $wgLang;
00191 $data = array();
00192 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
00193 {
00194 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
00195 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
00196 $data[] = $arr;
00197 }
00198 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
00199 return $this->getResult()->addValue( 'query', $property, $data );
00200 }
00201
00202 protected function appendMagicWords( $property ) {
00203 global $wgContLang;
00204 $data = array();
00205 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
00206 {
00207 $caseSensitive = array_shift($aliases);
00208 $arr = array('name' => $magicword, 'aliases' => $aliases);
00209 if($caseSensitive)
00210 $arr['case-sensitive'] = '';
00211 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
00212 $data[] = $arr;
00213 }
00214 $this->getResult()->setIndexedTagName($data, 'magicword');
00215 return $this->getResult()->addValue( 'query', $property, $data );
00216 }
00217
00218 protected function appendInterwikiMap( $property, $filter ) {
00219 $this->resetQueryParams();
00220 $this->addTables( 'interwiki' );
00221 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
00222
00223 if( $filter === 'local' )
00224 $this->addWhere( 'iw_local = 1' );
00225 elseif( $filter === '!local' )
00226 $this->addWhere( 'iw_local = 0' );
00227 elseif( $filter )
00228 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
00229
00230 $this->addOption( 'ORDER BY', 'iw_prefix' );
00231
00232 $db = $this->getDB();
00233 $res = $this->select( __METHOD__ );
00234
00235 $data = array();
00236 $langNames = Language::getLanguageNames();
00237 while( $row = $db->fetchObject($res) )
00238 {
00239 $val = array();
00240 $val['prefix'] = $row->iw_prefix;
00241 if( $row->iw_local == '1' )
00242 $val['local'] = '';
00243
00244 if( isset( $langNames[$row->iw_prefix] ) )
00245 $val['language'] = $langNames[$row->iw_prefix];
00246 $val['url'] = $row->iw_url;
00247
00248 $data[] = $val;
00249 }
00250 $db->freeResult( $res );
00251
00252 $this->getResult()->setIndexedTagName( $data, 'iw' );
00253 return $this->getResult()->addValue( 'query', $property, $data );
00254 }
00255
00256 protected function appendDbReplLagInfo( $property, $includeAll ) {
00257 global $wgShowHostnames;
00258 $data = array();
00259 if( $includeAll ) {
00260 if ( !$wgShowHostnames )
00261 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
00262
00263 $lb = wfGetLB();
00264 $lags = $lb->getLagTimes();
00265 foreach( $lags as $i => $lag ) {
00266 $data[] = array(
00267 'host' => $lb->getServerName( $i ),
00268 'lag' => $lag
00269 );
00270 }
00271 } else {
00272 list( $host, $lag ) = wfGetLB()->getMaxLag();
00273 $data[] = array(
00274 'host' => $wgShowHostnames ? $host : '',
00275 'lag' => intval( $lag )
00276 );
00277 }
00278
00279 $result = $this->getResult();
00280 $result->setIndexedTagName( $data, 'db' );
00281 return $this->getResult()->addValue( 'query', $property, $data );
00282 }
00283
00284 protected function appendStatistics( $property ) {
00285 global $wgDisableCounters;
00286 $data = array();
00287 $data['pages'] = intval( SiteStats::pages() );
00288 $data['articles'] = intval( SiteStats::articles() );
00289 if ( !$wgDisableCounters ) {
00290 $data['views'] = intval( SiteStats::views() );
00291 }
00292 $data['edits'] = intval( SiteStats::edits() );
00293 $data['images'] = intval( SiteStats::images() );
00294 $data['users'] = intval( SiteStats::users() );
00295 $data['activeusers'] = intval( SiteStats::activeUsers() );
00296 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
00297 $data['jobs'] = intval( SiteStats::jobs() );
00298 return $this->getResult()->addValue( 'query', $property, $data );
00299 }
00300
00301 protected function appendUserGroups( $property ) {
00302 global $wgGroupPermissions;
00303 $data = array();
00304 foreach( $wgGroupPermissions as $group => $permissions ) {
00305 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
00306 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
00307 $data[] = $arr;
00308 }
00309
00310 $this->getResult()->setIndexedTagName( $data, 'group' );
00311 return $this->getResult()->addValue( 'query', $property, $data );
00312 }
00313
00314 protected function appendFileExtensions( $property ) {
00315 global $wgFileExtensions;
00316
00317 $data = array();
00318 foreach( $wgFileExtensions as $ext ) {
00319 $data[] = array( 'ext' => $ext );
00320 }
00321 $this->getResult()->setIndexedTagName( $data, 'fe' );
00322 return $this->getResult()->addValue( 'query', $property, $data );
00323 }
00324
00325 protected function appendExtensions( $property ) {
00326 global $wgExtensionCredits;
00327 $data = array();
00328 foreach ( $wgExtensionCredits as $type => $extensions ) {
00329 foreach ( $extensions as $ext ) {
00330 $ret = array();
00331 $ret['type'] = $type;
00332 if ( isset( $ext['name'] ) )
00333 $ret['name'] = $ext['name'];
00334 if ( isset( $ext['description'] ) )
00335 $ret['description'] = $ext['description'];
00336 if ( isset( $ext['descriptionmsg'] ) )
00337 $ret['descriptionmsg'] = $ext['descriptionmsg'];
00338 if ( isset( $ext['author'] ) ) {
00339 $ret['author'] = is_array( $ext['author'] ) ?
00340 implode( ', ', $ext['author' ] ) : $ext['author'];
00341 }
00342 if ( isset( $ext['version'] ) ) {
00343 $ret['version'] = $ext['version'];
00344 } elseif ( isset( $ext['svn-revision'] ) &&
00345 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
00346 $ext['svn-revision'], $m ) )
00347 {
00348 $ret['version'] = 'r' . $m[1];
00349 }
00350 $data[] = $ret;
00351 }
00352 }
00353
00354 $this->getResult()->setIndexedTagName( $data, 'ext' );
00355 return $this->getResult()->addValue( 'query', $property, $data );
00356 }
00357
00358
00359 protected function appendRightsInfo( $property ) {
00360 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
00361 $title = Title::newFromText( $wgRightsPage );
00362 $url = $title ? $title->getFullURL() : $wgRightsUrl;
00363 $text = $wgRightsText;
00364 if( !$text && $title ) {
00365 $text = $title->getPrefixedText();
00366 }
00367
00368 $data = array(
00369 'url' => $url ? $url : '',
00370 'text' => $text ? $text : ''
00371 );
00372
00373 return $this->getResult()->addValue( 'query', $property, $data );
00374 }
00375
00376
00377 public function getAllowedParams() {
00378 return array(
00379 'prop' => array(
00380 ApiBase :: PARAM_DFLT => 'general',
00381 ApiBase :: PARAM_ISMULTI => true,
00382 ApiBase :: PARAM_TYPE => array(
00383 'general',
00384 'namespaces',
00385 'namespacealiases',
00386 'specialpagealiases',
00387 'magicwords',
00388 'interwikimap',
00389 'dbrepllag',
00390 'statistics',
00391 'usergroups',
00392 'extensions',
00393 'fileextensions',
00394 'rightsinfo',
00395 )
00396 ),
00397 'filteriw' => array(
00398 ApiBase :: PARAM_TYPE => array(
00399 'local',
00400 '!local',
00401 )
00402 ),
00403 'showalldb' => false,
00404 );
00405 }
00406
00407 public function getParamDescription() {
00408 return array(
00409 'prop' => array(
00410 'Which sysinfo properties to get:',
00411 ' general - Overall system information',
00412 ' namespaces - List of registered namespaces and their canonical names',
00413 ' namespacealiases - List of registered namespace aliases',
00414 ' specialpagealiases - List of special page aliases',
00415 ' magicwords - List of magic words and their aliases',
00416 ' statistics - Returns site statistics',
00417 ' interwikimap - Returns interwiki map (optionally filtered)',
00418 ' dbrepllag - Returns database server with the highest replication lag',
00419 ' usergroups - Returns user groups and the associated permissions',
00420 ' extensions - Returns extensions installed on the wiki',
00421 ' fileextensions - Returns list of file extensions allowed to be uploaded',
00422 ' rightsinfo - Returns wiki rights (license) information if available',
00423 ),
00424 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
00425 'showalldb' => 'List all database servers, not just the one lagging the most',
00426 );
00427 }
00428
00429 public function getDescription() {
00430 return 'Return general information about the site.';
00431 }
00432
00433 protected function getExamples() {
00434 return array(
00435 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
00436 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
00437 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
00438 );
00439 }
00440
00441 public function getVersion() {
00442 return __CLASS__ . ': $Id: ApiQuerySiteinfo.php 48060 2009-03-05 13:52:14Z demon $';
00443 }
00444 }