3 * API for MediaWiki 1.8+
5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to get information about the currently logged-in user
37 class ApiQueryUserInfo
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'ui' );
43 public function execute() {
44 $params = $this->extractRequestParams();
45 $result = $this->getResult();
48 if ( !is_null( $params['prop'] ) ) {
49 $this->prop
= array_flip( $params['prop'] );
51 $this->prop
= array();
53 $r = $this->getCurrentUserInfo();
54 $result->addValue( 'query', $this->getModuleName(), $r );
57 protected function getCurrentUserInfo() {
58 global $wgUser, $wgRequest;
59 $result = $this->getResult();
61 $vals['id'] = intval( $wgUser->getId() );
62 $vals['name'] = $wgUser->getName();
64 if ( $wgUser->isAnon() ) {
68 if ( isset( $this->prop
['blockinfo'] ) ) {
69 if ( $wgUser->isBlocked() ) {
70 $vals['blockedby'] = User
::whoIs( $wgUser->blockedBy() );
71 $vals['blockreason'] = $wgUser->blockedFor();
75 if ( isset( $this->prop
['hasmsg'] ) && $wgUser->getNewtalk() ) {
76 $vals['messages'] = '';
79 if ( isset( $this->prop
['groups'] ) ) {
80 $autolist = ApiQueryUsers
::getAutoGroups( $wgUser );
82 $vals['groups'] = array_merge( $autolist, $wgUser->getGroups() );
83 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
86 if ( isset( $this->prop
['rights'] ) ) {
87 // User::getRights() may return duplicate values, strip them
88 $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
89 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
92 if ( isset( $this->prop
['changeablegroups'] ) ) {
93 $vals['changeablegroups'] = $wgUser->changeableGroups();
94 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
95 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
96 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
100 if ( isset( $this->prop
['options'] ) ) {
101 $vals['options'] = $wgUser->getOptions();
105 isset( $this->prop
['preferencestoken'] ) &&
106 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
109 $vals['preferencestoken'] = $wgUser->editToken();
112 if ( isset( $this->prop
['editcount'] ) ) {
113 $vals['editcount'] = intval( $wgUser->getEditCount() );
116 if ( isset( $this->prop
['ratelimits'] ) ) {
117 $vals['ratelimits'] = $this->getRateLimits();
120 if ( isset( $this->prop
['email'] ) ) {
121 $vals['email'] = $wgUser->getEmail();
122 $auth = $wgUser->getEmailAuthenticationTimestamp();
123 if ( !is_null( $auth ) ) {
124 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
128 if ( isset( $this->prop
['acceptlang'] ) ) {
129 $langs = $wgRequest->getAcceptLang();
130 $acceptLang = array();
131 foreach ( $langs as $lang => $val ) {
132 $r = array( 'q' => $val );
133 ApiResult
::setContent( $r, $lang );
136 $result->setIndexedTagName( $acceptLang, 'lang' );
137 $vals['acceptlang'] = $acceptLang;
142 protected function getRateLimits() {
143 global $wgUser, $wgRateLimits;
144 if ( !$wgUser->isPingLimitable() ) {
145 return array(); // No limits
148 // Find out which categories we belong to
149 $categories = array();
150 if ( $wgUser->isAnon() ) {
151 $categories[] = 'anon';
153 $categories[] = 'user';
155 if ( $wgUser->isNewbie() ) {
156 $categories[] = 'ip';
157 $categories[] = 'subnet';
158 if ( !$wgUser->isAnon() )
159 $categories[] = 'newbie';
161 $categories = array_merge( $categories, $wgUser->getGroups() );
163 // Now get the actual limits
165 foreach ( $wgRateLimits as $action => $limits ) {
166 foreach ( $categories as $cat ) {
167 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
168 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
169 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
176 public function getAllowedParams() {
179 ApiBase
::PARAM_DFLT
=> null,
180 ApiBase
::PARAM_ISMULTI
=> true,
181 ApiBase
::PARAM_TYPE
=> array(
198 public function getParamDescription() {
201 'What pieces of information to include',
202 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
203 ' hasmsg - adds a tag "message" if the current user has pending messages',
204 ' groups - lists all the groups the current user belongs to',
205 ' rights - lists all the rights the current user has',
206 ' changeablegroups - lists the groups the current user can add to and remove from',
207 ' options - lists all preferences the current user has set',
208 ' editcount - adds the current user\'s edit count',
209 ' ratelimits - lists all rate limits applying to the current user',
210 ' email - adds the user\'s email address and email authentication date',
211 ' acceptlang - echoes the Accept-Language header sent by the client in a structured format',
216 public function getDescription() {
217 return 'Get information about the current user';
220 protected function getExamples() {
222 'api.php?action=query&meta=userinfo',
223 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
227 public function getVersion() {
228 return __CLASS__
. ': $Id$';