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 if ( count( $goodNames ) ) {
113 $this->addTables( 'user', 'u1' );
114 $this->addFields( 'u1.*' );
115 $this->addWhereFld( 'u1.user_name', $goodNames );
117 if ( isset( $this->prop
['groups'] ) ) {
118 $this->addTables( 'user_groups' );
119 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=u1.user_id' ) ) );
120 $this->addFields( 'ug_group' );
122 if ( isset( $this->prop
['blockinfo'] ) ) {
123 $this->addTables( 'ipblocks' );
124 $this->addJoinConds( array(
125 'ipblocks' => array( 'LEFT JOIN', 'ipb_user=u1.user_id' ),
127 $this->addFields( array( 'ipb_reason', 'ipb_by_text', 'ipb_expiry' ) );
131 $res = $this->select( __METHOD__
);
133 $result = $this->getResult();
135 foreach ( $res as $row ) {
136 $user = User
::newFromRow( $row );
137 $name = $user->getName();
138 $data[$name]['name'] = $name;
140 if ( isset( $this->prop
['editcount'] ) ) {
141 $data[$name]['editcount'] = intval( $user->getEditCount() );
144 if ( isset( $this->prop
['registration'] ) ) {
145 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601
, $user->getRegistration() );
148 if ( isset( $this->prop
['groups'] ) && !is_null( $row->ug_group
) ) {
149 if ( !isset( $data[$u]['groups'] ) ) {
150 $data[$u]['groups'] = ApiQueryUsers
::getAutoGroups( User
::newFromName( $u ) );
153 // This row contains only one group, others will be added from other rows
154 $data[$name]['groups'][] = $row->ug_group
;
155 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
158 if ( isset( $this->prop
['rights'] ) && !is_null( $row->ug_group
) ) {
159 if ( !isset( $data[$name]['rights'] ) ) {
160 $data[$name]['rights'] = User
::getGroupPermissions( User
::getImplicitGroups() );
163 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
164 User
::getGroupPermissions( array( $row->ug_group
) ) ) );
165 $result->setIndexedTagName( $data[$name]['rights'], 'r' );
168 if ( isset( $this->prop
['blockinfo'] ) && !is_null( $row->blocker_name
) ) {
169 $data[$name]['blockedby'] = $row->ipb_by_text
;
170 $data[$name]['blockreason'] = $row->ipb_reason
;
171 $data[$name]['blockexpiry'] = $row->ipb_expiry
;
174 if ( isset( $this->prop
['emailable'] ) && $user->canReceiveEmail() ) {
175 $data[$name]['emailable'] = '';
178 if ( isset( $this->prop
['gender'] ) ) {
179 $gender = $user->getOption( 'gender' );
180 if ( strval( $gender ) === '' ) {
183 $data[$name]['gender'] = $gender;
186 if ( !is_null( $params['token'] ) ) {
187 $tokenFunctions = $this->getTokenFunctions();
188 foreach ( $params['token'] as $t ) {
189 $val = call_user_func( $tokenFunctions[$t], $user );
190 if ( $val === false ) {
191 $this->setWarning( "Action '$t' is not allowed for the current user" );
193 $data[$name][$t . 'token'] = $val;
199 // Second pass: add result data to $retval
200 foreach ( $goodNames as $u ) {
201 if ( !isset( $data[$u] ) ) {
202 $data[$u] = array( 'name' => $u );
203 $urPage = new UserrightsPage
;
204 $iwUser = $urPage->fetchUser( $u );
206 if ( $iwUser instanceof UserRightsProxy
) {
207 $data[$u]['interwiki'] = '';
209 if ( !is_null( $params['token'] ) ) {
210 $tokenFunctions = $this->getTokenFunctions();
212 foreach ( $params['token'] as $t ) {
213 $val = call_user_func( $tokenFunctions[$t], $iwUser );
214 if ( $val === false ) {
215 $this->setWarning( "Action '$t' is not allowed for the current user" );
217 $data[$u][$t . 'token'] = $val;
222 $data[$u]['missing'] = '';
226 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
229 $this->setContinueEnumParameter( 'users',
230 implode( '|', array_diff( $users, $done ) ) );
235 return $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
239 * Gets all the groups that a user is automatically a member of
243 public static function getAutoGroups( $user ) {
244 $groups = array( '*' );
246 if ( !$user->isAnon() ) {
250 return array_merge( $groups, Autopromote
::getAutopromoteGroups( $user ) );
253 public function getCacheMode( $params ) {
254 if ( isset( $params['token'] ) ) {
261 public function getAllowedParams() {
264 ApiBase
::PARAM_DFLT
=> null,
265 ApiBase
::PARAM_ISMULTI
=> true,
266 ApiBase
::PARAM_TYPE
=> array(
277 ApiBase
::PARAM_ISMULTI
=> true
280 ApiBase
::PARAM_TYPE
=> array_keys( $this->getTokenFunctions() ),
281 ApiBase
::PARAM_ISMULTI
=> true
286 public function getParamDescription() {
289 'What pieces of information to include',
290 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
291 ' groups - Lists all the groups the user(s) belongs to',
292 ' rights - Lists all the rights the user(s) has',
293 ' editcount - Adds the user\'s edit count',
294 ' registration - Adds the user\'s registration timestamp',
295 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
296 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
298 'users' => 'A list of users to obtain the same information for',
299 'token' => 'Which tokens to obtain for each user',
303 public function getDescription() {
304 return 'Get information about a list of users';
307 protected function getExamples() {
308 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
311 public function getVersion() {
312 return __CLASS__
. ': $Id$';