ca1dfb6d47812441ccf9cbdfa9d3290cc8235d50
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to get information about the currently logged-in user
37 class ApiQueryUserInfo
extends ApiQueryBase
{
39 private $prop = array();
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'ui' );
45 public function execute() {
46 $params = $this->extractRequestParams();
47 $result = $this->getResult();
49 if ( !is_null( $params['prop'] ) ) {
50 $this->prop
= array_flip( $params['prop'] );
53 $r = $this->getCurrentUserInfo();
54 $result->addValue( 'query', $this->getModuleName(), $r );
57 protected function getCurrentUserInfo() {
58 global $wgRequest, $wgHiddenPrefs;
59 $user = $this->getUser();
60 $result = $this->getResult();
62 $vals['id'] = intval( $user->getId() );
63 $vals['name'] = $user->getName();
65 if ( $user->isAnon() ) {
69 if ( isset( $this->prop
['blockinfo'] ) ) {
70 if ( $user->isBlocked() ) {
71 $vals['blockedby'] = User
::whoIs( $user->blockedBy() );
72 $vals['blockreason'] = $user->blockedFor();
76 if ( isset( $this->prop
['hasmsg'] ) && $user->getNewtalk() ) {
77 $vals['messages'] = '';
80 if ( isset( $this->prop
['groups'] ) ) {
81 $autolist = ApiQueryUsers
::getAutoGroups( $user );
83 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
84 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
87 if ( isset( $this->prop
['implicitgroups'] ) ) {
88 $vals['implicitgroups'] = ApiQueryUsers
::getAutoGroups( $user );
89 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
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
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' );
106 if ( isset( $this->prop
['options'] ) ) {
107 $vals['options'] = $user->getOptions();
110 if ( isset( $this->prop
['preferencestoken'] ) &&
111 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
113 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
116 if ( isset( $this->prop
['editcount'] ) ) {
117 $vals['editcount'] = intval( $user->getEditCount() );
120 if ( isset( $this->prop
['ratelimits'] ) ) {
121 $vals['ratelimits'] = $this->getRateLimits();
124 if ( isset( $this->prop
['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
125 $vals['realname'] = $user->getRealName();
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 );
136 if ( isset( $this->prop
['registrationdate'] ) ) {
137 $regDate = $user->getRegistration();
138 if ( $regDate !== false ) {
139 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601
, $regDate );
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 );
151 $result->setIndexedTagName( $acceptLang, 'lang' );
152 $vals['acceptlang'] = $acceptLang;
157 protected function getRateLimits() {
158 global $wgRateLimits;
159 $user = $this->getUser();
160 if ( !$user->isPingLimitable() ) {
161 return array(); // No limits
164 // Find out which categories we belong to
165 $categories = array();
166 if ( $user->isAnon() ) {
167 $categories[] = 'anon';
169 $categories[] = 'user';
171 if ( $user->isNewbie() ) {
172 $categories[] = 'ip';
173 $categories[] = 'subnet';
174 if ( !$user->isAnon() )
175 $categories[] = 'newbie';
177 $categories = array_merge( $categories, $user->getGroups() );
179 // Now get the actual limits
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] );
192 public function getAllowedParams() {
195 ApiBase
::PARAM_DFLT
=> null,
196 ApiBase
::PARAM_ISMULTI
=> true,
197 ApiBase
::PARAM_TYPE
=> array(
217 public function getParamDescription() {
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',
239 public function getDescription() {
240 return 'Get information about the current user';
243 public function getExamples() {
245 'api.php?action=query&meta=userinfo',
246 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
250 public function getHelpUrls() {
251 return 'http://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
254 public function getVersion() {
255 return __CLASS__
. ': $Id$';