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
28 * Query module to get information about the currently logged-in user
32 class ApiQueryUserInfo
extends ApiQueryBase
{
34 private $prop = array();
36 public function __construct( $query, $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'ui' );
40 public function execute() {
41 $params = $this->extractRequestParams();
42 $result = $this->getResult();
44 if ( !is_null( $params['prop'] ) ) {
45 $this->prop
= array_flip( $params['prop'] );
48 $r = $this->getCurrentUserInfo();
49 $result->addValue( 'query', $this->getModuleName(), $r );
52 protected function getCurrentUserInfo() {
53 global $wgHiddenPrefs;
54 $user = $this->getUser();
55 $result = $this->getResult();
57 $vals['id'] = intval( $user->getId() );
58 $vals['name'] = $user->getName();
60 if ( $user->isAnon() ) {
64 if ( isset( $this->prop
['blockinfo'] ) ) {
65 if ( $user->isBlocked() ) {
66 $vals['blockedby'] = User
::whoIs( $user->blockedBy() );
67 $vals['blockreason'] = $user->blockedFor();
71 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
72 $vals['messages'] = '';
75 if ( isset( $this->prop
['groups'] ) ) {
76 $autolist = ApiQueryUsers
::getAutoGroups( $user );
78 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
79 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
82 if ( isset( $this->prop
['implicitgroups'] ) ) {
83 $vals['implicitgroups'] = ApiQueryUsers
::getAutoGroups( $user );
84 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
87 if ( isset( $this->prop
['rights'] ) ) {
88 // User::getRights() may return duplicate values, strip them
89 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
90 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
93 if ( isset( $this->prop
['changeablegroups'] ) ) {
94 $vals['changeablegroups'] = $user->changeableGroups();
95 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
96 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
101 if ( isset( $this->prop
['options'] ) ) {
102 $vals['options'] = $user->getOptions();
105 if ( isset( $this->prop
['optionstoken'] ) &&
106 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
108 $vals['optionstoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
111 if ( isset( $this->prop
['preferencestoken'] ) &&
112 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
114 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
117 if ( isset( $this->prop
['editcount'] ) ) {
118 $vals['editcount'] = intval( $user->getEditCount() );
121 if ( isset( $this->prop
['ratelimits'] ) ) {
122 $vals['ratelimits'] = $this->getRateLimits();
125 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
126 $vals['realname'] = $user->getRealName();
129 if ( isset( $this->prop
['email'] ) ) {
130 $vals['email'] = $user->getEmail();
131 $auth = $user->getEmailAuthenticationTimestamp();
132 if ( !is_null( $auth ) ) {
133 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
137 if ( isset( $this->prop
['registrationdate'] ) ) {
138 $regDate = $user->getRegistration();
139 if ( $regDate !== false ) {
140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
144 if ( isset( $this->prop
['acceptlang'] ) ) {
145 $langs = $this->getRequest()->getAcceptLang();
146 $acceptLang = array();
147 foreach ( $langs as $lang => $val ) {
148 $r = array( 'q' => $val );
149 ApiResult
::setContent( $r, $lang );
152 $result->setIndexedTagName( $acceptLang, 'lang' );
153 $vals['acceptlang'] = $acceptLang;
158 protected function getRateLimits() {
159 global $wgRateLimits;
160 $user = $this->getUser();
161 if ( !$user->isPingLimitable() ) {
162 return array(); // No limits
165 // Find out which categories we belong to
166 $categories = array();
167 if ( $user->isAnon() ) {
168 $categories[] = 'anon';
170 $categories[] = 'user';
172 if ( $user->isNewbie() ) {
173 $categories[] = 'ip';
174 $categories[] = 'subnet';
175 if ( !$user->isAnon() )
176 $categories[] = 'newbie';
178 $categories = array_merge( $categories, $user->getGroups() );
180 // Now get the actual limits
182 foreach ( $wgRateLimits as $action => $limits ) {
183 foreach ( $categories as $cat ) {
184 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
185 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
186 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
193 public function getAllowedParams() {
196 ApiBase
::PARAM_DFLT
=> null,
197 ApiBase
::PARAM_ISMULTI
=> true,
198 ApiBase
::PARAM_TYPE
=> array(
219 public function getParamDescription() {
222 'What pieces of information to include',
223 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
224 ' hasmsg - Adds a tag "message" if the current user has pending messages',
225 ' groups - Lists all the groups the current user belongs to',
226 ' implicitgroups - Lists all the groups the current user is automatically a member of',
227 ' rights - Lists all the rights the current user has',
228 ' changeablegroups - Lists the groups the current user can add to and remove from',
229 ' options - Lists all preferences the current user has set',
230 ' optionstoken - Get a token to change current user\'s preferences',
231 ' preferencestoken - Get a token to change current user\'s preferences',
232 ' editcount - Adds the current user\'s edit count',
233 ' ratelimits - Lists all rate limits applying to the current user',
234 ' realname - Adds the user\'s real name',
235 ' email - Adds the user\'s email address and email authentication date',
236 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
237 ' registrationdate - Adds the user\'s registration date',
242 public function getDescription() {
243 return 'Get information about the current user';
246 public function getExamples() {
248 'api.php?action=query&meta=userinfo',
249 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
253 public function getHelpUrls() {
254 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
257 public function getVersion() {
258 return __CLASS__
. ': $Id$';