Merge "Check validity and availability of usernames during signup via AJAX"
[lhc/web/wiklou.git] / includes / api / ApiQueryUserInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to get information about the currently logged-in user
29 *
30 * @ingroup API
31 */
32 class ApiQueryUserInfo extends ApiQueryBase {
33
34 private $prop = array();
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'ui' );
38 }
39
40 public function execute() {
41 $params = $this->extractRequestParams();
42 $result = $this->getResult();
43
44 if ( !is_null( $params['prop'] ) ) {
45 $this->prop = array_flip( $params['prop'] );
46 }
47
48 $r = $this->getCurrentUserInfo();
49 $result->addValue( 'query', $this->getModuleName(), $r );
50 }
51
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();
59
60 if ( $user->isAnon() ) {
61 $vals['anon'] = '';
62 }
63
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();
71 }
72 }
73
74 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
75 $vals['messages'] = '';
76 }
77
78 if ( isset( $this->prop['groups'] ) ) {
79 $vals['groups'] = $user->getEffectiveGroups();
80 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
81 }
82
83 if ( isset( $this->prop['implicitgroups'] ) ) {
84 $vals['implicitgroups'] = $user->getAutomaticGroups();
85 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
86 }
87
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
92 }
93
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' );
100 }
101
102 if ( isset( $this->prop['options'] ) ) {
103 $vals['options'] = $user->getOptions();
104 }
105
106 if ( isset( $this->prop['preferencestoken'] ) &&
107 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
108 $user->isAllowed( 'editmyoptions' )
109 ) {
110 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
111 }
112
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() );
117 }
118
119 if ( isset( $this->prop['ratelimits'] ) ) {
120 $vals['ratelimits'] = $this->getRateLimits();
121 }
122
123 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
124 $vals['realname'] = $user->getRealName();
125 }
126
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 );
133 }
134 }
135 }
136
137 if ( isset( $this->prop['registrationdate'] ) ) {
138 $regDate = $user->getRegistration();
139 if ( $regDate !== false ) {
140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
141 }
142 }
143
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;
151 }
152 $result->setIndexedTagName( $acceptLang, 'lang' );
153 $vals['acceptlang'] = $acceptLang;
154 }
155
156 return $vals;
157 }
158
159 protected function getRateLimits() {
160 global $wgRateLimits;
161 $user = $this->getUser();
162 if ( !$user->isPingLimitable() ) {
163 return array(); // No limits
164 }
165
166 // Find out which categories we belong to
167 $categories = array();
168 if ( $user->isAnon() ) {
169 $categories[] = 'anon';
170 } else {
171 $categories[] = 'user';
172 }
173 if ( $user->isNewbie() ) {
174 $categories[] = 'ip';
175 $categories[] = 'subnet';
176 if ( !$user->isAnon() ) {
177 $categories[] = 'newbie';
178 }
179 }
180 $categories = array_merge( $categories, $user->getGroups() );
181
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] );
189 }
190 }
191 }
192
193 return $retval;
194 }
195
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'
216 )
217 )
218 );
219 }
220
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',
240 )
241 );
242 }
243
244 public function getResultProperties() {
245 return array(
246 ApiBase::PROP_LIST => false,
247 '' => array(
248 'id' => 'integer',
249 'name' => 'string',
250 'anon' => 'boolean'
251 ),
252 'blockinfo' => array(
253 'blockid' => array(
254 ApiBase::PROP_TYPE => 'integer',
255 ApiBase::PROP_NULLABLE => true
256 ),
257 'blockedby' => array(
258 ApiBase::PROP_TYPE => 'string',
259 ApiBase::PROP_NULLABLE => true
260 ),
261 'blockedbyid' => array(
262 ApiBase::PROP_TYPE => 'integer',
263 ApiBase::PROP_NULLABLE => true
264 ),
265 'blockedreason' => array(
266 ApiBase::PROP_TYPE => 'string',
267 ApiBase::PROP_NULLABLE => true
268 )
269 ),
270 'hasmsg' => array(
271 'messages' => 'boolean'
272 ),
273 'preferencestoken' => array(
274 'preferencestoken' => 'string'
275 ),
276 'editcount' => array(
277 'editcount' => 'integer'
278 ),
279 'realname' => array(
280 'realname' => array(
281 ApiBase::PROP_TYPE => 'string',
282 ApiBase::PROP_NULLABLE => true
283 )
284 ),
285 'email' => array(
286 'email' => 'string',
287 'emailauthenticated' => array(
288 ApiBase::PROP_TYPE => 'timestamp',
289 ApiBase::PROP_NULLABLE => true
290 )
291 ),
292 'registrationdate' => array(
293 'registrationdate' => array(
294 ApiBase::PROP_TYPE => 'timestamp',
295 ApiBase::PROP_NULLABLE => true
296 )
297 )
298 );
299 }
300
301 public function getDescription() {
302 return 'Get information about the current user.';
303 }
304
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',
309 );
310 }
311
312 public function getHelpUrls() {
313 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
314 }
315 }