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;
36 public function __construct( $query, $moduleName ) {
37 parent
::__construct( $query, $moduleName, 'us' );
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
46 protected function getTokenFunctions() {
47 // Don't call the hooks twice
48 if ( isset( $this->tokenFunctions
) ) {
49 return $this->tokenFunctions
;
52 // If we're in JSON callback mode, no tokens can be obtained
53 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
57 $this->tokenFunctions
= array(
58 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
60 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions
) );
61 return $this->tokenFunctions
;
68 public static function getUserrightsToken( $user ) {
70 // Since the permissions check for userrights is non-trivial,
71 // don't bother with it here
72 return $wgUser->getEditToken( $user->getName() );
75 public function execute() {
76 $params = $this->extractRequestParams();
78 if ( !is_null( $params['prop'] ) ) {
79 $this->prop
= array_flip( $params['prop'] );
81 $this->prop
= array();
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() ),
95 $this->setContinueEnumParameter( 'users',
96 implode( '|', array_diff( $users, $done ) ) );
106 $result = $this->getResult();
108 if ( count( $goodNames ) ) {
109 $this->addTables( 'user' );
110 $this->addFields( User
::selectFields() );
111 $this->addWhereFld( 'user_name', $goodNames );
113 $this->showHiddenUsersAddBlockInfo( isset( $this->prop
['blockinfo'] ) );
116 $res = $this->select( __METHOD__
);
117 $this->resetQueryParams();
119 // get user groups if needed
120 if ( isset( $this->prop
['groups'] ) ||
isset( $this->prop
['rights'] ) ) {
121 $userGroups = array();
123 $this->addTables( 'user' );
124 $this->addWhereFld( 'user_name', $goodNames );
125 $this->addTables( 'user_groups' );
126 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
127 $this->addFields( array( 'user_name', 'ug_group' ) );
128 $userGroupsRes = $this->select( __METHOD__
);
130 foreach ( $userGroupsRes as $row ) {
131 $userGroups[$row->user_name
][] = $row->ug_group
;
135 foreach ( $res as $row ) {
136 // create user object and pass along $userGroups if set
137 // that reduces the number of database queries needed in User dramatically
138 if ( !isset( $userGroups ) ) {
139 $user = User
::newFromRow( $row );
141 if ( !isset( $userGroups[$row->user_name
] ) ||
!is_array( $userGroups[$row->user_name
] ) ) {
142 $userGroups[$row->user_name
] = array();
144 $user = User
::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name
] ) );
146 $name = $user->getName();
148 $data[$name]['userid'] = $user->getId();
149 $data[$name]['name'] = $name;
151 if ( isset( $this->prop
['editcount'] ) ) {
152 $data[$name]['editcount'] = intval( $user->getEditCount() );
155 if ( isset( $this->prop
['registration'] ) ) {
156 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
159 if ( isset( $this->prop
['groups'] ) ) {
160 $data[$name]['groups'] = $user->getEffectiveGroups();
163 if ( isset( $this->prop
['implicitgroups'] ) ) {
164 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
167 if ( isset( $this->prop
['rights'] ) ) {
168 $data[$name]['rights'] = $user->getRights();
170 if ( $row->ipb_deleted
) {
171 $data[$name]['hidden'] = '';
173 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->ipb_by_text
) ) {
174 $data[$name]['blockid'] = $row->ipb_id
;
175 $data[$name]['blockedby'] = $row->ipb_by_text
;
176 $data[$name]['blockedbyid'] = $row->ipb_by
;
177 $data[$name]['blockreason'] = $row->ipb_reason
;
178 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
181 if ( isset( $this->prop
['emailable'] ) && $user->canReceiveEmail() ) {
182 $data[$name]['emailable'] = '';
185 if ( isset( $this->prop
['gender'] ) ) {
186 $gender = $user->getOption( 'gender' );
187 if ( strval( $gender ) === '' ) {
190 $data[$name]['gender'] = $gender;
193 if ( !is_null( $params['token'] ) ) {
194 $tokenFunctions = $this->getTokenFunctions();
195 foreach ( $params['token'] as $t ) {
196 $val = call_user_func( $tokenFunctions[$t], $user );
197 if ( $val === false ) {
198 $this->setWarning( "Action '$t' is not allowed for the current user" );
200 $data[$name][$t . 'token'] = $val;
207 // Second pass: add result data to $retval
208 foreach ( $goodNames as $u ) {
209 if ( !isset( $data[$u] ) ) {
210 $data[$u] = array( 'name' => $u );
211 $urPage = new UserrightsPage
;
212 $iwUser = $urPage->fetchUser( $u );
214 if ( $iwUser instanceof UserRightsProxy
) {
215 $data[$u]['interwiki'] = '';
217 if ( !is_null( $params['token'] ) ) {
218 $tokenFunctions = $this->getTokenFunctions();
220 foreach ( $params['token'] as $t ) {
221 $val = call_user_func( $tokenFunctions[$t], $iwUser );
222 if ( $val === false ) {
223 $this->setWarning( "Action '$t' is not allowed for the current user" );
225 $data[$u][$t . 'token'] = $val;
230 $data[$u]['missing'] = '';
233 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
234 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
236 if ( isset( $this->prop
['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
237 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
239 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
240 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
244 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
247 $this->setContinueEnumParameter( 'users',
248 implode( '|', array_diff( $users, $done ) ) );
253 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
257 * Gets all the groups that a user is automatically a member of (implicit groups)
259 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
263 public static function getAutoGroups( $user ) {
264 wfDeprecated( __METHOD__
, '1.20' );
266 return $user->getAutomaticGroups();
269 public function getCacheMode( $params ) {
270 if ( isset( $params['token'] ) ) {
273 return 'anon-public-user-private';
277 public function getAllowedParams() {
280 ApiBase
::PARAM_DFLT
=> null,
281 ApiBase
::PARAM_ISMULTI
=> true,
282 ApiBase
::PARAM_TYPE
=> array(
294 ApiBase
::PARAM_ISMULTI
=> true
297 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
298 ApiBase
::PARAM_ISMULTI
=> true
303 public function getParamDescription() {
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 email through [[Special:Emailuser]]',
314 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
316 'users' => 'A list of users to obtain the same information for',
317 'token' => 'Which tokens to obtain for each user',
321 public function getResultProperties() {
325 ApiBase
::PROP_TYPE
=> 'integer',
326 ApiBase
::PROP_NULLABLE
=> true
329 'invalid' => 'boolean',
330 'hidden' => 'boolean',
331 'interwiki' => 'boolean',
332 'missing' => 'boolean'
334 'editcount' => array(
335 'editcount' => array(
336 ApiBase
::PROP_TYPE
=> 'integer',
337 ApiBase
::PROP_NULLABLE
=> true
340 'registration' => array(
341 'registration' => array(
342 ApiBase
::PROP_TYPE
=> 'timestamp',
343 ApiBase
::PROP_NULLABLE
=> true
346 'blockinfo' => array(
348 ApiBase
::PROP_TYPE
=> 'integer',
349 ApiBase
::PROP_NULLABLE
=> true
351 'blockedby' => array(
352 ApiBase
::PROP_TYPE
=> 'string',
353 ApiBase
::PROP_NULLABLE
=> true
355 'blockedbyid' => array(
356 ApiBase
::PROP_TYPE
=> 'integer',
357 ApiBase
::PROP_NULLABLE
=> true
359 'blockedreason' => array(
360 ApiBase
::PROP_TYPE
=> 'string',
361 ApiBase
::PROP_NULLABLE
=> true
363 'blockedexpiry' => array(
364 ApiBase
::PROP_TYPE
=> 'timestamp',
365 ApiBase
::PROP_NULLABLE
=> true
368 'emailable' => array(
369 'emailable' => 'boolean'
373 ApiBase
::PROP_TYPE
=> array(
378 ApiBase
::PROP_NULLABLE
=> true
383 self
::addTokenProperties( $props, $this->getTokenFunctions() );
388 public function getDescription() {
389 return 'Get information about a list of users';
392 public function getExamples() {
393 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
396 public function getHelpUrls() {
397 return 'https://www.mediawiki.org/wiki/API:Users';