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