3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\MediaWikiServices
;
26 * Query module to get information about the currently logged-in user
30 class ApiQueryUserInfo
extends ApiQueryBase
{
32 use ApiBlockInfoTrait
;
34 const WL_UNREAD_LIMIT
= 1000;
39 public function __construct( ApiQuery
$query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'ui' );
43 public function execute() {
44 $this->params
= $this->extractRequestParams();
45 $result = $this->getResult();
47 if ( !is_null( $this->params
['prop'] ) ) {
48 $this->prop
= array_flip( $this->params
['prop'] );
51 $r = $this->getCurrentUserInfo();
52 $result->addValue( 'query', $this->getModuleName(), $r );
56 * Get central user info
57 * @param Config $config
59 * @param string|null $attachedWiki
60 * @return array Central user info
61 * - centralids: Array mapping non-local Central ID provider names to IDs
62 * - attachedlocal: Array mapping Central ID provider names to booleans
63 * indicating whether the local user is attached.
64 * - attachedwiki: Array mapping Central ID provider names to booleans
65 * indicating whether the user is attached to $attachedWiki.
67 public static function getCentralUserInfo( Config
$config, User
$user, $attachedWiki = null ) {
68 $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
72 'attachedlocal' => [],
74 ApiResult
::setArrayType( $ret['centralids'], 'assoc' );
75 ApiResult
::setArrayType( $ret['attachedlocal'], 'assoc' );
76 if ( $attachedWiki ) {
77 $ret['attachedwiki'] = [];
78 ApiResult
::setArrayType( $ret['attachedwiki'], 'assoc' );
81 $name = $user->getName();
82 foreach ( $providerIds as $providerId ) {
83 $provider = CentralIdLookup
::factory( $providerId );
84 $ret['centralids'][$providerId] = $provider->centralIdFromName( $name );
85 $ret['attachedlocal'][$providerId] = $provider->isAttached( $user );
86 if ( $attachedWiki ) {
87 $ret['attachedwiki'][$providerId] = $provider->isAttached( $user, $attachedWiki );
94 protected function getCurrentUserInfo() {
95 $user = $this->getUser();
97 $vals['id'] = (int)$user->getId();
98 $vals['name'] = $user->getName();
100 if ( $user->isAnon() ) {
101 $vals['anon'] = true;
104 if ( isset( $this->prop
['blockinfo'] ) ) {
105 $block = $user->getBlock();
107 $vals = array_merge( $vals, $this->getBlockInfo( $block ) );
111 if ( isset( $this->prop
['hasmsg'] ) ) {
112 $vals['messages'] = $user->getNewtalk();
115 if ( isset( $this->prop
['groups'] ) ) {
116 $vals['groups'] = $user->getEffectiveGroups();
117 ApiResult
::setArrayType( $vals['groups'], 'array' ); // even if empty
118 ApiResult
::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
121 if ( isset( $this->prop
['groupmemberships'] ) ) {
122 $ugms = $user->getGroupMemberships();
123 $vals['groupmemberships'] = [];
124 foreach ( $ugms as $group => $ugm ) {
125 $vals['groupmemberships'][] = [
127 'expiry' => ApiResult
::formatExpiry( $ugm->getExpiry() ),
130 ApiResult
::setArrayType( $vals['groupmemberships'], 'array' ); // even if empty
131 ApiResult
::setIndexedTagName( $vals['groupmemberships'], 'groupmembership' ); // even if empty
134 if ( isset( $this->prop
['implicitgroups'] ) ) {
135 $vals['implicitgroups'] = $user->getAutomaticGroups();
136 ApiResult
::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty
137 ApiResult
::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
140 if ( isset( $this->prop
['rights'] ) ) {
141 // User::getRights() may return duplicate values, strip them
142 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
143 ApiResult
::setArrayType( $vals['rights'], 'array' ); // even if empty
144 ApiResult
::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
147 if ( isset( $this->prop
['changeablegroups'] ) ) {
148 $vals['changeablegroups'] = $user->changeableGroups();
149 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
150 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
151 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
152 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
155 if ( isset( $this->prop
['options'] ) ) {
156 $vals['options'] = $user->getOptions();
157 $vals['options'][ApiResult
::META_BC_BOOLS
] = array_keys( $vals['options'] );
160 if ( isset( $this->prop
['preferencestoken'] ) &&
161 !$this->lacksSameOriginSecurity() &&
162 $user->isAllowed( 'editmyoptions' )
164 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
167 if ( isset( $this->prop
['editcount'] ) ) {
168 // use intval to prevent null if a non-logged-in user calls
169 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
170 $vals['editcount'] = (int)$user->getEditCount();
173 if ( isset( $this->prop
['ratelimits'] ) ) {
174 $vals['ratelimits'] = $this->getRateLimits();
177 if ( isset( $this->prop
['realname'] ) &&
178 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
180 $vals['realname'] = $user->getRealName();
183 if ( $user->isAllowed( 'viewmyprivateinfo' ) && isset( $this->prop
['email'] ) ) {
184 $vals['email'] = $user->getEmail();
185 $auth = $user->getEmailAuthenticationTimestamp();
186 if ( $auth !== null ) {
187 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
191 if ( isset( $this->prop
['registrationdate'] ) ) {
192 $regDate = $user->getRegistration();
193 if ( $regDate !== false ) {
194 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
198 if ( isset( $this->prop
['acceptlang'] ) ) {
199 $langs = $this->getRequest()->getAcceptLang();
201 foreach ( $langs as $lang => $val ) {
202 $r = [ 'q' => $val ];
203 ApiResult
::setContentValue( $r, 'code', $lang );
206 ApiResult
::setIndexedTagName( $acceptLang, 'lang' );
207 $vals['acceptlang'] = $acceptLang;
210 if ( isset( $this->prop
['unreadcount'] ) ) {
211 $store = MediaWikiServices
::getInstance()->getWatchedItemStore();
212 $unreadNotifications = $store->countUnreadNotifications(
214 self
::WL_UNREAD_LIMIT
217 if ( $unreadNotifications === true ) {
218 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
220 $vals['unreadcount'] = $unreadNotifications;
224 if ( isset( $this->prop
['centralids'] ) ) {
225 $vals +
= self
::getCentralUserInfo(
226 $this->getConfig(), $this->getUser(), $this->params
['attachedwiki']
230 if ( isset( $this->prop
['latestcontrib'] ) ) {
231 $ts = $this->getLatestContributionTime();
232 if ( $ts !== null ) {
233 $vals['latestcontrib'] = $ts;
240 protected function getRateLimits() {
242 ApiResult
::META_TYPE
=> 'assoc',
245 $user = $this->getUser();
246 if ( !$user->isPingLimitable() ) {
247 return $retval; // No limits
250 // Find out which categories we belong to
252 if ( $user->isAnon() ) {
253 $categories[] = 'anon';
255 $categories[] = 'user';
257 if ( $user->isNewbie() ) {
258 $categories[] = 'ip';
259 $categories[] = 'subnet';
260 if ( !$user->isAnon() ) {
261 $categories[] = 'newbie';
264 $categories = array_merge( $categories, $user->getGroups() );
266 // Now get the actual limits
267 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
268 foreach ( $categories as $cat ) {
269 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
270 $retval[$action][$cat]['hits'] = (int)$limits[$cat][0];
271 $retval[$action][$cat]['seconds'] = (int)$limits[$cat][1];
280 * @return string|null ISO 8601 timestamp of current user's last contribution or null if none
282 protected function getLatestContributionTime() {
283 global $wgActorTableSchemaMigrationStage;
285 $user = $this->getUser();
286 $dbr = $this->getDB();
288 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW
) {
289 if ( $user->getActorId() === null ) {
292 $res = $dbr->selectField( 'revision_actor_temp',
293 'MAX(revactor_timestamp)',
294 [ 'revactor_actor' => $user->getActorId() ],
298 if ( $user->isLoggedIn() ) {
299 $conds = [ 'rev_user' => $user->getId() ];
301 $conds = [ 'rev_user_text' => $user->getName() ];
303 $res = $dbr->selectField( 'revision',
304 'MAX(rev_timestamp)',
310 return $res ?
wfTimestamp( TS_ISO_8601
, $res ) : null;
313 public function getAllowedParams() {
316 ApiBase
::PARAM_ISMULTI
=> true,
317 ApiBase
::PARAM_TYPE
=> [
337 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [
339 'apihelp-query+userinfo-paramvalue-prop-unreadcount',
340 self
::WL_UNREAD_LIMIT
- 1,
341 self
::WL_UNREAD_LIMIT
. '+',
344 ApiBase
::PARAM_DEPRECATED_VALUES
=> [
345 'preferencestoken' => [
346 'apiwarn-deprecation-withreplacement',
347 $this->getModulePrefix() . "prop=preferencestoken",
348 'action=query&meta=tokens',
352 'attachedwiki' => null,
356 protected function getExamplesMessages() {
358 'action=query&meta=userinfo'
359 => 'apihelp-query+userinfo-example-simple',
360 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
361 => 'apihelp-query+userinfo-example-data',
365 public function getHelpUrls() {
366 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Userinfo';