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() {
57 $user = $this->getUser();
58 $result = $this->getResult();
60 $vals['id'] = intval( $user->getId() );
61 $vals['name'] = $user->getName();
63 if ( $user->isAnon() ) {
67 if ( isset( $this->prop
['blockinfo'] ) ) {
68 if ( $user->isBlocked() ) {
69 $block = $user->getBlock();
70 $vals['blockid'] = $block->getId();
71 $vals['blockedby'] = $block->getByName();
72 $vals['blockedbyid'] = $block->getBy();
73 $vals['blockreason'] = $user->blockedFor();
74 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $block->mTimestamp
);
75 $vals['blockexpiry'] = $wgContLang->formatExpiry(
76 $block->getExpiry(), TS_ISO_8601
, 'infinite'
81 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
82 $vals['messages'] = '';
85 if ( isset( $this->prop
['groups'] ) ) {
86 $vals['groups'] = $user->getEffectiveGroups();
87 ApiResult
::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
90 if ( isset( $this->prop
['implicitgroups'] ) ) {
91 $vals['implicitgroups'] = $user->getAutomaticGroups();
92 ApiResult
::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
95 if ( isset( $this->prop
['rights'] ) ) {
96 // User::getRights() may return duplicate values, strip them
97 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
98 ApiResult
::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
101 if ( isset( $this->prop
['changeablegroups'] ) ) {
102 $vals['changeablegroups'] = $user->changeableGroups();
103 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
104 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
105 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
106 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
109 if ( isset( $this->prop
['options'] ) ) {
110 $vals['options'] = $user->getOptions();
113 if ( isset( $this->prop
['preferencestoken'] ) ) {
114 $p = $this->getModulePrefix();
116 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
119 if ( isset( $this->prop
['preferencestoken'] ) &&
120 !$this->lacksSameOriginSecurity() &&
121 $user->isAllowed( 'editmyoptions' )
123 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
126 if ( isset( $this->prop
['editcount'] ) ) {
127 // use intval to prevent null if a non-logged-in user calls
128 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
129 $vals['editcount'] = intval( $user->getEditCount() );
132 if ( isset( $this->prop
['ratelimits'] ) ) {
133 $vals['ratelimits'] = $this->getRateLimits();
136 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
137 $vals['realname'] = $user->getRealName();
140 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
141 if ( isset( $this->prop
['email'] ) ) {
142 $vals['email'] = $user->getEmail();
143 $auth = $user->getEmailAuthenticationTimestamp();
144 if ( !is_null( $auth ) ) {
145 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
150 if ( isset( $this->prop
['registrationdate'] ) ) {
151 $regDate = $user->getRegistration();
152 if ( $regDate !== false ) {
153 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
157 if ( isset( $this->prop
['acceptlang'] ) ) {
158 $langs = $this->getRequest()->getAcceptLang();
159 $acceptLang = array();
160 foreach ( $langs as $lang => $val ) {
161 $r = array( 'q' => $val );
162 ApiResult
::setContentValue( $r, 'code', $lang );
165 ApiResult
::setIndexedTagName( $acceptLang, 'lang' );
166 $vals['acceptlang'] = $acceptLang;
169 if ( isset( $this->prop
['unreadcount'] ) ) {
170 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
172 $count = $dbr->selectRowCount(
176 'wl_user' => $user->getId(),
177 'wl_notificationtimestamp IS NOT NULL',
180 array( 'LIMIT' => self
::WL_UNREAD_LIMIT
)
183 if ( $count >= self
::WL_UNREAD_LIMIT
) {
184 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
186 $vals['unreadcount'] = $count;
193 protected function getRateLimits() {
194 $user = $this->getUser();
195 if ( !$user->isPingLimitable() ) {
196 return array(); // No limits
199 // Find out which categories we belong to
200 $categories = array();
201 if ( $user->isAnon() ) {
202 $categories[] = 'anon';
204 $categories[] = 'user';
206 if ( $user->isNewbie() ) {
207 $categories[] = 'ip';
208 $categories[] = 'subnet';
209 if ( !$user->isAnon() ) {
210 $categories[] = 'newbie';
213 $categories = array_merge( $categories, $user->getGroups() );
215 // Now get the actual limits
217 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
218 foreach ( $categories as $cat ) {
219 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
220 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
221 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
229 public function getAllowedParams() {
232 ApiBase
::PARAM_DFLT
=> null,
233 ApiBase
::PARAM_ISMULTI
=> true,
234 ApiBase
::PARAM_TYPE
=> array(
251 ApiBase
::PARAM_HELP_MSG
=> array(
252 'apihelp-query+userinfo-param-prop',
253 self
::WL_UNREAD_LIMIT
- 1,
254 self
::WL_UNREAD_LIMIT
. '+',
260 protected function getExamplesMessages() {
262 'action=query&meta=userinfo'
263 => 'apihelp-query+userinfo-example-simple',
264 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
265 => 'apihelp-query+userinfo-example-data',
269 public function getHelpUrls() {
270 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';