Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryUserInfo.php
blob3c85ea69d567f4ddd8b8b1990cd122139e8889c6
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' ) ) &&
108 $user->isAllowed( 'editmyoptions' )
110 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
113 if ( isset( $this->prop['editcount'] ) ) {
114 // use intval to prevent null if a non-logged-in user calls
115 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
116 $vals['editcount'] = intval( $user->getEditCount() );
119 if ( isset( $this->prop['ratelimits'] ) ) {
120 $vals['ratelimits'] = $this->getRateLimits();
123 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
124 $vals['realname'] = $user->getRealName();
127 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
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 );
137 if ( isset( $this->prop['registrationdate'] ) ) {
138 $regDate = $user->getRegistration();
139 if ( $regDate !== false ) {
140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
144 if ( isset( $this->prop['acceptlang'] ) ) {
145 $langs = $this->getRequest()->getAcceptLang();
146 $acceptLang = array();
147 foreach ( $langs as $lang => $val ) {
148 $r = array( 'q' => $val );
149 ApiResult::setContent( $r, $lang );
150 $acceptLang[] = $r;
152 $result->setIndexedTagName( $acceptLang, 'lang' );
153 $vals['acceptlang'] = $acceptLang;
155 return $vals;
158 protected function getRateLimits() {
159 global $wgRateLimits;
160 $user = $this->getUser();
161 if ( !$user->isPingLimitable() ) {
162 return array(); // No limits
165 // Find out which categories we belong to
166 $categories = array();
167 if ( $user->isAnon() ) {
168 $categories[] = 'anon';
169 } else {
170 $categories[] = 'user';
172 if ( $user->isNewbie() ) {
173 $categories[] = 'ip';
174 $categories[] = 'subnet';
175 if ( !$user->isAnon() ) {
176 $categories[] = 'newbie';
179 $categories = array_merge( $categories, $user->getGroups() );
181 // Now get the actual limits
182 $retval = array();
183 foreach ( $wgRateLimits as $action => $limits ) {
184 foreach ( $categories as $cat ) {
185 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
186 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
187 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
191 return $retval;
194 public function getAllowedParams() {
195 return array(
196 'prop' => array(
197 ApiBase::PARAM_DFLT => null,
198 ApiBase::PARAM_ISMULTI => true,
199 ApiBase::PARAM_TYPE => array(
200 'blockinfo',
201 'hasmsg',
202 'groups',
203 'implicitgroups',
204 'rights',
205 'changeablegroups',
206 'options',
207 'preferencestoken',
208 'editcount',
209 'ratelimits',
210 'email',
211 'realname',
212 'acceptlang',
213 'registrationdate'
219 public function getParamDescription() {
220 return array(
221 'prop' => array(
222 'What pieces of information to include',
223 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
224 ' hasmsg - Adds a tag "message" if the current user has pending messages',
225 ' groups - Lists all the groups the current user belongs to',
226 ' implicitgroups - Lists all the groups the current user is automatically a member of',
227 ' rights - Lists all the rights the current user has',
228 ' changeablegroups - Lists the groups the current user can add to and remove from',
229 ' options - Lists all preferences the current user has set',
230 ' preferencestoken - Get a token to change current user\'s preferences',
231 ' editcount - Adds the current user\'s edit count',
232 ' ratelimits - Lists all rate limits applying to the current user',
233 ' realname - Adds the user\'s real name',
234 ' email - Adds the user\'s email address and email authentication date',
235 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
236 ' registrationdate - Adds the user\'s registration date',
241 public function getResultProperties() {
242 return array(
243 ApiBase::PROP_LIST => false,
244 '' => array(
245 'id' => 'integer',
246 'name' => 'string',
247 'anon' => 'boolean'
249 'blockinfo' => array(
250 'blockid' => array(
251 ApiBase::PROP_TYPE => 'integer',
252 ApiBase::PROP_NULLABLE => true
254 'blockedby' => array(
255 ApiBase::PROP_TYPE => 'string',
256 ApiBase::PROP_NULLABLE => true
258 'blockedbyid' => array(
259 ApiBase::PROP_TYPE => 'integer',
260 ApiBase::PROP_NULLABLE => true
262 'blockedreason' => array(
263 ApiBase::PROP_TYPE => 'string',
264 ApiBase::PROP_NULLABLE => true
267 'hasmsg' => array(
268 'messages' => 'boolean'
270 'preferencestoken' => array(
271 'preferencestoken' => 'string'
273 'editcount' => array(
274 'editcount' => 'integer'
276 'realname' => array(
277 'realname' => array(
278 ApiBase::PROP_TYPE => 'string',
279 ApiBase::PROP_NULLABLE => true
282 'email' => array(
283 'email' => 'string',
284 'emailauthenticated' => array(
285 ApiBase::PROP_TYPE => 'timestamp',
286 ApiBase::PROP_NULLABLE => true
289 'registrationdate' => array(
290 'registrationdate' => array(
291 ApiBase::PROP_TYPE => 'timestamp',
292 ApiBase::PROP_NULLABLE => true
298 public function getDescription() {
299 return 'Get information about the current user';
302 public function getExamples() {
303 return array(
304 'api.php?action=query&meta=userinfo',
305 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
309 public function getHelpUrls() {
310 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';