Merge "LoginForm: Pass username as first parameter to error message if aborted"
[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 const WL_UNREAD_LIMIT = 1000;
35
36 private $prop = array();
37
38 public function __construct( ApiQuery $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ui' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45
46 if ( !is_null( $params['prop'] ) ) {
47 $this->prop = array_flip( $params['prop'] );
48 }
49
50 $r = $this->getCurrentUserInfo();
51 $result->addValue( 'query', $this->getModuleName(), $r );
52 }
53
54 protected function getCurrentUserInfo() {
55 global $wgHiddenPrefs, $wgRCMaxAge;
56 $user = $this->getUser();
57 $result = $this->getResult();
58 $vals = array();
59 $vals['id'] = intval( $user->getId() );
60 $vals['name'] = $user->getName();
61
62 if ( $user->isAnon() ) {
63 $vals['anon'] = '';
64 }
65
66 if ( isset( $this->prop['blockinfo'] ) ) {
67 if ( $user->isBlocked() ) {
68 $block = $user->getBlock();
69 $vals['blockid'] = $block->getId();
70 $vals['blockedby'] = $block->getByName();
71 $vals['blockedbyid'] = $block->getBy();
72 $vals['blockreason'] = $user->blockedFor();
73 }
74 }
75
76 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
77 $vals['messages'] = '';
78 }
79
80 if ( isset( $this->prop['groups'] ) ) {
81 $vals['groups'] = $user->getEffectiveGroups();
82 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
83 }
84
85 if ( isset( $this->prop['implicitgroups'] ) ) {
86 $vals['implicitgroups'] = $user->getAutomaticGroups();
87 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
88 }
89
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
94 }
95
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' );
102 }
103
104 if ( isset( $this->prop['options'] ) ) {
105 $vals['options'] = $user->getOptions();
106 }
107
108 if ( isset( $this->prop['preferencestoken'] ) &&
109 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
110 $user->isAllowed( 'editmyoptions' )
111 ) {
112 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
113 }
114
115 if ( isset( $this->prop['editcount'] ) ) {
116 // use intval to prevent null if a non-logged-in user calls
117 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
118 $vals['editcount'] = intval( $user->getEditCount() );
119 }
120
121 if ( isset( $this->prop['ratelimits'] ) ) {
122 $vals['ratelimits'] = $this->getRateLimits();
123 }
124
125 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
126 $vals['realname'] = $user->getRealName();
127 }
128
129 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
130 if ( isset( $this->prop['email'] ) ) {
131 $vals['email'] = $user->getEmail();
132 $auth = $user->getEmailAuthenticationTimestamp();
133 if ( !is_null( $auth ) ) {
134 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
135 }
136 }
137 }
138
139 if ( isset( $this->prop['registrationdate'] ) ) {
140 $regDate = $user->getRegistration();
141 if ( $regDate !== false ) {
142 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
143 }
144 }
145
146 if ( isset( $this->prop['acceptlang'] ) ) {
147 $langs = $this->getRequest()->getAcceptLang();
148 $acceptLang = array();
149 foreach ( $langs as $lang => $val ) {
150 $r = array( 'q' => $val );
151 ApiResult::setContent( $r, $lang );
152 $acceptLang[] = $r;
153 }
154 $result->setIndexedTagName( $acceptLang, 'lang' );
155 $vals['acceptlang'] = $acceptLang;
156 }
157
158 if ( isset( $this->prop['unreadcount'] ) ) {
159 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
160
161 $sql = $dbr->selectSQLText(
162 'watchlist',
163 array( 'dummy' => 1 ),
164 array(
165 'wl_user' => $user->getId(),
166 'wl_notificationtimestamp IS NOT NULL',
167 ),
168 __METHOD__,
169 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
170 );
171 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
172
173 if ( $count >= self::WL_UNREAD_LIMIT ) {
174 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
175 } else {
176 $vals['unreadcount'] = (int)$count;
177 }
178 }
179
180 return $vals;
181 }
182
183 protected function getRateLimits() {
184 global $wgRateLimits;
185 $user = $this->getUser();
186 if ( !$user->isPingLimitable() ) {
187 return array(); // No limits
188 }
189
190 // Find out which categories we belong to
191 $categories = array();
192 if ( $user->isAnon() ) {
193 $categories[] = 'anon';
194 } else {
195 $categories[] = 'user';
196 }
197 if ( $user->isNewbie() ) {
198 $categories[] = 'ip';
199 $categories[] = 'subnet';
200 if ( !$user->isAnon() ) {
201 $categories[] = 'newbie';
202 }
203 }
204 $categories = array_merge( $categories, $user->getGroups() );
205
206 // Now get the actual limits
207 $retval = array();
208 foreach ( $wgRateLimits as $action => $limits ) {
209 foreach ( $categories as $cat ) {
210 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
211 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
212 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
213 }
214 }
215 }
216
217 return $retval;
218 }
219
220 public function getAllowedParams() {
221 return array(
222 'prop' => array(
223 ApiBase::PARAM_DFLT => null,
224 ApiBase::PARAM_ISMULTI => true,
225 ApiBase::PARAM_TYPE => array(
226 'blockinfo',
227 'hasmsg',
228 'groups',
229 'implicitgroups',
230 'rights',
231 'changeablegroups',
232 'options',
233 'preferencestoken',
234 'editcount',
235 'ratelimits',
236 'email',
237 'realname',
238 'acceptlang',
239 'registrationdate',
240 'unreadcount',
241 )
242 )
243 );
244 }
245
246 public function getParamDescription() {
247 return array(
248 'prop' => array(
249 'What pieces of information to include',
250 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
251 ' hasmsg - Adds a tag "message" if the current user has pending messages',
252 ' groups - Lists all the groups the current user belongs to',
253 ' implicitgroups - Lists all the groups the current user is automatically a member of',
254 ' rights - Lists all the rights the current user has',
255 ' changeablegroups - Lists the groups the current user can add to and remove from',
256 ' options - Lists all preferences the current user has set',
257 ' preferencestoken - Get a token to change current user\'s preferences',
258 ' editcount - Adds the current user\'s edit count',
259 ' ratelimits - Lists all rate limits applying to the current user',
260 ' realname - Adds the user\'s real name',
261 ' email - Adds the user\'s email address and email authentication date',
262 ' acceptlang - Echoes the Accept-Language header sent by ' .
263 'the client in a structured format',
264 ' registrationdate - Adds the user\'s registration date',
265 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
266 '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
267 self::WL_UNREAD_LIMIT . '+" if more)',
268 )
269 );
270 }
271
272 public function getResultProperties() {
273 return array(
274 ApiBase::PROP_LIST => false,
275 '' => array(
276 'id' => 'integer',
277 'name' => 'string',
278 'anon' => 'boolean'
279 ),
280 'blockinfo' => array(
281 'blockid' => array(
282 ApiBase::PROP_TYPE => 'integer',
283 ApiBase::PROP_NULLABLE => true
284 ),
285 'blockedby' => array(
286 ApiBase::PROP_TYPE => 'string',
287 ApiBase::PROP_NULLABLE => true
288 ),
289 'blockedbyid' => array(
290 ApiBase::PROP_TYPE => 'integer',
291 ApiBase::PROP_NULLABLE => true
292 ),
293 'blockedreason' => array(
294 ApiBase::PROP_TYPE => 'string',
295 ApiBase::PROP_NULLABLE => true
296 )
297 ),
298 'hasmsg' => array(
299 'messages' => 'boolean'
300 ),
301 'preferencestoken' => array(
302 'preferencestoken' => 'string'
303 ),
304 'editcount' => array(
305 'editcount' => 'integer'
306 ),
307 'realname' => array(
308 'realname' => array(
309 ApiBase::PROP_TYPE => 'string',
310 ApiBase::PROP_NULLABLE => true
311 )
312 ),
313 'email' => array(
314 'email' => 'string',
315 'emailauthenticated' => array(
316 ApiBase::PROP_TYPE => 'timestamp',
317 ApiBase::PROP_NULLABLE => true
318 )
319 ),
320 'registrationdate' => array(
321 'registrationdate' => array(
322 ApiBase::PROP_TYPE => 'timestamp',
323 ApiBase::PROP_NULLABLE => true
324 )
325 )
326 );
327 }
328
329 public function getDescription() {
330 return 'Get information about the current user.';
331 }
332
333 public function getExamples() {
334 return array(
335 'api.php?action=query&meta=userinfo',
336 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
337 );
338 }
339
340 public function getHelpUrls() {
341 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
342 }
343 }