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 $block = $user->getBlock();
67 $vals['blockid'] = $block->getId();
68 $vals['blockedby'] = $block->getByName();
69 $vals['blockedbyid'] = $block->getBy();
70 $vals['blockreason'] = $user->blockedFor();
74 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
75 $vals['messages'] = '';
78 if ( isset( $this->prop
['groups'] ) ) {
79 $vals['groups'] = $user->getEffectiveGroups();
80 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
83 if ( isset( $this->prop
['implicitgroups'] ) ) {
84 $vals['implicitgroups'] = $user->getAutomaticGroups();
85 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
88 if ( isset( $this->prop
['rights'] ) ) {
89 // User::getRights() may return duplicate values, strip them
90 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
91 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
94 if ( isset( $this->prop
['changeablegroups'] ) ) {
95 $vals['changeablegroups'] = $user->changeableGroups();
96 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
102 if ( isset( $this->prop
['options'] ) ) {
103 $vals['options'] = $user->getOptions();
106 if ( isset( $this->prop
['preferencestoken'] ) &&
107 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
108 $user->isAllowed( 'editmyoptions' )
110 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
113 if ( isset( $this->prop
['editcount'] ) ) {
114 $vals['editcount'] = intval( $user->getEditCount() );
117 if ( isset( $this->prop
['ratelimits'] ) ) {
118 $vals['ratelimits'] = $this->getRateLimits();
121 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
122 $vals['realname'] = $user->getRealName();
125 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
126 if ( isset( $this->prop
['email'] ) ) {
127 $vals['email'] = $user->getEmail();
128 $auth = $user->getEmailAuthenticationTimestamp();
129 if ( !is_null( $auth ) ) {
130 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
135 if ( isset( $this->prop
['registrationdate'] ) ) {
136 $regDate = $user->getRegistration();
137 if ( $regDate !== false ) {
138 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
142 if ( isset( $this->prop
['acceptlang'] ) ) {
143 $langs = $this->getRequest()->getAcceptLang();
144 $acceptLang = array();
145 foreach ( $langs as $lang => $val ) {
146 $r = array( 'q' => $val );
147 ApiResult
::setContent( $r, $lang );
150 $result->setIndexedTagName( $acceptLang, 'lang' );
151 $vals['acceptlang'] = $acceptLang;
156 protected function getRateLimits() {
157 global $wgRateLimits;
158 $user = $this->getUser();
159 if ( !$user->isPingLimitable() ) {
160 return array(); // No limits
163 // Find out which categories we belong to
164 $categories = array();
165 if ( $user->isAnon() ) {
166 $categories[] = 'anon';
168 $categories[] = 'user';
170 if ( $user->isNewbie() ) {
171 $categories[] = 'ip';
172 $categories[] = 'subnet';
173 if ( !$user->isAnon() ) {
174 $categories[] = 'newbie';
177 $categories = array_merge( $categories, $user->getGroups() );
179 // Now get the actual limits
181 foreach ( $wgRateLimits as $action => $limits ) {
182 foreach ( $categories as $cat ) {
183 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
184 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
185 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
192 public function getAllowedParams() {
195 ApiBase
::PARAM_DFLT
=> null,
196 ApiBase
::PARAM_ISMULTI
=> true,
197 ApiBase
::PARAM_TYPE
=> array(
217 public function getParamDescription() {
220 'What pieces of information to include',
221 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
222 ' hasmsg - Adds a tag "message" if the current user has pending messages',
223 ' groups - Lists all the groups the current user belongs to',
224 ' implicitgroups - Lists all the groups the current user is automatically a member of',
225 ' rights - Lists all the rights the current user has',
226 ' changeablegroups - Lists the groups the current user can add to and remove from',
227 ' options - Lists all preferences the current user has set',
228 ' preferencestoken - Get a token to change current user\'s preferences',
229 ' editcount - Adds the current user\'s edit count',
230 ' ratelimits - Lists all rate limits applying to the current user',
231 ' realname - Adds the user\'s real name',
232 ' email - Adds the user\'s email address and email authentication date',
233 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
234 ' registrationdate - Adds the user\'s registration date',
239 public function getResultProperties() {
241 ApiBase
::PROP_LIST
=> false,
247 'blockinfo' => array(
249 ApiBase
::PROP_TYPE
=> 'integer',
250 ApiBase
::PROP_NULLABLE
=> true
252 'blockedby' => array(
253 ApiBase
::PROP_TYPE
=> 'string',
254 ApiBase
::PROP_NULLABLE
=> true
256 'blockedbyid' => array(
257 ApiBase
::PROP_TYPE
=> 'integer',
258 ApiBase
::PROP_NULLABLE
=> true
260 'blockedreason' => array(
261 ApiBase
::PROP_TYPE
=> 'string',
262 ApiBase
::PROP_NULLABLE
=> true
266 'messages' => 'boolean'
268 'preferencestoken' => array(
269 'preferencestoken' => 'string'
271 'editcount' => array(
272 'editcount' => 'integer'
276 ApiBase
::PROP_TYPE
=> 'string',
277 ApiBase
::PROP_NULLABLE
=> true
282 'emailauthenticated' => array(
283 ApiBase
::PROP_TYPE
=> 'timestamp',
284 ApiBase
::PROP_NULLABLE
=> true
287 'registrationdate' => array(
288 'registrationdate' => array(
289 ApiBase
::PROP_TYPE
=> 'timestamp',
290 ApiBase
::PROP_NULLABLE
=> true
296 public function getDescription() {
297 return 'Get information about the current user';
300 public function getExamples() {
302 'api.php?action=query&meta=userinfo',
303 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
307 public function getHelpUrls() {
308 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';