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 $prop = array();
38 public function __construct( ApiQuery
$query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'ui' );
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
46 if ( !is_null( $params['prop'] ) ) {
47 $this->prop
= array_flip( $params['prop'] );
50 $r = $this->getCurrentUserInfo();
51 $result->addValue( 'query', $this->getModuleName(), $r );
54 protected function getCurrentUserInfo() {
55 global $wgHiddenPrefs, $wgRCMaxAge;
56 $user = $this->getUser();
57 $result = $this->getResult();
59 $vals['id'] = intval( $user->getId() );
60 $vals['name'] = $user->getName();
62 if ( $user->isAnon() ) {
66 if ( isset( $this->prop
['blockinfo'] ) ) {
67 if ( $user->isBlocked() ) {
68 $block = $user->getBlock();
69 $vals['blockid'] = $block->getId();
70 $vals['blockedby'] = $block->getByName();
71 $vals['blockedbyid'] = $block->getBy();
72 $vals['blockreason'] = $user->blockedFor();
76 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
77 $vals['messages'] = '';
80 if ( isset( $this->prop
['groups'] ) ) {
81 $vals['groups'] = $user->getEffectiveGroups();
82 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
85 if ( isset( $this->prop
['implicitgroups'] ) ) {
86 $vals['implicitgroups'] = $user->getAutomaticGroups();
87 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
90 if ( isset( $this->prop
['rights'] ) ) {
91 // User::getRights() may return duplicate values, strip them
92 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
93 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
96 if ( isset( $this->prop
['changeablegroups'] ) ) {
97 $vals['changeablegroups'] = $user->changeableGroups();
98 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
101 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
104 if ( isset( $this->prop
['options'] ) ) {
105 $vals['options'] = $user->getOptions();
108 if ( isset( $this->prop
['preferencestoken'] ) &&
109 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
110 $user->isAllowed( 'editmyoptions' )
112 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
115 if ( isset( $this->prop
['editcount'] ) ) {
116 // use intval to prevent null if a non-logged-in user calls
117 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
118 $vals['editcount'] = intval( $user->getEditCount() );
121 if ( isset( $this->prop
['ratelimits'] ) ) {
122 $vals['ratelimits'] = $this->getRateLimits();
125 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
126 $vals['realname'] = $user->getRealName();
129 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
130 if ( isset( $this->prop
['email'] ) ) {
131 $vals['email'] = $user->getEmail();
132 $auth = $user->getEmailAuthenticationTimestamp();
133 if ( !is_null( $auth ) ) {
134 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
139 if ( isset( $this->prop
['registrationdate'] ) ) {
140 $regDate = $user->getRegistration();
141 if ( $regDate !== false ) {
142 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
146 if ( isset( $this->prop
['acceptlang'] ) ) {
147 $langs = $this->getRequest()->getAcceptLang();
148 $acceptLang = array();
149 foreach ( $langs as $lang => $val ) {
150 $r = array( 'q' => $val );
151 ApiResult
::setContent( $r, $lang );
154 $result->setIndexedTagName( $acceptLang, 'lang' );
155 $vals['acceptlang'] = $acceptLang;
158 if ( isset( $this->prop
['unreadcount'] ) ) {
159 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
161 $sql = $dbr->selectSQLText(
163 array( 'dummy' => 1 ),
165 'wl_user' => $user->getId(),
166 'wl_notificationtimestamp IS NOT NULL',
169 array( 'LIMIT' => self
::WL_UNREAD_LIMIT
)
171 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
173 if ( $count >= self
::WL_UNREAD_LIMIT
) {
174 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
176 $vals['unreadcount'] = (int)$count;
183 protected function getRateLimits() {
184 global $wgRateLimits;
185 $user = $this->getUser();
186 if ( !$user->isPingLimitable() ) {
187 return array(); // No limits
190 // Find out which categories we belong to
191 $categories = array();
192 if ( $user->isAnon() ) {
193 $categories[] = 'anon';
195 $categories[] = 'user';
197 if ( $user->isNewbie() ) {
198 $categories[] = 'ip';
199 $categories[] = 'subnet';
200 if ( !$user->isAnon() ) {
201 $categories[] = 'newbie';
204 $categories = array_merge( $categories, $user->getGroups() );
206 // Now get the actual limits
208 foreach ( $wgRateLimits as $action => $limits ) {
209 foreach ( $categories as $cat ) {
210 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
211 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
212 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
220 public function getAllowedParams() {
223 ApiBase
::PARAM_DFLT
=> null,
224 ApiBase
::PARAM_ISMULTI
=> true,
225 ApiBase
::PARAM_TYPE
=> array(
246 public function getParamDescription() {
249 'What pieces of information to include',
250 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
251 ' hasmsg - Adds a tag "message" if the current user has pending messages',
252 ' groups - Lists all the groups the current user belongs to',
253 ' implicitgroups - Lists all the groups the current user is automatically a member of',
254 ' rights - Lists all the rights the current user has',
255 ' changeablegroups - Lists the groups the current user can add to and remove from',
256 ' options - Lists all preferences the current user has set',
257 ' preferencestoken - Get a token to change current user\'s preferences',
258 ' editcount - Adds the current user\'s edit count',
259 ' ratelimits - Lists all rate limits applying to the current user',
260 ' realname - Adds the user\'s real name',
261 ' email - Adds the user\'s email address and email authentication date',
262 ' acceptlang - Echoes the Accept-Language header sent by ' .
263 'the client in a structured format',
264 ' registrationdate - Adds the user\'s registration date',
265 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
266 '(maximum ' . ( self
::WL_UNREAD_LIMIT
- 1 ) . '; returns "' .
267 self
::WL_UNREAD_LIMIT
. '+" if more)',
272 public function getResultProperties() {
274 ApiBase
::PROP_LIST
=> false,
280 'blockinfo' => array(
282 ApiBase
::PROP_TYPE
=> 'integer',
283 ApiBase
::PROP_NULLABLE
=> true
285 'blockedby' => array(
286 ApiBase
::PROP_TYPE
=> 'string',
287 ApiBase
::PROP_NULLABLE
=> true
289 'blockedbyid' => array(
290 ApiBase
::PROP_TYPE
=> 'integer',
291 ApiBase
::PROP_NULLABLE
=> true
293 'blockedreason' => array(
294 ApiBase
::PROP_TYPE
=> 'string',
295 ApiBase
::PROP_NULLABLE
=> true
299 'messages' => 'boolean'
301 'preferencestoken' => array(
302 'preferencestoken' => 'string'
304 'editcount' => array(
305 'editcount' => 'integer'
309 ApiBase
::PROP_TYPE
=> 'string',
310 ApiBase
::PROP_NULLABLE
=> true
315 'emailauthenticated' => array(
316 ApiBase
::PROP_TYPE
=> 'timestamp',
317 ApiBase
::PROP_NULLABLE
=> true
320 'registrationdate' => array(
321 'registrationdate' => array(
322 ApiBase
::PROP_TYPE
=> 'timestamp',
323 ApiBase
::PROP_NULLABLE
=> true
329 public function getDescription() {
330 return 'Get information about the current user.';
333 public function getExamples() {
335 'api.php?action=query&meta=userinfo',
336 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
340 public function getHelpUrls() {
341 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';