Merge "Fix font of mw-ui-button"
[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 $user = $this->getUser();
56 $result = $this->getResult();
57 $vals = array();
58 $vals['id'] = intval( $user->getId() );
59 $vals['name'] = $user->getName();
60
61 if ( $user->isAnon() ) {
62 $vals['anon'] = '';
63 }
64
65 if ( isset( $this->prop['blockinfo'] ) ) {
66 if ( $user->isBlocked() ) {
67 $block = $user->getBlock();
68 $vals['blockid'] = $block->getId();
69 $vals['blockedby'] = $block->getByName();
70 $vals['blockedbyid'] = $block->getBy();
71 $vals['blockreason'] = $user->blockedFor();
72 }
73 }
74
75 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
76 $vals['messages'] = '';
77 }
78
79 if ( isset( $this->prop['groups'] ) ) {
80 $vals['groups'] = $user->getEffectiveGroups();
81 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
82 }
83
84 if ( isset( $this->prop['implicitgroups'] ) ) {
85 $vals['implicitgroups'] = $user->getAutomaticGroups();
86 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
87 }
88
89 if ( isset( $this->prop['rights'] ) ) {
90 // User::getRights() may return duplicate values, strip them
91 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
92 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
93 }
94
95 if ( isset( $this->prop['changeablegroups'] ) ) {
96 $vals['changeablegroups'] = $user->changeableGroups();
97 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
101 }
102
103 if ( isset( $this->prop['options'] ) ) {
104 $vals['options'] = $user->getOptions();
105 }
106
107 if ( isset( $this->prop['preferencestoken'] ) ) {
108 $p = $this->getModulePrefix();
109 $this->setWarning(
110 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
111 );
112 }
113 if ( isset( $this->prop['preferencestoken'] ) &&
114 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
115 $user->isAllowed( 'editmyoptions' )
116 ) {
117 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
118 }
119
120 if ( isset( $this->prop['editcount'] ) ) {
121 // use intval to prevent null if a non-logged-in user calls
122 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
123 $vals['editcount'] = intval( $user->getEditCount() );
124 }
125
126 if ( isset( $this->prop['ratelimits'] ) ) {
127 $vals['ratelimits'] = $this->getRateLimits();
128 }
129
130 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
131 $vals['realname'] = $user->getRealName();
132 }
133
134 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
135 if ( isset( $this->prop['email'] ) ) {
136 $vals['email'] = $user->getEmail();
137 $auth = $user->getEmailAuthenticationTimestamp();
138 if ( !is_null( $auth ) ) {
139 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
140 }
141 }
142 }
143
144 if ( isset( $this->prop['registrationdate'] ) ) {
145 $regDate = $user->getRegistration();
146 if ( $regDate !== false ) {
147 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
148 }
149 }
150
151 if ( isset( $this->prop['acceptlang'] ) ) {
152 $langs = $this->getRequest()->getAcceptLang();
153 $acceptLang = array();
154 foreach ( $langs as $lang => $val ) {
155 $r = array( 'q' => $val );
156 ApiResult::setContent( $r, $lang );
157 $acceptLang[] = $r;
158 }
159 $result->setIndexedTagName( $acceptLang, 'lang' );
160 $vals['acceptlang'] = $acceptLang;
161 }
162
163 if ( isset( $this->prop['unreadcount'] ) ) {
164 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
165
166 $sql = $dbr->selectSQLText(
167 'watchlist',
168 array( 'dummy' => 1 ),
169 array(
170 'wl_user' => $user->getId(),
171 'wl_notificationtimestamp IS NOT NULL',
172 ),
173 __METHOD__,
174 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
175 );
176 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
177
178 if ( $count >= self::WL_UNREAD_LIMIT ) {
179 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
180 } else {
181 $vals['unreadcount'] = (int)$count;
182 }
183 }
184
185 return $vals;
186 }
187
188 protected function getRateLimits() {
189 $user = $this->getUser();
190 if ( !$user->isPingLimitable() ) {
191 return array(); // No limits
192 }
193
194 // Find out which categories we belong to
195 $categories = array();
196 if ( $user->isAnon() ) {
197 $categories[] = 'anon';
198 } else {
199 $categories[] = 'user';
200 }
201 if ( $user->isNewbie() ) {
202 $categories[] = 'ip';
203 $categories[] = 'subnet';
204 if ( !$user->isAnon() ) {
205 $categories[] = 'newbie';
206 }
207 }
208 $categories = array_merge( $categories, $user->getGroups() );
209
210 // Now get the actual limits
211 $retval = array();
212 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
213 foreach ( $categories as $cat ) {
214 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
215 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
216 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
217 }
218 }
219 }
220
221 return $retval;
222 }
223
224 public function getAllowedParams() {
225 return array(
226 'prop' => array(
227 ApiBase::PARAM_DFLT => null,
228 ApiBase::PARAM_ISMULTI => true,
229 ApiBase::PARAM_TYPE => array(
230 'blockinfo',
231 'hasmsg',
232 'groups',
233 'implicitgroups',
234 'rights',
235 'changeablegroups',
236 'options',
237 'preferencestoken',
238 'editcount',
239 'ratelimits',
240 'email',
241 'realname',
242 'acceptlang',
243 'registrationdate',
244 'unreadcount',
245 )
246 )
247 );
248 }
249
250 public function getParamDescription() {
251 return array(
252 'prop' => array(
253 'What pieces of information to include',
254 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
255 ' hasmsg - Adds a tag "message" if the current user has pending messages',
256 ' groups - Lists all the groups the current user belongs to',
257 ' implicitgroups - Lists all the groups the current user is automatically a member of',
258 ' rights - Lists all the rights the current user has',
259 ' changeablegroups - Lists the groups the current user can add to and remove from',
260 ' options - Lists all preferences the current user has set',
261 ' preferencestoken - DEPRECATED! Get a token to change current user\'s preferences',
262 ' editcount - Adds the current user\'s edit count',
263 ' ratelimits - Lists all rate limits applying to the current user',
264 ' realname - Adds the user\'s real name',
265 ' email - Adds the user\'s email address and email authentication date',
266 ' acceptlang - Echoes the Accept-Language header sent by ' .
267 'the client in a structured format',
268 ' registrationdate - Adds the user\'s registration date',
269 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
270 '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
271 self::WL_UNREAD_LIMIT . '+" if more)',
272 )
273 );
274 }
275
276 public function getDescription() {
277 return 'Get information about the current user.';
278 }
279
280 public function getExamples() {
281 return array(
282 'api.php?action=query&meta=userinfo',
283 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
284 );
285 }
286
287 public function getHelpUrls() {
288 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
289 }
290 }