(bug 18195) Allow changing preferences via API
[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 /**
28 * Query module to get information about the currently logged-in user
29 *
30 * @ingroup API
31 */
32 class ApiQueryUserInfo extends ApiQueryBase {
33
34 private $prop = array();
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'ui' );
38 }
39
40 public function execute() {
41 $params = $this->extractRequestParams();
42 $result = $this->getResult();
43
44 if ( !is_null( $params['prop'] ) ) {
45 $this->prop = array_flip( $params['prop'] );
46 }
47
48 $r = $this->getCurrentUserInfo();
49 $result->addValue( 'query', $this->getModuleName(), $r );
50 }
51
52 protected function getCurrentUserInfo() {
53 global $wgRequest, $wgHiddenPrefs;
54 $user = $this->getUser();
55 $result = $this->getResult();
56 $vals = array();
57 $vals['id'] = intval( $user->getId() );
58 $vals['name'] = $user->getName();
59
60 if ( $user->isAnon() ) {
61 $vals['anon'] = '';
62 }
63
64 if ( isset( $this->prop['blockinfo'] ) ) {
65 if ( $user->isBlocked() ) {
66 $vals['blockedby'] = User::whoIs( $user->blockedBy() );
67 $vals['blockreason'] = $user->blockedFor();
68 }
69 }
70
71 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
72 $vals['messages'] = '';
73 }
74
75 if ( isset( $this->prop['groups'] ) ) {
76 $autolist = ApiQueryUsers::getAutoGroups( $user );
77
78 $vals['groups'] = array_merge( $autolist, $user->getGroups() );
79 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
80 }
81
82 if ( isset( $this->prop['implicitgroups'] ) ) {
83 $vals['implicitgroups'] = ApiQueryUsers::getAutoGroups( $user );
84 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
85 }
86
87 if ( isset( $this->prop['rights'] ) ) {
88 // User::getRights() may return duplicate values, strip them
89 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
90 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
91 }
92
93 if ( isset( $this->prop['changeablegroups'] ) ) {
94 $vals['changeablegroups'] = $user->changeableGroups();
95 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
96 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
97 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
99 }
100
101 if ( isset( $this->prop['options'] ) ) {
102 $vals['options'] = $user->getOptions();
103 }
104
105 if ( isset( $this->prop['optionstoken'] ) &&
106 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
107 ) {
108 $vals['optionstoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
109 }
110
111 if ( isset( $this->prop['preferencestoken'] ) &&
112 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) )
113 ) {
114 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
115 }
116
117 if ( isset( $this->prop['editcount'] ) ) {
118 $vals['editcount'] = intval( $user->getEditCount() );
119 }
120
121 if ( isset( $this->prop['ratelimits'] ) ) {
122 $vals['ratelimits'] = $this->getRateLimits();
123 }
124
125 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $wgHiddenPrefs ) ) {
126 $vals['realname'] = $user->getRealName();
127 }
128
129 if ( isset( $this->prop['email'] ) ) {
130 $vals['email'] = $user->getEmail();
131 $auth = $user->getEmailAuthenticationTimestamp();
132 if ( !is_null( $auth ) ) {
133 $vals['emailauthenticated'] = wfTimestamp( TS_ISO_8601, $auth );
134 }
135 }
136
137 if ( isset( $this->prop['registrationdate'] ) ) {
138 $regDate = $user->getRegistration();
139 if ( $regDate !== false ) {
140 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
141 }
142 }
143
144 if ( isset( $this->prop['acceptlang'] ) ) {
145 $langs = $wgRequest->getAcceptLang();
146 $acceptLang = array();
147 foreach ( $langs as $lang => $val ) {
148 $r = array( 'q' => $val );
149 ApiResult::setContent( $r, $lang );
150 $acceptLang[] = $r;
151 }
152 $result->setIndexedTagName( $acceptLang, 'lang' );
153 $vals['acceptlang'] = $acceptLang;
154 }
155 return $vals;
156 }
157
158 protected function getRateLimits() {
159 global $wgRateLimits;
160 $user = $this->getUser();
161 if ( !$user->isPingLimitable() ) {
162 return array(); // No limits
163 }
164
165 // Find out which categories we belong to
166 $categories = array();
167 if ( $user->isAnon() ) {
168 $categories[] = 'anon';
169 } else {
170 $categories[] = 'user';
171 }
172 if ( $user->isNewbie() ) {
173 $categories[] = 'ip';
174 $categories[] = 'subnet';
175 if ( !$user->isAnon() )
176 $categories[] = 'newbie';
177 }
178 $categories = array_merge( $categories, $user->getGroups() );
179
180 // Now get the actual limits
181 $retval = array();
182 foreach ( $wgRateLimits as $action => $limits ) {
183 foreach ( $categories as $cat ) {
184 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
185 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
186 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
187 }
188 }
189 }
190 return $retval;
191 }
192
193 public function getAllowedParams() {
194 return array(
195 'prop' => array(
196 ApiBase::PARAM_DFLT => null,
197 ApiBase::PARAM_ISMULTI => true,
198 ApiBase::PARAM_TYPE => array(
199 'blockinfo',
200 'hasmsg',
201 'groups',
202 'implicitgroups',
203 'rights',
204 'changeablegroups',
205 'options',
206 'optionstoken',
207 'preferencestoken',
208 'editcount',
209 'ratelimits',
210 'email',
211 'realname',
212 'acceptlang',
213 'registrationdate'
214 )
215 )
216 );
217 }
218
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 ' optionstoken - Get a token to change current user\'s preferences',
231 ' preferencestoken - Get a token to change current user\'s preferences',
232 ' editcount - Adds the current user\'s edit count',
233 ' ratelimits - Lists all rate limits applying to the current user',
234 ' realname - Adds the user\'s real name',
235 ' email - Adds the user\'s email address and email authentication date',
236 ' acceptlang - Echoes the Accept-Language header sent by the client in a structured format',
237 ' registrationdate - Adds the user\'s registration date',
238 )
239 );
240 }
241
242 public function getDescription() {
243 return 'Get information about the current user';
244 }
245
246 public function getExamples() {
247 return array(
248 'api.php?action=query&meta=userinfo',
249 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
250 );
251 }
252
253 public function getHelpUrls() {
254 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
255 }
256
257 public function getVersion() {
258 return __CLASS__ . ': $Id$';
259 }
260 }