Merge "Make query pages not accept offset > 10000 in miser mode"
[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 $params = array();
37 private $prop = array();
38
39 public function __construct( ApiQuery $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'ui' );
41 }
42
43 public function execute() {
44 $this->params = $this->extractRequestParams();
45 $result = $this->getResult();
46
47 if ( !is_null( $this->params['prop'] ) ) {
48 $this->prop = array_flip( $this->params['prop'] );
49 }
50
51 $r = $this->getCurrentUserInfo();
52 $result->addValue( 'query', $this->getModuleName(), $r );
53 }
54
55 /**
56 * Get basic info about a given block
57 * @param Block $block
58 * @return array Array containing several keys:
59 * - blockid - ID of the block
60 * - blockedby - username of the blocker
61 * - blockedbyid - user ID of the blocker
62 * - blockreason - reason provided for the block
63 * - blockedtimestamp - timestamp for when the block was placed/modified
64 * - blockexpiry - expiry time of the block
65 */
66 public static function getBlockInfo( Block $block ) {
67 global $wgContLang;
68 $vals = array();
69 $vals['blockid'] = $block->getId();
70 $vals['blockedby'] = $block->getByName();
71 $vals['blockedbyid'] = $block->getBy();
72 $vals['blockreason'] = $block->mReason;
73 $vals['blockedtimestamp'] = wfTimestamp( TS_ISO_8601, $block->mTimestamp );
74 $vals['blockexpiry'] = $wgContLang->formatExpiry(
75 $block->getExpiry(), TS_ISO_8601, 'infinite'
76 );
77 return $vals;
78 }
79
80 /**
81 * Get central user info
82 * @param Config $config
83 * @param User $user
84 * @param string|null $attachedWiki
85 * @return array Central user info
86 * - centralids: Array mapping non-local Central ID provider names to IDs
87 * - attachedlocal: Array mapping Central ID provider names to booleans
88 * indicating whether the local user is attached.
89 * - attachedwiki: Array mapping Central ID provider names to booleans
90 * indicating whether the user is attached to $attachedWiki.
91 */
92 public static function getCentralUserInfo( Config $config, User $user, $attachedWiki = null ) {
93 $providerIds = array_keys( $config->get( 'CentralIdLookupProviders' ) );
94
95 $ret = array(
96 'centralids' => array(),
97 'attachedlocal' => array(),
98 );
99 ApiResult::setArrayType( $ret['centralids'], 'assoc' );
100 ApiResult::setArrayType( $ret['attachedlocal'], 'assoc' );
101 if ( $attachedWiki ) {
102 $ret['attachedwiki'] = array();
103 ApiResult::setArrayType( $ret['attachedwiki'], 'assoc' );
104 }
105
106 $name = $user->getName();
107 foreach ( $providerIds as $providerId ) {
108 $provider = CentralIdLookup::factory( $providerId );
109 $ret['centralids'][$providerId] = $provider->centralIdFromName( $name );
110 $ret['attachedlocal'][$providerId] = $provider->isAttached( $user );
111 if ( $attachedWiki ) {
112 $ret['attachedwiki'][$providerId] = $provider->isAttached( $user, $attachedWiki );
113 }
114 }
115
116 return $ret;
117 }
118
119 protected function getCurrentUserInfo() {
120 $user = $this->getUser();
121 $vals = array();
122 $vals['id'] = intval( $user->getId() );
123 $vals['name'] = $user->getName();
124
125 if ( $user->isAnon() ) {
126 $vals['anon'] = true;
127 }
128
129 if ( isset( $this->prop['blockinfo'] ) && $user->isBlocked() ) {
130 $vals = array_merge( $vals, self::getBlockInfo( $user->getBlock() ) );
131 }
132
133 if ( isset( $this->prop['hasmsg'] ) ) {
134 $vals['messages'] = $user->getNewtalk();
135 }
136
137 if ( isset( $this->prop['groups'] ) ) {
138 $vals['groups'] = $user->getEffectiveGroups();
139 ApiResult::setArrayType( $vals['groups'], 'array' ); // even if empty
140 ApiResult::setIndexedTagName( $vals['groups'], 'g' ); // even if empty
141 }
142
143 if ( isset( $this->prop['implicitgroups'] ) ) {
144 $vals['implicitgroups'] = $user->getAutomaticGroups();
145 ApiResult::setArrayType( $vals['implicitgroups'], 'array' ); // even if empty
146 ApiResult::setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
147 }
148
149 if ( isset( $this->prop['rights'] ) ) {
150 // User::getRights() may return duplicate values, strip them
151 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
152 ApiResult::setArrayType( $vals['rights'], 'array' ); // even if empty
153 ApiResult::setIndexedTagName( $vals['rights'], 'r' ); // even if empty
154 }
155
156 if ( isset( $this->prop['changeablegroups'] ) ) {
157 $vals['changeablegroups'] = $user->changeableGroups();
158 ApiResult::setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
159 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
160 ApiResult::setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
161 ApiResult::setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
162 }
163
164 if ( isset( $this->prop['options'] ) ) {
165 $vals['options'] = $user->getOptions();
166 $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] );
167 }
168
169 if ( isset( $this->prop['preferencestoken'] ) ) {
170 $p = $this->getModulePrefix();
171 $this->setWarning(
172 "{$p}prop=preferencestoken has been deprecated. Please use action=query&meta=tokens instead."
173 );
174 }
175 if ( isset( $this->prop['preferencestoken'] ) &&
176 !$this->lacksSameOriginSecurity() &&
177 $user->isAllowed( 'editmyoptions' )
178 ) {
179 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
180 }
181
182 if ( isset( $this->prop['editcount'] ) ) {
183 // use intval to prevent null if a non-logged-in user calls
184 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
185 $vals['editcount'] = intval( $user->getEditCount() );
186 }
187
188 if ( isset( $this->prop['ratelimits'] ) ) {
189 $vals['ratelimits'] = $this->getRateLimits();
190 }
191
192 if ( isset( $this->prop['realname'] ) &&
193 !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) )
194 ) {
195 $vals['realname'] = $user->getRealName();
196 }
197
198 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
199 if ( isset( $this->prop['email'] ) ) {
200 $vals['email'] = $user->getEmail();
201 $auth = $user->getEmailAuthenticationTimestamp();
202 if ( !is_null( $auth ) ) {
203 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
204 }
205 }
206 }
207
208 if ( isset( $this->prop['registrationdate'] ) ) {
209 $regDate = $user->getRegistration();
210 if ( $regDate !== false ) {
211 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
212 }
213 }
214
215 if ( isset( $this->prop['acceptlang'] ) ) {
216 $langs = $this->getRequest()->getAcceptLang();
217 $acceptLang = array();
218 foreach ( $langs as $lang => $val ) {
219 $r = array( 'q' => $val );
220 ApiResult::setContentValue( $r, 'code', $lang );
221 $acceptLang[] = $r;
222 }
223 ApiResult::setIndexedTagName( $acceptLang, 'lang' );
224 $vals['acceptlang'] = $acceptLang;
225 }
226
227 if ( isset( $this->prop['unreadcount'] ) ) {
228 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
229
230 $count = $dbr->selectRowCount(
231 'watchlist',
232 '1',
233 array(
234 'wl_user' => $user->getId(),
235 'wl_notificationtimestamp IS NOT NULL',
236 ),
237 __METHOD__,
238 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
239 );
240
241 if ( $count >= self::WL_UNREAD_LIMIT ) {
242 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
243 } else {
244 $vals['unreadcount'] = $count;
245 }
246 }
247
248 if ( isset( $this->prop['centralids'] ) ) {
249 $vals += self::getCentralUserInfo(
250 $this->getConfig(), $this->getUser(), $this->params['attachedwiki']
251 );
252 }
253
254 return $vals;
255 }
256
257 protected function getRateLimits() {
258 $retval = array(
259 ApiResult::META_TYPE => 'assoc',
260 );
261
262 $user = $this->getUser();
263 if ( !$user->isPingLimitable() ) {
264 return $retval; // No limits
265 }
266
267 // Find out which categories we belong to
268 $categories = array();
269 if ( $user->isAnon() ) {
270 $categories[] = 'anon';
271 } else {
272 $categories[] = 'user';
273 }
274 if ( $user->isNewbie() ) {
275 $categories[] = 'ip';
276 $categories[] = 'subnet';
277 if ( !$user->isAnon() ) {
278 $categories[] = 'newbie';
279 }
280 }
281 $categories = array_merge( $categories, $user->getGroups() );
282
283 // Now get the actual limits
284 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
285 foreach ( $categories as $cat ) {
286 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
287 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
288 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
289 }
290 }
291 }
292
293 return $retval;
294 }
295
296 public function getAllowedParams() {
297 return array(
298 'prop' => array(
299 ApiBase::PARAM_ISMULTI => true,
300 ApiBase::PARAM_TYPE => array(
301 'blockinfo',
302 'hasmsg',
303 'groups',
304 'implicitgroups',
305 'rights',
306 'changeablegroups',
307 'options',
308 'preferencestoken',
309 'editcount',
310 'ratelimits',
311 'email',
312 'realname',
313 'acceptlang',
314 'registrationdate',
315 'unreadcount',
316 'centralids',
317 ),
318 ApiBase::PARAM_HELP_MSG_PER_VALUE => array(
319 'unreadcount' => array(
320 'apihelp-query+userinfo-paramvalue-prop-unreadcount',
321 self::WL_UNREAD_LIMIT - 1,
322 self::WL_UNREAD_LIMIT . '+',
323 ),
324 ),
325 ),
326 'attachedwiki' => null,
327 );
328 }
329
330 protected function getExamplesMessages() {
331 return array(
332 'action=query&meta=userinfo'
333 => 'apihelp-query+userinfo-example-simple',
334 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
335 => 'apihelp-query+userinfo-example-data',
336 );
337 }
338
339 public function getHelpUrls() {
340 return 'https://www.mediawiki.org/wiki/API:Userinfo';
341 }
342 }