Check validity and availability of usernames during signup via AJAX
[lhc/web/wiklou.git] / includes / api / ApiQueryUsers.php
1 <?php
2 /**
3 *
4 *
5 * Created on July 30, 2007
6 *
7 * Copyright © 2007 Roan Kattouw "<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 a list of users
29 *
30 * @ingroup API
31 */
32 class ApiQueryUsers extends ApiQueryBase {
33
34 private $tokenFunctions, $prop;
35
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'us' );
38 }
39
40 /**
41 * Get an array mapping token names to their handler functions.
42 * The prototype for a token function is func($user)
43 * it should return a token or false (permission denied)
44 * @return Array tokenname => function
45 */
46 protected function getTokenFunctions() {
47 // Don't call the hooks twice
48 if ( isset( $this->tokenFunctions ) ) {
49 return $this->tokenFunctions;
50 }
51
52 // If we're in JSON callback mode, no tokens can be obtained
53 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
54 return array();
55 }
56
57 $this->tokenFunctions = array(
58 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
59 );
60 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
61
62 return $this->tokenFunctions;
63 }
64
65 /**
66 * @param $user User
67 * @return String
68 */
69 public static function getUserrightsToken( $user ) {
70 global $wgUser;
71
72 // Since the permissions check for userrights is non-trivial,
73 // don't bother with it here
74 return $wgUser->getEditToken( $user->getName() );
75 }
76
77 public function execute() {
78 $params = $this->extractRequestParams();
79
80 if ( !is_null( $params['prop'] ) ) {
81 $this->prop = array_flip( $params['prop'] );
82 } else {
83 $this->prop = array();
84 }
85
86 $users = (array)$params['users'];
87 $goodNames = $done = array();
88 $result = $this->getResult();
89 // Canonicalize user names
90 foreach ( $users as $u ) {
91 $n = User::getCanonicalName( $u );
92 if ( $n === false || $n === '' ) {
93 $vals = array( 'name' => $u, 'invalid' => '' );
94 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
95 null, $vals );
96 if ( !$fit ) {
97 $this->setContinueEnumParameter( 'users',
98 implode( '|', array_diff( $users, $done ) ) );
99 $goodNames = array();
100 break;
101 }
102 $done[] = $u;
103 } else {
104 $goodNames[] = $n;
105 }
106 }
107
108 $result = $this->getResult();
109
110 if ( count( $goodNames ) ) {
111 $this->addTables( 'user' );
112 $this->addFields( User::selectFields() );
113 $this->addWhereFld( 'user_name', $goodNames );
114
115 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
116
117 $data = array();
118 $res = $this->select( __METHOD__ );
119 $this->resetQueryParams();
120
121 // get user groups if needed
122 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
123 $userGroups = array();
124
125 $this->addTables( 'user' );
126 $this->addWhereFld( 'user_name', $goodNames );
127 $this->addTables( 'user_groups' );
128 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
129 $this->addFields( array( 'user_name', 'ug_group' ) );
130 $userGroupsRes = $this->select( __METHOD__ );
131
132 foreach ( $userGroupsRes as $row ) {
133 $userGroups[$row->user_name][] = $row->ug_group;
134 }
135 }
136
137 foreach ( $res as $row ) {
138 // create user object and pass along $userGroups if set
139 // that reduces the number of database queries needed in User dramatically
140 if ( !isset( $userGroups ) ) {
141 $user = User::newFromRow( $row );
142 } else {
143 if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
144 $userGroups[$row->user_name] = array();
145 }
146 $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) );
147 }
148 $name = $user->getName();
149
150 $data[$name]['userid'] = $user->getId();
151 $data[$name]['name'] = $name;
152
153 if ( isset( $this->prop['editcount'] ) ) {
154 $data[$name]['editcount'] = $user->getEditCount();
155 }
156
157 if ( isset( $this->prop['registration'] ) ) {
158 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
159 }
160
161 if ( isset( $this->prop['groups'] ) ) {
162 $data[$name]['groups'] = $user->getEffectiveGroups();
163 }
164
165 if ( isset( $this->prop['implicitgroups'] ) ) {
166 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
167 }
168
169 if ( isset( $this->prop['rights'] ) ) {
170 $data[$name]['rights'] = $user->getRights();
171 }
172 if ( $row->ipb_deleted ) {
173 $data[$name]['hidden'] = '';
174 }
175 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
176 $data[$name]['blockid'] = $row->ipb_id;
177 $data[$name]['blockedby'] = $row->ipb_by_text;
178 $data[$name]['blockedbyid'] = $row->ipb_by;
179 $data[$name]['blockreason'] = $row->ipb_reason;
180 $data[$name]['blockexpiry'] = $row->ipb_expiry;
181 }
182
183 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
184 $data[$name]['emailable'] = '';
185 }
186
187 if ( isset( $this->prop['gender'] ) ) {
188 $gender = $user->getOption( 'gender' );
189 if ( strval( $gender ) === '' ) {
190 $gender = 'unknown';
191 }
192 $data[$name]['gender'] = $gender;
193 }
194
195 if ( !is_null( $params['token'] ) ) {
196 $tokenFunctions = $this->getTokenFunctions();
197 foreach ( $params['token'] as $t ) {
198 $val = call_user_func( $tokenFunctions[$t], $user );
199 if ( $val === false ) {
200 $this->setWarning( "Action '$t' is not allowed for the current user" );
201 } else {
202 $data[$name][$t . 'token'] = $val;
203 }
204 }
205 }
206 }
207 }
208
209 $context = $this->getContext();
210 // Second pass: add result data to $retval
211 foreach ( $goodNames as $u ) {
212 if ( !isset( $data[$u] ) ) {
213 $data[$u] = array( 'name' => $u );
214 $urPage = new UserrightsPage;
215 $urPage->setContext( $context );
216 $iwUser = $urPage->fetchUser( $u );
217
218 if ( $iwUser instanceof UserRightsProxy ) {
219 $data[$u]['interwiki'] = '';
220
221 if ( !is_null( $params['token'] ) ) {
222 $tokenFunctions = $this->getTokenFunctions();
223
224 foreach ( $params['token'] as $t ) {
225 $val = call_user_func( $tokenFunctions[$t], $iwUser );
226 if ( $val === false ) {
227 $this->setWarning( "Action '$t' is not allowed for the current user" );
228 } else {
229 $data[$u][$t . 'token'] = $val;
230 }
231 }
232 }
233 } else {
234 $data[$u]['missing'] = '';
235 }
236 } else {
237 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
238 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
239 }
240 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
241 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
242 }
243 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
244 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
245 }
246 }
247
248 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
249 null, $data[$u] );
250 if ( !$fit ) {
251 $this->setContinueEnumParameter( 'users',
252 implode( '|', array_diff( $users, $done ) ) );
253 break;
254 }
255 $done[] = $u;
256 }
257 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
258 }
259
260 /**
261 * Gets all the groups that a user is automatically a member of (implicit groups)
262 *
263 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
264 * @param $user User
265 * @return array
266 */
267 public static function getAutoGroups( $user ) {
268 wfDeprecated( __METHOD__, '1.20' );
269
270 return $user->getAutomaticGroups();
271 }
272
273 public function getCacheMode( $params ) {
274 return isset( $params['token'] ) ? 'private' : 'anon-public-user-private';
275 }
276
277 public function getAllowedParams() {
278 return array(
279 'prop' => array(
280 ApiBase::PARAM_DFLT => null,
281 ApiBase::PARAM_ISMULTI => true,
282 ApiBase::PARAM_TYPE => array(
283 'blockinfo',
284 'groups',
285 'implicitgroups',
286 'rights',
287 'editcount',
288 'registration',
289 'emailable',
290 'gender',
291 )
292 ),
293 'users' => array(
294 ApiBase::PARAM_ISMULTI => true
295 ),
296 'token' => array(
297 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
298 ApiBase::PARAM_ISMULTI => true
299 ),
300 );
301 }
302
303 public function getParamDescription() {
304 return array(
305 'prop' => array(
306 'What pieces of information to include',
307 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
308 ' groups - Lists all the groups the user(s) belongs to',
309 ' implicitgroups - Lists all the groups a user is automatically a member of',
310 ' rights - Lists all the rights the user(s) has',
311 ' editcount - Adds the user\'s edit count',
312 ' registration - Adds the user\'s registration timestamp',
313 ' emailable - Tags if the user can and wants to receive ' .
314 'email through [[Special:Emailuser]]',
315 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
316 ),
317 'users' => 'A list of users to obtain the same information for',
318 'token' => 'Which tokens to obtain for each user',
319 );
320 }
321
322 public function getResultProperties() {
323 $props = array(
324 '' => array(
325 'userid' => array(
326 ApiBase::PROP_TYPE => 'integer',
327 ApiBase::PROP_NULLABLE => true
328 ),
329 'name' => 'string',
330 'invalid' => 'boolean',
331 'hidden' => 'boolean',
332 'interwiki' => 'boolean',
333 'missing' => 'boolean'
334 ),
335 'editcount' => array(
336 'editcount' => array(
337 ApiBase::PROP_TYPE => 'integer',
338 ApiBase::PROP_NULLABLE => true
339 )
340 ),
341 'registration' => array(
342 'registration' => array(
343 ApiBase::PROP_TYPE => 'timestamp',
344 ApiBase::PROP_NULLABLE => true
345 )
346 ),
347 'blockinfo' => array(
348 'blockid' => array(
349 ApiBase::PROP_TYPE => 'integer',
350 ApiBase::PROP_NULLABLE => true
351 ),
352 'blockedby' => array(
353 ApiBase::PROP_TYPE => 'string',
354 ApiBase::PROP_NULLABLE => true
355 ),
356 'blockedbyid' => array(
357 ApiBase::PROP_TYPE => 'integer',
358 ApiBase::PROP_NULLABLE => true
359 ),
360 'blockedreason' => array(
361 ApiBase::PROP_TYPE => 'string',
362 ApiBase::PROP_NULLABLE => true
363 ),
364 'blockedexpiry' => array(
365 ApiBase::PROP_TYPE => 'timestamp',
366 ApiBase::PROP_NULLABLE => true
367 )
368 ),
369 'emailable' => array(
370 'emailable' => 'boolean'
371 ),
372 'gender' => array(
373 'gender' => array(
374 ApiBase::PROP_TYPE => array(
375 'male',
376 'female',
377 'unknown'
378 ),
379 ApiBase::PROP_NULLABLE => true
380 )
381 )
382 );
383
384 self::addTokenProperties( $props, $this->getTokenFunctions() );
385
386 return $props;
387 }
388
389 public function getDescription() {
390 return 'Get information about a list of users';
391 }
392
393 public function getExamples() {
394 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
395 }
396
397 public function getHelpUrls() {
398 return 'https://www.mediawiki.org/wiki/API:Users';
399 }
400 }