Merge "(bug 30625) Return warnings, if they exist, despite ignorewarnings"
[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 $autolist = ApiQueryUsers::getAutoGroups( $user );
80
81 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
82 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
83 }
84
85 if ( isset( $this->prop['implicitgroups'] ) ) {
86 $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
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 ) {
111 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
112 }
113
114 if ( isset( $this->prop['editcount'] ) ) {
115 $vals['editcount'] = intval( $user->getEditCount() );
116 }
117
118 if ( isset( $this->prop['ratelimits'] ) ) {
119 $vals['ratelimits'] = $this->getRateLimits();
120 }
121
122 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
123 $vals['realname'] = $user->getRealName();
124 }
125
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 if ( isset( $this->prop['registrationdate'] ) ) {
135 $regDate = $user->getRegistration();
136 if ( $regDate !== false ) {
137 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
138 }
139 }
140
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;
148 }
149 $result->setIndexedTagName( $acceptLang, 'lang' );
150 $vals['acceptlang'] = $acceptLang;
151 }
152 return $vals;
153 }
154
155 protected function getRateLimits() {
156 global $wgRateLimits;
157 $user = $this->getUser();
158 if ( !$user->isPingLimitable() ) {
159 return array(); // No limits
160 }
161
162 // Find out which categories we belong to
163 $categories = array();
164 if ( $user->isAnon() ) {
165 $categories[] = 'anon';
166 } else {
167 $categories[] = 'user';
168 }
169 if ( $user->isNewbie() ) {
170 $categories[] = 'ip';
171 $categories[] = 'subnet';
172 if ( !$user->isAnon() )
173 $categories[] = 'newbie';
174 }
175 $categories = array_merge( $categories, $user->getGroups() );
176
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] );
184 }
185 }
186 }
187 return $retval;
188 }
189
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'
210 )
211 )
212 );
213 }
214
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',
233 )
234 );
235 }
236
237 public function getResultProperties() {
238 return array(
239 ApiBase::PROP_LIST => false,
240 '' => array(
241 'id' => 'integer',
242 'name' => 'string',
243 'anon' => 'boolean'
244 ),
245 'blockinfo' => array(
246 'blockid' => array(
247 ApiBase::PROP_TYPE => 'integer',
248 ApiBase::PROP_NULLABLE => true
249 ),
250 'blockedby' => array(
251 ApiBase::PROP_TYPE => 'string',
252 ApiBase::PROP_NULLABLE => true
253 ),
254 'blockedbyid' => array(
255 ApiBase::PROP_TYPE => 'integer',
256 ApiBase::PROP_NULLABLE => true
257 ),
258 'blockedreason' => array(
259 ApiBase::PROP_TYPE => 'string',
260 ApiBase::PROP_NULLABLE => true
261 )
262 ),
263 'hasmsg' => array(
264 'messages' => 'boolean'
265 ),
266 'preferencestoken' => array(
267 'preferencestoken' => 'string'
268 ),
269 'editcount' => array(
270 'editcount' => 'integer'
271 ),
272 'realname' => array(
273 'realname' => array(
274 ApiBase::PROP_TYPE => 'string',
275 ApiBase::PROP_NULLABLE => true
276 )
277 ),
278 'email' => array(
279 'email' => 'string',
280 'emailauthenticated' => array(
281 ApiBase::PROP_TYPE => 'timestamp',
282 ApiBase::PROP_NULLABLE => true
283 )
284 ),
285 'registrationdate' => array(
286 'registrationdate' => array(
287 ApiBase::PROP_TYPE => 'timestamp',
288 ApiBase::PROP_NULLABLE => true
289 )
290 )
291 );
292 }
293
294 public function getDescription() {
295 return 'Get information about the current user';
296 }
297
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',
302 );
303 }
304
305 public function getHelpUrls() {
306 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
307 }
308
309 public function getVersion() {
310 return __CLASS__ . ': $Id$';
311 }
312 }