ca1dfb6d47812441ccf9cbdfa9d3290cc8235d50
[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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query module to get information about the currently logged-in user
34 *
35 * @ingroup API
36 */
37 class ApiQueryUserInfo extends ApiQueryBase {
38
39 private $prop = array();
40
41 public function __construct( $query, $moduleName ) {
42 parent::__construct( $query, $moduleName, 'ui' );
43 }
44
45 public function execute() {
46 $params = $this->extractRequestParams();
47 $result = $this->getResult();
48
49 if ( !is_null( $params['prop'] ) ) {
50 $this->prop = array_flip( $params['prop'] );
51 }
52
53 $r = $this->getCurrentUserInfo();
54 $result->addValue( 'query', $this->getModuleName(), $r );
55 }
56
57 protected function getCurrentUserInfo() {
58 global $wgRequest, $wgHiddenPrefs;
59 $user = $this->getUser();
60 $result = $this->getResult();
61 $vals = array();
62 $vals['id'] = intval( $user->getId() );
63 $vals['name'] = $user->getName();
64
65 if ( $user->isAnon() ) {
66 $vals['anon'] = '';
67 }
68
69 if ( isset( $this->prop['blockinfo'] ) ) {
70 if ( $user->isBlocked() ) {
71 $vals['blockedby'] = User::whoIs( $user->blockedBy() );
72 $vals['blockreason'] = $user->blockedFor();
73 }
74 }
75
76 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
77 $vals['messages'] = '';
78 }
79
80 if ( isset( $this->prop['groups'] ) ) {
81 $autolist = ApiQueryUsers::getAutoGroups( $user );
82
83 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
84 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
85 }
86
87 if ( isset( $this->prop['implicitgroups'] ) ) {
88 $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
89 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
90 }
91
92 if ( isset( $this->prop['rights'] ) ) {
93 // User::getRights() may return duplicate values, strip them
94 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
95 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
96 }
97
98 if ( isset( $this->prop['changeablegroups'] ) ) {
99 $vals['changeablegroups'] = $user->changeableGroups();
100 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
101 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
102 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
103 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
104 }
105
106 if ( isset( $this->prop['options'] ) ) {
107 $vals['options'] = $user->getOptions();
108 }
109
110 if ( isset( $this->prop['preferencestoken'] ) &&
111 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
112 ) {
113 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
114 }
115
116 if ( isset( $this->prop['editcount'] ) ) {
117 $vals['editcount'] = intval( $user->getEditCount() );
118 }
119
120 if ( isset( $this->prop['ratelimits'] ) ) {
121 $vals['ratelimits'] = $this->getRateLimits();
122 }
123
124 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
125 $vals['realname'] = $user->getRealName();
126 }
127
128 if ( isset( $this->prop['email'] ) ) {
129 $vals['email'] = $user->getEmail();
130 $auth = $user->getEmailAuthenticationTimestamp();
131 if ( !is_null( $auth ) ) {
132 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
133 }
134 }
135
136 if ( isset( $this->prop['registrationdate'] ) ) {
137 $regDate = $user->getRegistration();
138 if ( $regDate !== false ) {
139 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
140 }
141 }
142
143 if ( isset( $this->prop['acceptlang'] ) ) {
144 $langs = $wgRequest->getAcceptLang();
145 $acceptLang = array();
146 foreach ( $langs as $lang => $val ) {
147 $r = array( 'q' => $val );
148 ApiResult::setContent( $r, $lang );
149 $acceptLang[] = $r;
150 }
151 $result->setIndexedTagName( $acceptLang, 'lang' );
152 $vals['acceptlang'] = $acceptLang;
153 }
154 return $vals;
155 }
156
157 protected function getRateLimits() {
158 global $wgRateLimits;
159 $user = $this->getUser();
160 if ( !$user->isPingLimitable() ) {
161 return array(); // No limits
162 }
163
164 // Find out which categories we belong to
165 $categories = array();
166 if ( $user->isAnon() ) {
167 $categories[] = 'anon';
168 } else {
169 $categories[] = 'user';
170 }
171 if ( $user->isNewbie() ) {
172 $categories[] = 'ip';
173 $categories[] = 'subnet';
174 if ( !$user->isAnon() )
175 $categories[] = 'newbie';
176 }
177 $categories = array_merge( $categories, $user->getGroups() );
178
179 // Now get the actual limits
180 $retval = array();
181 foreach ( $wgRateLimits as $action => $limits ) {
182 foreach ( $categories as $cat ) {
183 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
184 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
185 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
186 }
187 }
188 }
189 return $retval;
190 }
191
192 public function getAllowedParams() {
193 return array(
194 'prop' => array(
195 ApiBase::PARAM_DFLT => null,
196 ApiBase::PARAM_ISMULTI => true,
197 ApiBase::PARAM_TYPE => array(
198 'blockinfo',
199 'hasmsg',
200 'groups',
201 'implicitgroups',
202 'rights',
203 'changeablegroups',
204 'options',
205 'preferencestoken',
206 'editcount',
207 'ratelimits',
208 'email',
209 'realname',
210 'acceptlang',
211 'registrationdate'
212 )
213 )
214 );
215 }
216
217 public function getParamDescription() {
218 return array(
219 'prop' => array(
220 'What pieces of information to include',
221 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
222 ' hasmsg - Adds a tag "message" if the current user has pending messages',
223 ' groups - Lists all the groups the current user belongs to',
224 ' implicitgroups - Lists all the groups the current user is automatically a member of',
225 ' rights - Lists all the rights the current user has',
226 ' changeablegroups - Lists the groups the current user can add to and remove from',
227 ' options - Lists all preferences the current user has set',
228 ' preferencestoken - Get a token to change current user\'s preferences',
229 ' editcount - Adds the current user\'s edit count',
230 ' ratelimits - Lists all rate limits applying to the current user',
231 ' realname - Adds the user\'s real name',
232 ' email - Adds the user\'s email address and email authentication date',
233 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
234 ' registrationdate - Adds the user\'s registration date',
235 )
236 );
237 }
238
239 public function getDescription() {
240 return 'Get information about the current user';
241 }
242
243 public function getExamples() {
244 return array(
245 'api.php?action=query&meta=userinfo',
246 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
247 );
248 }
249
250 public function getHelpUrls() {
251 return 'http://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
252 }
253
254 public function getVersion() {
255 return __CLASS__ . ': $Id$';
256 }
257 }