Schema is not PostgreSQL connection parameter
[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 const WL_UNREAD_LIMIT = 1000;
35
36 private $prop = array();
37
38 public function __construct( ApiQuery $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'ui' );
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $result = $this->getResult();
45
46 if ( !is_null( $params['prop'] ) ) {
47 $this->prop = array_flip( $params['prop'] );
48 }
49
50 $r = $this->getCurrentUserInfo();
51 $result->addValue( 'query', $this->getModuleName(), $r );
52 }
53
54 protected function getCurrentUserInfo() {
55 $user = $this->getUser();
56 $result = $this->getResult();
57 $vals = array();
58 $vals['id'] = intval( $user->getId() );
59 $vals['name'] = $user->getName();
60
61 if ( $user->isAnon() ) {
62 $vals['anon'] = '';
63 }
64
65 if ( isset( $this->prop['blockinfo'] ) ) {
66 if ( $user->isBlocked() ) {
67 $block = $user->getBlock();
68 $vals['blockid'] = $block->getId();
69 $vals['blockedby'] = $block->getByName();
70 $vals['blockedbyid'] = $block->getBy();
71 $vals['blockreason'] = $user->blockedFor();
72 }
73 }
74
75 if ( isset( $this->prop['hasmsg'] ) && $user->getNewtalk() ) {
76 $vals['messages'] = '';
77 }
78
79 if ( isset( $this->prop['groups'] ) ) {
80 $vals['groups'] = $user->getEffectiveGroups();
81 $result->setIndexedTagName( $vals['groups'], 'g' ); // even if empty
82 }
83
84 if ( isset( $this->prop['implicitgroups'] ) ) {
85 $vals['implicitgroups'] = $user->getAutomaticGroups();
86 $result->setIndexedTagName( $vals['implicitgroups'], 'g' ); // even if empty
87 }
88
89 if ( isset( $this->prop['rights'] ) ) {
90 // User::getRights() may return duplicate values, strip them
91 $vals['rights'] = array_values( array_unique( $user->getRights() ) );
92 $result->setIndexedTagName( $vals['rights'], 'r' ); // even if empty
93 }
94
95 if ( isset( $this->prop['changeablegroups'] ) ) {
96 $vals['changeablegroups'] = $user->changeableGroups();
97 $result->setIndexedTagName( $vals['changeablegroups']['add'], 'g' );
98 $result->setIndexedTagName( $vals['changeablegroups']['remove'], 'g' );
99 $result->setIndexedTagName( $vals['changeablegroups']['add-self'], 'g' );
100 $result->setIndexedTagName( $vals['changeablegroups']['remove-self'], 'g' );
101 }
102
103 if ( isset( $this->prop['options'] ) ) {
104 $vals['options'] = $user->getOptions();
105 }
106
107 if ( isset( $this->prop['preferencestoken'] ) &&
108 is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) &&
109 $user->isAllowed( 'editmyoptions' )
110 ) {
111 $vals['preferencestoken'] = $user->getEditToken( '', $this->getMain()->getRequest() );
112 }
113
114 if ( isset( $this->prop['editcount'] ) ) {
115 // use intval to prevent null if a non-logged-in user calls
116 // api.php?format=jsonfm&action=query&meta=userinfo&uiprop=editcount
117 $vals['editcount'] = intval( $user->getEditCount() );
118 }
119
120 if ( isset( $this->prop['ratelimits'] ) ) {
121 $vals['ratelimits'] = $this->getRateLimits();
122 }
123
124 if ( isset( $this->prop['realname'] ) && !in_array( 'realname', $this->getConfig()->get( 'HiddenPrefs' ) ) ) {
125 $vals['realname'] = $user->getRealName();
126 }
127
128 if ( $user->isAllowed( 'viewmyprivateinfo' ) ) {
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
138 if ( isset( $this->prop['registrationdate'] ) ) {
139 $regDate = $user->getRegistration();
140 if ( $regDate !== false ) {
141 $vals['registrationdate'] = wfTimestamp( TS_ISO_8601, $regDate );
142 }
143 }
144
145 if ( isset( $this->prop['acceptlang'] ) ) {
146 $langs = $this->getRequest()->getAcceptLang();
147 $acceptLang = array();
148 foreach ( $langs as $lang => $val ) {
149 $r = array( 'q' => $val );
150 ApiResult::setContent( $r, $lang );
151 $acceptLang[] = $r;
152 }
153 $result->setIndexedTagName( $acceptLang, 'lang' );
154 $vals['acceptlang'] = $acceptLang;
155 }
156
157 if ( isset( $this->prop['unreadcount'] ) ) {
158 $dbr = $this->getQuery()->getNamedDB( 'watchlist', DB_SLAVE, 'watchlist' );
159
160 $sql = $dbr->selectSQLText(
161 'watchlist',
162 array( 'dummy' => 1 ),
163 array(
164 'wl_user' => $user->getId(),
165 'wl_notificationtimestamp IS NOT NULL',
166 ),
167 __METHOD__,
168 array( 'LIMIT' => self::WL_UNREAD_LIMIT )
169 );
170 $count = $dbr->selectField( array( 'c' => "($sql)" ), 'COUNT(*)' );
171
172 if ( $count >= self::WL_UNREAD_LIMIT ) {
173 $vals['unreadcount'] = self::WL_UNREAD_LIMIT . '+';
174 } else {
175 $vals['unreadcount'] = (int)$count;
176 }
177 }
178
179 return $vals;
180 }
181
182 protected function getRateLimits() {
183 $user = $this->getUser();
184 if ( !$user->isPingLimitable() ) {
185 return array(); // No limits
186 }
187
188 // Find out which categories we belong to
189 $categories = array();
190 if ( $user->isAnon() ) {
191 $categories[] = 'anon';
192 } else {
193 $categories[] = 'user';
194 }
195 if ( $user->isNewbie() ) {
196 $categories[] = 'ip';
197 $categories[] = 'subnet';
198 if ( !$user->isAnon() ) {
199 $categories[] = 'newbie';
200 }
201 }
202 $categories = array_merge( $categories, $user->getGroups() );
203
204 // Now get the actual limits
205 $retval = array();
206 foreach ( $this->getConfig()->get( 'RateLimits' ) as $action => $limits ) {
207 foreach ( $categories as $cat ) {
208 if ( isset( $limits[$cat] ) && !is_null( $limits[$cat] ) ) {
209 $retval[$action][$cat]['hits'] = intval( $limits[$cat][0] );
210 $retval[$action][$cat]['seconds'] = intval( $limits[$cat][1] );
211 }
212 }
213 }
214
215 return $retval;
216 }
217
218 public function getAllowedParams() {
219 return array(
220 'prop' => array(
221 ApiBase::PARAM_DFLT => null,
222 ApiBase::PARAM_ISMULTI => true,
223 ApiBase::PARAM_TYPE => array(
224 'blockinfo',
225 'hasmsg',
226 'groups',
227 'implicitgroups',
228 'rights',
229 'changeablegroups',
230 'options',
231 'preferencestoken',
232 'editcount',
233 'ratelimits',
234 'email',
235 'realname',
236 'acceptlang',
237 'registrationdate',
238 'unreadcount',
239 )
240 )
241 );
242 }
243
244 public function getParamDescription() {
245 return array(
246 'prop' => array(
247 'What pieces of information to include',
248 ' blockinfo - Tags if the current user is blocked, by whom, and for what reason',
249 ' hasmsg - Adds a tag "message" if the current user has pending messages',
250 ' groups - Lists all the groups the current user belongs to',
251 ' implicitgroups - Lists all the groups the current user is automatically a member of',
252 ' rights - Lists all the rights the current user has',
253 ' changeablegroups - Lists the groups the current user can add to and remove from',
254 ' options - Lists all preferences the current user has set',
255 ' preferencestoken - Get a token to change current user\'s preferences',
256 ' editcount - Adds the current user\'s edit count',
257 ' ratelimits - Lists all rate limits applying to the current user',
258 ' realname - Adds the user\'s real name',
259 ' email - Adds the user\'s email address and email authentication date',
260 ' acceptlang - Echoes the Accept-Language header sent by ' .
261 'the client in a structured format',
262 ' registrationdate - Adds the user\'s registration date',
263 ' unreadcount - Adds the count of unread pages on the user\'s watchlist ' .
264 '(maximum ' . ( self::WL_UNREAD_LIMIT - 1 ) . '; returns "' .
265 self::WL_UNREAD_LIMIT . '+" if more)',
266 )
267 );
268 }
269
270 public function getDescription() {
271 return 'Get information about the current user.';
272 }
273
274 public function getExamples() {
275 return array(
276 'api.php?action=query&meta=userinfo',
277 'api.php?action=query&meta=userinfo&uiprop=blockinfo|groups|rights|hasmsg',
278 );
279 }
280
281 public function getHelpUrls() {
282 return 'https://www.mediawiki.org/wiki/API:Meta#userinfo_.2F_ui';
283 }
284 }