Merge "Use PHP_OS rather than php_uname, which may be disabled"
[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 global $wgContLang;
56
57 $user = $this->getUser();
58 $result = $this->getResult();
59 $vals = array();
60 $vals['id'] = intval( $user->getId() );
61 $vals['name'] = $user->getName();
62
63 if ( $user->isAnon() ) {
64 $vals['anon'] = true;
65 }
66
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'
77 );
78 }
79 }
80
81 if ( isset( $this->prop['hasmsg'] ) ) {
82 $vals['messages'] = $user->getNewtalk();
83 }
84
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
89 }
90
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
95 }
96
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
102 }
103
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' );
110 }
111
112 if ( isset( $this->prop['options'] ) ) {
113 $vals['options'] = $user->getOptions();
114 $vals['options'][ApiResult::META_BC_BOOLS] = array_keys( $vals['options'] );
115 }
116
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."
121 );
122 }
123 if ( isset( $this->prop['preferencestoken'] ) &&
124 !$this->lacksSameOriginSecurity() &&
125 $user->isAllowed( 'editmyoptions' )
126 ) {
127 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
128 }
129
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() );
134 }
135
136 if ( isset( $this->prop['ratelimits'] ) ) {
137 $vals['ratelimits'] = $this->getRateLimits();
138 }
139
140 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
141 $vals['realname'] = $user->getRealName();
142 }
143
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 );
150 }
151 }
152 }
153
154 if ( isset( $this->prop['registrationdate'] ) ) {
155 $regDate = $user->getRegistration();
156 if ( $regDate !== false ) {
157 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
158 }
159 }
160
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;
168 }
169 ApiResult::setIndexedTagName( $acceptLang, 'lang' );
170 $vals['acceptlang'] = $acceptLang;
171 }
172
173 if ( isset( $this->prop['unreadcount'] ) ) {
174 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
175
176 $count = $dbr->selectRowCount(
177 'watchlist',
178 '1',
179 array(
180 'wl_user' => $user->getId(),
181 'wl_notificationtimestamp IS NOT NULL',
182 ),
183 __METHOD__,
184 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
185 );
186
187 if ( $count >= self::WL_UNREAD_LIMIT ) {
188 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
189 } else {
190 $vals['unreadcount'] = $count;
191 }
192 }
193
194 return $vals;
195 }
196
197 protected function getRateLimits() {
198 $retval = array(
199 ApiResult::META_TYPE => 'assoc',
200 );
201
202 $user = $this->getUser();
203 if ( !$user->isPingLimitable() ) {
204 return $retval; // No limits
205 }
206
207 // Find out which categories we belong to
208 $categories = array();
209 if ( $user->isAnon() ) {
210 $categories[] = 'anon';
211 } else {
212 $categories[] = 'user';
213 }
214 if ( $user->isNewbie() ) {
215 $categories[] = 'ip';
216 $categories[] = 'subnet';
217 if ( !$user->isAnon() ) {
218 $categories[] = 'newbie';
219 }
220 }
221 $categories = array_merge( $categories, $user->getGroups() );
222
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] );
229 }
230 }
231 }
232
233 return $retval;
234 }
235
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',
257 ),
258 ApiBase::PARAM_HELP_MSG => array(
259 'apihelp-query+userinfo-param-prop',
260 self::WL_UNREAD_LIMIT - 1,
261 self::WL_UNREAD_LIMIT . '+',
262 ),
263 )
264 );
265 }
266
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',
273 );
274 }
275
276 public function getHelpUrls() {
277 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
278 }
279 }