Localisation updates from http://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blobe4617d976cec3445b40c66d159d33552704f6f9c
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to get information about the currently logged-in user
30 * @ingroup API
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();
56 $vals = array();
57 $vals['id'] = intval( $user->getId() );
58 $vals['name'] = $user->getName();
60 if ( $user->isAnon() ) {
61 $vals['anon'] = '';
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 );
147 $acceptLang[] = $r;
149 $result->setIndexedTagName( $acceptLang, 'lang' );
150 $vals['acceptlang'] = $acceptLang;
152 return $vals;
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';
166 } else {
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
178 $retval = array();
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] );
187 return $retval;
190 public function getAllowedParams() {
191 return array(
192 'prop' => array(
193 ApiBase::PARAM_DFLT => null,
194 ApiBase::PARAM_ISMULTI => true,
195 ApiBase::PARAM_TYPE => array(
196 'blockinfo',
197 'hasmsg',
198 'groups',
199 'implicitgroups',
200 'rights',
201 'changeablegroups',
202 'options',
203 'preferencestoken',
204 'editcount',
205 'ratelimits',
206 'email',
207 'realname',
208 'acceptlang',
209 'registrationdate'
215 public function getParamDescription() {
216 return array(
217 'prop' => array(
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() {
238 return array(
239 ApiBase::PROP_LIST => false,
240 '' => array(
241 'id' => 'integer',
242 'name' => 'string',
243 'anon' => 'boolean'
245 'blockinfo' => array(
246 'blockid' => 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
263 'hasmsg' => array(
264 'messages' => 'boolean'
266 'preferencestoken' => array(
267 'preferencestoken' => 'string'
269 'editcount' => array(
270 'editcount' => 'integer'
272 'realname' => array(
273 'realname' => array(
274 ApiBase::PROP_TYPE => 'string',
275 ApiBase::PROP_NULLABLE => true
278 'email' => array(
279 'email' => 'string',
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() {
299 return array(
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$';