Merge "Add CollationFa"
[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 use MediaWiki\MediaWikiServices;
28
29 /**
30 * Query module to get information about the currently logged-in user
31 *
32 * @ingroup API
33 */
34 class ApiQueryUserInfo extends ApiQueryBase {
35
36 const WL_UNREAD_LIMIT = 1000;
37
38 private $params = [];
39 private $prop = [];
40
41 public function __construct( ApiQuery $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'ui' );
43 }
44
45 public function execute() {
46 $this->params = $this->extractRequestParams();
47 $result = $this->getResult();
48
49 if ( !is_null( $this->params['prop'] ) ) {
50 $this->prop = array_flip( $this->params['prop'] );
51 }
52
53 $r = $this->getCurrentUserInfo();
54 $result->addValue( 'query', $this->getModuleName(), $r );
55 }
56
57 /**
58 * Get basic info about a given block
59 * @param Block $block
60 * @return array Array containing several keys:
61 * - blockid - ID of the block
62 * - blockedby - username of the blocker
63 * - blockedbyid - user ID of the blocker
64 * - blockreason - reason provided for the block
65 * - blockedtimestamp - timestamp for when the block was placed/modified
66 * - blockexpiry - expiry time of the block
67 */
68 public static function getBlockInfo( Block $block ) {
69 global $wgContLang;
70 $vals = [];
71 $vals['blockid'] = $block->getId();
72 $vals['blockedby'] = $block->getByName();
73 $vals['blockedbyid'] = $block->getBy();
74 $vals['blockreason'] = $block->mReason;
75 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
76 $vals['blockexpiry'] = $wgContLang->formatExpiry(
77 $block->getExpiry(), TS_ISO_8601, 'infinite'
78 );
79 return $vals;
80 }
81
82 /**
83 * Get central user info
84 * @param Config $config
85 * @param User $user
86 * @param string|null $attachedWiki
87 * @return array Central user info
88 * - centralids: Array mapping non-local Central ID provider names to IDs
89 * - attachedlocal: Array mapping Central ID provider names to booleans
90 * indicating whether the local user is attached.
91 * - attachedwiki: Array mapping Central ID provider names to booleans
92 * indicating whether the user is attached to $attachedWiki.
93 */
94 public static function getCentralUserInfo( Config $config, User $user, $attachedWiki = null ) {
95 $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
96
97 $ret = [
98 'centralids' => [],
99 'attachedlocal' => [],
100 ];
101 ApiResult::setArrayType( $ret['centralids'], 'assoc' );
102 ApiResult::setArrayType( $ret['attachedlocal'], 'assoc' );
103 if ( $attachedWiki ) {
104 $ret['attachedwiki'] = [];
105 ApiResult::setArrayType( $ret['attachedwiki'], 'assoc' );
106 }
107
108 $name = $user->getName();
109 foreach ( $providerIds as $providerId ) {
110 $provider = CentralIdLookup::factory( $providerId );
111 $ret['centralids'][$providerId] = $provider->centralIdFromName( $name );
112 $ret['attachedlocal'][$providerId] = $provider->isAttached( $user );
113 if ( $attachedWiki ) {
114 $ret['attachedwiki'][$providerId] = $provider->isAttached( $user, $attachedWiki );
115 }
116 }
117
118 return $ret;
119 }
120
121 protected function getCurrentUserInfo() {
122 $user = $this->getUser();
123 $vals = [];
124 $vals['id'] = intval( $user->getId() );
125 $vals['name'] = $user->getName();
126
127 if ( $user->isAnon() ) {
128 $vals['anon'] = true;
129 }
130
131 if ( isset( $this->prop['blockinfo'] ) && $user->isBlocked() ) {
132 $vals = array_merge( $vals, self::getBlockInfo( $user->getBlock() ) );
133 }
134
135 if ( isset( $this->prop['hasmsg'] ) ) {
136 $vals['messages'] = $user->getNewtalk();
137 }
138
139 if ( isset( $this->prop['groups'] ) ) {
140 $vals['groups'] = $user->getEffectiveGroups();
141 ApiResult::setArrayType( $vals['groups'], 'array' ); // even if empty
142 ApiResult::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
143 }
144
145 if ( isset( $this->prop['implicitgroups'] ) ) {
146 $vals['implicitgroups'] = $user->getAutomaticGroups();
147 ApiResult::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty
148 ApiResult::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
149 }
150
151 if ( isset( $this->prop['rights'] ) ) {
152 // User::getRights() may return duplicate values, strip them
153 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
154 ApiResult::setArrayType( $vals['rights'], 'array' ); // even if empty
155 ApiResult::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
156 }
157
158 if ( isset( $this->prop['changeablegroups'] ) ) {
159 $vals['changeablegroups'] = $user->changeableGroups();
160 ApiResult::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
161 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
162 ApiResult::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
163 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
164 }
165
166 if ( isset( $this->prop['options'] ) ) {
167 $vals['options'] = $user->getOptions();
168 $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] );
169 }
170
171 if ( isset( $this->prop['preferencestoken'] ) ) {
172 $p = $this->getModulePrefix();
173 $this->addDeprecation(
174 [
175 'apiwarn-deprecation-withreplacement',
176 "{$p}prop=preferencestoken",
177 'action=query&meta=tokens',
178 ],
179 "meta=userinfo&{$p}prop=preferencestoken"
180 );
181 }
182 if ( isset( $this->prop['preferencestoken'] ) &&
183 !$this->lacksSameOriginSecurity() &&
184 $user->isAllowed( 'editmyoptions' )
185 ) {
186 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
187 }
188
189 if ( isset( $this->prop['editcount'] ) ) {
190 // use intval to prevent null if a non-logged-in user calls
191 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
192 $vals['editcount'] = intval( $user->getEditCount() );
193 }
194
195 if ( isset( $this->prop['ratelimits'] ) ) {
196 $vals['ratelimits'] = $this->getRateLimits();
197 }
198
199 if ( isset( $this->prop['realname'] ) &&
200 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
201 ) {
202 $vals['realname'] = $user->getRealName();
203 }
204
205 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
206 if ( isset( $this->prop['email'] ) ) {
207 $vals['email'] = $user->getEmail();
208 $auth = $user->getEmailAuthenticationTimestamp();
209 if ( !is_null( $auth ) ) {
210 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
211 }
212 }
213 }
214
215 if ( isset( $this->prop['registrationdate'] ) ) {
216 $regDate = $user->getRegistration();
217 if ( $regDate !== false ) {
218 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
219 }
220 }
221
222 if ( isset( $this->prop['acceptlang'] ) ) {
223 $langs = $this->getRequest()->getAcceptLang();
224 $acceptLang = [];
225 foreach ( $langs as $lang => $val ) {
226 $r = [ 'q' => $val ];
227 ApiResult::setContentValue( $r, 'code', $lang );
228 $acceptLang[] = $r;
229 }
230 ApiResult::setIndexedTagName( $acceptLang, 'lang' );
231 $vals['acceptlang'] = $acceptLang;
232 }
233
234 if ( isset( $this->prop['unreadcount'] ) ) {
235 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
236 $unreadNotifications = $store->countUnreadNotifications(
237 $user,
238 self::WL_UNREAD_LIMIT
239 );
240
241 if ( $unreadNotifications === true ) {
242 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
243 } else {
244 $vals['unreadcount'] = $unreadNotifications;
245 }
246 }
247
248 if ( isset( $this->prop['centralids'] ) ) {
249 $vals += self::getCentralUserInfo(
250 $this->getConfig(), $this->getUser(), $this->params['attachedwiki']
251 );
252 }
253
254 return $vals;
255 }
256
257 protected function getRateLimits() {
258 $retval = [
259 ApiResult::META_TYPE => 'assoc',
260 ];
261
262 $user = $this->getUser();
263 if ( !$user->isPingLimitable() ) {
264 return $retval; // No limits
265 }
266
267 // Find out which categories we belong to
268 $categories = [];
269 if ( $user->isAnon() ) {
270 $categories[] = 'anon';
271 } else {
272 $categories[] = 'user';
273 }
274 if ( $user->isNewbie() ) {
275 $categories[] = 'ip';
276 $categories[] = 'subnet';
277 if ( !$user->isAnon() ) {
278 $categories[] = 'newbie';
279 }
280 }
281 $categories = array_merge( $categories, $user->getGroups() );
282
283 // Now get the actual limits
284 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
285 foreach ( $categories as $cat ) {
286 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
287 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
288 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
289 }
290 }
291 }
292
293 return $retval;
294 }
295
296 public function getAllowedParams() {
297 return [
298 'prop' => [
299 ApiBase::PARAM_ISMULTI => true,
300 ApiBase::PARAM_TYPE => [
301 'blockinfo',
302 'hasmsg',
303 'groups',
304 'implicitgroups',
305 'rights',
306 'changeablegroups',
307 'options',
308 'preferencestoken',
309 'editcount',
310 'ratelimits',
311 'email',
312 'realname',
313 'acceptlang',
314 'registrationdate',
315 'unreadcount',
316 'centralids',
317 ],
318 ApiBase::PARAM_HELP_MSG_PER_VALUE => [
319 'unreadcount' => [
320 'apihelp-query+userinfo-paramvalue-prop-unreadcount',
321 self::WL_UNREAD_LIMIT - 1,
322 self::WL_UNREAD_LIMIT . '+',
323 ],
324 ],
325 ],
326 'attachedwiki' => null,
327 ];
328 }
329
330 protected function getExamplesMessages() {
331 return [
332 'action=query&meta=userinfo'
333 => 'apihelp-query+userinfo-example-simple',
334 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
335 => 'apihelp-query+userinfo-example-data',
336 ];
337 }
338
339 public function getHelpUrls() {
340 return 'https://www.mediawiki.org/wiki/API:Userinfo';
341 }
342 }