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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query module to get information about a list of users
37 class ApiQueryUsers
extends ApiQueryBase
{
39 private $tokenFunctions, $prop;
41 public function __construct( $query, $moduleName ) {
42 parent
::__construct( $query, $moduleName, 'us' );
46 * Get an array mapping token names to their handler functions.
47 * The prototype for a token function is func($user)
48 * it should return a token or false (permission denied)
49 * @return Array tokenname => function
51 protected function getTokenFunctions() {
52 // Don't call the hooks twice
53 if ( isset( $this->tokenFunctions
) ) {
54 return $this->tokenFunctions
;
57 // If we're in JSON callback mode, no tokens can be obtained
58 if ( !is_null( $this->getMain()->getRequest()->getVal( 'callback' ) ) ) {
62 $this->tokenFunctions
= array(
63 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
65 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions
) );
66 return $this->tokenFunctions
;
74 public static function getUserrightsToken( $user ) {
76 // Since the permissions check for userrights is non-trivial,
77 // don't bother with it here
78 return $wgUser->editToken( $user->getName() );
81 public function execute() {
82 $params = $this->extractRequestParams();
84 if ( !is_null( $params['prop'] ) ) {
85 $this->prop
= array_flip( $params['prop'] );
87 $this->prop
= array();
90 $users = (array)$params['users'];
91 $goodNames = $done = array();
92 $result = $this->getResult();
93 // Canonicalize user names
94 foreach ( $users as $u ) {
95 $n = User
::getCanonicalName( $u );
96 if ( $n === false ||
$n === '' ) {
97 $vals = array( 'name' => $u, 'invalid' => '' );
98 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
101 $this->setContinueEnumParameter( 'users',
102 implode( '|', array_diff( $users, $done ) ) );
103 $goodNames = array();
112 $result = $this->getResult();
114 if ( count( $goodNames ) ) {
115 $this->addTables( 'user' );
116 $this->addFields( '*' );
117 $this->addWhereFld( 'user_name', $goodNames );
119 if ( isset( $this->prop
['groups'] ) ||
isset( $this->prop
['rights'] ) ) {
120 $this->addTables( 'user_groups' );
121 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
122 $this->addFields( 'ug_group' );
125 $this->showHiddenUsersAddBlockInfo( isset( $this->prop
['blockinfo'] ) );
128 $res = $this->select( __METHOD__
);
130 foreach ( $res as $row ) {
131 $user = User
::newFromRow( $row );
132 $name = $user->getName();
134 $data[$name]['userid'] = $user->getId();
135 $data[$name]['name'] = $name;
137 if ( isset( $this->prop
['editcount'] ) ) {
138 $data[$name]['editcount'] = intval( $user->getEditCount() );
141 if ( isset( $this->prop
['registration'] ) ) {
142 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
145 if ( isset( $this->prop
['groups'] ) ) {
146 if ( !isset( $data[$name]['groups'] ) ) {
147 $data[$name]['groups'] = self
::getAutoGroups( $user );
150 if ( !is_null( $row->ug_group
) ) {
151 // This row contains only one group, others will be added from other rows
152 $data[$name]['groups'][] = $row->ug_group
;
156 if ( isset( $this->prop
['rights'] ) ) {
157 if ( !isset( $data[$name]['rights'] ) ) {
158 $data[$name]['rights'] = User
::getGroupPermissions( User
::getImplicitGroups() );
161 if ( !is_null( $row->ug_group
) ) {
162 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
163 User
::getGroupPermissions( array( $row->ug_group
) ) ) );
166 if ( $row->ipb_deleted
) {
167 $data[$name]['hidden'] = '';
169 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->ipb_by_text
) ) {
170 $data[$name]['blockedby'] = $row->ipb_by_text
;
171 $data[$name]['blockreason'] = $row->ipb_reason
;
172 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
175 if ( isset( $this->prop
['emailable'] ) && $user->canReceiveEmail() ) {
176 $data[$name]['emailable'] = '';
179 if ( isset( $this->prop
['gender'] ) ) {
180 $gender = $user->getOption( 'gender' );
181 if ( strval( $gender ) === '' ) {
184 $data[$name]['gender'] = $gender;
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" );
194 $data[$name][$t . 'token'] = $val;
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 );
208 if ( $iwUser instanceof UserRightsProxy
) {
209 $data[$u]['interwiki'] = '';
211 if ( !is_null( $params['token'] ) ) {
212 $tokenFunctions = $this->getTokenFunctions();
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" );
219 $data[$u][$t . 'token'] = $val;
224 $data[$u]['missing'] = '';
227 if ( isset( $this->prop
['groups'] ) && isset( $data[$u]['groups'] ) ) {
228 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
230 if ( isset( $this->prop
['rights'] ) && isset( $data[$u]['rights'] ) ) {
231 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
235 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
238 $this->setContinueEnumParameter( 'users',
239 implode( '|', array_diff( $users, $done ) ) );
244 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
248 * Gets all the groups that a user is automatically a member of (implicit groups)
252 public static function getAutoGroups( $user ) {
256 if ( !$user->isAnon() ) {
260 $builtGroups = array();
262 foreach( array_merge( $groups, Autopromote
::getAutopromoteGroups( $user ) ) as $g ) {
263 $builtGroups[$i] = array( 'implicit' => '' );
264 ApiResult
::setContent( $builtGroups[$i], $g );
270 public function getCacheMode( $params ) {
271 if ( isset( $params['token'] ) ) {
274 return 'anon-public-user-private';
278 public function getAllowedParams() {
281 ApiBase
::PARAM_DFLT
=> null,
282 ApiBase
::PARAM_ISMULTI
=> true,
283 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 ' 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"',
315 'users' => 'A list of users to obtain the same information for',
316 'token' => 'Which tokens to obtain for each user',
320 public function getDescription() {
321 return 'Get information about a list of users';
324 protected function getExamples() {
325 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
328 public function getVersion() {
329 return __CLASS__
. ': $Id$';