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