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