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