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 );
55 * Get basic info about a given block
57 * @return array Array containing several keys:
58 * - blockid - ID of the block
59 * - blockedby - username of the blocker
60 * - blockedbyid - user ID of the blocker
61 * - blockreason - reason provided for the block
62 * - blockedtimestamp - timestamp for when the block was placed/modified
63 * - blockexpiry - expiry time of the block
65 public static function getBlockInfo( Block
$block ) {
68 $vals['blockid'] = $block->getId();
69 $vals['blockedby'] = $block->getByName();
70 $vals['blockedbyid'] = $block->getBy();
71 $vals['blockreason'] = $block->mReason
;
72 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $block->mTimestamp
);
73 $vals['blockexpiry'] = $wgContLang->formatExpiry(
74 $block->getExpiry(), TS_ISO_8601
, 'infinite'
79 protected function getCurrentUserInfo() {
80 $user = $this->getUser();
81 $result = $this->getResult();
83 $vals['id'] = intval( $user->getId() );
84 $vals['name'] = $user->getName();
86 if ( $user->isAnon() ) {
90 if ( isset( $this->prop
['blockinfo'] ) && $user->isBlocked() ) {
91 $vals = array_merge( $vals, self
::getBlockInfo( $user->getBlock() ) );
94 if ( isset( $this->prop
['hasmsg'] ) ) {
95 $vals['messages'] = $user->getNewtalk();
98 if ( isset( $this->prop
['groups'] ) ) {
99 $vals['groups'] = $user->getEffectiveGroups();
100 ApiResult
::setArrayType( $vals['groups'], 'array' ); // even if empty
101 ApiResult
::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
104 if ( isset( $this->prop
['implicitgroups'] ) ) {
105 $vals['implicitgroups'] = $user->getAutomaticGroups();
106 ApiResult
::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty
107 ApiResult
::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
110 if ( isset( $this->prop
['rights'] ) ) {
111 // User::getRights() may return duplicate values, strip them
112 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
113 ApiResult
::setArrayType( $vals['rights'], 'array' ); // even if empty
114 ApiResult
::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
117 if ( isset( $this->prop
['changeablegroups'] ) ) {
118 $vals['changeablegroups'] = $user->changeableGroups();
119 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
120 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
121 ApiResult
::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
122 ApiResult
::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
125 if ( isset( $this->prop
['options'] ) ) {
126 $vals['options'] = $user->getOptions();
127 $vals['options'][ApiResult
::META_BC_BOOLS
] = array_keys( $vals['options'] );
130 if ( isset( $this->prop
['preferencestoken'] ) ) {
131 $p = $this->getModulePrefix();
133 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
136 if ( isset( $this->prop
['preferencestoken'] ) &&
137 !$this->lacksSameOriginSecurity() &&
138 $user->isAllowed( 'editmyoptions' )
140 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
143 if ( isset( $this->prop
['editcount'] ) ) {
144 // use intval to prevent null if a non-logged-in user calls
145 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
146 $vals['editcount'] = intval( $user->getEditCount() );
149 if ( isset( $this->prop
['ratelimits'] ) ) {
150 $vals['ratelimits'] = $this->getRateLimits();
153 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
154 $vals['realname'] = $user->getRealName();
157 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
158 if ( isset( $this->prop
['email'] ) ) {
159 $vals['email'] = $user->getEmail();
160 $auth = $user->getEmailAuthenticationTimestamp();
161 if ( !is_null( $auth ) ) {
162 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
167 if ( isset( $this->prop
['registrationdate'] ) ) {
168 $regDate = $user->getRegistration();
169 if ( $regDate !== false ) {
170 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
174 if ( isset( $this->prop
['acceptlang'] ) ) {
175 $langs = $this->getRequest()->getAcceptLang();
176 $acceptLang = array();
177 foreach ( $langs as $lang => $val ) {
178 $r = array( 'q' => $val );
179 ApiResult
::setContentValue( $r, 'code', $lang );
182 ApiResult
::setIndexedTagName( $acceptLang, 'lang' );
183 $vals['acceptlang'] = $acceptLang;
186 if ( isset( $this->prop
['unreadcount'] ) ) {
187 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
189 $count = $dbr->selectRowCount(
193 'wl_user' => $user->getId(),
194 'wl_notificationtimestamp IS NOT NULL',
197 array( 'LIMIT' => self
::WL_UNREAD_LIMIT
)
200 if ( $count >= self
::WL_UNREAD_LIMIT
) {
201 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
203 $vals['unreadcount'] = $count;
210 protected function getRateLimits() {
212 ApiResult
::META_TYPE
=> 'assoc',
215 $user = $this->getUser();
216 if ( !$user->isPingLimitable() ) {
217 return $retval; // No limits
220 // Find out which categories we belong to
221 $categories = array();
222 if ( $user->isAnon() ) {
223 $categories[] = 'anon';
225 $categories[] = 'user';
227 if ( $user->isNewbie() ) {
228 $categories[] = 'ip';
229 $categories[] = 'subnet';
230 if ( !$user->isAnon() ) {
231 $categories[] = 'newbie';
234 $categories = array_merge( $categories, $user->getGroups() );
236 // Now get the actual limits
237 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
238 foreach ( $categories as $cat ) {
239 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
240 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
241 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
249 public function getAllowedParams() {
252 ApiBase
::PARAM_DFLT
=> null,
253 ApiBase
::PARAM_ISMULTI
=> true,
254 ApiBase
::PARAM_TYPE
=> array(
271 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> array(
272 'unreadcount' => array(
273 'apihelp-query+userinfo-paramvalue-prop-unreadcount',
274 self
::WL_UNREAD_LIMIT
- 1,
275 self
::WL_UNREAD_LIMIT
. '+',
282 protected function getExamplesMessages() {
284 'action=query&meta=userinfo'
285 => 'apihelp-query+userinfo-example-simple',
286 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
287 => 'apihelp-query+userinfo-example-data',
291 public function getHelpUrls() {
292 return 'https://www.mediawiki.org/wiki/API:Userinfo';