13550d6c27a1204568a131d735d648eaf60cb364
5 * Created on Sep 25, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * A query action to return meta information about the wiki site.
37 class ApiQuerySiteinfo
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'si' );
43 public function execute() {
44 $params = $this->extractRequestParams();
46 foreach ( $params['prop'] as $p ) {
49 $fit = $this->appendGeneralInfo( $p );
52 $fit = $this->appendNamespaces( $p );
54 case 'namespacealiases':
55 $fit = $this->appendNamespaceAliases( $p );
57 case 'specialpagealiases':
58 $fit = $this->appendSpecialPageAliases( $p );
61 $fit = $this->appendMagicWords( $p );
64 $filteriw = isset( $params['filteriw'] ) ?
$params['filteriw'] : false;
65 $fit = $this->appendInterwikiMap( $p, $filteriw );
68 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
71 $fit = $this->appendStatistics( $p );
74 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
77 $fit = $this->appendExtensions( $p );
79 case 'fileextensions':
80 $fit = $this->appendFileExtensions( $p );
83 $fit = $this->appendRightsInfo( $p );
86 $fit = $this->appendLanguages( $p );
89 $fit = $this->appendSkins( $p );
92 $fit = $this->appendExtensionTags( $p );
95 $fit = $this->appendFunctionHooks( $p );
98 ApiBase
::dieDebug( __METHOD__
, "Unknown prop=$p" );
101 // Abuse siprop as a query-continue parameter
102 // and set it to all unprocessed props
103 $this->setContinueEnumParameter( 'prop', implode( '|',
104 array_diff( $params['prop'], $done ) ) );
111 protected function appendGeneralInfo( $property ) {
115 $mainPage = Title
::newMainPage();
116 $data['mainpage'] = $mainPage->getPrefixedText();
117 $data['base'] = $mainPage->getFullUrl();
118 $data['sitename'] = $GLOBALS['wgSitename'];
119 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
120 $data['phpversion'] = phpversion();
121 $data['phpsapi'] = php_sapi_name();
122 $data['dbtype'] = $GLOBALS['wgDBtype'];
123 $data['dbversion'] = $this->getDB()->getServerVersion();
125 $svn = SpecialVersion
::getSvnRevision( $GLOBALS['IP'] );
130 // 'case-insensitive' option is reserved for future
131 $data['case'] = $GLOBALS['wgCapitalLinks'] ?
'first-letter' : 'case-sensitive';
133 if ( isset( $GLOBALS['wgRightsCode'] ) ) {
134 $data['rightscode'] = $GLOBALS['wgRightsCode'];
136 $data['rights'] = $GLOBALS['wgRightsText'];
137 $data['lang'] = $GLOBALS['wgLanguageCode'];
138 if ( $wgContLang->isRTL() ) {
141 $data['fallback8bitEncoding'] = $wgContLang->fallback8bitEncoding();
143 if ( wfReadOnly() ) {
144 $data['readonly'] = '';
145 $data['readonlyreason'] = wfReadOnlyReason();
147 if ( $GLOBALS['wgEnableWriteAPI'] ) {
148 $data['writeapi'] = '';
151 $tz = $GLOBALS['wgLocaltimezone'];
152 $offset = $GLOBALS['wgLocalTZoffset'];
153 if ( is_null( $tz ) ) {
156 } elseif ( is_null( $offset ) ) {
159 $data['timezone'] = $tz;
160 $data['timeoffset'] = intval( $offset );
161 $data['articlepath'] = $GLOBALS['wgArticlePath'];
162 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
163 $data['script'] = $GLOBALS['wgScript'];
164 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
165 $data['server'] = $GLOBALS['wgServer'];
166 $data['wikiid'] = wfWikiID();
167 $data['time'] = wfTimestamp( TS_ISO_8601
, time() );
169 if ( $GLOBALS['wgMiserMode'] ) {
170 $data['misermode'] = '';
173 wfRunHooks( 'APIQuerySiteInfoGeneralInfo', array( $this, &$data ) );
175 return $this->getResult()->addValue( 'query', $property, $data );
178 protected function appendNamespaces( $property ) {
181 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title ) {
183 'id' => intval( $ns ),
184 'case' => MWNamespace
::isCapitalized( $ns ) ?
'first-letter' : 'case-sensitive',
186 ApiResult
::setContent( $data[$ns], $title );
187 $canonical = MWNamespace
::getCanonicalName( $ns );
189 if ( MWNamespace
::hasSubpages( $ns ) ) {
190 $data[$ns]['subpages'] = '';
194 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
197 if ( MWNamespace
::isContent( $ns ) ) {
198 $data[$ns]['content'] = '';
202 $this->getResult()->setIndexedTagName( $data, 'ns' );
203 return $this->getResult()->addValue( 'query', $property, $data );
206 protected function appendNamespaceAliases( $property ) {
207 global $wgNamespaceAliases, $wgContLang;
208 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
209 $namespaces = $wgContLang->getNamespaces();
211 foreach ( $aliases as $title => $ns ) {
212 if ( $namespaces[$ns] == $title ) {
213 // Don't list duplicates
217 'id' => intval( $ns )
219 ApiResult
::setContent( $item, strtr( $title, '_', ' ' ) );
223 $this->getResult()->setIndexedTagName( $data, 'ns' );
224 return $this->getResult()->addValue( 'query', $property, $data );
227 protected function appendSpecialPageAliases( $property ) {
230 foreach ( $wgContLang->getSpecialPageAliases() as $specialpage => $aliases ) {
231 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
232 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
235 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
236 return $this->getResult()->addValue( 'query', $property, $data );
239 protected function appendMagicWords( $property ) {
242 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases ) {
243 $caseSensitive = array_shift( $aliases );
244 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
245 if ( $caseSensitive ) {
246 $arr['case-sensitive'] = '';
248 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
251 $this->getResult()->setIndexedTagName( $data, 'magicword' );
252 return $this->getResult()->addValue( 'query', $property, $data );
255 protected function appendInterwikiMap( $property, $filter ) {
256 $this->resetQueryParams();
257 $this->addTables( 'interwiki' );
258 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url', 'iw_wikiid', 'iw_api' ) );
260 if ( $filter === 'local' ) {
261 $this->addWhere( 'iw_local = 1' );
262 } elseif ( $filter === '!local' ) {
263 $this->addWhere( 'iw_local = 0' );
264 } elseif ( $filter ) {
265 ApiBase
::dieDebug( __METHOD__
, "Unknown filter=$filter" );
268 $this->addOption( 'ORDER BY', 'iw_prefix' );
270 $res = $this->select( __METHOD__
);
273 $langNames = Language
::getLanguageNames();
274 foreach ( $res as $row ) {
276 $val['prefix'] = $row->iw_prefix
;
277 if ( $row->iw_local
== '1' ) {
280 // $val['trans'] = intval( $row->iw_trans ); // should this be exposed?
281 if ( isset( $langNames[$row->iw_prefix
] ) ) {
282 $val['language'] = $langNames[$row->iw_prefix
];
284 $val['url'] = $row->iw_url
;
285 $val['wikiid'] = $row->iw_wikiid
;
286 $val['api'] = $row->iw_api
;
291 $this->getResult()->setIndexedTagName( $data, 'iw' );
292 return $this->getResult()->addValue( 'query', $property, $data );
295 protected function appendDbReplLagInfo( $property, $includeAll ) {
296 global $wgShowHostnames;
299 if ( !$wgShowHostnames ) {
300 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
304 $lags = $lb->getLagTimes();
305 foreach ( $lags as $i => $lag ) {
307 'host' => $lb->getServerName( $i ),
312 list( $host, $lag ) = wfGetLB()->getMaxLag();
314 'host' => $wgShowHostnames ?
$host : '',
315 'lag' => intval( $lag )
319 $result = $this->getResult();
320 $result->setIndexedTagName( $data, 'db' );
321 return $this->getResult()->addValue( 'query', $property, $data );
324 protected function appendStatistics( $property ) {
325 global $wgDisableCounters;
327 $data['pages'] = intval( SiteStats
::pages() );
328 $data['articles'] = intval( SiteStats
::articles() );
329 if ( !$wgDisableCounters ) {
330 $data['views'] = intval( SiteStats
::views() );
332 $data['edits'] = intval( SiteStats
::edits() );
333 $data['images'] = intval( SiteStats
::images() );
334 $data['users'] = intval( SiteStats
::users() );
335 $data['activeusers'] = intval( SiteStats
::activeUsers() );
336 $data['admins'] = intval( SiteStats
::numberingroup( 'sysop' ) );
337 $data['jobs'] = intval( SiteStats
::jobs() );
338 return $this->getResult()->addValue( 'query', $property, $data );
341 protected function appendUserGroups( $property, $numberInGroup ) {
342 global $wgGroupPermissions, $wgAddGroups, $wgRemoveGroups, $wgGroupsAddToSelf, $wgGroupsRemoveFromSelf;
345 foreach ( $wgGroupPermissions as $group => $permissions ) {
348 'rights' => array_keys( $permissions, true ),
351 if ( $numberInGroup ) {
352 global $wgAutopromote;
354 if ( $group == 'user' ) {
355 $arr['number'] = SiteStats
::users();
357 // '*' and autopromote groups have no size
358 } elseif ( $group !== '*' && !isset( $wgAutopromote[$group] ) ) {
359 $arr['number'] = SiteStats
::numberInGroup( $group );
364 'add' => $wgAddGroups,
365 'remove' => $wgRemoveGroups,
366 'add-self' => $wgGroupsAddToSelf,
367 'remove-self' => $wgGroupsRemoveFromSelf
370 foreach ( $groupArr as $type => $rights ) {
371 if ( isset( $rights[$group] ) ) {
372 $arr[$type] = $rights[$group];
373 $this->getResult()->setIndexedTagName( $arr[$type], 'group' );
377 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
381 $this->getResult()->setIndexedTagName( $data, 'group' );
382 return $this->getResult()->addValue( 'query', $property, $data );
385 protected function appendFileExtensions( $property ) {
386 global $wgFileExtensions;
389 foreach ( $wgFileExtensions as $ext ) {
390 $data[] = array( 'ext' => $ext );
392 $this->getResult()->setIndexedTagName( $data, 'fe' );
393 return $this->getResult()->addValue( 'query', $property, $data );
396 protected function appendExtensions( $property ) {
397 global $wgExtensionCredits;
399 foreach ( $wgExtensionCredits as $type => $extensions ) {
400 foreach ( $extensions as $ext ) {
402 $ret['type'] = $type;
403 if ( isset( $ext['name'] ) ) {
404 $ret['name'] = $ext['name'];
406 if ( isset( $ext['description'] ) ) {
407 $ret['description'] = $ext['description'];
409 if ( isset( $ext['descriptionmsg'] ) ) {
410 // Can be a string or array( key, param1, param2, ... )
411 if ( is_array( $ext['descriptionmsg'] ) ) {
412 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
413 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
414 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
416 $ret['descriptionmsg'] = $ext['descriptionmsg'];
419 if ( isset( $ext['author'] ) ) {
420 $ret['author'] = is_array( $ext['author'] ) ?
421 implode( ', ', $ext['author' ] ) : $ext['author'];
423 if ( isset( $ext['url'] ) ) {
424 $ret['url'] = $ext['url'];
426 if ( isset( $ext['version'] ) ) {
427 $ret['version'] = $ext['version'];
428 } elseif ( isset( $ext['svn-revision'] ) &&
429 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
430 $ext['svn-revision'], $m ) )
432 $ret['version'] = 'r' . $m[1];
438 $this->getResult()->setIndexedTagName( $data, 'ext' );
439 return $this->getResult()->addValue( 'query', $property, $data );
442 protected function appendRightsInfo( $property ) {
443 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
444 $title = Title
::newFromText( $wgRightsPage );
445 $url = $title ?
$title->getFullURL() : $wgRightsUrl;
446 $text = $wgRightsText;
447 if ( !$text && $title ) {
448 $text = $title->getPrefixedText();
452 'url' => $url ?
$url : '',
453 'text' => $text ?
$text : ''
456 return $this->getResult()->addValue( 'query', $property, $data );
459 public function appendLanguages( $property ) {
461 foreach ( Language
::getLanguageNames() as $code => $name ) {
462 $lang = array( 'code' => $code );
463 ApiResult
::setContent( $lang, $name );
466 $this->getResult()->setIndexedTagName( $data, 'lang' );
467 return $this->getResult()->addValue( 'query', $property, $data );
470 public function appendSkins( $property ) {
472 foreach ( Skin
::getSkinNames() as $name => $displayName ) {
473 $skin = array( 'code' => $name );
474 ApiResult
::setContent( $skin, $displayName );
477 $this->getResult()->setIndexedTagName( $data, 'skin' );
478 return $this->getResult()->addValue( 'query', $property, $data );
481 public function appendExtensionTags( $property ) {
483 $wgParser->firstCallInit();
484 $tags = array_map( array( $this, 'formatParserTags'), $wgParser->getTags() );
485 $this->getResult()->setIndexedTagName( $tags, 't' );
486 return $this->getResult()->addValue( 'query', $property, $tags );
489 public function appendFunctionHooks( $property ) {
491 $wgParser->firstCallInit();
492 $hooks = $wgParser->getFunctionHooks();
493 $this->getResult()->setIndexedTagName( $hooks, 'h' );
494 return $this->getResult()->addValue( 'query', $property, $hooks );
497 private function formatParserTags( $item ) {
498 return "<{$item}>";
501 public function getCacheMode( $params ) {
505 public function getAllowedParams() {
508 ApiBase
::PARAM_DFLT
=> 'general',
509 ApiBase
::PARAM_ISMULTI
=> true,
510 ApiBase
::PARAM_TYPE
=> array(
514 'specialpagealiases',
530 ApiBase
::PARAM_TYPE
=> array(
535 'showalldb' => false,
536 'numberingroup' => false,
540 public function getParamDescription() {
543 'Which sysinfo properties to get:',
544 ' general - Overall system information',
545 ' namespaces - List of registered namespaces and their canonical names',
546 ' namespacealiases - List of registered namespace aliases',
547 ' specialpagealiases - List of special page aliases',
548 ' magicwords - List of magic words and their aliases',
549 ' statistics - Returns site statistics',
550 ' interwikimap - Returns interwiki map (optionally filtered)',
551 ' dbrepllag - Returns database server with the highest replication lag',
552 ' usergroups - Returns user groups and the associated permissions',
553 ' extensions - Returns extensions installed on the wiki',
554 ' fileextensions - Returns list of file extensions allowed to be uploaded',
555 ' rightsinfo - Returns wiki rights (license) information if available',
556 ' languages - Returns a list of languages MediaWiki supports',
557 ' skins - Returns a list of all enabled skins',
558 ' extensiontags - Returns a list of parser extension tags',
559 ' functionhooks - Returns a list of parser function hooks',
561 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
562 'showalldb' => 'List all database servers, not just the one lagging the most',
563 'numberingroup' => 'Lists the number of users in user groups',
567 public function getDescription() {
568 return 'Return general information about the site';
571 public function getPossibleErrors() {
572 return array_merge( parent
::getPossibleErrors(), array(
573 array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
577 protected function getExamples() {
579 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
580 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
581 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb=',
585 public function getVersion() {
586 return __CLASS__
. ': $Id$';