5 * Created on July 30, 2007
7 * Copyright © 2007 Roan Kattouw "<Firstname>.<Lastname>@gmail.com"
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.
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.
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
28 * Query module to get information about a list of users
32 class ApiQueryUsers
extends ApiQueryBase
{
34 private $tokenFunctions, $prop;
37 * Properties whose contents does not depend on who is looking at them. If the usprops field
38 * contains anything not listed here, the cache mode will never be public for logged-in users.
41 protected static $publicProps = array(
42 // everything except 'blockinfo' which might show hidden records if the user
43 // making the request has the appropriate permissions
53 public function __construct( $query, $moduleName ) {
54 parent
::__construct( $query, $moduleName, 'us' );
58 * Get an array mapping token names to their handler functions.
59 * The prototype for a token function is func($user)
60 * it should return a token or false (permission denied)
61 * @return array Array of tokenname => function
63 protected function getTokenFunctions() {
64 // Don't call the hooks twice
65 if ( isset( $this->tokenFunctions
) ) {
66 return $this->tokenFunctions
;
69 // If we're in JSON callback mode, no tokens can be obtained
70 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
74 $this->tokenFunctions
= array(
75 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
77 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions
) );
79 return $this->tokenFunctions
;
86 public static function getUserrightsToken( $user ) {
89 // Since the permissions check for userrights is non-trivial,
90 // don't bother with it here
91 return $wgUser->getEditToken( $user->getName() );
94 public function execute() {
95 $params = $this->extractRequestParams();
97 if ( !is_null( $params['prop'] ) ) {
98 $this->prop
= array_flip( $params['prop'] );
100 $this->prop
= array();
103 $users = (array)$params['users'];
104 $goodNames = $done = array();
105 $result = $this->getResult();
106 // Canonicalize user names
107 foreach ( $users as $u ) {
108 $n = User
::getCanonicalName( $u );
109 if ( $n === false ||
$n === '' ) {
110 $vals = array( 'name' => $u, 'invalid' => '' );
111 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
114 $this->setContinueEnumParameter( 'users',
115 implode( '|', array_diff( $users, $done ) ) );
116 $goodNames = array();
125 $result = $this->getResult();
127 if ( count( $goodNames ) ) {
128 $this->addTables( 'user' );
129 $this->addFields( User
::selectFields() );
130 $this->addWhereFld( 'user_name', $goodNames );
132 $this->showHiddenUsersAddBlockInfo( isset( $this->prop
['blockinfo'] ) );
135 $res = $this->select( __METHOD__
);
136 $this->resetQueryParams();
138 // get user groups if needed
139 if ( isset( $this->prop
['groups'] ) ||
isset( $this->prop
['rights'] ) ) {
140 $userGroups = array();
142 $this->addTables( 'user' );
143 $this->addWhereFld( 'user_name', $goodNames );
144 $this->addTables( 'user_groups' );
145 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
146 $this->addFields( array( 'user_name', 'ug_group' ) );
147 $userGroupsRes = $this->select( __METHOD__
);
149 foreach ( $userGroupsRes as $row ) {
150 $userGroups[$row->user_name
][] = $row->ug_group
;
154 foreach ( $res as $row ) {
155 // create user object and pass along $userGroups if set
156 // that reduces the number of database queries needed in User dramatically
157 if ( !isset( $userGroups ) ) {
158 $user = User
::newFromRow( $row );
160 if ( !isset( $userGroups[$row->user_name
] ) ||
!is_array( $userGroups[$row->user_name
] ) ) {
161 $userGroups[$row->user_name
] = array();
163 $user = User
::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name
] ) );
165 $name = $user->getName();
167 $data[$name]['userid'] = $user->getId();
168 $data[$name]['name'] = $name;
170 if ( isset( $this->prop
['editcount'] ) ) {
171 $data[$name]['editcount'] = $user->getEditCount();
174 if ( isset( $this->prop
['registration'] ) ) {
175 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
178 if ( isset( $this->prop
['groups'] ) ) {
179 $data[$name]['groups'] = $user->getEffectiveGroups();
182 if ( isset( $this->prop
['implicitgroups'] ) ) {
183 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
186 if ( isset( $this->prop
['rights'] ) ) {
187 $data[$name]['rights'] = $user->getRights();
189 if ( $row->ipb_deleted
) {
190 $data[$name]['hidden'] = '';
192 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->ipb_by_text
) ) {
193 $data[$name]['blockid'] = $row->ipb_id
;
194 $data[$name]['blockedby'] = $row->ipb_by_text
;
195 $data[$name]['blockedbyid'] = $row->ipb_by
;
196 $data[$name]['blockreason'] = $row->ipb_reason
;
197 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
200 if ( isset( $this->prop
['emailable'] ) && $user->canReceiveEmail() ) {
201 $data[$name]['emailable'] = '';
204 if ( isset( $this->prop
['gender'] ) ) {
205 $gender = $user->getOption( 'gender' );
206 if ( strval( $gender ) === '' ) {
209 $data[$name]['gender'] = $gender;
212 if ( !is_null( $params['token'] ) ) {
213 $tokenFunctions = $this->getTokenFunctions();
214 foreach ( $params['token'] as $t ) {
215 $val = call_user_func( $tokenFunctions[$t], $user );
216 if ( $val === false ) {
217 $this->setWarning( "Action '$t' is not allowed for the current user" );
219 $data[$name][$t . 'token'] = $val;
226 $context = $this->getContext();
227 // Second pass: add result data to $retval
228 foreach ( $goodNames as $u ) {
229 if ( !isset( $data[$u] ) ) {
230 $data[$u] = array( 'name' => $u );
231 $urPage = new UserrightsPage
;
232 $urPage->setContext( $context );
233 $iwUser = $urPage->fetchUser( $u );
235 if ( $iwUser instanceof UserRightsProxy
) {
236 $data[$u]['interwiki'] = '';
238 if ( !is_null( $params['token'] ) ) {
239 $tokenFunctions = $this->getTokenFunctions();
241 foreach ( $params['token'] as $t ) {
242 $val = call_user_func( $tokenFunctions[$t], $iwUser );
243 if ( $val === false ) {
244 $this->setWarning( "Action '$t' is not allowed for the current user" );
246 $data[$u][$t . 'token'] = $val;
251 $data[$u]['missing'] = '';
254 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
255 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
257 if ( isset( $this->prop
['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
258 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
260 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
261 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
265 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
268 $this->setContinueEnumParameter( 'users',
269 implode( '|', array_diff( $users, $done ) ) );
274 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
278 * Gets all the groups that a user is automatically a member of (implicit groups)
280 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
284 public static function getAutoGroups( $user ) {
285 wfDeprecated( __METHOD__
, '1.20' );
287 return $user->getAutomaticGroups();
290 public function getCacheMode( $params ) {
291 if ( isset( $params['token'] ) ) {
293 } elseif ( array_diff( (array)$params['prop'], static::$publicProps ) ) {
294 return 'anon-public-user-private';
300 public function getAllowedParams() {
303 ApiBase
::PARAM_DFLT
=> null,
304 ApiBase
::PARAM_ISMULTI
=> true,
305 ApiBase
::PARAM_TYPE
=> array(
317 ApiBase
::PARAM_ISMULTI
=> true
320 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
321 ApiBase
::PARAM_ISMULTI
=> true
326 public function getParamDescription() {
329 'What pieces of information to include',
330 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
331 ' groups - Lists all the groups the user(s) belongs to',
332 ' implicitgroups - Lists all the groups a user is automatically a member of',
333 ' rights - Lists all the rights the user(s) has',
334 ' editcount - Adds the user\'s edit count',
335 ' registration - Adds the user\'s registration timestamp',
336 ' emailable - Tags if the user can and wants to receive ' .
337 'email through [[Special:Emailuser]]',
338 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
340 'users' => 'A list of users to obtain the same information for',
341 'token' => 'Which tokens to obtain for each user',
345 public function getResultProperties() {
349 ApiBase
::PROP_TYPE
=> 'integer',
350 ApiBase
::PROP_NULLABLE
=> true
353 'invalid' => 'boolean',
354 'hidden' => 'boolean',
355 'interwiki' => 'boolean',
356 'missing' => 'boolean'
358 'editcount' => array(
359 'editcount' => array(
360 ApiBase
::PROP_TYPE
=> 'integer',
361 ApiBase
::PROP_NULLABLE
=> true
364 'registration' => array(
365 'registration' => array(
366 ApiBase
::PROP_TYPE
=> 'timestamp',
367 ApiBase
::PROP_NULLABLE
=> true
370 'blockinfo' => array(
372 ApiBase
::PROP_TYPE
=> 'integer',
373 ApiBase
::PROP_NULLABLE
=> true
375 'blockedby' => array(
376 ApiBase
::PROP_TYPE
=> 'string',
377 ApiBase
::PROP_NULLABLE
=> true
379 'blockedbyid' => array(
380 ApiBase
::PROP_TYPE
=> 'integer',
381 ApiBase
::PROP_NULLABLE
=> true
383 'blockedreason' => array(
384 ApiBase
::PROP_TYPE
=> 'string',
385 ApiBase
::PROP_NULLABLE
=> true
387 'blockedexpiry' => array(
388 ApiBase
::PROP_TYPE
=> 'timestamp',
389 ApiBase
::PROP_NULLABLE
=> true
392 'emailable' => array(
393 'emailable' => 'boolean'
397 ApiBase
::PROP_TYPE
=> array(
402 ApiBase
::PROP_NULLABLE
=> true
407 self
::addTokenProperties( $props, $this->getTokenFunctions() );
412 public function getDescription() {
413 return 'Get information about a list of users.';
416 public function getExamples() {
417 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
420 public function getHelpUrls() {
421 return 'https://www.mediawiki.org/wiki/API:Users';