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' ) )
109 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
112 if ( isset( $this->prop
['editcount'] ) ) {
113 $vals['editcount'] = intval( $user->getEditCount() );
116 if ( isset( $this->prop
['ratelimits'] ) ) {
117 $vals['ratelimits'] = $this->getRateLimits();
120 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
121 $vals['realname'] = $user->getRealName();
124 if ( isset( $this->prop
['email'] ) ) {
125 $vals['email'] = $user->getEmail();
126 $auth = $user->getEmailAuthenticationTimestamp();
127 if ( !is_null( $auth ) ) {
128 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
132 if ( isset( $this->prop
['registrationdate'] ) ) {
133 $regDate = $user->getRegistration();
134 if ( $regDate !== false ) {
135 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
139 if ( isset( $this->prop
['acceptlang'] ) ) {
140 $langs = $this->getRequest()->getAcceptLang();
141 $acceptLang = array();
142 foreach ( $langs as $lang => $val ) {
143 $r = array( 'q' => $val );
144 ApiResult
::setContent( $r, $lang );
147 $result->setIndexedTagName( $acceptLang, 'lang' );
148 $vals['acceptlang'] = $acceptLang;
153 protected function getRateLimits() {
154 global $wgRateLimits;
155 $user = $this->getUser();
156 if ( !$user->isPingLimitable() ) {
157 return array(); // No limits
160 // Find out which categories we belong to
161 $categories = array();
162 if ( $user->isAnon() ) {
163 $categories[] = 'anon';
165 $categories[] = 'user';
167 if ( $user->isNewbie() ) {
168 $categories[] = 'ip';
169 $categories[] = 'subnet';
170 if ( !$user->isAnon() ) {
171 $categories[] = 'newbie';
174 $categories = array_merge( $categories, $user->getGroups() );
176 // Now get the actual limits
178 foreach ( $wgRateLimits as $action => $limits ) {
179 foreach ( $categories as $cat ) {
180 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
181 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
182 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
189 public function getAllowedParams() {
192 ApiBase
::PARAM_DFLT
=> null,
193 ApiBase
::PARAM_ISMULTI
=> true,
194 ApiBase
::PARAM_TYPE
=> array(
214 public function getParamDescription() {
217 'What pieces of information to include',
218 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
219 ' hasmsg - Adds a tag "message" if the current user has pending messages',
220 ' groups - Lists all the groups the current user belongs to',
221 ' implicitgroups - Lists all the groups the current user is automatically a member of',
222 ' rights - Lists all the rights the current user has',
223 ' changeablegroups - Lists the groups the current user can add to and remove from',
224 ' options - Lists all preferences the current user has set',
225 ' preferencestoken - Get a token to change current user\'s preferences',
226 ' editcount - Adds the current user\'s edit count',
227 ' ratelimits - Lists all rate limits applying to the current user',
228 ' realname - Adds the user\'s real name',
229 ' email - Adds the user\'s email address and email authentication date',
230 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
231 ' registrationdate - Adds the user\'s registration date',
236 public function getResultProperties() {
238 ApiBase
::PROP_LIST
=> false,
244 'blockinfo' => array(
246 ApiBase
::PROP_TYPE
=> 'integer',
247 ApiBase
::PROP_NULLABLE
=> true
249 'blockedby' => array(
250 ApiBase
::PROP_TYPE
=> 'string',
251 ApiBase
::PROP_NULLABLE
=> true
253 'blockedbyid' => array(
254 ApiBase
::PROP_TYPE
=> 'integer',
255 ApiBase
::PROP_NULLABLE
=> true
257 'blockedreason' => array(
258 ApiBase
::PROP_TYPE
=> 'string',
259 ApiBase
::PROP_NULLABLE
=> true
263 'messages' => 'boolean'
265 'preferencestoken' => array(
266 'preferencestoken' => 'string'
268 'editcount' => array(
269 'editcount' => 'integer'
273 ApiBase
::PROP_TYPE
=> 'string',
274 ApiBase
::PROP_NULLABLE
=> true
279 'emailauthenticated' => array(
280 ApiBase
::PROP_TYPE
=> 'timestamp',
281 ApiBase
::PROP_NULLABLE
=> true
284 'registrationdate' => array(
285 'registrationdate' => array(
286 ApiBase
::PROP_TYPE
=> 'timestamp',
287 ApiBase
::PROP_NULLABLE
=> true
293 public function getDescription() {
294 return 'Get information about the current user';
297 public function getExamples() {
299 'api.php?action=query&meta=userinfo',
300 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
304 public function getHelpUrls() {
305 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';