Back off from job types longer for DB read-only errors
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob3b604786ab55a4a5c789b8279b1b4b9645d8f96c
1 <?php
2 /**
5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 use MediaWiki\MediaWikiServices;
29 /**
30 * Query module to get information about the currently logged-in user
32 * @ingroup API
34 class ApiQueryUserInfo extends ApiQueryBase {
36 const WL_UNREAD_LIMIT = 1000;
38 private $params = [];
39 private $prop = [];
41 public function __construct( ApiQuery $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'ui' );
45 public function execute() {
46 $this->params = $this->extractRequestParams();
47 $result = $this->getResult();
49 if ( !is_null( $this->params['prop'] ) ) {
50 $this->prop = array_flip( $this->params['prop'] );
53 $r = $this->getCurrentUserInfo();
54 $result->addValue( 'query', $this->getModuleName(), $r );
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
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'
79 return $vals;
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.
94 public static function getCentralUserInfo( Config $config, User $user, $attachedWiki = null ) {
95 $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
97 $ret = [
98 'centralids' => [],
99 'attachedlocal' => [],
101 ApiResult::setArrayType( $ret['centralids'], 'assoc' );
102 ApiResult::setArrayType( $ret['attachedlocal'], 'assoc' );
103 if ( $attachedWiki ) {
104 $ret['attachedwiki'] = [];
105 ApiResult::setArrayType( $ret['attachedwiki'], 'assoc' );
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 );
118 return $ret;
121 protected function getCurrentUserInfo() {
122 $user = $this->getUser();
123 $vals = [];
124 $vals['id'] = intval( $user->getId() );
125 $vals['name'] = $user->getName();
127 if ( $user->isAnon() ) {
128 $vals['anon'] = true;
131 if ( isset( $this->prop['blockinfo'] ) && $user->isBlocked() ) {
132 $vals = array_merge( $vals, self::getBlockInfo( $user->getBlock() ) );
135 if ( isset( $this->prop['hasmsg'] ) ) {
136 $vals['messages'] = $user->getNewtalk();
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
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
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
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' );
166 if ( isset( $this->prop['options'] ) ) {
167 $vals['options'] = $user->getOptions();
168 $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] );
171 if ( isset( $this->prop['preferencestoken'] ) ) {
172 $p = $this->getModulePrefix();
173 $this->addDeprecation(
175 'apiwarn-deprecation-withreplacement',
176 "{$p}prop=preferencestoken",
177 'action=query&meta=tokens',
179 "meta=userinfo&{$p}prop=preferencestoken"
182 if ( isset( $this->prop['preferencestoken'] ) &&
183 !$this->lacksSameOriginSecurity() &&
184 $user->isAllowed( 'editmyoptions' )
186 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
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() );
195 if ( isset( $this->prop['ratelimits'] ) ) {
196 $vals['ratelimits'] = $this->getRateLimits();
199 if ( isset( $this->prop['realname'] ) &&
200 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
202 $vals['realname'] = $user->getRealName();
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 );
215 if ( isset( $this->prop['registrationdate'] ) ) {
216 $regDate = $user->getRegistration();
217 if ( $regDate !== false ) {
218 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
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;
230 ApiResult::setIndexedTagName( $acceptLang, 'lang' );
231 $vals['acceptlang'] = $acceptLang;
234 if ( isset( $this->prop['unreadcount'] ) ) {
235 $store = MediaWikiServices::getInstance()->getWatchedItemStore();
236 $unreadNotifications = $store->countUnreadNotifications(
237 $user,
238 self::WL_UNREAD_LIMIT
241 if ( $unreadNotifications === true ) {
242 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
243 } else {
244 $vals['unreadcount'] = $unreadNotifications;
248 if ( isset( $this->prop['centralids'] ) ) {
249 $vals += self::getCentralUserInfo(
250 $this->getConfig(), $this->getUser(), $this->params['attachedwiki']
254 return $vals;
257 protected function getRateLimits() {
258 $retval = [
259 ApiResult::META_TYPE => 'assoc',
262 $user = $this->getUser();
263 if ( !$user->isPingLimitable() ) {
264 return $retval; // No limits
267 // Find out which categories we belong to
268 $categories = [];
269 if ( $user->isAnon() ) {
270 $categories[] = 'anon';
271 } else {
272 $categories[] = 'user';
274 if ( $user->isNewbie() ) {
275 $categories[] = 'ip';
276 $categories[] = 'subnet';
277 if ( !$user->isAnon() ) {
278 $categories[] = 'newbie';
281 $categories = array_merge( $categories, $user->getGroups() );
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] );
293 return $retval;
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',
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 . '+',
326 'attachedwiki' => null,
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',
339 public function getHelpUrls() {
340 return 'https://www.mediawiki.org/wiki/API:Userinfo';