Add lots more information to the siteinfo query. Based on the JS variable list.
[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 );
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 default :
87 ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
88 }
89 if(!$fit)
90 {
91 # Abuse siprop as a query-continue parameter
92 # and set it to all unprocessed props
93 $this->setContinueEnumParameter('prop', implode('|',
94 array_diff($params['prop'], $done)));
95 break;
96 }
97 $done[] = $p;
98 }
99 }
100
101 protected function appendGeneralInfo( $property ) {
102 global $wgContLang;
103 global $wgLang;
104
105 $data = array();
106 $mainPage = Title :: newFromText(wfMsgForContent('mainpage'));
107 $data['mainpage'] = $mainPage->getPrefixedText();
108 $data['base'] = $mainPage->getFullUrl();
109 $data['sitename'] = $GLOBALS['wgSitename'];
110 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
111
112 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
113 if( $svn )
114 $data['rev'] = $svn;
115
116 // 'case-insensitive' option is reserved for future
117 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
118
119 if( isset( $GLOBALS['wgRightsCode'] ) )
120 $data['rightscode'] = $GLOBALS['wgRightsCode'];
121 $data['rights'] = $GLOBALS['wgRightsText'];
122 $data['lang'] = $GLOBALS['wgLanguageCode'];
123 if( $wgContLang->isRTL() )
124 $data['rtl'] = '';
125 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
126
127 if( wfReadOnly() )
128 $data['readonly'] = '';
129 if( $GLOBALS['wgEnableWriteAPI'] )
130 $data['writeapi'] = '';
131
132 $tz = $GLOBALS['wgLocaltimezone'];
133 $offset = $GLOBALS['wgLocalTZoffset'];
134 if( is_null( $tz ) ) {
135 $tz = 'UTC';
136 $offset = 0;
137 } elseif( is_null( $offset ) ) {
138 $offset = 0;
139 }
140 $data['timezone'] = $tz;
141 $data['timeoffset'] = intval($offset);
142 $data['articlepath'] = $GLOBALS['wgArticlePath'];
143 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
144 $data['script'] = $GLOBALS['wgScript'];
145 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
146 $data['server'] = $GLOBALS['wgServer'];
147 $data['wikiid'] = wfWikiID();
148
149 return $this->getResult()->addValue( 'query', $property, $data );
150 }
151
152 protected function appendNamespaces( $property ) {
153 global $wgContLang;
154 $data = array();
155 foreach( $wgContLang->getFormattedNamespaces() as $ns => $title )
156 {
157 $data[$ns] = array(
158 'id' => intval($ns)
159 );
160 ApiResult :: setContent( $data[$ns], $title );
161 $canonical = MWNamespace::getCanonicalName( $ns );
162
163 if( MWNamespace::hasSubpages( $ns ) )
164 $data[$ns]['subpages'] = '';
165
166 if( $canonical )
167 $data[$ns]['canonical'] = strtr($canonical, '_', ' ');
168 }
169
170 $this->getResult()->setIndexedTagName( $data, 'ns' );
171 return $this->getResult()->addValue( 'query', $property, $data );
172 }
173
174 protected function appendNamespaceAliases( $property ) {
175 global $wgNamespaceAliases, $wgContLang;
176 $wgContLang->load();
177 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->namespaceAliases );
178 $namespaces = $wgContLang->getNamespaces();
179 $data = array();
180 foreach( $aliases as $title => $ns ) {
181 if( $namespaces[$ns] == $title ) {
182 // Don't list duplicates
183 continue;
184 }
185 $item = array(
186 'id' => intval($ns)
187 );
188 ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
189 $data[] = $item;
190 }
191
192 $this->getResult()->setIndexedTagName( $data, 'ns' );
193 return $this->getResult()->addValue( 'query', $property, $data );
194 }
195
196 protected function appendSpecialPageAliases( $property ) {
197 global $wgLang;
198 $data = array();
199 foreach( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
200 {
201 $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
202 $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
203 $data[] = $arr;
204 }
205 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
206 return $this->getResult()->addValue( 'query', $property, $data );
207 }
208
209 protected function appendMagicWords( $property ) {
210 global $wgContLang;
211 $data = array();
212 foreach($wgContLang->getMagicWords() as $magicword => $aliases)
213 {
214 $caseSensitive = array_shift($aliases);
215 $arr = array('name' => $magicword, 'aliases' => $aliases);
216 if($caseSensitive)
217 $arr['case-sensitive'] = '';
218 $this->getResult()->setIndexedTagName($arr['aliases'], 'alias');
219 $data[] = $arr;
220 }
221 $this->getResult()->setIndexedTagName($data, 'magicword');
222 return $this->getResult()->addValue( 'query', $property, $data );
223 }
224
225 protected function appendInterwikiMap( $property, $filter ) {
226 $this->resetQueryParams();
227 $this->addTables( 'interwiki' );
228 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
229
230 if( $filter === 'local' )
231 $this->addWhere( 'iw_local = 1' );
232 elseif( $filter === '!local' )
233 $this->addWhere( 'iw_local = 0' );
234 elseif( $filter )
235 ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
236
237 $this->addOption( 'ORDER BY', 'iw_prefix' );
238
239 $db = $this->getDB();
240 $res = $this->select( __METHOD__ );
241
242 $data = array();
243 $langNames = Language::getLanguageNames();
244 while( $row = $db->fetchObject($res) )
245 {
246 $val = array();
247 $val['prefix'] = $row->iw_prefix;
248 if( $row->iw_local == '1' )
249 $val['local'] = '';
250 // $val['trans'] = intval($row->iw_trans); // should this be exposed?
251 if( isset( $langNames[$row->iw_prefix] ) )
252 $val['language'] = $langNames[$row->iw_prefix];
253 $val['url'] = $row->iw_url;
254
255 $data[] = $val;
256 }
257 $db->freeResult( $res );
258
259 $this->getResult()->setIndexedTagName( $data, 'iw' );
260 return $this->getResult()->addValue( 'query', $property, $data );
261 }
262
263 protected function appendDbReplLagInfo( $property, $includeAll ) {
264 global $wgShowHostnames;
265 $data = array();
266 if( $includeAll ) {
267 if ( !$wgShowHostnames )
268 $this->dieUsage('Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied');
269
270 $lb = wfGetLB();
271 $lags = $lb->getLagTimes();
272 foreach( $lags as $i => $lag ) {
273 $data[] = array(
274 'host' => $lb->getServerName( $i ),
275 'lag' => $lag
276 );
277 }
278 } else {
279 list( $host, $lag ) = wfGetLB()->getMaxLag();
280 $data[] = array(
281 'host' => $wgShowHostnames ? $host : '',
282 'lag' => intval( $lag )
283 );
284 }
285
286 $result = $this->getResult();
287 $result->setIndexedTagName( $data, 'db' );
288 return $this->getResult()->addValue( 'query', $property, $data );
289 }
290
291 protected function appendStatistics( $property ) {
292 global $wgDisableCounters;
293 $data = array();
294 $data['pages'] = intval( SiteStats::pages() );
295 $data['articles'] = intval( SiteStats::articles() );
296 if ( !$wgDisableCounters ) {
297 $data['views'] = intval( SiteStats::views() );
298 }
299 $data['edits'] = intval( SiteStats::edits() );
300 $data['images'] = intval( SiteStats::images() );
301 $data['users'] = intval( SiteStats::users() );
302 $data['activeusers'] = intval( SiteStats::activeUsers() );
303 $data['admins'] = intval( SiteStats::numberingroup('sysop') );
304 $data['jobs'] = intval( SiteStats::jobs() );
305 return $this->getResult()->addValue( 'query', $property, $data );
306 }
307
308 protected function appendUserGroups( $property ) {
309 global $wgGroupPermissions;
310 $data = array();
311 foreach( $wgGroupPermissions as $group => $permissions ) {
312 $arr = array( 'name' => $group, 'rights' => array_keys( $permissions, true ) );
313 $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
314 $data[] = $arr;
315 }
316
317 $this->getResult()->setIndexedTagName( $data, 'group' );
318 return $this->getResult()->addValue( 'query', $property, $data );
319 }
320
321 protected function appendFileExtensions( $property ) {
322 global $wgFileExtensions;
323
324 $data = array();
325 foreach( $wgFileExtensions as $ext ) {
326 $data[] = array( 'ext' => $ext );
327 }
328 $this->getResult()->setIndexedTagName( $data, 'fe' );
329 return $this->getResult()->addValue( 'query', $property, $data );
330 }
331
332 protected function appendExtensions( $property ) {
333 global $wgExtensionCredits;
334 $data = array();
335 foreach ( $wgExtensionCredits as $type => $extensions ) {
336 foreach ( $extensions as $ext ) {
337 $ret = array();
338 $ret['type'] = $type;
339 if ( isset( $ext['name'] ) )
340 $ret['name'] = $ext['name'];
341 if ( isset( $ext['description'] ) )
342 $ret['description'] = $ext['description'];
343 if ( isset( $ext['descriptionmsg'] ) )
344 $ret['descriptionmsg'] = $ext['descriptionmsg'];
345 if ( isset( $ext['author'] ) ) {
346 $ret['author'] = is_array( $ext['author'] ) ?
347 implode( ', ', $ext['author' ] ) : $ext['author'];
348 }
349 if ( isset( $ext['version'] ) ) {
350 $ret['version'] = $ext['version'];
351 } elseif ( isset( $ext['svn-revision'] ) &&
352 preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
353 $ext['svn-revision'], $m ) )
354 {
355 $ret['version'] = 'r' . $m[1];
356 }
357 $data[] = $ret;
358 }
359 }
360
361 $this->getResult()->setIndexedTagName( $data, 'ext' );
362 return $this->getResult()->addValue( 'query', $property, $data );
363 }
364
365
366 protected function appendRightsInfo( $property ) {
367 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
368 $title = Title::newFromText( $wgRightsPage );
369 $url = $title ? $title->getFullURL() : $wgRightsUrl;
370 $text = $wgRightsText;
371 if( !$text && $title ) {
372 $text = $title->getPrefixedText();
373 }
374
375 $data = array(
376 'url' => $url ? $url : '',
377 'text' => $text ? $text : ''
378 );
379
380 return $this->getResult()->addValue( 'query', $property, $data );
381 }
382
383
384 public function getAllowedParams() {
385 return array(
386 'prop' => array(
387 ApiBase :: PARAM_DFLT => 'general',
388 ApiBase :: PARAM_ISMULTI => true,
389 ApiBase :: PARAM_TYPE => array(
390 'general',
391 'namespaces',
392 'namespacealiases',
393 'specialpagealiases',
394 'magicwords',
395 'interwikimap',
396 'dbrepllag',
397 'statistics',
398 'usergroups',
399 'extensions',
400 'fileextensions',
401 'rightsinfo',
402 )
403 ),
404 'filteriw' => array(
405 ApiBase :: PARAM_TYPE => array(
406 'local',
407 '!local',
408 )
409 ),
410 'showalldb' => false,
411 );
412 }
413
414 public function getParamDescription() {
415 return array(
416 'prop' => array(
417 'Which sysinfo properties to get:',
418 ' general - Overall system information',
419 ' namespaces - List of registered namespaces and their canonical names',
420 ' namespacealiases - List of registered namespace aliases',
421 ' specialpagealiases - List of special page aliases',
422 ' magicwords - List of magic words and their aliases',
423 ' statistics - Returns site statistics',
424 ' interwikimap - Returns interwiki map (optionally filtered)',
425 ' dbrepllag - Returns database server with the highest replication lag',
426 ' usergroups - Returns user groups and the associated permissions',
427 ' extensions - Returns extensions installed on the wiki',
428 ' fileextensions - Returns list of file extensions allowed to be uploaded',
429 ' rightsinfo - Returns wiki rights (license) information if available',
430 ),
431 'filteriw' => 'Return only local or only nonlocal entries of the interwiki map',
432 'showalldb' => 'List all database servers, not just the one lagging the most',
433 );
434 }
435
436 public function getDescription() {
437 return 'Return general information about the site.';
438 }
439
440 protected function getExamples() {
441 return array(
442 'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
443 'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
444 'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
445 );
446 }
447
448 public function getVersion() {
449 return __CLASS__ . ': $Id$';
450 }
451 }