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
27 use MediaWiki\MediaWikiServices
;
30 * Query module to get information about the currently logged-in user
34 class ApiQueryUserInfo
extends ApiQueryBase
{
36 const WL_UNREAD_LIMIT
= 1000;
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 );
58 * Get basic info about a given 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 ) {
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'
83 * Get central user info
84 * @param Config $config
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' ) );
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 );
121 protected function getCurrentUserInfo() {
122 $user = $this->getUser();
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();
174 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
177 if ( isset( $this->prop
['preferencestoken'] ) &&
178 !$this->lacksSameOriginSecurity() &&
179 $user->isAllowed( 'editmyoptions' )
181 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
184 if ( isset( $this->prop
['editcount'] ) ) {
185 // use intval to prevent null if a non-logged-in user calls
186 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
187 $vals['editcount'] = intval( $user->getEditCount() );
190 if ( isset( $this->prop
['ratelimits'] ) ) {
191 $vals['ratelimits'] = $this->getRateLimits();
194 if ( isset( $this->prop
['realname'] ) &&
195 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
197 $vals['realname'] = $user->getRealName();
200 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
201 if ( isset( $this->prop
['email'] ) ) {
202 $vals['email'] = $user->getEmail();
203 $auth = $user->getEmailAuthenticationTimestamp();
204 if ( !is_null( $auth ) ) {
205 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
210 if ( isset( $this->prop
['registrationdate'] ) ) {
211 $regDate = $user->getRegistration();
212 if ( $regDate !== false ) {
213 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
217 if ( isset( $this->prop
['acceptlang'] ) ) {
218 $langs = $this->getRequest()->getAcceptLang();
220 foreach ( $langs as $lang => $val ) {
221 $r = [ 'q' => $val ];
222 ApiResult
::setContentValue( $r, 'code', $lang );
225 ApiResult
::setIndexedTagName( $acceptLang, 'lang' );
226 $vals['acceptlang'] = $acceptLang;
229 if ( isset( $this->prop
['unreadcount'] ) ) {
230 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
231 $unreadNotifications = $store->countUnreadNotifications(
233 self
::WL_UNREAD_LIMIT
236 if ( $unreadNotifications === true ) {
237 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
239 $vals['unreadcount'] = $unreadNotifications;
243 if ( isset( $this->prop
['centralids'] ) ) {
244 $vals +
= self
::getCentralUserInfo(
245 $this->getConfig(), $this->getUser(), $this->params
['attachedwiki']
252 protected function getRateLimits() {
254 ApiResult
::META_TYPE
=> 'assoc',
257 $user = $this->getUser();
258 if ( !$user->isPingLimitable() ) {
259 return $retval; // No limits
262 // Find out which categories we belong to
264 if ( $user->isAnon() ) {
265 $categories[] = 'anon';
267 $categories[] = 'user';
269 if ( $user->isNewbie() ) {
270 $categories[] = 'ip';
271 $categories[] = 'subnet';
272 if ( !$user->isAnon() ) {
273 $categories[] = 'newbie';
276 $categories = array_merge( $categories, $user->getGroups() );
278 // Now get the actual limits
279 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
280 foreach ( $categories as $cat ) {
281 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
282 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
283 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
291 public function getAllowedParams() {
294 ApiBase
::PARAM_ISMULTI
=> true,
295 ApiBase
::PARAM_TYPE
=> [
313 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
315 'apihelp-query+userinfo-paramvalue-prop-unreadcount',
316 self
::WL_UNREAD_LIMIT
- 1,
317 self
::WL_UNREAD_LIMIT
. '+',
321 'attachedwiki' => null,
325 protected function getExamplesMessages() {
327 'action=query&meta=userinfo'
328 => 'apihelp-query+userinfo-example-simple',
329 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
330 => 'apihelp-query+userinfo-example-data',
334 public function getHelpUrls() {
335 return 'https://www.mediawiki.org/wiki/API:Userinfo';