Merge "Update indentation"
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob37cf4830399ff16bd8655de7d41428796d468541
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 $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 // use intval to prevent null if a non-logged-in user calls
115 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
116 $vals['editcount'] = intval( $user->getEditCount() );
119 if ( isset( $this->prop['ratelimits'] ) ) {
120 $vals['ratelimits'] = $this->getRateLimits();
123 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
124 $vals['realname'] = $user->getRealName();
127 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
128 if ( isset( $this->prop['email'] ) ) {
129 $vals['email'] = $user->getEmail();
130 $auth = $user->getEmailAuthenticationTimestamp();
131 if ( !is_null( $auth ) ) {
132 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
137 if ( isset( $this->prop['registrationdate'] ) ) {
138 $regDate = $user->getRegistration();
139 if ( $regDate !== false ) {
140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
144 if ( isset( $this->prop['acceptlang'] ) ) {
145 $langs = $this->getRequest()->getAcceptLang();
146 $acceptLang = array();
147 foreach ( $langs as $lang => $val ) {
148 $r = array( 'q' => $val );
149 ApiResult::setContent( $r, $lang );
150 $acceptLang[] = $r;
152 $result->setIndexedTagName( $acceptLang, 'lang' );
153 $vals['acceptlang'] = $acceptLang;
156 return $vals;
159 protected function getRateLimits() {
160 global $wgRateLimits;
161 $user = $this->getUser();
162 if ( !$user->isPingLimitable() ) {
163 return array(); // No limits
166 // Find out which categories we belong to
167 $categories = array();
168 if ( $user->isAnon() ) {
169 $categories[] = 'anon';
170 } else {
171 $categories[] = 'user';
173 if ( $user->isNewbie() ) {
174 $categories[] = 'ip';
175 $categories[] = 'subnet';
176 if ( !$user->isAnon() ) {
177 $categories[] = 'newbie';
180 $categories = array_merge( $categories, $user->getGroups() );
182 // Now get the actual limits
183 $retval = array();
184 foreach ( $wgRateLimits as $action => $limits ) {
185 foreach ( $categories as $cat ) {
186 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
187 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
188 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
193 return $retval;
196 public function getAllowedParams() {
197 return array(
198 'prop' => array(
199 ApiBase::PARAM_DFLT => null,
200 ApiBase::PARAM_ISMULTI => true,
201 ApiBase::PARAM_TYPE => array(
202 'blockinfo',
203 'hasmsg',
204 'groups',
205 'implicitgroups',
206 'rights',
207 'changeablegroups',
208 'options',
209 'preferencestoken',
210 'editcount',
211 'ratelimits',
212 'email',
213 'realname',
214 'acceptlang',
215 'registrationdate'
221 public function getParamDescription() {
222 return array(
223 'prop' => array(
224 'What pieces of information to include',
225 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
226 ' hasmsg - Adds a tag "message" if the current user has pending messages',
227 ' groups - Lists all the groups the current user belongs to',
228 ' implicitgroups - Lists all the groups the current user is automatically a member of',
229 ' rights - Lists all the rights the current user has',
230 ' changeablegroups - Lists the groups the current user can add to and remove from',
231 ' options - Lists all preferences the current user has set',
232 ' preferencestoken - Get a token to change current user\'s preferences',
233 ' editcount - Adds the current user\'s edit count',
234 ' ratelimits - Lists all rate limits applying to the current user',
235 ' realname - Adds the user\'s real name',
236 ' email - Adds the user\'s email address and email authentication date',
237 ' acceptlang - Echoes the Accept-Language header sent by ' .
238 'the client in a structured format',
239 ' registrationdate - Adds the user\'s registration date',
244 public function getResultProperties() {
245 return array(
246 ApiBase::PROP_LIST => false,
247 '' => array(
248 'id' => 'integer',
249 'name' => 'string',
250 'anon' => 'boolean'
252 'blockinfo' => array(
253 'blockid' => array(
254 ApiBase::PROP_TYPE => 'integer',
255 ApiBase::PROP_NULLABLE => true
257 'blockedby' => array(
258 ApiBase::PROP_TYPE => 'string',
259 ApiBase::PROP_NULLABLE => true
261 'blockedbyid' => array(
262 ApiBase::PROP_TYPE => 'integer',
263 ApiBase::PROP_NULLABLE => true
265 'blockedreason' => array(
266 ApiBase::PROP_TYPE => 'string',
267 ApiBase::PROP_NULLABLE => true
270 'hasmsg' => array(
271 'messages' => 'boolean'
273 'preferencestoken' => array(
274 'preferencestoken' => 'string'
276 'editcount' => array(
277 'editcount' => 'integer'
279 'realname' => array(
280 'realname' => array(
281 ApiBase::PROP_TYPE => 'string',
282 ApiBase::PROP_NULLABLE => true
285 'email' => array(
286 'email' => 'string',
287 'emailauthenticated' => array(
288 ApiBase::PROP_TYPE => 'timestamp',
289 ApiBase::PROP_NULLABLE => true
292 'registrationdate' => array(
293 'registrationdate' => array(
294 ApiBase::PROP_TYPE => 'timestamp',
295 ApiBase::PROP_NULLABLE => true
301 public function getDescription() {
302 return 'Get information about the current user';
305 public function getExamples() {
306 return array(
307 'api.php?action=query&meta=userinfo',
308 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
312 public function getHelpUrls() {
313 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';