Merge "fix 2 missing "local var" JavaScript statements"
[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 $vals['blockedby'] = User::whoIs( $user->blockedBy() );
67 $vals['blockreason'] = $user->blockedFor();
68 }
69 }
70
71 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
72 $vals['messages'] = '';
73 }
74
75 if ( isset( $this->prop['groups'] ) ) {
76 $autolist = ApiQueryUsers::getAutoGroups( $user );
77
78 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
79 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
80 }
81
82 if ( isset( $this->prop['implicitgroups'] ) ) {
83 $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
84 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
85 }
86
87 if ( isset( $this->prop['rights'] ) ) {
88 // User::getRights() may return duplicate values, strip them
89 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
90 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
91 }
92
93 if ( isset( $this->prop['changeablegroups'] ) ) {
94 $vals['changeablegroups'] = $user->changeableGroups();
95 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
96 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
99 }
100
101 if ( isset( $this->prop['options'] ) ) {
102 $vals['options'] = $user->getOptions();
103 }
104
105 if ( isset( $this->prop['preferencestoken'] ) &&
106 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
107 ) {
108 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
109 }
110
111 if ( isset( $this->prop['editcount'] ) ) {
112 $vals['editcount'] = intval( $user->getEditCount() );
113 }
114
115 if ( isset( $this->prop['ratelimits'] ) ) {
116 $vals['ratelimits'] = $this->getRateLimits();
117 }
118
119 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
120 $vals['realname'] = $user->getRealName();
121 }
122
123 if ( isset( $this->prop['email'] ) ) {
124 $vals['email'] = $user->getEmail();
125 $auth = $user->getEmailAuthenticationTimestamp();
126 if ( !is_null( $auth ) ) {
127 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
128 }
129 }
130
131 if ( isset( $this->prop['registrationdate'] ) ) {
132 $regDate = $user->getRegistration();
133 if ( $regDate !== false ) {
134 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
135 }
136 }
137
138 if ( isset( $this->prop['acceptlang'] ) ) {
139 $langs = $this->getRequest()->getAcceptLang();
140 $acceptLang = array();
141 foreach ( $langs as $lang => $val ) {
142 $r = array( 'q' => $val );
143 ApiResult::setContent( $r, $lang );
144 $acceptLang[] = $r;
145 }
146 $result->setIndexedTagName( $acceptLang, 'lang' );
147 $vals['acceptlang'] = $acceptLang;
148 }
149 return $vals;
150 }
151
152 protected function getRateLimits() {
153 global $wgRateLimits;
154 $user = $this->getUser();
155 if ( !$user->isPingLimitable() ) {
156 return array(); // No limits
157 }
158
159 // Find out which categories we belong to
160 $categories = array();
161 if ( $user->isAnon() ) {
162 $categories[] = 'anon';
163 } else {
164 $categories[] = 'user';
165 }
166 if ( $user->isNewbie() ) {
167 $categories[] = 'ip';
168 $categories[] = 'subnet';
169 if ( !$user->isAnon() )
170 $categories[] = 'newbie';
171 }
172 $categories = array_merge( $categories, $user->getGroups() );
173
174 // Now get the actual limits
175 $retval = array();
176 foreach ( $wgRateLimits as $action => $limits ) {
177 foreach ( $categories as $cat ) {
178 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
179 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
180 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
181 }
182 }
183 }
184 return $retval;
185 }
186
187 public function getAllowedParams() {
188 return array(
189 'prop' => array(
190 ApiBase::PARAM_DFLT => null,
191 ApiBase::PARAM_ISMULTI => true,
192 ApiBase::PARAM_TYPE => array(
193 'blockinfo',
194 'hasmsg',
195 'groups',
196 'implicitgroups',
197 'rights',
198 'changeablegroups',
199 'options',
200 'preferencestoken',
201 'editcount',
202 'ratelimits',
203 'email',
204 'realname',
205 'acceptlang',
206 'registrationdate'
207 )
208 )
209 );
210 }
211
212 public function getParamDescription() {
213 return array(
214 'prop' => array(
215 'What pieces of information to include',
216 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
217 ' hasmsg - Adds a tag "message" if the current user has pending messages',
218 ' groups - Lists all the groups the current user belongs to',
219 ' implicitgroups - Lists all the groups the current user is automatically a member of',
220 ' rights - Lists all the rights the current user has',
221 ' changeablegroups - Lists the groups the current user can add to and remove from',
222 ' options - Lists all preferences the current user has set',
223 ' preferencestoken - Get a token to change current user\'s preferences',
224 ' editcount - Adds the current user\'s edit count',
225 ' ratelimits - Lists all rate limits applying to the current user',
226 ' realname - Adds the user\'s real name',
227 ' email - Adds the user\'s email address and email authentication date',
228 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
229 ' registrationdate - Adds the user\'s registration date',
230 )
231 );
232 }
233
234 public function getDescription() {
235 return 'Get information about the current user';
236 }
237
238 public function getExamples() {
239 return array(
240 'api.php?action=query&meta=userinfo',
241 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
242 );
243 }
244
245 public function getHelpUrls() {
246 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
247 }
248
249 public function getVersion() {
250 return __CLASS__ . ': $Id$';
251 }
252 }