Added post-commit callback support to DB classes.
[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 return $this->tokenFunctions;
62 }
63
64 /**
65 * @param $user User
66 * @return String
67 */
68 public static function getUserrightsToken( $user ) {
69 global $wgUser;
70 // Since the permissions check for userrights is non-trivial,
71 // don't bother with it here
72 return $wgUser->getEditToken( $user->getName() );
73 }
74
75 public function execute() {
76 $params = $this->extractRequestParams();
77
78 if ( !is_null( $params['prop'] ) ) {
79 $this->prop = array_flip( $params['prop'] );
80 } else {
81 $this->prop = array();
82 }
83
84 $users = (array)$params['users'];
85 $goodNames = $done = array();
86 $result = $this->getResult();
87 // Canonicalize user names
88 foreach ( $users as $u ) {
89 $n = User::getCanonicalName( $u );
90 if ( $n === false || $n === '' ) {
91 $vals = array( 'name' => $u, 'invalid' => '' );
92 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
93 null, $vals );
94 if ( !$fit ) {
95 $this->setContinueEnumParameter( 'users',
96 implode( '|', array_diff( $users, $done ) ) );
97 $goodNames = array();
98 break;
99 }
100 $done[] = $u;
101 } else {
102 $goodNames[] = $n;
103 }
104 }
105
106 $result = $this->getResult();
107
108 if ( count( $goodNames ) ) {
109 $this->addTables( 'user' );
110 $this->addFields( User::selectFields() );
111 $this->addWhereFld( 'user_name', $goodNames );
112
113 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
114 $this->addTables( 'user_groups' );
115 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
116 $this->addFields( 'ug_group' );
117 }
118
119 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
120
121 $data = array();
122 $res = $this->select( __METHOD__ );
123
124 foreach ( $res as $row ) {
125 $user = User::newFromRow( $row );
126 $name = $user->getName();
127
128 $data[$name]['userid'] = $user->getId();
129 $data[$name]['name'] = $name;
130
131 if ( isset( $this->prop['editcount'] ) ) {
132 $data[$name]['editcount'] = intval( $user->getEditCount() );
133 }
134
135 if ( isset( $this->prop['registration'] ) ) {
136 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
137 }
138
139 if ( isset( $this->prop['groups'] ) ) {
140 if ( !isset( $data[$name]['groups'] ) ) {
141 $data[$name]['groups'] = self::getAutoGroups( $user );
142 }
143
144 if ( !is_null( $row->ug_group ) ) {
145 // This row contains only one group, others will be added from other rows
146 $data[$name]['groups'][] = $row->ug_group;
147 }
148 }
149
150 if ( isset( $this->prop['implicitgroups'] ) && !isset( $data[$name]['implicitgroups'] ) ) {
151 $data[$name]['implicitgroups'] = self::getAutoGroups( $user );
152 }
153
154 if ( isset( $this->prop['rights'] ) ) {
155 if ( !isset( $data[$name]['rights'] ) ) {
156 $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
157 }
158
159 if ( !is_null( $row->ug_group ) ) {
160 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
161 User::getGroupPermissions( array( $row->ug_group ) ) ) );
162 }
163 }
164 if ( $row->ipb_deleted ) {
165 $data[$name]['hidden'] = '';
166 }
167 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
168 $data[$name]['blockid'] = $row->ipb_id;
169 $data[$name]['blockedby'] = $row->ipb_by_text;
170 $data[$name]['blockedbyid'] = $row->ipb_by;
171 $data[$name]['blockreason'] = $row->ipb_reason;
172 $data[$name]['blockexpiry'] = $row->ipb_expiry;
173 }
174
175 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
176 $data[$name]['emailable'] = '';
177 }
178
179 if ( isset( $this->prop['gender'] ) ) {
180 $gender = $user->getOption( 'gender' );
181 if ( strval( $gender ) === '' ) {
182 $gender = 'unknown';
183 }
184 $data[$name]['gender'] = $gender;
185 }
186
187 if ( !is_null( $params['token'] ) ) {
188 $tokenFunctions = $this->getTokenFunctions();
189 foreach ( $params['token'] as $t ) {
190 $val = call_user_func( $tokenFunctions[$t], $user );
191 if ( $val === false ) {
192 $this->setWarning( "Action '$t' is not allowed for the current user" );
193 } else {
194 $data[$name][$t . 'token'] = $val;
195 }
196 }
197 }
198 }
199 }
200
201 // Second pass: add result data to $retval
202 foreach ( $goodNames as $u ) {
203 if ( !isset( $data[$u] ) ) {
204 $data[$u] = array( 'name' => $u );
205 $urPage = new UserrightsPage;
206 $iwUser = $urPage->fetchUser( $u );
207
208 if ( $iwUser instanceof UserRightsProxy ) {
209 $data[$u]['interwiki'] = '';
210
211 if ( !is_null( $params['token'] ) ) {
212 $tokenFunctions = $this->getTokenFunctions();
213
214 foreach ( $params['token'] as $t ) {
215 $val = call_user_func( $tokenFunctions[$t], $iwUser );
216 if ( $val === false ) {
217 $this->setWarning( "Action '$t' is not allowed for the current user" );
218 } else {
219 $data[$u][$t . 'token'] = $val;
220 }
221 }
222 }
223 } else {
224 $data[$u]['missing'] = '';
225 }
226 } else {
227 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
228 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
229 }
230 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
231 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
232 }
233 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
234 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
235 }
236 }
237
238 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
239 null, $data[$u] );
240 if ( !$fit ) {
241 $this->setContinueEnumParameter( 'users',
242 implode( '|', array_diff( $users, $done ) ) );
243 break;
244 }
245 $done[] = $u;
246 }
247 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
248 }
249
250 /**
251 * Gets all the groups that a user is automatically a member of (implicit groups)
252 * @param $user User
253 * @return array
254 */
255 public static function getAutoGroups( $user ) {
256 // FIXME this logic is duplicated from User::getEffectiveGroups(), centralize this
257 $groups = array();
258 $groups[] = '*';
259
260 if ( !$user->isAnon() ) {
261 $groups[] = 'user';
262 $groups = array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
263 }
264
265 return $groups;
266 }
267
268 public function getCacheMode( $params ) {
269 if ( isset( $params['token'] ) ) {
270 return 'private';
271 } else {
272 return 'anon-public-user-private';
273 }
274 }
275
276 public function getAllowedParams() {
277 return array(
278 'prop' => array(
279 ApiBase::PARAM_DFLT => null,
280 ApiBase::PARAM_ISMULTI => true,
281 ApiBase::PARAM_TYPE => array(
282 'blockinfo',
283 'groups',
284 'implicitgroups',
285 'rights',
286 'editcount',
287 'registration',
288 'emailable',
289 'gender',
290 )
291 ),
292 'users' => array(
293 ApiBase::PARAM_ISMULTI => true
294 ),
295 'token' => array(
296 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
297 ApiBase::PARAM_ISMULTI => true
298 ),
299 );
300 }
301
302 public function getParamDescription() {
303 return array(
304 'prop' => array(
305 'What pieces of information to include',
306 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
307 ' groups - Lists all the groups the user(s) belongs to',
308 ' implicitgroups - Lists all the groups a user is automatically a member of',
309 ' rights - Lists all the rights the user(s) has',
310 ' editcount - Adds the user\'s edit count',
311 ' registration - Adds the user\'s registration timestamp',
312 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
313 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
314 ),
315 'users' => 'A list of users to obtain the same information for',
316 'token' => 'Which tokens to obtain for each user',
317 );
318 }
319
320 public function getResultProperties() {
321 $props = array(
322 '' => array(
323 'userid' => array(
324 ApiBase::PROP_TYPE => 'integer',
325 ApiBase::PROP_NULLABLE => true
326 ),
327 'name' => 'string',
328 'invalid' => 'boolean',
329 'hidden' => 'boolean',
330 'interwiki' => 'boolean',
331 'missing' => 'boolean'
332 ),
333 'editcount' => array(
334 'editcount' => array(
335 ApiBase::PROP_TYPE => 'integer',
336 ApiBase::PROP_NULLABLE => true
337 )
338 ),
339 'registration' => array(
340 'registration' => array(
341 ApiBase::PROP_TYPE => 'timestamp',
342 ApiBase::PROP_NULLABLE => true
343 )
344 ),
345 'blockinfo' => array(
346 'blockid' => array(
347 ApiBase::PROP_TYPE => 'integer',
348 ApiBase::PROP_NULLABLE => true
349 ),
350 'blockedby' => array(
351 ApiBase::PROP_TYPE => 'string',
352 ApiBase::PROP_NULLABLE => true
353 ),
354 'blockedbyid' => array(
355 ApiBase::PROP_TYPE => 'integer',
356 ApiBase::PROP_NULLABLE => true
357 ),
358 'blockedreason' => array(
359 ApiBase::PROP_TYPE => 'string',
360 ApiBase::PROP_NULLABLE => true
361 ),
362 'blockedexpiry' => array(
363 ApiBase::PROP_TYPE => 'timestamp',
364 ApiBase::PROP_NULLABLE => true
365 )
366 ),
367 'emailable' => array(
368 'emailable' => 'boolean'
369 ),
370 'gender' => array(
371 'gender' => array(
372 ApiBase::PROP_TYPE => array(
373 'male',
374 'female',
375 'unknown'
376 ),
377 ApiBase::PROP_NULLABLE => true
378 )
379 )
380 );
381
382 self::addTokenProperties( $props, $this->getTokenFunctions() );
383
384 return $props;
385 }
386
387 public function getDescription() {
388 return 'Get information about a list of users';
389 }
390
391 public function getExamples() {
392 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
393 }
394
395 public function getHelpUrls() {
396 return 'https://www.mediawiki.org/wiki/API:Users';
397 }
398
399 public function getVersion() {
400 return __CLASS__ . ': $Id$';
401 }
402 }