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'] = $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 $context = $this->getContext();
208 // Second pass: add result data to $retval
209 foreach ( $goodNames as $u ) {
210 if ( !isset( $data[$u] ) ) {
211 $data[$u] = array( 'name' => $u );
212 $urPage = new UserrightsPage
;
213 $urPage->setContext( $context );
214 $iwUser = $urPage->fetchUser( $u );
216 if ( $iwUser instanceof UserRightsProxy
) {
217 $data[$u]['interwiki'] = '';
219 if ( !is_null( $params['token'] ) ) {
220 $tokenFunctions = $this->getTokenFunctions();
222 foreach ( $params['token'] as $t ) {
223 $val = call_user_func( $tokenFunctions[$t], $iwUser );
224 if ( $val === false ) {
225 $this->setWarning( "Action '$t' is not allowed for the current user" );
227 $data[$u][$t . 'token'] = $val;
232 $data[$u]['missing'] = '';
235 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
236 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
238 if ( isset( $this->prop
['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
239 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
241 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
242 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
246 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
249 $this->setContinueEnumParameter( 'users',
250 implode( '|', array_diff( $users, $done ) ) );
255 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
259 * Gets all the groups that a user is automatically a member of (implicit groups)
261 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
265 public static function getAutoGroups( $user ) {
266 wfDeprecated( __METHOD__
, '1.20' );
268 return $user->getAutomaticGroups();
271 public function getCacheMode( $params ) {
272 if ( isset( $params['token'] ) ) {
275 return 'anon-public-user-private';
279 public function getAllowedParams() {
282 ApiBase
::PARAM_DFLT
=> null,
283 ApiBase
::PARAM_ISMULTI
=> true,
284 ApiBase
::PARAM_TYPE
=> array(
296 ApiBase
::PARAM_ISMULTI
=> true
299 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
300 ApiBase
::PARAM_ISMULTI
=> true
305 public function getParamDescription() {
308 'What pieces of information to include',
309 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
310 ' groups - Lists all the groups the user(s) belongs to',
311 ' implicitgroups - Lists all the groups a user is automatically a member of',
312 ' rights - Lists all the rights the user(s) has',
313 ' editcount - Adds the user\'s edit count',
314 ' registration - Adds the user\'s registration timestamp',
315 ' emailable - Tags if the user can and wants to receive email through [[Special:Emailuser]]',
316 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
318 'users' => 'A list of users to obtain the same information for',
319 'token' => 'Which tokens to obtain for each user',
323 public function getResultProperties() {
327 ApiBase
::PROP_TYPE
=> 'integer',
328 ApiBase
::PROP_NULLABLE
=> true
331 'invalid' => 'boolean',
332 'hidden' => 'boolean',
333 'interwiki' => 'boolean',
334 'missing' => 'boolean'
336 'editcount' => array(
337 'editcount' => array(
338 ApiBase
::PROP_TYPE
=> 'integer',
339 ApiBase
::PROP_NULLABLE
=> true
342 'registration' => array(
343 'registration' => array(
344 ApiBase
::PROP_TYPE
=> 'timestamp',
345 ApiBase
::PROP_NULLABLE
=> true
348 'blockinfo' => array(
350 ApiBase
::PROP_TYPE
=> 'integer',
351 ApiBase
::PROP_NULLABLE
=> true
353 'blockedby' => array(
354 ApiBase
::PROP_TYPE
=> 'string',
355 ApiBase
::PROP_NULLABLE
=> true
357 'blockedbyid' => array(
358 ApiBase
::PROP_TYPE
=> 'integer',
359 ApiBase
::PROP_NULLABLE
=> true
361 'blockedreason' => array(
362 ApiBase
::PROP_TYPE
=> 'string',
363 ApiBase
::PROP_NULLABLE
=> true
365 'blockedexpiry' => array(
366 ApiBase
::PROP_TYPE
=> 'timestamp',
367 ApiBase
::PROP_NULLABLE
=> true
370 'emailable' => array(
371 'emailable' => 'boolean'
375 ApiBase
::PROP_TYPE
=> array(
380 ApiBase
::PROP_NULLABLE
=> true
385 self
::addTokenProperties( $props, $this->getTokenFunctions() );
390 public function getDescription() {
391 return 'Get information about a list of users';
394 public function getExamples() {
395 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
398 public function getHelpUrls() {
399 return 'https://www.mediawiki.org/wiki/API:Users';