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 $wgRequest, $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
['preferencestoken'] ) &&
106 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
108 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
111 if ( isset( $this->prop
['editcount'] ) ) {
112 $vals['editcount'] = intval( $user->getEditCount() );
115 if ( isset( $this->prop
['ratelimits'] ) ) {
116 $vals['ratelimits'] = $this->getRateLimits();
119 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
120 $vals['realname'] = $user->getRealName();
123 if ( isset( $this->prop
['email'] ) ) {
124 $vals['email'] = $user->getEmail();
125 $auth = $user->getEmailAuthenticationTimestamp();
126 if ( !is_null( $auth ) ) {
127 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
131 if ( isset( $this->prop
['registrationdate'] ) ) {
132 $regDate = $user->getRegistration();
133 if ( $regDate !== false ) {
134 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
138 if ( isset( $this->prop
['acceptlang'] ) ) {
139 $langs = $wgRequest->getAcceptLang();
140 $acceptLang = array();
141 foreach ( $langs as $lang => $val ) {
142 $r = array( 'q' => $val );
143 ApiResult
::setContent( $r, $lang );
146 $result->setIndexedTagName( $acceptLang, 'lang' );
147 $vals['acceptlang'] = $acceptLang;
152 protected function getRateLimits() {
153 global $wgRateLimits;
154 $user = $this->getUser();
155 if ( !$user->isPingLimitable() ) {
156 return array(); // No limits
159 // Find out which categories we belong to
160 $categories = array();
161 if ( $user->isAnon() ) {
162 $categories[] = 'anon';
164 $categories[] = 'user';
166 if ( $user->isNewbie() ) {
167 $categories[] = 'ip';
168 $categories[] = 'subnet';
169 if ( !$user->isAnon() )
170 $categories[] = 'newbie';
172 $categories = array_merge( $categories, $user->getGroups() );
174 // Now get the actual limits
176 foreach ( $wgRateLimits as $action => $limits ) {
177 foreach ( $categories as $cat ) {
178 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
179 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
180 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
187 public function getAllowedParams() {
190 ApiBase
::PARAM_DFLT
=> null,
191 ApiBase
::PARAM_ISMULTI
=> true,
192 ApiBase
::PARAM_TYPE
=> array(
212 public function getParamDescription() {
215 'What pieces of information to include',
216 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
217 ' hasmsg - Adds a tag "message" if the current user has pending messages',
218 ' groups - Lists all the groups the current user belongs to',
219 ' implicitgroups - Lists all the groups the current user is automatically a member of',
220 ' rights - Lists all the rights the current user has',
221 ' changeablegroups - Lists the groups the current user can add to and remove from',
222 ' options - Lists all preferences the current user has set',
223 ' preferencestoken - Get a token to change current user\'s preferences',
224 ' editcount - Adds the current user\'s edit count',
225 ' ratelimits - Lists all rate limits applying to the current user',
226 ' realname - Adds the user\'s real name',
227 ' email - Adds the user\'s email address and email authentication date',
228 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
229 ' registrationdate - Adds the user\'s registration date',
234 public function getDescription() {
235 return 'Get information about the current user';
238 public function getExamples() {
240 'api.php?action=query&meta=userinfo',
241 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
245 public function getHelpUrls() {
246 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
249 public function getVersion() {
250 return __CLASS__
. ': $Id$';