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 $user = $this->getUser();
56 $result = $this->getResult();
58 $vals['id'] = intval( $user->getId() );
59 $vals['name'] = $user->getName();
61 if ( $user->isAnon() ) {
65 if ( isset( $this->prop
['blockinfo'] ) ) {
66 if ( $user->isBlocked() ) {
67 $block = $user->getBlock();
68 $vals['blockid'] = $block->getId();
69 $vals['blockedby'] = $block->getByName();
70 $vals['blockedbyid'] = $block->getBy();
71 $vals['blockreason'] = $user->blockedFor();
72 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601
, $block->mTimestamp
);
73 $vals['blockexpiry'] = $block->getExpiry() === 'infinity'
75 : wfTimestamp( TS_ISO_8601
, $block->getExpiry() );
79 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
80 $vals['messages'] = '';
83 if ( isset( $this->prop
['groups'] ) ) {
84 $vals['groups'] = $user->getEffectiveGroups();
85 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
88 if ( isset( $this->prop
['implicitgroups'] ) ) {
89 $vals['implicitgroups'] = $user->getAutomaticGroups();
90 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
93 if ( isset( $this->prop
['rights'] ) ) {
94 // User::getRights() may return duplicate values, strip them
95 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
96 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
99 if ( isset( $this->prop
['changeablegroups'] ) ) {
100 $vals['changeablegroups'] = $user->changeableGroups();
101 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
102 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
103 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
104 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
107 if ( isset( $this->prop
['options'] ) ) {
108 $vals['options'] = $user->getOptions();
111 if ( isset( $this->prop
['preferencestoken'] ) ) {
112 $p = $this->getModulePrefix();
114 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
117 if ( isset( $this->prop
['preferencestoken'] ) &&
118 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
119 $user->isAllowed( 'editmyoptions' )
121 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
124 if ( isset( $this->prop
['editcount'] ) ) {
125 // use intval to prevent null if a non-logged-in user calls
126 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
127 $vals['editcount'] = intval( $user->getEditCount() );
130 if ( isset( $this->prop
['ratelimits'] ) ) {
131 $vals['ratelimits'] = $this->getRateLimits();
134 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
135 $vals['realname'] = $user->getRealName();
138 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
139 if ( isset( $this->prop
['email'] ) ) {
140 $vals['email'] = $user->getEmail();
141 $auth = $user->getEmailAuthenticationTimestamp();
142 if ( !is_null( $auth ) ) {
143 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601
, $auth );
148 if ( isset( $this->prop
['registrationdate'] ) ) {
149 $regDate = $user->getRegistration();
150 if ( $regDate !== false ) {
151 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
155 if ( isset( $this->prop
['acceptlang'] ) ) {
156 $langs = $this->getRequest()->getAcceptLang();
157 $acceptLang = array();
158 foreach ( $langs as $lang => $val ) {
159 $r = array( 'q' => $val );
160 ApiResult
::setContent( $r, $lang );
163 $result->setIndexedTagName( $acceptLang, 'lang' );
164 $vals['acceptlang'] = $acceptLang;
167 if ( isset( $this->prop
['unreadcount'] ) ) {
168 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE
, 'watchlist' );
170 $sql = $dbr->selectSQLText(
172 array( 'dummy' => 1 ),
174 'wl_user' => $user->getId(),
175 'wl_notificationtimestamp IS NOT NULL',
178 array( 'LIMIT' => self
::WL_UNREAD_LIMIT
)
180 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
182 if ( $count >= self
::WL_UNREAD_LIMIT
) {
183 $vals['unreadcount'] = self
::WL_UNREAD_LIMIT
. '+';
185 $vals['unreadcount'] = (int)$count;
192 protected function getRateLimits() {
193 $user = $this->getUser();
194 if ( !$user->isPingLimitable() ) {
195 return array(); // No limits
198 // Find out which categories we belong to
199 $categories = array();
200 if ( $user->isAnon() ) {
201 $categories[] = 'anon';
203 $categories[] = 'user';
205 if ( $user->isNewbie() ) {
206 $categories[] = 'ip';
207 $categories[] = 'subnet';
208 if ( !$user->isAnon() ) {
209 $categories[] = 'newbie';
212 $categories = array_merge( $categories, $user->getGroups() );
214 // Now get the actual limits
216 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
217 foreach ( $categories as $cat ) {
218 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
219 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
220 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
228 public function getAllowedParams() {
231 ApiBase
::PARAM_DFLT
=> null,
232 ApiBase
::PARAM_ISMULTI
=> true,
233 ApiBase
::PARAM_TYPE
=> array(
250 ApiBase
::PARAM_HELP_MSG
=> array(
251 'apihelp-query+userinfo-param-prop',
252 self
::WL_UNREAD_LIMIT
- 1,
253 self
::WL_UNREAD_LIMIT
. '+',
259 protected function getExamplesMessages() {
261 'action=query&meta=userinfo'
262 => 'apihelp-query+userinfo-example-simple',
263 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
264 => 'apihelp-query+userinfo-example-data',
268 public function getHelpUrls() {
269 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';