add possibility to close a LCStore_CDB
[mediawiki.git] / includes / api / ApiQueryUsers.php
blob8cc7eb0cefff39f280bf9e46dcdebf7c61d20162
1 <?php
2 /**
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
24 * @file
27 /**
28 * Query module to get information about a list of users
30 * @ingroup API
32 class ApiQueryUsers extends ApiQueryBase {
34 private $tokenFunctions, $prop;
36 public function __construct( $query, $moduleName ) {
37 parent::__construct( $query, $moduleName, 'us' );
40 /**
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' ) ) ) {
54 return array();
57 $this->tokenFunctions = array(
58 'userrights' => array( 'ApiQueryUsers', 'getUserrightsToken' ),
60 wfRunHooks( 'APIQueryUsersTokens', array( &$this->tokenFunctions ) );
61 return $this->tokenFunctions;
64 /**
65 * @param $user User
66 * @return String
68 public static function getUserrightsToken( $user ) {
69 global $wgUser;
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'] );
80 } else {
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() ),
93 null, $vals );
94 if ( !$fit ) {
95 $this->setContinueEnumParameter( 'users',
96 implode( '|', array_diff( $users, $done ) ) );
97 $goodNames = array();
98 break;
100 $done[] = $u;
101 } else {
102 $goodNames[] = $n;
106 $result = $this->getResult();
108 if ( count( $goodNames ) ) {
109 $this->addTables( 'user' );
110 $this->addFields( '*' );
111 $this->addWhereFld( 'user_name', $goodNames );
113 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
114 $this->addTables( 'user_groups' );
115 $this->addJoinConds( array( 'user_groups' => array( 'LEFT JOIN', 'ug_user=user_id' ) ) );
116 $this->addFields( 'ug_group' );
119 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
121 $data = array();
122 $res = $this->select( __METHOD__ );
124 foreach ( $res as $row ) {
125 $user = User::newFromRow( $row );
126 $name = $user->getName();
128 $data[$name]['userid'] = $user->getId();
129 $data[$name]['name'] = $name;
131 if ( isset( $this->prop['editcount'] ) ) {
132 $data[$name]['editcount'] = intval( $user->getEditCount() );
135 if ( isset( $this->prop['registration'] ) ) {
136 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
139 if ( isset( $this->prop['groups'] ) ) {
140 if ( !isset( $data[$name]['groups'] ) ) {
141 $data[$name]['groups'] = self::getAutoGroups( $user );
144 if ( !is_null( $row->ug_group ) ) {
145 // This row contains only one group, others will be added from other rows
146 $data[$name]['groups'][] = $row->ug_group;
150 if ( isset( $this->prop['implicitgroups'] ) && !isset( $data[$name]['implicitgroups'] ) ) {
151 $data[$name]['implicitgroups'] = self::getAutoGroups( $user );
154 if ( isset( $this->prop['rights'] ) ) {
155 if ( !isset( $data[$name]['rights'] ) ) {
156 $data[$name]['rights'] = User::getGroupPermissions( $user->getAutomaticGroups() );
159 if ( !is_null( $row->ug_group ) ) {
160 $data[$name]['rights'] = array_unique( array_merge( $data[$name]['rights'],
161 User::getGroupPermissions( array( $row->ug_group ) ) ) );
164 if ( $row->ipb_deleted ) {
165 $data[$name]['hidden'] = '';
167 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
168 $data[$name]['blockedby'] = $row->ipb_by_text;
169 $data[$name]['blockreason'] = $row->ipb_reason;
170 $data[$name]['blockexpiry'] = $row->ipb_expiry;
173 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
174 $data[$name]['emailable'] = '';
177 if ( isset( $this->prop['gender'] ) ) {
178 $gender = $user->getOption( 'gender' );
179 if ( strval( $gender ) === '' ) {
180 $gender = 'unknown';
182 $data[$name]['gender'] = $gender;
185 if ( !is_null( $params['token'] ) ) {
186 $tokenFunctions = $this->getTokenFunctions();
187 foreach ( $params['token'] as $t ) {
188 $val = call_user_func( $tokenFunctions[$t], $user );
189 if ( $val === false ) {
190 $this->setWarning( "Action '$t' is not allowed for the current user" );
191 } else {
192 $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" );
216 } else {
217 $data[$u][$t . 'token'] = $val;
221 } else {
222 $data[$u]['missing'] = '';
224 } else {
225 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
226 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
228 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
229 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
231 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
232 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
236 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
237 null, $data[$u] );
238 if ( !$fit ) {
239 $this->setContinueEnumParameter( 'users',
240 implode( '|', array_diff( $users, $done ) ) );
241 break;
243 $done[] = $u;
245 return $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
249 * Gets all the groups that a user is automatically a member of (implicit groups)
250 * @param $user User
251 * @return array
253 public static function getAutoGroups( $user ) {
254 $groups = array();
255 $groups[] = '*';
257 if ( !$user->isAnon() ) {
258 $groups[] = 'user';
261 return array_merge( $groups, Autopromote::getAutopromoteGroups( $user ) );
264 public function getCacheMode( $params ) {
265 if ( isset( $params['token'] ) ) {
266 return 'private';
267 } else {
268 return 'anon-public-user-private';
272 public function getAllowedParams() {
273 return array(
274 'prop' => array(
275 ApiBase::PARAM_DFLT => null,
276 ApiBase::PARAM_ISMULTI => true,
277 ApiBase::PARAM_TYPE => array(
278 'blockinfo',
279 'groups',
280 'implicitgroups',
281 'rights',
282 'editcount',
283 'registration',
284 'emailable',
285 'gender',
288 'users' => array(
289 ApiBase::PARAM_ISMULTI => true
291 'token' => array(
292 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
293 ApiBase::PARAM_ISMULTI => true
298 public function getParamDescription() {
299 return array(
300 'prop' => array(
301 'What pieces of information to include',
302 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
303 ' groups - Lists all the groups the user(s) belongs to',
304 ' implicitgroups - Lists all the groups a user is automatically a member of',
305 ' rights - Lists all the rights the user(s) has',
306 ' editcount - Adds the user\'s edit count',
307 ' registration - Adds the user\'s registration timestamp',
308 ' emailable - Tags if the user can and wants to receive e-mail through [[Special:Emailuser]]',
309 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
311 'users' => 'A list of users to obtain the same information for',
312 'token' => 'Which tokens to obtain for each user',
316 public function getDescription() {
317 return 'Get information about a list of users';
320 public function getExamples() {
321 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
324 public function getHelpUrls() {
325 return 'http://www.mediawiki.org/wiki/API:Users';
328 public function getVersion() {
329 return __CLASS__ . ': $Id$';