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