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