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 $autolist = ApiQueryUsers
::getAutoGroups( $user );
81 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
82 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
85 if ( isset( $this->prop
['implicitgroups'] ) ) {
86 $vals['implicitgroups'] = ApiQueryUsers
::getAutoGroups( $user );
87 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
90 if ( isset( $this->prop
['rights'] ) ) {
91 // User::getRights() may return duplicate values, strip them
92 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
93 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
96 if ( isset( $this->prop
['changeablegroups'] ) ) {
97 $vals['changeablegroups'] = $user->changeableGroups();
98 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
101 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
104 if ( isset( $this->prop
['options'] ) ) {
105 $vals['options'] = $user->getOptions();
108 if ( isset( $this->prop
['preferencestoken'] ) &&
109 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
111 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
114 if ( isset( $this->prop
['editcount'] ) ) {
115 $vals['editcount'] = intval( $user->getEditCount() );
118 if ( isset( $this->prop
['ratelimits'] ) ) {
119 $vals['ratelimits'] = $this->getRateLimits();
122 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
123 $vals['realname'] = $user->getRealName();
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 );
134 if ( isset( $this->prop
['registrationdate'] ) ) {
135 $regDate = $user->getRegistration();
136 if ( $regDate !== false ) {
137 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
141 if ( isset( $this->prop
['acceptlang'] ) ) {
142 $langs = $this->getRequest()->getAcceptLang();
143 $acceptLang = array();
144 foreach ( $langs as $lang => $val ) {
145 $r = array( 'q' => $val );
146 ApiResult
::setContent( $r, $lang );
149 $result->setIndexedTagName( $acceptLang, 'lang' );
150 $vals['acceptlang'] = $acceptLang;
155 protected function getRateLimits() {
156 global $wgRateLimits;
157 $user = $this->getUser();
158 if ( !$user->isPingLimitable() ) {
159 return array(); // No limits
162 // Find out which categories we belong to
163 $categories = array();
164 if ( $user->isAnon() ) {
165 $categories[] = 'anon';
167 $categories[] = 'user';
169 if ( $user->isNewbie() ) {
170 $categories[] = 'ip';
171 $categories[] = 'subnet';
172 if ( !$user->isAnon() )
173 $categories[] = 'newbie';
175 $categories = array_merge( $categories, $user->getGroups() );
177 // Now get the actual limits
179 foreach ( $wgRateLimits as $action => $limits ) {
180 foreach ( $categories as $cat ) {
181 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
182 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
183 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
190 public function getAllowedParams() {
193 ApiBase
::PARAM_DFLT
=> null,
194 ApiBase
::PARAM_ISMULTI
=> true,
195 ApiBase
::PARAM_TYPE
=> array(
215 public function getParamDescription() {
218 'What pieces of information to include',
219 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
220 ' hasmsg - Adds a tag "message" if the current user has pending messages',
221 ' groups - Lists all the groups the current user belongs to',
222 ' implicitgroups - Lists all the groups the current user is automatically a member of',
223 ' rights - Lists all the rights the current user has',
224 ' changeablegroups - Lists the groups the current user can add to and remove from',
225 ' options - Lists all preferences the current user has set',
226 ' preferencestoken - Get a token to change current user\'s preferences',
227 ' editcount - Adds the current user\'s edit count',
228 ' ratelimits - Lists all rate limits applying to the current user',
229 ' realname - Adds the user\'s real name',
230 ' email - Adds the user\'s email address and email authentication date',
231 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
232 ' registrationdate - Adds the user\'s registration date',
237 public function getResultProperties() {
239 ApiBase
::PROP_LIST
=> false,
245 'blockinfo' => array(
247 ApiBase
::PROP_TYPE
=> 'integer',
248 ApiBase
::PROP_NULLABLE
=> true
250 'blockedby' => array(
251 ApiBase
::PROP_TYPE
=> 'string',
252 ApiBase
::PROP_NULLABLE
=> true
254 'blockedbyid' => array(
255 ApiBase
::PROP_TYPE
=> 'integer',
256 ApiBase
::PROP_NULLABLE
=> true
258 'blockedreason' => array(
259 ApiBase
::PROP_TYPE
=> 'string',
260 ApiBase
::PROP_NULLABLE
=> true
264 'messages' => 'boolean'
266 'preferencestoken' => array(
267 'preferencestoken' => 'string'
269 'editcount' => array(
270 'editcount' => 'integer'
274 ApiBase
::PROP_TYPE
=> 'string',
275 ApiBase
::PROP_NULLABLE
=> true
280 'emailauthenticated' => array(
281 ApiBase
::PROP_TYPE
=> 'timestamp',
282 ApiBase
::PROP_NULLABLE
=> true
285 'registrationdate' => array(
286 'registrationdate' => array(
287 ApiBase
::PROP_TYPE
=> 'timestamp',
288 ApiBase
::PROP_NULLABLE
=> true
294 public function getDescription() {
295 return 'Get information about the current user';
298 public function getExamples() {
300 'api.php?action=query&meta=userinfo',
301 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
305 public function getHelpUrls() {
306 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
309 public function getVersion() {
310 return __CLASS__
. ': $Id$';