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