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
28 * Query module to get information about the currently logged-in user
32 class ApiQueryUserInfo
extends ApiQueryBase
{
34 const WL_UNREAD_LIMIT
= 1000;
36 private $params = array();
37 private $prop = array();
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 basic info about a given 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
66 public static function getBlockInfo( Block
$block ) {
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'
81 * Get central user info
82 * @param Config $config
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.
92 public static function getCentralUserInfo( Config
$config, User
$user, $attachedWiki = null ) {
93 $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
96 'centralids' => array(),
97 'attachedlocal' => array(),
99 ApiResult
::setArrayType( $ret['centralids'], 'assoc' );
100 ApiResult
::setArrayType( $ret['attachedlocal'], 'assoc' );
101 if ( $attachedWiki ) {
102 $ret['attachedwiki'] = array();
103 ApiResult
::setArrayType( $ret['attachedwiki'], 'assoc' );
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 );
119 protected function getCurrentUserInfo() {
120 $user = $this->getUser();
122 $vals['id'] = intval( $user->getId() );
123 $vals['name'] = $user->getName();
125 if ( $user->isAnon() ) {
126 $vals['anon'] = true;
129 if ( isset( $this->prop
['blockinfo'] ) && $user->isBlocked() ) {
130 $vals = array_merge( $vals, self
::getBlockInfo( $user->getBlock() ) );
133 if ( isset( $this->prop
['hasmsg'] ) ) {
134 $vals['messages'] = $user->getNewtalk();
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
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
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
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' );
164 if ( isset( $this->prop
['options'] ) ) {
165 $vals['options'] = $user->getOptions();
166 $vals['options'][ApiResult
::META_BC_BOOLS
] = array_keys( $vals['options'] );
169 if ( isset( $this->prop
['preferencestoken'] ) ) {
170 $p = $this->getModulePrefix();
172 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
175 if ( isset( $this->prop
['preferencestoken'] ) &&
176 !$this->lacksSameOriginSecurity() &&
177 $user->isAllowed( 'editmyoptions' )
179 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
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() );
188 if ( isset( $this->prop
['ratelimits'] ) ) {
189 $vals['ratelimits'] = $this->getRateLimits();
192 if ( isset( $this->prop
['realname'] ) &&
193 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
195 $vals['realname'] = $user->getRealName();
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 );
208 if ( isset( $this->prop
['registrationdate'] ) ) {
209 $regDate = $user->getRegistration();
210 if ( $regDate !== false ) {
211 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
215 if ( isset( $this->prop
['acceptlang'] ) ) {
216 $langs = $this->getRequest()->getAcceptLang();
217 $acceptLang = array();
218 foreach ( $langs as $lang => $val ) {
219 $r = array( 'q' => $val );
220 ApiResult
::setContentValue( $r, 'code', $lang );
223 ApiResult
::setIndexedTagName( $acceptLang, 'lang' );
224 $vals['acceptlang'] = $acceptLang;
227 if ( isset( $this->prop
['unreadcount'] ) ) {
228 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
230 $count = $dbr->selectRowCount(
234 'wl_user' => $user->getId(),
235 'wl_notificationtimestamp IS NOT NULL',
238 array( 'LIMIT' => self
::WL_UNREAD_LIMIT
)
241 if ( $count >= self
::WL_UNREAD_LIMIT
) {
242 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
244 $vals['unreadcount'] = $count;
248 if ( isset( $this->prop
['centralids'] ) ) {
249 $vals +
= self
::getCentralUserInfo(
250 $this->getConfig(), $this->getUser(), $this->params
['attachedwiki']
257 protected function getRateLimits() {
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 = array();
269 if ( $user->isAnon() ) {
270 $categories[] = 'anon';
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] );
296 public function getAllowedParams() {
299 ApiBase
::PARAM_ISMULTI
=> true,
300 ApiBase
::PARAM_TYPE
=> array(
318 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(
319 'unreadcount' => array(
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() {
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';