stylize.php on API code
[lhc/web/wiklou.git] / includes / api / ApiQuerySiteinfo.php
1 <?php
2
3 /*
4 * Created on Sep 25, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * A query action to return meta information about the wiki site.
33 *
34 * @ingroup API
35 */
36 class ApiQuerySiteinfo extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent :: __construct( $query, $moduleName, 'si' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $done = array();
45 foreach ( $params['prop'] as $p )
46 {
47 switch ( $p )
48 {
49 case 'general':
50 $fit = $this->appendGeneralInfo( $p );
51 break;
52 case 'namespaces':
53 $fit = $this->appendNamespaces( $p );
54 break;
55 case 'namespacealiases':
56 $fit = $this->appendNamespaceAliases( $p );
57 break;
58 case 'specialpagealiases':
59 $fit = $this->appendSpecialPageAliases( $p );
60 break;
61 case 'magicwords':
62 $fit = $this->appendMagicWords( $p );
63 break;
64 case 'interwikimap':
65 $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
66 $fit = $this->appendInterwikiMap( $p, $filteriw );
67 break;
68 case 'dbrepllag':
69 $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
70 break;
71 case 'statistics':
72 $fit = $this->appendStatistics( $p );
73 break;
74 case 'usergroups':
75 $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
76 break;
77 case 'extensions':
78 $fit = $this->appendExtensions( $p );
79 break;
80 case 'fileextensions':
81 $fit = $this->appendFileExtensions( $p );
82 break;
83 case 'rightsinfo':
84 $fit = $this->appendRightsInfo( $p );
85 break;
86 case 'languages':
87 $fit = $this->appendLanguages( $p );
88 break;
89 default :
90 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
91 }
92 if ( !$fit )
93 {
94 # Abuse siprop as a query-continue parameter
95 # and set it to all unprocessed props
96 $this->setContinueEnumParameter( 'prop', implode( '|',
97 array_diff( $params['prop'], $done ) ) );
98 break;
99 }
100 $done[] = $p;
101 }
102 }
103
104 protected function appendGeneralInfo( $property ) {
105 global $wgContLang;
106 global $wgLang;
107
108 $data = array();
109 $mainPage = Title :: newFromText( wfMsgForContent( 'mainpage' ) );
110 $data['mainpage'] = $mainPage->getPrefixedText();
111 $data['base'] = $mainPage->getFullUrl();
112 $data['sitename'] = $GLOBALS['wgSitename'];
113 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
114 $data['phpversion'] = phpversion();
115 $data['phpsapi'] = php_sapi_name();
116 $data['dbtype'] = $GLOBALS['wgDBtype'];
117 $data['dbversion'] = $this->getDB()->getServerVersion();
118
119 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
120 if ( $svn )
121 $data['rev'] = $svn;
122
123 // 'case-insensitive' option is reserved for future
124 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
125
126 if ( isset( $GLOBALS['wgRightsCode'] ) )
127 $data['rightscode'] = $GLOBALS['wgRightsCode'];
128 $data['rights'] = $GLOBALS['wgRightsText'];
129 $data['lang'] = $GLOBALS['wgLanguageCode'];
130 if ( $wgContLang->isRTL() )
131 $data['rtl'] = '';
132 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
133
134 if ( wfReadOnly() ) {
135 $data['readonly'] = '';
136 $data['readonlyreason'] = wfReadOnlyReason();
137 }
138 if ( $GLOBALS['wgEnableWriteAPI'] )
139 $data['writeapi'] = '';
140
141 $tz = $GLOBALS['wgLocaltimezone'];
142 $offset = $GLOBALS['wgLocalTZoffset'];
143 if ( is_null( $tz ) ) {
144 $tz = 'UTC';
145 $offset = 0;
146 } elseif ( is_null( $offset ) ) {
147 $offset = 0;
148 }
149 $data['timezone'] = $tz;
150 $data['timeoffset'] = intval( $offset );
151 $data['articlepath'] = $GLOBALS['wgArticlePath'];
152 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
153 $data['script'] = $GLOBALS['wgScript'];
154 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
155 $data['server'] = $GLOBALS['wgServer'];
156 $data['wikiid'] = wfWikiID();
157
158 return $this->getResult()->addValue( 'query', $property, $data );
159 }
160
161 protected function appendNamespaces( $property ) {
162 global $wgContLang;
163 $data = array();
164 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title )
165 {
166 $data[$ns] = array(
167 'id' => intval( $ns ),
168 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
169 );
170 ApiResult :: setContent( $data[$ns], $title );
171 $canonical = MWNamespace::getCanonicalName( $ns );
172
173 if ( MWNamespace::hasSubpages( $ns ) )
174 $data[$ns]['subpages'] = '';
175
176 if ( $canonical )
177 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
178
179 if ( MWNamespace::isContent( $ns ) )
180 $data[$ns]['content'] = '';
181 }
182
183 $this->getResult()->setIndexedTagName( $data, 'ns' );
184 return $this->getResult()->addValue( 'query', $property, $data );
185 }
186
187 protected function appendNamespaceAliases( $property ) {
188 global $wgNamespaceAliases, $wgContLang;
189 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
190 $namespaces = $wgContLang->getNamespaces();
191 $data = array();
192 foreach ( $aliases as $title => $ns ) {
193 if ( $namespaces[$ns] == $title ) {
194 // Don't list duplicates
195 continue;
196 }
197 $item = array(
198 'id' => intval( $ns )
199 );
200 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
201 $data[] = $item;
202 }
203
204 $this->getResult()->setIndexedTagName( $data, 'ns' );
205 return $this->getResult()->addValue( 'query', $property, $data );
206 }
207
208 protected function appendSpecialPageAliases( $property ) {
209 global $wgLang;
210 $data = array();
211 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
212 {
213 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
214 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
215 $data[] = $arr;
216 }
217 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
218 return $this->getResult()->addValue( 'query', $property, $data );
219 }
220
221 protected function appendMagicWords( $property ) {
222 global $wgContLang;
223 $data = array();
224 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
225 {
226 $caseSensitive = array_shift( $aliases );
227 $arr = array( 'name' => $magicword, 'aliases' => $aliases );
228 if ( $caseSensitive )
229 $arr['case-sensitive'] = '';
230 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
231 $data[] = $arr;
232 }
233 $this->getResult()->setIndexedTagName( $data, 'magicword' );
234 return $this->getResult()->addValue( 'query', $property, $data );
235 }
236
237 protected function appendInterwikiMap( $property, $filter ) {
238 $this->resetQueryParams();
239 $this->addTables( 'interwiki' );
240 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
241
242 if ( $filter === 'local' )
243 $this->addWhere( 'iw_local = 1' );
244 elseif ( $filter === '!local' )
245 $this->addWhere( 'iw_local = 0' );
246 elseif ( $filter )
247 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
248
249 $this->addOption( 'ORDER BY', 'iw_prefix' );
250
251 $db = $this->getDB();
252 $res = $this->select( __METHOD__ );
253
254 $data = array();
255 $langNames = Language::getLanguageNames();
256 while ( $row = $db->fetchObject( $res ) )
257 {
258 $val = array();
259 $val['prefix'] = $row->iw_prefix;
260 if ( $row->iw_local == '1' )
261 $val['local'] = '';
262 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
263 if ( isset( $langNames[$row->iw_prefix] ) )
264 $val['language'] = $langNames[$row->iw_prefix];
265 $val['url'] = $row->iw_url;
266
267 $data[] = $val;
268 }
269 $db->freeResult( $res );
270
271 $this->getResult()->setIndexedTagName( $data, 'iw' );
272 return $this->getResult()->addValue( 'query', $property, $data );
273 }
274
275 protected function appendDbReplLagInfo( $property, $includeAll ) {
276 global $wgShowHostnames;
277 $data = array();
278 if ( $includeAll ) {
279 if ( !$wgShowHostnames )
280 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
281
282 $lb = wfGetLB();
283 $lags = $lb->getLagTimes();
284 foreach ( $lags as $i => $lag ) {
285 $data[] = array(
286 'host' => $lb->getServerName( $i ),
287 'lag' => $lag
288 );
289 }
290 } else {
291 list( $host, $lag ) = wfGetLB()->getMaxLag();
292 $data[] = array(
293 'host' => $wgShowHostnames ? $host : '',
294 'lag' => intval( $lag )
295 );
296 }
297
298 $result = $this->getResult();
299 $result->setIndexedTagName( $data, 'db' );
300 return $this->getResult()->addValue( 'query', $property, $data );
301 }
302
303 protected function appendStatistics( $property ) {
304 global $wgDisableCounters;
305 $data = array();
306 $data['pages'] = intval( SiteStats::pages() );
307 $data['articles'] = intval( SiteStats::articles() );
308 if ( !$wgDisableCounters ) {
309 $data['views'] = intval( SiteStats::views() );
310 }
311 $data['edits'] = intval( SiteStats::edits() );
312 $data['images'] = intval( SiteStats::images() );
313 $data['users'] = intval( SiteStats::users() );
314 $data['activeusers'] = intval( SiteStats::activeUsers() );
315 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
316 $data['jobs'] = intval( SiteStats::jobs() );
317 return $this->getResult()->addValue( 'query', $property, $data );
318 }
319
320 protected function appendUserGroups( $property, $numberInGroup ) {
321 global $wgGroupPermissions;
322 $data = array();
323 foreach ( $wgGroupPermissions as $group => $permissions ) {
324 $arr = array(
325 'name' => $group,
326 'rights' => array_keys( $permissions, true ),
327 );
328 if ( $numberInGroup )
329 $arr['number'] = SiteStats::numberInGroup( $group );
330
331 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
332 $data[] = $arr;
333 }
334
335 $this->getResult()->setIndexedTagName( $data, 'group' );
336 return $this->getResult()->addValue( 'query', $property, $data );
337 }
338
339 protected function appendFileExtensions( $property ) {
340 global $wgFileExtensions;
341
342 $data = array();
343 foreach ( $wgFileExtensions as $ext ) {
344 $data[] = array( 'ext' => $ext );
345 }
346 $this->getResult()->setIndexedTagName( $data, 'fe' );
347 return $this->getResult()->addValue( 'query', $property, $data );
348 }
349
350 protected function appendExtensions( $property ) {
351 global $wgExtensionCredits;
352 $data = array();
353 foreach ( $wgExtensionCredits as $type => $extensions ) {
354 foreach ( $extensions as $ext ) {
355 $ret = array();
356 $ret['type'] = $type;
357 if ( isset( $ext['name'] ) )
358 $ret['name'] = $ext['name'];
359 if ( isset( $ext['description'] ) )
360 $ret['description'] = $ext['description'];
361 if ( isset( $ext['descriptionmsg'] ) )
362 $ret['descriptionmsg'] = $ext['descriptionmsg'];
363 if ( isset( $ext['author'] ) ) {
364 $ret['author'] = is_array( $ext['author'] ) ?
365 implode( ', ', $ext['author' ] ) : $ext['author'];
366 }
367 if ( isset( $ext['version'] ) ) {
368 $ret['version'] = $ext['version'];
369 } elseif ( isset( $ext['svn-revision'] ) &&
370 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
371 $ext['svn-revision'], $m ) )
372 {
373 $ret['version'] = 'r' . $m[1];
374 }
375 $data[] = $ret;
376 }
377 }
378
379 $this->getResult()->setIndexedTagName( $data, 'ext' );
380 return $this->getResult()->addValue( 'query', $property, $data );
381 }
382
383
384 protected function appendRightsInfo( $property ) {
385 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
386 $title = Title::newFromText( $wgRightsPage );
387 $url = $title ? $title->getFullURL() : $wgRightsUrl;
388 $text = $wgRightsText;
389 if ( !$text && $title ) {
390 $text = $title->getPrefixedText();
391 }
392
393 $data = array(
394 'url' => $url ? $url : '',
395 'text' => $text ? $text : ''
396 );
397
398 return $this->getResult()->addValue( 'query', $property, $data );
399 }
400
401 public function appendLanguages( $property ) {
402 $data = array();
403 foreach ( Language::getLanguageNames() as $code => $name ) {
404 $lang = array( 'code' => $code );
405 ApiResult::setContent( $lang, $name );
406 $data[] = $lang;
407 }
408 $this->getResult()->setIndexedTagName( $data, 'lang' );
409 return $this->getResult()->addValue( 'query', $property, $data );
410 }
411
412
413 public function getAllowedParams() {
414 return array(
415 'prop' => array(
416 ApiBase :: PARAM_DFLT => 'general',
417 ApiBase :: PARAM_ISMULTI => true,
418 ApiBase :: PARAM_TYPE => array(
419 'general',
420 'namespaces',
421 'namespacealiases',
422 'specialpagealiases',
423 'magicwords',
424 'interwikimap',
425 'dbrepllag',
426 'statistics',
427 'usergroups',
428 'extensions',
429 'fileextensions',
430 'rightsinfo',
431 'languages',
432 )
433 ),
434 'filteriw' => array(
435 ApiBase :: PARAM_TYPE => array(
436 'local',
437 '!local',
438 )
439 ),
440 'showalldb' => false,
441 'numberingroup' => false,
442 );
443 }
444
445 public function getParamDescription() {
446 return array(
447 'prop' => array(
448 'Which sysinfo properties to get:',
449 ' general - Overall system information',
450 ' namespaces - List of registered namespaces and their canonical names',
451 ' namespacealiases - List of registered namespace aliases',
452 ' specialpagealiases - List of special page aliases',
453 ' magicwords - List of magic words and their aliases',
454 ' statistics - Returns site statistics',
455 ' interwikimap - Returns interwiki map (optionally filtered)',
456 ' dbrepllag - Returns database server with the highest replication lag',
457 ' usergroups - Returns user groups and the associated permissions',
458 ' extensions - Returns extensions installed on the wiki',
459 ' fileextensions - Returns list of file extensions allowed to be uploaded',
460 ' rightsinfo - Returns wiki rights (license) information if available',
461 ' languages - Returns a list of languages MediaWiki supports',
462 ),
463 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
464 'showalldb' => 'List all database servers, not just the one lagging the most',
465 'numberingroup' => 'Lists the number of users in user groups',
466 );
467 }
468
469 public function getDescription() {
470 return 'Return general information about the site.';
471 }
472
473 protected function getExamples() {
474 return array(
475 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
476 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
477 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
478 );
479 }
480
481 public function getVersion() {
482 return __CLASS__ . ': $Id$';
483 }
484 }