Use 'email' instead of 'e-mail' in API texts.
[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 $categories = array_merge( $categories, $user->getGroups() );
174
175 // Now get the actual limits
176 $retval = array();
177 foreach ( $wgRateLimits as $action => $limits ) {
178 foreach ( $categories as $cat ) {
179 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
180 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
181 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
182 }
183 }
184 }
185 return $retval;
186 }
187
188 public function getAllowedParams() {
189 return array(
190 'prop' => array(
191 ApiBase::PARAM_DFLT => null,
192 ApiBase::PARAM_ISMULTI => true,
193 ApiBase::PARAM_TYPE => array(
194 'blockinfo',
195 'hasmsg',
196 'groups',
197 'implicitgroups',
198 'rights',
199 'changeablegroups',
200 'options',
201 'preferencestoken',
202 'editcount',
203 'ratelimits',
204 'email',
205 'realname',
206 'acceptlang',
207 'registrationdate'
208 )
209 )
210 );
211 }
212
213 public function getParamDescription() {
214 return array(
215 'prop' => array(
216 'What pieces of information to include',
217 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
218 ' hasmsg - Adds a tag "message" if the current user has pending messages',
219 ' groups - Lists all the groups the current user belongs to',
220 ' implicitgroups - Lists all the groups the current user is automatically a member of',
221 ' rights - Lists all the rights the current user has',
222 ' changeablegroups - Lists the groups the current user can add to and remove from',
223 ' options - Lists all preferences the current user has set',
224 ' preferencestoken - Get a token to change current user\'s preferences',
225 ' editcount - Adds the current user\'s edit count',
226 ' ratelimits - Lists all rate limits applying to the current user',
227 ' realname - Adds the user\'s real name',
228 ' email - Adds the user\'s email address and email authentication date',
229 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
230 ' registrationdate - Adds the user\'s registration date',
231 )
232 );
233 }
234
235 public function getResultProperties() {
236 return array(
237 ApiBase::PROP_LIST => false,
238 '' => array(
239 'id' => 'integer',
240 'name' => 'string',
241 'anon' => 'boolean'
242 ),
243 'blockinfo' => array(
244 'blockid' => array(
245 ApiBase::PROP_TYPE => 'integer',
246 ApiBase::PROP_NULLABLE => true
247 ),
248 'blockedby' => array(
249 ApiBase::PROP_TYPE => 'string',
250 ApiBase::PROP_NULLABLE => true
251 ),
252 'blockedbyid' => array(
253 ApiBase::PROP_TYPE => 'integer',
254 ApiBase::PROP_NULLABLE => true
255 ),
256 'blockedreason' => array(
257 ApiBase::PROP_TYPE => 'string',
258 ApiBase::PROP_NULLABLE => true
259 )
260 ),
261 'hasmsg' => array(
262 'messages' => 'boolean'
263 ),
264 'preferencestoken' => array(
265 'preferencestoken' => 'string'
266 ),
267 'editcount' => array(
268 'editcount' => 'integer'
269 ),
270 'realname' => array(
271 'realname' => array(
272 ApiBase::PROP_TYPE => 'string',
273 ApiBase::PROP_NULLABLE => true
274 )
275 ),
276 'email' => array(
277 'email' => 'string',
278 'emailauthenticated' => array(
279 ApiBase::PROP_TYPE => 'timestamp',
280 ApiBase::PROP_NULLABLE => true
281 )
282 ),
283 'registrationdate' => array(
284 'registrationdate' => array(
285 ApiBase::PROP_TYPE => 'timestamp',
286 ApiBase::PROP_NULLABLE => true
287 )
288 )
289 );
290 }
291
292 public function getDescription() {
293 return 'Get information about the current user';
294 }
295
296 public function getExamples() {
297 return array(
298 'api.php?action=query&meta=userinfo',
299 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
300 );
301 }
302
303 public function getHelpUrls() {
304 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
305 }
306 }