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