Merge "Remove unused variable in ProfilerMwprof"
[lhc/web/wiklou.git] / includes / api / ApiQueryUserInfo.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 /**
28 * Query module to get information about the currently logged-in user
29 *
30 * @ingroup API
31 */
32 class ApiQueryUserInfo extends ApiQueryBase {
33
34 const WL_UNREAD_LIMIT = 1000;
35
36 private $prop = array();
37
38 public function __construct( ApiQuery $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ui' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45
46 if ( !is_null( $params['prop'] ) ) {
47 $this->prop = array_flip( $params['prop'] );
48 }
49
50 $r = $this->getCurrentUserInfo();
51 $result->addValue( 'query', $this->getModuleName(), $r );
52 }
53
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();
60
61 if ( $user->isAnon() ) {
62 $vals['anon'] = '';
63 }
64
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'
74 ? 'infinite'
75 : wfTimestamp( TS_ISO_8601, $block->getExpiry() );
76 }
77 }
78
79 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
80 $vals['messages'] = '';
81 }
82
83 if ( isset( $this->prop['groups'] ) ) {
84 $vals['groups'] = $user->getEffectiveGroups();
85 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
86 }
87
88 if ( isset( $this->prop['implicitgroups'] ) ) {
89 $vals['implicitgroups'] = $user->getAutomaticGroups();
90 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
91 }
92
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
97 }
98
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' );
105 }
106
107 if ( isset( $this->prop['options'] ) ) {
108 $vals['options'] = $user->getOptions();
109 }
110
111 if ( isset( $this->prop['preferencestoken'] ) ) {
112 $p = $this->getModulePrefix();
113 $this->setWarning(
114 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
115 );
116 }
117 if ( isset( $this->prop['preferencestoken'] ) &&
118 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
119 $user->isAllowed( 'editmyoptions' )
120 ) {
121 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
122 }
123
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() );
128 }
129
130 if ( isset( $this->prop['ratelimits'] ) ) {
131 $vals['ratelimits'] = $this->getRateLimits();
132 }
133
134 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
135 $vals['realname'] = $user->getRealName();
136 }
137
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 );
144 }
145 }
146 }
147
148 if ( isset( $this->prop['registrationdate'] ) ) {
149 $regDate = $user->getRegistration();
150 if ( $regDate !== false ) {
151 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
152 }
153 }
154
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 );
161 $acceptLang[] = $r;
162 }
163 $result->setIndexedTagName( $acceptLang, 'lang' );
164 $vals['acceptlang'] = $acceptLang;
165 }
166
167 if ( isset( $this->prop['unreadcount'] ) ) {
168 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
169
170 $sql = $dbr->selectSQLText(
171 'watchlist',
172 array( 'dummy' => 1 ),
173 array(
174 'wl_user' => $user->getId(),
175 'wl_notificationtimestamp IS NOT NULL',
176 ),
177 __METHOD__,
178 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
179 );
180 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
181
182 if ( $count >= self::WL_UNREAD_LIMIT ) {
183 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
184 } else {
185 $vals['unreadcount'] = (int)$count;
186 }
187 }
188
189 return $vals;
190 }
191
192 protected function getRateLimits() {
193 $user = $this->getUser();
194 if ( !$user->isPingLimitable() ) {
195 return array(); // No limits
196 }
197
198 // Find out which categories we belong to
199 $categories = array();
200 if ( $user->isAnon() ) {
201 $categories[] = 'anon';
202 } else {
203 $categories[] = 'user';
204 }
205 if ( $user->isNewbie() ) {
206 $categories[] = 'ip';
207 $categories[] = 'subnet';
208 if ( !$user->isAnon() ) {
209 $categories[] = 'newbie';
210 }
211 }
212 $categories = array_merge( $categories, $user->getGroups() );
213
214 // Now get the actual limits
215 $retval = array();
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] );
221 }
222 }
223 }
224
225 return $retval;
226 }
227
228 public function getAllowedParams() {
229 return array(
230 'prop' => array(
231 ApiBase::PARAM_DFLT => null,
232 ApiBase::PARAM_ISMULTI => true,
233 ApiBase::PARAM_TYPE => array(
234 'blockinfo',
235 'hasmsg',
236 'groups',
237 'implicitgroups',
238 'rights',
239 'changeablegroups',
240 'options',
241 'preferencestoken',
242 'editcount',
243 'ratelimits',
244 'email',
245 'realname',
246 'acceptlang',
247 'registrationdate',
248 'unreadcount',
249 ),
250 ApiBase::PARAM_HELP_MSG => array(
251 'apihelp-query+userinfo-param-prop',
252 self::WL_UNREAD_LIMIT - 1,
253 self::WL_UNREAD_LIMIT . '+',
254 ),
255 )
256 );
257 }
258
259 public function getExamplesMessages() {
260 return array(
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',
265 );
266 }
267
268 public function getHelpUrls() {
269 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
270 }
271 }