Merge "SpecialTrackingCategories: Read from the extension registry"
[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 $count = $dbr->selectRowCount(
171 'watchlist',
172 '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
181 if ( $count >= self::WL_UNREAD_LIMIT ) {
182 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
183 } else {
184 $vals['unreadcount'] = (int)$count;
185 }
186 }
187
188 return $vals;
189 }
190
191 protected function getRateLimits() {
192 $user = $this->getUser();
193 if ( !$user->isPingLimitable() ) {
194 return array(); // No limits
195 }
196
197 // Find out which categories we belong to
198 $categories = array();
199 if ( $user->isAnon() ) {
200 $categories[] = 'anon';
201 } else {
202 $categories[] = 'user';
203 }
204 if ( $user->isNewbie() ) {
205 $categories[] = 'ip';
206 $categories[] = 'subnet';
207 if ( !$user->isAnon() ) {
208 $categories[] = 'newbie';
209 }
210 }
211 $categories = array_merge( $categories, $user->getGroups() );
212
213 // Now get the actual limits
214 $retval = array();
215 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
216 foreach ( $categories as $cat ) {
217 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
218 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
219 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
220 }
221 }
222 }
223
224 return $retval;
225 }
226
227 public function getAllowedParams() {
228 return array(
229 'prop' => array(
230 ApiBase::PARAM_DFLT => null,
231 ApiBase::PARAM_ISMULTI => true,
232 ApiBase::PARAM_TYPE => array(
233 'blockinfo',
234 'hasmsg',
235 'groups',
236 'implicitgroups',
237 'rights',
238 'changeablegroups',
239 'options',
240 'preferencestoken',
241 'editcount',
242 'ratelimits',
243 'email',
244 'realname',
245 'acceptlang',
246 'registrationdate',
247 'unreadcount',
248 ),
249 ApiBase::PARAM_HELP_MSG => array(
250 'apihelp-query+userinfo-param-prop',
251 self::WL_UNREAD_LIMIT - 1,
252 self::WL_UNREAD_LIMIT . '+',
253 ),
254 )
255 );
256 }
257
258 protected function getExamplesMessages() {
259 return array(
260 'action=query&meta=userinfo'
261 => 'apihelp-query+userinfo-example-simple',
262 'action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg'
263 => 'apihelp-query+userinfo-example-data',
264 );
265 }
266
267 public function getHelpUrls() {
268 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
269 }
270 }