Merge "jobrunner: Change logging level for STARTING messages"
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob4302ef39f563cc7fa6275680af2647a47bfd6e6b
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 global $wgContLang;
57 $user = $this->getUser();
58 $result = $this->getResult();
59 $vals = array();
60 $vals['id'] = intval( $user->getId() );
61 $vals['name'] = $user->getName();
63 if ( $user->isAnon() ) {
64 $vals['anon'] = true;
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'] ) ) {
82 $vals['messages'] = $user->getNewtalk();
85 if ( isset( $this->prop['groups'] ) ) {
86 $vals['groups'] = $user->getEffectiveGroups();
87 ApiResult::setArrayType( $vals['groups'], 'array' ); // even if empty
88 ApiResult::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
91 if ( isset( $this->prop['implicitgroups'] ) ) {
92 $vals['implicitgroups'] = $user->getAutomaticGroups();
93 ApiResult::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty
94 ApiResult::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
97 if ( isset( $this->prop['rights'] ) ) {
98 // User::getRights() may return duplicate values, strip them
99 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
100 ApiResult::setArrayType( $vals['rights'], 'array' ); // even if empty
101 ApiResult::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
104 if ( isset( $this->prop['changeablegroups'] ) ) {
105 $vals['changeablegroups'] = $user->changeableGroups();
106 ApiResult::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
107 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
108 ApiResult::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
109 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
112 if ( isset( $this->prop['options'] ) ) {
113 $vals['options'] = $user->getOptions();
114 $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] );
117 if ( isset( $this->prop['preferencestoken'] ) ) {
118 $p = $this->getModulePrefix();
119 $this->setWarning(
120 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
123 if ( isset( $this->prop['preferencestoken'] ) &&
124 !$this->lacksSameOriginSecurity() &&
125 $user->isAllowed( 'editmyoptions' )
127 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
130 if ( isset( $this->prop['editcount'] ) ) {
131 // use intval to prevent null if a non-logged-in user calls
132 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
133 $vals['editcount'] = intval( $user->getEditCount() );
136 if ( isset( $this->prop['ratelimits'] ) ) {
137 $vals['ratelimits'] = $this->getRateLimits();
140 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
141 $vals['realname'] = $user->getRealName();
144 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
145 if ( isset( $this->prop['email'] ) ) {
146 $vals['email'] = $user->getEmail();
147 $auth = $user->getEmailAuthenticationTimestamp();
148 if ( !is_null( $auth ) ) {
149 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
154 if ( isset( $this->prop['registrationdate'] ) ) {
155 $regDate = $user->getRegistration();
156 if ( $regDate !== false ) {
157 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
161 if ( isset( $this->prop['acceptlang'] ) ) {
162 $langs = $this->getRequest()->getAcceptLang();
163 $acceptLang = array();
164 foreach ( $langs as $lang => $val ) {
165 $r = array( 'q' => $val );
166 ApiResult::setContentValue( $r, 'code', $lang );
167 $acceptLang[] = $r;
169 ApiResult::setIndexedTagName( $acceptLang, 'lang' );
170 $vals['acceptlang'] = $acceptLang;
173 if ( isset( $this->prop['unreadcount'] ) ) {
174 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
176 $count = $dbr->selectRowCount(
177 'watchlist',
178 '1',
179 array(
180 'wl_user' => $user->getId(),
181 'wl_notificationtimestamp IS NOT NULL',
183 __METHOD__,
184 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
187 if ( $count >= self::WL_UNREAD_LIMIT ) {
188 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
189 } else {
190 $vals['unreadcount'] = $count;
194 return $vals;
197 protected function getRateLimits() {
198 $retval = array(
199 ApiResult::META_TYPE => 'assoc',
202 $user = $this->getUser();
203 if ( !$user->isPingLimitable() ) {
204 return $retval; // No limits
207 // Find out which categories we belong to
208 $categories = array();
209 if ( $user->isAnon() ) {
210 $categories[] = 'anon';
211 } else {
212 $categories[] = 'user';
214 if ( $user->isNewbie() ) {
215 $categories[] = 'ip';
216 $categories[] = 'subnet';
217 if ( !$user->isAnon() ) {
218 $categories[] = 'newbie';
221 $categories = array_merge( $categories, $user->getGroups() );
223 // Now get the actual limits
224 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
225 foreach ( $categories as $cat ) {
226 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
227 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
228 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
233 return $retval;
236 public function getAllowedParams() {
237 return array(
238 'prop' => array(
239 ApiBase::PARAM_DFLT => null,
240 ApiBase::PARAM_ISMULTI => true,
241 ApiBase::PARAM_TYPE => array(
242 'blockinfo',
243 'hasmsg',
244 'groups',
245 'implicitgroups',
246 'rights',
247 'changeablegroups',
248 'options',
249 'preferencestoken',
250 'editcount',
251 'ratelimits',
252 'email',
253 'realname',
254 'acceptlang',
255 'registrationdate',
256 'unreadcount',
258 ApiBase::PARAM_HELP_MSG => array(
259 'apihelp-query+userinfo-param-prop',
260 self::WL_UNREAD_LIMIT - 1,
261 self::WL_UNREAD_LIMIT . '+',
267 protected function getExamplesMessages() {
268 return array(
269 'action=query&meta=userinfo'
270 => 'apihelp-query+userinfo-example-simple',
271 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
272 => 'apihelp-query+userinfo-example-data',
276 public function getHelpUrls() {
277 return 'https://www.mediawiki.org/wiki/API:Userinfo';