Remove useless temporary variable in Setup.php
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob140f35a8323da558d6bcdb3a095991638ce5e147
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to get information about the currently logged-in user
30 * @ingroup API
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();
57 $vals = array();
58 $vals['id'] = intval( $user->getId() );
59 $vals['name'] = $user->getName();
61 if ( $user->isAnon() ) {
62 $vals['anon'] = '';
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();
75 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
76 $vals['messages'] = '';
79 if ( isset( $this->prop['groups'] ) ) {
80 $vals['groups'] = $user->getEffectiveGroups();
81 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
84 if ( isset( $this->prop['implicitgroups'] ) ) {
85 $vals['implicitgroups'] = $user->getAutomaticGroups();
86 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
89 if ( isset( $this->prop['rights'] ) ) {
90 // User::getRights() may return duplicate values, strip them
91 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
92 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
95 if ( isset( $this->prop['changeablegroups'] ) ) {
96 $vals['changeablegroups'] = $user->changeableGroups();
97 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
103 if ( isset( $this->prop['options'] ) ) {
104 $vals['options'] = $user->getOptions();
107 if ( isset( $this->prop['preferencestoken'] ) &&
108 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
109 $user->isAllowed( 'editmyoptions' )
111 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
114 if ( isset( $this->prop['editcount'] ) ) {
115 // use intval to prevent null if a non-logged-in user calls
116 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
117 $vals['editcount'] = intval( $user->getEditCount() );
120 if ( isset( $this->prop['ratelimits'] ) ) {
121 $vals['ratelimits'] = $this->getRateLimits();
124 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
125 $vals['realname'] = $user->getRealName();
128 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
129 if ( isset( $this->prop['email'] ) ) {
130 $vals['email'] = $user->getEmail();
131 $auth = $user->getEmailAuthenticationTimestamp();
132 if ( !is_null( $auth ) ) {
133 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
138 if ( isset( $this->prop['registrationdate'] ) ) {
139 $regDate = $user->getRegistration();
140 if ( $regDate !== false ) {
141 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
145 if ( isset( $this->prop['acceptlang'] ) ) {
146 $langs = $this->getRequest()->getAcceptLang();
147 $acceptLang = array();
148 foreach ( $langs as $lang => $val ) {
149 $r = array( 'q' => $val );
150 ApiResult::setContent( $r, $lang );
151 $acceptLang[] = $r;
153 $result->setIndexedTagName( $acceptLang, 'lang' );
154 $vals['acceptlang'] = $acceptLang;
157 if ( isset( $this->prop['unreadcount'] ) ) {
158 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
160 $sql = $dbr->selectSQLText(
161 'watchlist',
162 array( 'dummy' => 1 ),
163 array(
164 'wl_user' => $user->getId(),
165 'wl_notificationtimestamp IS NOT NULL',
167 __METHOD__,
168 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
170 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
172 if ( $count >= self::WL_UNREAD_LIMIT ) {
173 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
174 } else {
175 $vals['unreadcount'] = (int)$count;
179 return $vals;
182 protected function getRateLimits() {
183 $user = $this->getUser();
184 if ( !$user->isPingLimitable() ) {
185 return array(); // No limits
188 // Find out which categories we belong to
189 $categories = array();
190 if ( $user->isAnon() ) {
191 $categories[] = 'anon';
192 } else {
193 $categories[] = 'user';
195 if ( $user->isNewbie() ) {
196 $categories[] = 'ip';
197 $categories[] = 'subnet';
198 if ( !$user->isAnon() ) {
199 $categories[] = 'newbie';
202 $categories = array_merge( $categories, $user->getGroups() );
204 // Now get the actual limits
205 $retval = array();
206 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
207 foreach ( $categories as $cat ) {
208 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
209 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
210 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
215 return $retval;
218 public function getAllowedParams() {
219 return array(
220 'prop' => array(
221 ApiBase::PARAM_DFLT => null,
222 ApiBase::PARAM_ISMULTI => true,
223 ApiBase::PARAM_TYPE => array(
224 'blockinfo',
225 'hasmsg',
226 'groups',
227 'implicitgroups',
228 'rights',
229 'changeablegroups',
230 'options',
231 'preferencestoken',
232 'editcount',
233 'ratelimits',
234 'email',
235 'realname',
236 'acceptlang',
237 'registrationdate',
238 'unreadcount',
244 public function getParamDescription() {
245 return array(
246 'prop' => array(
247 'What pieces of information to include',
248 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
249 ' hasmsg - Adds a tag "message" if the current user has pending messages',
250 ' groups - Lists all the groups the current user belongs to',
251 ' implicitgroups - Lists all the groups the current user is automatically a member of',
252 ' rights - Lists all the rights the current user has',
253 ' changeablegroups - Lists the groups the current user can add to and remove from',
254 ' options - Lists all preferences the current user has set',
255 ' preferencestoken - Get a token to change current user\'s preferences',
256 ' editcount - Adds the current user\'s edit count',
257 ' ratelimits - Lists all rate limits applying to the current user',
258 ' realname - Adds the user\'s real name',
259 ' email - Adds the user\'s email address and email authentication date',
260 ' acceptlang - Echoes the Accept-Language header sent by ' .
261 'the client in a structured format',
262 ' registrationdate - Adds the user\'s registration date',
263 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
264 '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
265 self::WL_UNREAD_LIMIT . '+" if more)',
270 public function getResultProperties() {
271 return array(
272 ApiBase::PROP_LIST => false,
273 '' => array(
274 'id' => 'integer',
275 'name' => 'string',
276 'anon' => 'boolean'
278 'blockinfo' => array(
279 'blockid' => array(
280 ApiBase::PROP_TYPE => 'integer',
281 ApiBase::PROP_NULLABLE => true
283 'blockedby' => array(
284 ApiBase::PROP_TYPE => 'string',
285 ApiBase::PROP_NULLABLE => true
287 'blockedbyid' => array(
288 ApiBase::PROP_TYPE => 'integer',
289 ApiBase::PROP_NULLABLE => true
291 'blockedreason' => array(
292 ApiBase::PROP_TYPE => 'string',
293 ApiBase::PROP_NULLABLE => true
296 'hasmsg' => array(
297 'messages' => 'boolean'
299 'preferencestoken' => array(
300 'preferencestoken' => 'string'
302 'editcount' => array(
303 'editcount' => 'integer'
305 'realname' => array(
306 'realname' => array(
307 ApiBase::PROP_TYPE => 'string',
308 ApiBase::PROP_NULLABLE => true
311 'email' => array(
312 'email' => 'string',
313 'emailauthenticated' => array(
314 ApiBase::PROP_TYPE => 'timestamp',
315 ApiBase::PROP_NULLABLE => true
318 'registrationdate' => array(
319 'registrationdate' => array(
320 ApiBase::PROP_TYPE => 'timestamp',
321 ApiBase::PROP_NULLABLE => true
327 public function getDescription() {
328 return 'Get information about the current user.';
331 public function getExamples() {
332 return array(
333 'api.php?action=query&meta=userinfo',
334 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
338 public function getHelpUrls() {
339 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';