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