Merge "Make it show email as required if you choose to email a random password."
[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 $vals['editcount'] = intval( $user->getEditCount() );
115 }
116
117 if ( isset( $this->prop['ratelimits'] ) ) {
118 $vals['ratelimits'] = $this->getRateLimits();
119 }
120
121 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
122 $vals['realname'] = $user->getRealName();
123 }
124
125 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
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 );
131 }
132 }
133 }
134
135 if ( isset( $this->prop['registrationdate'] ) ) {
136 $regDate = $user->getRegistration();
137 if ( $regDate !== false ) {
138 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
139 }
140 }
141
142 if ( isset( $this->prop['acceptlang'] ) ) {
143 $langs = $this->getRequest()->getAcceptLang();
144 $acceptLang = array();
145 foreach ( $langs as $lang => $val ) {
146 $r = array( 'q' => $val );
147 ApiResult::setContent( $r, $lang );
148 $acceptLang[] = $r;
149 }
150 $result->setIndexedTagName( $acceptLang, 'lang' );
151 $vals['acceptlang'] = $acceptLang;
152 }
153 return $vals;
154 }
155
156 protected function getRateLimits() {
157 global $wgRateLimits;
158 $user = $this->getUser();
159 if ( !$user->isPingLimitable() ) {
160 return array(); // No limits
161 }
162
163 // Find out which categories we belong to
164 $categories = array();
165 if ( $user->isAnon() ) {
166 $categories[] = 'anon';
167 } else {
168 $categories[] = 'user';
169 }
170 if ( $user->isNewbie() ) {
171 $categories[] = 'ip';
172 $categories[] = 'subnet';
173 if ( !$user->isAnon() ) {
174 $categories[] = 'newbie';
175 }
176 }
177 $categories = array_merge( $categories, $user->getGroups() );
178
179 // Now get the actual limits
180 $retval = array();
181 foreach ( $wgRateLimits as $action => $limits ) {
182 foreach ( $categories as $cat ) {
183 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
184 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
185 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
186 }
187 }
188 }
189 return $retval;
190 }
191
192 public function getAllowedParams() {
193 return array(
194 'prop' => array(
195 ApiBase::PARAM_DFLT => null,
196 ApiBase::PARAM_ISMULTI => true,
197 ApiBase::PARAM_TYPE => array(
198 'blockinfo',
199 'hasmsg',
200 'groups',
201 'implicitgroups',
202 'rights',
203 'changeablegroups',
204 'options',
205 'preferencestoken',
206 'editcount',
207 'ratelimits',
208 'email',
209 'realname',
210 'acceptlang',
211 'registrationdate'
212 )
213 )
214 );
215 }
216
217 public function getParamDescription() {
218 return array(
219 'prop' => array(
220 'What pieces of information to include',
221 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
222 ' hasmsg - Adds a tag "message" if the current user has pending messages',
223 ' groups - Lists all the groups the current user belongs to',
224 ' implicitgroups - Lists all the groups the current user is automatically a member of',
225 ' rights - Lists all the rights the current user has',
226 ' changeablegroups - Lists the groups the current user can add to and remove from',
227 ' options - Lists all preferences the current user has set',
228 ' preferencestoken - Get a token to change current user\'s preferences',
229 ' editcount - Adds the current user\'s edit count',
230 ' ratelimits - Lists all rate limits applying to the current user',
231 ' realname - Adds the user\'s real name',
232 ' email - Adds the user\'s email address and email authentication date',
233 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
234 ' registrationdate - Adds the user\'s registration date',
235 )
236 );
237 }
238
239 public function getResultProperties() {
240 return array(
241 ApiBase::PROP_LIST => false,
242 '' => array(
243 'id' => 'integer',
244 'name' => 'string',
245 'anon' => 'boolean'
246 ),
247 'blockinfo' => array(
248 'blockid' => array(
249 ApiBase::PROP_TYPE => 'integer',
250 ApiBase::PROP_NULLABLE => true
251 ),
252 'blockedby' => array(
253 ApiBase::PROP_TYPE => 'string',
254 ApiBase::PROP_NULLABLE => true
255 ),
256 'blockedbyid' => array(
257 ApiBase::PROP_TYPE => 'integer',
258 ApiBase::PROP_NULLABLE => true
259 ),
260 'blockedreason' => array(
261 ApiBase::PROP_TYPE => 'string',
262 ApiBase::PROP_NULLABLE => true
263 )
264 ),
265 'hasmsg' => array(
266 'messages' => 'boolean'
267 ),
268 'preferencestoken' => array(
269 'preferencestoken' => 'string'
270 ),
271 'editcount' => array(
272 'editcount' => 'integer'
273 ),
274 'realname' => array(
275 'realname' => array(
276 ApiBase::PROP_TYPE => 'string',
277 ApiBase::PROP_NULLABLE => true
278 )
279 ),
280 'email' => array(
281 'email' => 'string',
282 'emailauthenticated' => array(
283 ApiBase::PROP_TYPE => 'timestamp',
284 ApiBase::PROP_NULLABLE => true
285 )
286 ),
287 'registrationdate' => array(
288 'registrationdate' => array(
289 ApiBase::PROP_TYPE => 'timestamp',
290 ApiBase::PROP_NULLABLE => true
291 )
292 )
293 );
294 }
295
296 public function getDescription() {
297 return 'Get information about the current user';
298 }
299
300 public function getExamples() {
301 return array(
302 'api.php?action=query&meta=userinfo',
303 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
304 );
305 }
306
307 public function getHelpUrls() {
308 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
309 }
310 }