Merge "Fix method declaration in UploadFromStash"
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob6690665944bf2c1d156c75bb1c63ed9417e393cf
1 <?php
2 /**
5 * Created on July 30, 2007
7 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * Query module to get information about the currently logged-in user
30 * @ingroup API
32 class ApiQueryUserInfo extends ApiQueryBase {
34 private $prop = array();
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'ui' );
40 public function execute() {
41 $params = $this->extractRequestParams();
42 $result = $this->getResult();
44 if ( !is_null( $params['prop'] ) ) {
45 $this->prop = array_flip( $params['prop'] );
48 $r = $this->getCurrentUserInfo();
49 $result->addValue( 'query', $this->getModuleName(), $r );
52 protected function getCurrentUserInfo() {
53 global $wgHiddenPrefs;
54 $user = $this->getUser();
55 $result = $this->getResult();
56 $vals = array();
57 $vals['id'] = intval( $user->getId() );
58 $vals['name'] = $user->getName();
60 if ( $user->isAnon() ) {
61 $vals['anon'] = '';
64 if ( isset( $this->prop['blockinfo'] ) ) {
65 if ( $user->isBlocked() ) {
66 $block = $user->getBlock();
67 $vals['blockid'] = $block->getId();
68 $vals['blockedby'] = $block->getByName();
69 $vals['blockedbyid'] = $block->getBy();
70 $vals['blockreason'] = $user->blockedFor();
74 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
75 $vals['messages'] = '';
78 if ( isset( $this->prop['groups'] ) ) {
79 $vals['groups'] = $user->getEffectiveGroups();
80 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
83 if ( isset( $this->prop['implicitgroups'] ) ) {
84 $vals['implicitgroups'] = $user->getAutomaticGroups();
85 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
88 if ( isset( $this->prop['rights'] ) ) {
89 // User::getRights() may return duplicate values, strip them
90 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
91 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
94 if ( isset( $this->prop['changeablegroups'] ) ) {
95 $vals['changeablegroups'] = $user->changeableGroups();
96 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
102 if ( isset( $this->prop['options'] ) ) {
103 $vals['options'] = $user->getOptions();
106 if ( isset( $this->prop['preferencestoken'] ) &&
107 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
109 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
112 if ( isset( $this->prop['editcount'] ) ) {
113 $vals['editcount'] = intval( $user->getEditCount() );
116 if ( isset( $this->prop['ratelimits'] ) ) {
117 $vals['ratelimits'] = $this->getRateLimits();
120 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
121 $vals['realname'] = $user->getRealName();
124 if ( isset( $this->prop['email'] ) ) {
125 $vals['email'] = $user->getEmail();
126 $auth = $user->getEmailAuthenticationTimestamp();
127 if ( !is_null( $auth ) ) {
128 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
132 if ( isset( $this->prop['registrationdate'] ) ) {
133 $regDate = $user->getRegistration();
134 if ( $regDate !== false ) {
135 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
139 if ( isset( $this->prop['acceptlang'] ) ) {
140 $langs = $this->getRequest()->getAcceptLang();
141 $acceptLang = array();
142 foreach ( $langs as $lang => $val ) {
143 $r = array( 'q' => $val );
144 ApiResult::setContent( $r, $lang );
145 $acceptLang[] = $r;
147 $result->setIndexedTagName( $acceptLang, 'lang' );
148 $vals['acceptlang'] = $acceptLang;
150 return $vals;
153 protected function getRateLimits() {
154 global $wgRateLimits;
155 $user = $this->getUser();
156 if ( !$user->isPingLimitable() ) {
157 return array(); // No limits
160 // Find out which categories we belong to
161 $categories = array();
162 if ( $user->isAnon() ) {
163 $categories[] = 'anon';
164 } else {
165 $categories[] = 'user';
167 if ( $user->isNewbie() ) {
168 $categories[] = 'ip';
169 $categories[] = 'subnet';
170 if ( !$user->isAnon() )
171 $categories[] = 'newbie';
173 $categories = array_merge( $categories, $user->getGroups() );
175 // Now get the actual limits
176 $retval = array();
177 foreach ( $wgRateLimits as $action => $limits ) {
178 foreach ( $categories as $cat ) {
179 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
180 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
181 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
185 return $retval;
188 public function getAllowedParams() {
189 return array(
190 'prop' => array(
191 ApiBase::PARAM_DFLT => null,
192 ApiBase::PARAM_ISMULTI => true,
193 ApiBase::PARAM_TYPE => array(
194 'blockinfo',
195 'hasmsg',
196 'groups',
197 'implicitgroups',
198 'rights',
199 'changeablegroups',
200 'options',
201 'preferencestoken',
202 'editcount',
203 'ratelimits',
204 'email',
205 'realname',
206 'acceptlang',
207 'registrationdate'
213 public function getParamDescription() {
214 return array(
215 'prop' => array(
216 'What pieces of information to include',
217 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
218 ' hasmsg - Adds a tag "message" if the current user has pending messages',
219 ' groups - Lists all the groups the current user belongs to',
220 ' implicitgroups - Lists all the groups the current user is automatically a member of',
221 ' rights - Lists all the rights the current user has',
222 ' changeablegroups - Lists the groups the current user can add to and remove from',
223 ' options - Lists all preferences the current user has set',
224 ' preferencestoken - Get a token to change current user\'s preferences',
225 ' editcount - Adds the current user\'s edit count',
226 ' ratelimits - Lists all rate limits applying to the current user',
227 ' realname - Adds the user\'s real name',
228 ' email - Adds the user\'s email address and email authentication date',
229 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
230 ' registrationdate - Adds the user\'s registration date',
235 public function getResultProperties() {
236 return array(
237 ApiBase::PROP_LIST => false,
238 '' => array(
239 'id' => 'integer',
240 'name' => 'string',
241 'anon' => 'boolean'
243 'blockinfo' => array(
244 'blockid' => array(
245 ApiBase::PROP_TYPE => 'integer',
246 ApiBase::PROP_NULLABLE => true
248 'blockedby' => array(
249 ApiBase::PROP_TYPE => 'string',
250 ApiBase::PROP_NULLABLE => true
252 'blockedbyid' => array(
253 ApiBase::PROP_TYPE => 'integer',
254 ApiBase::PROP_NULLABLE => true
256 'blockedreason' => array(
257 ApiBase::PROP_TYPE => 'string',
258 ApiBase::PROP_NULLABLE => true
261 'hasmsg' => array(
262 'messages' => 'boolean'
264 'preferencestoken' => array(
265 'preferencestoken' => 'string'
267 'editcount' => array(
268 'editcount' => 'integer'
270 'realname' => array(
271 'realname' => array(
272 ApiBase::PROP_TYPE => 'string',
273 ApiBase::PROP_NULLABLE => true
276 'email' => array(
277 'email' => 'string',
278 'emailauthenticated' => array(
279 ApiBase::PROP_TYPE => 'timestamp',
280 ApiBase::PROP_NULLABLE => true
283 'registrationdate' => array(
284 'registrationdate' => array(
285 ApiBase::PROP_TYPE => 'timestamp',
286 ApiBase::PROP_NULLABLE => true
292 public function getDescription() {
293 return 'Get information about the current user';
296 public function getExamples() {
297 return array(
298 'api.php?action=query&meta=userinfo',
299 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
303 public function getHelpUrls() {
304 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
307 public function getVersion() {
308 return __CLASS__ . ': $Id$';