5a3092ca16c8148e330fc413a4a129c493a5f766
[lhc/web/wiklou.git] / includes / api / ApiQueryUserInfo.php
1 <?php
2
3 /**
4 * Created on July 30, 2007
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2007 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * Query module to get information about the currently logged-in user
33 *
34 * @ingroup API
35 */
36 class ApiQueryUserInfo extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ui' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45 $r = array();
46
47 if ( !is_null( $params['prop'] ) ) {
48 $this->prop = array_flip( $params['prop'] );
49 } else {
50 $this->prop = array();
51 }
52 $r = $this->getCurrentUserInfo();
53 $result->addValue( 'query', $this->getModuleName(), $r );
54 }
55
56 protected function getCurrentUserInfo() {
57 global $wgUser, $wgRequest;
58 $result = $this->getResult();
59 $vals = array();
60 $vals['id'] = intval( $wgUser->getId() );
61 $vals['name'] = $wgUser->getName();
62
63 if ( $wgUser->isAnon() ) {
64 $vals['anon'] = '';
65 }
66
67 if ( isset( $this->prop['blockinfo'] ) ) {
68 if ( $wgUser->isBlocked() ) {
69 $vals['blockedby'] = User::whoIs( $wgUser->blockedBy() );
70 $vals['blockreason'] = $wgUser->blockedFor();
71 }
72 }
73
74 if ( isset( $this->prop['hasmsg'] ) && $wgUser->getNewtalk() ) {
75 $vals['messages'] = '';
76 }
77
78 if ( isset( $this->prop['groups'] ) ) {
79 $autolist = ApiQueryUsers::getAutoGroups( $wgUser );
80
81 $vals['groups'] = array_merge( $autolist, $wgUser->getGroups() );
82 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
83 }
84
85 if ( isset( $this->prop['rights'] ) ) {
86 // User::getRights() may return duplicate values, strip them
87 $vals['rights'] = array_values( array_unique( $wgUser->getRights() ) );
88 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
89 }
90
91 if ( isset( $this->prop['changeablegroups'] ) ) {
92 $vals['changeablegroups'] = $wgUser->changeableGroups();
93 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
94 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
95 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
96 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
97 }
98
99 if ( isset( $this->prop['options'] ) ) {
100 $vals['options'] = $wgUser->getOptions();
101 }
102
103 if (
104 isset( $this->prop['preferencestoken'] ) &&
105 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
106 )
107 {
108 $vals['preferencestoken'] = $wgUser->editToken();
109 }
110
111 if ( isset( $this->prop['editcount'] ) ) {
112 $vals['editcount'] = intval( $wgUser->getEditCount() );
113 }
114
115 if ( isset( $this->prop['ratelimits'] ) ) {
116 $vals['ratelimits'] = $this->getRateLimits();
117 }
118
119 if ( isset( $this->prop['email'] ) ) {
120 $vals['email'] = $wgUser->getEmail();
121 $auth = $wgUser->getEmailAuthenticationTimestamp();
122 if ( !is_null( $auth ) ) {
123 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
124 }
125 }
126
127 if ( isset( $this->prop['acceptlang'] ) ) {
128 $langs = $wgRequest->getAcceptLang();
129 $acceptLang = array();
130 foreach ( $langs as $lang => $val ) {
131 $r = array( 'q' => $val );
132 ApiResult::setContent( $r, $lang );
133 $acceptLang[] = $r;
134 }
135 $result->setIndexedTagName( $acceptLang, 'lang' );
136 $vals['acceptlang'] = $acceptLang;
137 }
138 return $vals;
139 }
140
141 protected function getRateLimits() {
142 global $wgUser, $wgRateLimits;
143 if ( !$wgUser->isPingLimitable() ) {
144 return array(); // No limits
145 }
146
147 // Find out which categories we belong to
148 $categories = array();
149 if ( $wgUser->isAnon() ) {
150 $categories[] = 'anon';
151 } else {
152 $categories[] = 'user';
153 }
154 if ( $wgUser->isNewbie() ) {
155 $categories[] = 'ip';
156 $categories[] = 'subnet';
157 if ( !$wgUser->isAnon() )
158 $categories[] = 'newbie';
159 }
160 $categories = array_merge( $categories, $wgUser->getGroups() );
161
162 // Now get the actual limits
163 $retval = array();
164 foreach ( $wgRateLimits as $action => $limits ) {
165 foreach ( $categories as $cat ) {
166 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
167 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
168 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
169 }
170 }
171 }
172 return $retval;
173 }
174
175 public function getAllowedParams() {
176 return array(
177 'prop' => array(
178 ApiBase::PARAM_DFLT => null,
179 ApiBase::PARAM_ISMULTI => true,
180 ApiBase::PARAM_TYPE => array(
181 'blockinfo',
182 'hasmsg',
183 'groups',
184 'rights',
185 'changeablegroups',
186 'options',
187 'preferencestoken',
188 'editcount',
189 'ratelimits',
190 'email',
191 'acceptlang',
192 )
193 )
194 );
195 }
196
197 public function getParamDescription() {
198 return array(
199 'prop' => array(
200 'What pieces of information to include',
201 ' blockinfo - tags if the current user is blocked, by whom, and for what reason',
202 ' hasmsg - adds a tag "message" if the current user has pending messages',
203 ' groups - lists all the groups the current user belongs to',
204 ' rights - lists all the rights the current user has',
205 ' changeablegroups - lists the groups the current user can add to and remove from',
206 ' options - lists all preferences the current user has set',
207 ' editcount - adds the current user\'s edit count',
208 ' ratelimits - lists all rate limits applying to the current user',
209 ' email - adds the user\'s email address and email authentication date',
210 ' acceptlang - echoes the Accept-Language header sent by the client in a structured format',
211 )
212 );
213 }
214
215 public function getDescription() {
216 return 'Get information about the current user';
217 }
218
219 protected function getExamples() {
220 return array(
221 'api.php?action=query&meta=userinfo',
222 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
223 );
224 }
225
226 public function getVersion() {
227 return __CLASS__ . ': $Id$';
228 }
229 }