]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/api/ApiQuerySiteinfo.php
MediaWiki 1.16.0
[autoinstalls/mediawiki.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                 $done = array();
45                 foreach ( $params['prop'] as $p )
46                 {
47                         switch ( $p )
48                         {
49                                 case 'general':
50                                         $fit = $this->appendGeneralInfo( $p );
51                                         break;
52                                 case 'namespaces':
53                                         $fit = $this->appendNamespaces( $p );
54                                         break;
55                                 case 'namespacealiases':
56                                         $fit = $this->appendNamespaceAliases( $p );
57                                         break;
58                                 case 'specialpagealiases':
59                                         $fit = $this->appendSpecialPageAliases( $p );
60                                         break;
61                                 case 'magicwords':
62                                         $fit = $this->appendMagicWords( $p );
63                                         break;
64                                 case 'interwikimap':
65                                         $filteriw = isset( $params['filteriw'] ) ? $params['filteriw'] : false;
66                                         $fit = $this->appendInterwikiMap( $p, $filteriw );
67                                         break;
68                                 case 'dbrepllag':
69                                         $fit = $this->appendDbReplLagInfo( $p, $params['showalldb'] );
70                                         break;
71                                 case 'statistics':
72                                         $fit = $this->appendStatistics( $p );
73                                         break;
74                                 case 'usergroups':
75                                         $fit = $this->appendUserGroups( $p, $params['numberingroup'] );
76                                         break;
77                                 case 'extensions':
78                                         $fit = $this->appendExtensions( $p );
79                                         break;
80                                 case 'fileextensions':
81                                         $fit = $this->appendFileExtensions( $p );
82                                         break;
83                                 case 'rightsinfo':
84                                         $fit = $this->appendRightsInfo( $p );
85                                         break;
86                                 case 'languages':
87                                         $fit = $this->appendLanguages( $p );
88                                         break;
89                                 default :
90                                         ApiBase :: dieDebug( __METHOD__, "Unknown prop=$p" );
91                         }
92                         if ( !$fit )
93                         {
94                                 // Abuse siprop as a query-continue parameter
95                                 // and set it to all unprocessed props
96                                 $this->setContinueEnumParameter( 'prop', implode( '|',
97                                                 array_diff( $params['prop'], $done ) ) );
98                                 break;
99                         }
100                         $done[] = $p;
101                 }
102         }
103
104         protected function appendGeneralInfo( $property ) {
105                 global $wgContLang;
106                 global $wgLang;
107
108                 $data = array();
109                 $mainPage = Title :: newFromText( wfMsgForContent( 'mainpage' ) );
110                 $data['mainpage'] = $mainPage->getPrefixedText();
111                 $data['base'] = $mainPage->getFullUrl();
112                 $data['sitename'] = $GLOBALS['wgSitename'];
113                 $data['generator'] = "MediaWiki {$GLOBALS['wgVersion']}";
114                 $data['phpversion'] = phpversion();
115                 $data['phpsapi'] = php_sapi_name();
116                 $data['dbtype'] = $GLOBALS['wgDBtype'];
117                 $data['dbversion'] = $this->getDB()->getServerVersion();
118
119                 $svn = SpecialVersion::getSvnRevision( $GLOBALS['IP'] );
120                 if ( $svn )
121                         $data['rev'] = $svn;
122
123                 // 'case-insensitive' option is reserved for future
124                 $data['case'] = $GLOBALS['wgCapitalLinks'] ? 'first-letter' : 'case-sensitive';
125
126                 if ( isset( $GLOBALS['wgRightsCode'] ) )
127                         $data['rightscode'] = $GLOBALS['wgRightsCode'];
128                 $data['rights'] = $GLOBALS['wgRightsText'];
129                 $data['lang'] = $GLOBALS['wgLanguageCode'];
130                 if ( $wgContLang->isRTL() )
131                         $data['rtl'] = '';
132                 $data['fallback8bitEncoding'] = $wgLang->fallback8bitEncoding();
133                 
134                 if ( wfReadOnly() ) {
135                         $data['readonly'] = '';
136                         $data['readonlyreason'] = wfReadOnlyReason();
137                 }
138                 if ( $GLOBALS['wgEnableWriteAPI'] )
139                         $data['writeapi'] = '';
140
141                 $tz = $GLOBALS['wgLocaltimezone'];
142                 $offset = $GLOBALS['wgLocalTZoffset'];
143                 if ( is_null( $tz ) ) {
144                         $tz = 'UTC';
145                         $offset = 0;
146                 } elseif ( is_null( $offset ) ) {
147                         $offset = 0;
148                 }
149                 $data['timezone'] = $tz;
150                 $data['timeoffset'] = intval( $offset );
151                 $data['articlepath'] = $GLOBALS['wgArticlePath'];
152                 $data['scriptpath'] = $GLOBALS['wgScriptPath'];
153                 $data['script'] = $GLOBALS['wgScript'];
154                 $data['variantarticlepath'] = $GLOBALS['wgVariantArticlePath'];
155                 $data['server'] = $GLOBALS['wgServer'];
156                 $data['wikiid'] = wfWikiID();
157                 $data['time'] = wfTimestamp( TS_ISO_8601, time() );
158
159                 return $this->getResult()->addValue( 'query', $property, $data );
160         }
161
162         protected function appendNamespaces( $property ) {
163                 global $wgContLang;
164                 $data = array();
165                 foreach ( $wgContLang->getFormattedNamespaces() as $ns => $title )
166                 {
167                         $data[$ns] = array(
168                                 'id' => intval( $ns ),
169                                 'case' => MWNamespace::isCapitalized( $ns ) ? 'first-letter' : 'case-sensitive',
170                         );
171                         ApiResult :: setContent( $data[$ns], $title );
172                         $canonical = MWNamespace::getCanonicalName( $ns );
173                         
174                         if ( MWNamespace::hasSubpages( $ns ) )
175                                 $data[$ns]['subpages'] = '';
176                         
177                         if ( $canonical )
178                                 $data[$ns]['canonical'] = strtr( $canonical, '_', ' ' );
179                         
180                         if ( MWNamespace::isContent( $ns ) )
181                                 $data[$ns]['content'] = '';
182                 }
183
184                 $this->getResult()->setIndexedTagName( $data, 'ns' );
185                 return $this->getResult()->addValue( 'query', $property, $data );
186         }
187
188         protected function appendNamespaceAliases( $property ) {
189                 global $wgNamespaceAliases, $wgContLang;
190                 $aliases = array_merge( $wgNamespaceAliases, $wgContLang->getNamespaceAliases() );
191                 $namespaces = $wgContLang->getNamespaces();
192                 $data = array();
193                 foreach ( $aliases as $title => $ns ) {
194                         if ( $namespaces[$ns] == $title ) {
195                                 // Don't list duplicates
196                                 continue;
197                         }
198                         $item = array(
199                                 'id' => intval( $ns )
200                         );
201                         ApiResult :: setContent( $item, strtr( $title, '_', ' ' ) );
202                         $data[] = $item;
203                 }
204
205                 $this->getResult()->setIndexedTagName( $data, 'ns' );
206                 return $this->getResult()->addValue( 'query', $property, $data );
207         }
208
209         protected function appendSpecialPageAliases( $property ) {
210                 global $wgLang;
211                 $data = array();
212                 foreach ( $wgLang->getSpecialPageAliases() as $specialpage => $aliases )
213                 {
214                         $arr = array( 'realname' => $specialpage, 'aliases' => $aliases );
215                         $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
216                         $data[] = $arr;
217                 }
218                 $this->getResult()->setIndexedTagName( $data, 'specialpage' );
219                 return $this->getResult()->addValue( 'query', $property, $data );
220         }
221         
222         protected function appendMagicWords( $property ) {
223                 global $wgContLang;
224                 $data = array();
225                 foreach ( $wgContLang->getMagicWords() as $magicword => $aliases )
226                 {
227                         $caseSensitive = array_shift( $aliases );
228                         $arr = array( 'name' => $magicword, 'aliases' => $aliases );
229                         if ( $caseSensitive )
230                                 $arr['case-sensitive'] = '';
231                         $this->getResult()->setIndexedTagName( $arr['aliases'], 'alias' );
232                         $data[] = $arr;
233                 }
234                 $this->getResult()->setIndexedTagName( $data, 'magicword' );
235                 return $this->getResult()->addValue( 'query', $property, $data );
236         }
237
238         protected function appendInterwikiMap( $property, $filter ) {
239                 $this->resetQueryParams();
240                 $this->addTables( 'interwiki' );
241                 $this->addFields( array( 'iw_prefix', 'iw_local', 'iw_url' ) );
242
243                 if ( $filter === 'local' )
244                         $this->addWhere( 'iw_local = 1' );
245                 elseif ( $filter === '!local' )
246                         $this->addWhere( 'iw_local = 0' );
247                 elseif ( $filter )
248                         ApiBase :: dieDebug( __METHOD__, "Unknown filter=$filter" );
249
250                 $this->addOption( 'ORDER BY', 'iw_prefix' );
251
252                 $db = $this->getDB();
253                 $res = $this->select( __METHOD__ );
254
255                 $data = array();
256                 $langNames = Language::getLanguageNames();
257                 while ( $row = $db->fetchObject( $res ) )
258                 {
259                         $val = array();
260                         $val['prefix'] = $row->iw_prefix;
261                         if ( $row->iw_local == '1' )
262                                 $val['local'] = '';
263 //                      $val['trans'] = intval($row->iw_trans); // should this be exposed?
264                         if ( isset( $langNames[$row->iw_prefix] ) )
265                                 $val['language'] = $langNames[$row->iw_prefix];
266                         $val['url'] = $row->iw_url;
267
268                         $data[] = $val;
269                 }
270                 $db->freeResult( $res );
271
272                 $this->getResult()->setIndexedTagName( $data, 'iw' );
273                 return $this->getResult()->addValue( 'query', $property, $data );
274         }
275
276         protected function appendDbReplLagInfo( $property, $includeAll ) {
277                 global $wgShowHostnames;
278                 $data = array();
279                 if ( $includeAll ) {
280                         if ( !$wgShowHostnames )
281                                 $this->dieUsage( 'Cannot view all servers info unless $wgShowHostnames is true', 'includeAllDenied' );
282
283                         $lb = wfGetLB();
284                         $lags = $lb->getLagTimes();
285                         foreach ( $lags as $i => $lag ) {
286                                 $data[] = array(
287                                         'host' => $lb->getServerName( $i ),
288                                         'lag' => $lag
289                                 );
290                         }
291                 } else {
292                         list( $host, $lag ) = wfGetLB()->getMaxLag();
293                         $data[] = array(
294                                 'host' => $wgShowHostnames ? $host : '',
295                                 'lag' => intval( $lag )
296                         );
297                 }
298
299                 $result = $this->getResult();
300                 $result->setIndexedTagName( $data, 'db' );
301                 return $this->getResult()->addValue( 'query', $property, $data );
302         }
303
304         protected function appendStatistics( $property ) {
305                 global $wgDisableCounters;
306                 $data = array();
307                 $data['pages'] = intval( SiteStats::pages() );
308                 $data['articles'] = intval( SiteStats::articles() );
309                 if ( !$wgDisableCounters ) {
310                         $data['views'] = intval( SiteStats::views() );
311                 }
312                 $data['edits'] = intval( SiteStats::edits() );
313                 $data['images'] = intval( SiteStats::images() );
314                 $data['users'] = intval( SiteStats::users() );
315                 $data['activeusers'] = intval( SiteStats::activeUsers() );
316                 $data['admins'] = intval( SiteStats::numberingroup( 'sysop' ) );
317                 $data['jobs'] = intval( SiteStats::jobs() );
318                 return $this->getResult()->addValue( 'query', $property, $data );
319         }
320
321         protected function appendUserGroups( $property, $numberInGroup ) {
322                 global $wgGroupPermissions;
323                 $data = array();
324                 foreach ( $wgGroupPermissions as $group => $permissions ) {
325                         $arr = array(
326                                 'name' => $group,
327                                 'rights' => array_keys( $permissions, true ),
328                         );
329                         if ( $numberInGroup )
330                                 $arr['number'] = SiteStats::numberInGroup( $group );
331                         
332                         $this->getResult()->setIndexedTagName( $arr['rights'], 'permission' );
333                         $data[] = $arr;
334                 }
335
336                 $this->getResult()->setIndexedTagName( $data, 'group' );
337                 return $this->getResult()->addValue( 'query', $property, $data );
338         }
339         
340         protected function appendFileExtensions( $property ) {
341                 global $wgFileExtensions;
342                 
343                 $data = array();
344                 foreach ( $wgFileExtensions as $ext ) {
345                         $data[] = array( 'ext' => $ext );
346                 }
347                 $this->getResult()->setIndexedTagName( $data, 'fe' );
348                 return $this->getResult()->addValue( 'query', $property, $data );
349         }
350
351         protected function appendExtensions( $property ) {
352                 global $wgExtensionCredits;
353                 $data = array();
354                 foreach ( $wgExtensionCredits as $type => $extensions ) {
355                         foreach ( $extensions as $ext ) {
356                                 $ret = array();
357                                 $ret['type'] = $type;
358                                 if ( isset( $ext['name'] ) )
359                                         $ret['name'] = $ext['name'];
360                                 if ( isset( $ext['description'] ) )
361                                         $ret['description'] = $ext['description'];
362                                 if ( isset( $ext['descriptionmsg'] ) ) {
363                                         // Can be a string or array( key, param1, param2, ... )
364                                         if ( is_array( $ext['descriptionmsg'] ) ) {
365                                                 $ret['descriptionmsg'] = $ext['descriptionmsg'][0];
366                                                 $ret['descriptionmsgparams'] = array_slice( $ext['descriptionmsg'], 1 );
367                                                 $this->getResult()->setIndexedTagName( $ret['descriptionmsgparams'], 'param' );
368                                         } else {
369                                                 $ret['descriptionmsg'] = $ext['descriptionmsg'];
370                                         }
371                                 }
372                                 if ( isset( $ext['author'] ) ) {
373                                         $ret['author'] = is_array( $ext['author'] ) ?
374                                                 implode( ', ', $ext['author' ] ) : $ext['author'];
375                                 }
376                                 if ( isset( $ext['url'] ) ) {
377                                         $ret['url'] = $ext['url'];
378                                 }
379                                 if ( isset( $ext['version'] ) ) {
380                                                 $ret['version'] = $ext['version'];
381                                 } elseif ( isset( $ext['svn-revision'] ) &&
382                                         preg_match( '/\$(?:Rev|LastChangedRevision|Revision): *(\d+)/',
383                                                 $ext['svn-revision'], $m ) )
384                                 {
385                                                 $ret['version'] = 'r' . $m[1];
386                                 }
387                                 $data[] = $ret;
388                         }
389                 }
390
391                 $this->getResult()->setIndexedTagName( $data, 'ext' );
392                 return $this->getResult()->addValue( 'query', $property, $data );
393         }
394
395
396         protected function appendRightsInfo( $property ) {
397                 global $wgRightsPage, $wgRightsUrl, $wgRightsText;
398                 $title = Title::newFromText( $wgRightsPage );
399                 $url = $title ? $title->getFullURL() : $wgRightsUrl;
400                 $text = $wgRightsText;
401                 if ( !$text && $title ) {
402                         $text = $title->getPrefixedText();
403                 }
404
405                 $data = array(
406                         'url' => $url ? $url : '',
407                         'text' => $text ?  $text : ''
408                 );
409
410                 return $this->getResult()->addValue( 'query', $property, $data );
411         }
412
413         public function appendLanguages( $property ) {
414                 $data = array();
415                 foreach ( Language::getLanguageNames() as $code => $name ) {
416                         $lang = array( 'code' => $code );
417                         ApiResult::setContent( $lang, $name );
418                         $data[] = $lang;
419                 }
420                 $this->getResult()->setIndexedTagName( $data, 'lang' );
421                 return $this->getResult()->addValue( 'query', $property, $data );
422         }
423
424         public function getCacheMode( $params ) {
425                 return 'public';
426         }
427
428         public function getAllowedParams() {
429                 return array(
430                         'prop' => array(
431                                 ApiBase :: PARAM_DFLT => 'general',
432                                 ApiBase :: PARAM_ISMULTI => true,
433                                 ApiBase :: PARAM_TYPE => array(
434                                         'general',
435                                         'namespaces',
436                                         'namespacealiases',
437                                         'specialpagealiases',
438                                         'magicwords',
439                                         'interwikimap',
440                                         'dbrepllag',
441                                         'statistics',
442                                         'usergroups',
443                                         'extensions',
444                                         'fileextensions',
445                                         'rightsinfo',
446                                         'languages',
447                                 )
448                         ),
449                         'filteriw' => array(
450                                 ApiBase :: PARAM_TYPE => array(
451                                         'local',
452                                         '!local',
453                                 )
454                         ),
455                         'showalldb' => false,
456                         'numberingroup' => false,
457                 );
458         }
459
460         public function getParamDescription() {
461                 return array(
462                         'prop' => array(
463                                 'Which sysinfo properties to get:',
464                                 ' general      - Overall system information',
465                                 ' namespaces   - List of registered namespaces and their canonical names',
466                                 ' namespacealiases - List of registered namespace aliases',
467                                 ' specialpagealiases - List of special page aliases',
468                                 ' magicwords   - List of magic words and their aliases',
469                                 ' statistics   - Returns site statistics',
470                                 ' interwikimap - Returns interwiki map (optionally filtered)',
471                                 ' dbrepllag    - Returns database server with the highest replication lag',
472                                 ' usergroups   - Returns user groups and the associated permissions',
473                                 ' extensions   - Returns extensions installed on the wiki',
474                                 ' fileextensions - Returns list of file extensions allowed to be uploaded',
475                                 ' rightsinfo   - Returns wiki rights (license) information if available',
476                                 ' languages    - Returns a list of languages MediaWiki supports',
477                         ),
478                         'filteriw' =>  'Return only local or only nonlocal entries of the interwiki map',
479                         'showalldb' => 'List all database servers, not just the one lagging the most',
480                         'numberingroup' => 'Lists the number of users in user groups',
481                 );
482         }
483
484         public function getDescription() {
485                 return 'Return general information about the site.';
486         }
487
488         public function getPossibleErrors() {
489                 return array_merge( parent::getPossibleErrors(), array(
490                         array( 'code' => 'includeAllDenied', 'info' => 'Cannot view all servers info unless $wgShowHostnames is true' ),
491                 ) );
492         }
493
494         protected function getExamples() {
495                 return array(
496                         'api.php?action=query&meta=siteinfo&siprop=general|namespaces|namespacealiases|statistics',
497                         'api.php?action=query&meta=siteinfo&siprop=interwikimap&sifilteriw=local',
498                         'api.php?action=query&meta=siteinfo&siprop=dbrepllag&sishowalldb',
499                         );
500         }
501
502         public function getVersion() {
503                 return __CLASS__ . ': $Id: ApiQuerySiteinfo.php 69932 2010-07-26 08:03:21Z tstarling $';
504         }
505 }