Merge "Add additional information to FileRepo::getInfo"
[mediawiki.git] / includes / api / ApiQueryUsers.php
blobf22140a0df846bd1e0fd15b394a56a9673576f1a
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 ) );
62 return $this->tokenFunctions;
65 /**
66 * @param $user User
67 * @return String
69 public static function getUserrightsToken( $user ) {
70 global $wgUser;
72 // Since the permissions check for userrights is non-trivial,
73 // don't bother with it here
74 return $wgUser->getEditToken( $user->getName() );
77 public function execute() {
78 $params = $this->extractRequestParams();
80 if ( !is_null( $params['prop'] ) ) {
81 $this->prop = array_flip( $params['prop'] );
82 } else {
83 $this->prop = array();
86 $users = (array)$params['users'];
87 $goodNames = $done = array();
88 $result = $this->getResult();
89 // Canonicalize user names
90 foreach ( $users as $u ) {
91 $n = User::getCanonicalName( $u );
92 if ( $n === false || $n === '' ) {
93 $vals = array( 'name' => $u, 'invalid' => '' );
94 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
95 null, $vals );
96 if ( !$fit ) {
97 $this->setContinueEnumParameter( 'users',
98 implode( '|', array_diff( $users, $done ) ) );
99 $goodNames = array();
100 break;
102 $done[] = $u;
103 } else {
104 $goodNames[] = $n;
108 $result = $this->getResult();
110 if ( count( $goodNames ) ) {
111 $this->addTables( 'user' );
112 $this->addFields( User::selectFields() );
113 $this->addWhereFld( 'user_name', $goodNames );
115 $this->showHiddenUsersAddBlockInfo( isset( $this->prop['blockinfo'] ) );
117 $data = array();
118 $res = $this->select( __METHOD__ );
119 $this->resetQueryParams();
121 // get user groups if needed
122 if ( isset( $this->prop['groups'] ) || isset( $this->prop['rights'] ) ) {
123 $userGroups = array();
125 $this->addTables( 'user' );
126 $this->addWhereFld( 'user_name', $goodNames );
127 $this->addTables( 'user_groups' );
128 $this->addJoinConds( array( 'user_groups' => array( 'INNER JOIN', 'ug_user=user_id' ) ) );
129 $this->addFields( array( 'user_name', 'ug_group' ) );
130 $userGroupsRes = $this->select( __METHOD__ );
132 foreach ( $userGroupsRes as $row ) {
133 $userGroups[$row->user_name][] = $row->ug_group;
137 foreach ( $res as $row ) {
138 // create user object and pass along $userGroups if set
139 // that reduces the number of database queries needed in User dramatically
140 if ( !isset( $userGroups ) ) {
141 $user = User::newFromRow( $row );
142 } else {
143 if ( !isset( $userGroups[$row->user_name] ) || !is_array( $userGroups[$row->user_name] ) ) {
144 $userGroups[$row->user_name] = array();
146 $user = User::newFromRow( $row, array( 'user_groups' => $userGroups[$row->user_name] ) );
148 $name = $user->getName();
150 $data[$name]['userid'] = $user->getId();
151 $data[$name]['name'] = $name;
153 if ( isset( $this->prop['editcount'] ) ) {
154 $data[$name]['editcount'] = $user->getEditCount();
157 if ( isset( $this->prop['registration'] ) ) {
158 $data[$name]['registration'] = wfTimestampOrNull( TS_ISO_8601, $user->getRegistration() );
161 if ( isset( $this->prop['groups'] ) ) {
162 $data[$name]['groups'] = $user->getEffectiveGroups();
165 if ( isset( $this->prop['implicitgroups'] ) ) {
166 $data[$name]['implicitgroups'] = $user->getAutomaticGroups();
169 if ( isset( $this->prop['rights'] ) ) {
170 $data[$name]['rights'] = $user->getRights();
172 if ( $row->ipb_deleted ) {
173 $data[$name]['hidden'] = '';
175 if ( isset( $this->prop['blockinfo'] ) && !is_null( $row->ipb_by_text ) ) {
176 $data[$name]['blockid'] = $row->ipb_id;
177 $data[$name]['blockedby'] = $row->ipb_by_text;
178 $data[$name]['blockedbyid'] = $row->ipb_by;
179 $data[$name]['blockreason'] = $row->ipb_reason;
180 $data[$name]['blockexpiry'] = $row->ipb_expiry;
183 if ( isset( $this->prop['emailable'] ) && $user->canReceiveEmail() ) {
184 $data[$name]['emailable'] = '';
187 if ( isset( $this->prop['gender'] ) ) {
188 $gender = $user->getOption( 'gender' );
189 if ( strval( $gender ) === '' ) {
190 $gender = 'unknown';
192 $data[$name]['gender'] = $gender;
195 if ( !is_null( $params['token'] ) ) {
196 $tokenFunctions = $this->getTokenFunctions();
197 foreach ( $params['token'] as $t ) {
198 $val = call_user_func( $tokenFunctions[$t], $user );
199 if ( $val === false ) {
200 $this->setWarning( "Action '$t' is not allowed for the current user" );
201 } else {
202 $data[$name][$t . 'token'] = $val;
209 $context = $this->getContext();
210 // Second pass: add result data to $retval
211 foreach ( $goodNames as $u ) {
212 if ( !isset( $data[$u] ) ) {
213 $data[$u] = array( 'name' => $u );
214 $urPage = new UserrightsPage;
215 $urPage->setContext( $context );
216 $iwUser = $urPage->fetchUser( $u );
218 if ( $iwUser instanceof UserRightsProxy ) {
219 $data[$u]['interwiki'] = '';
221 if ( !is_null( $params['token'] ) ) {
222 $tokenFunctions = $this->getTokenFunctions();
224 foreach ( $params['token'] as $t ) {
225 $val = call_user_func( $tokenFunctions[$t], $iwUser );
226 if ( $val === false ) {
227 $this->setWarning( "Action '$t' is not allowed for the current user" );
228 } else {
229 $data[$u][$t . 'token'] = $val;
233 } else {
234 $data[$u]['missing'] = '';
236 } else {
237 if ( isset( $this->prop['groups'] ) && isset( $data[$u]['groups'] ) ) {
238 $result->setIndexedTagName( $data[$u]['groups'], 'g' );
240 if ( isset( $this->prop['implicitgroups'] ) && isset( $data[$u]['implicitgroups'] ) ) {
241 $result->setIndexedTagName( $data[$u]['implicitgroups'], 'g' );
243 if ( isset( $this->prop['rights'] ) && isset( $data[$u]['rights'] ) ) {
244 $result->setIndexedTagName( $data[$u]['rights'], 'r' );
248 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
249 null, $data[$u] );
250 if ( !$fit ) {
251 $this->setContinueEnumParameter( 'users',
252 implode( '|', array_diff( $users, $done ) ) );
253 break;
255 $done[] = $u;
257 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'user' );
261 * Gets all the groups that a user is automatically a member of (implicit groups)
263 * @deprecated since 1.20; call User::getAutomaticGroups() directly.
264 * @param $user User
265 * @return array
267 public static function getAutoGroups( $user ) {
268 wfDeprecated( __METHOD__, '1.20' );
270 return $user->getAutomaticGroups();
273 public function getCacheMode( $params ) {
274 if ( isset( $params['token'] ) ) {
275 return 'private';
276 } else {
277 return 'anon-public-user-private';
281 public function getAllowedParams() {
282 return array(
283 'prop' => array(
284 ApiBase::PARAM_DFLT => null,
285 ApiBase::PARAM_ISMULTI => true,
286 ApiBase::PARAM_TYPE => array(
287 'blockinfo',
288 'groups',
289 'implicitgroups',
290 'rights',
291 'editcount',
292 'registration',
293 'emailable',
294 'gender',
297 'users' => array(
298 ApiBase::PARAM_ISMULTI => true
300 'token' => array(
301 ApiBase::PARAM_TYPE => array_keys( $this->getTokenFunctions() ),
302 ApiBase::PARAM_ISMULTI => true
307 public function getParamDescription() {
308 return array(
309 'prop' => array(
310 'What pieces of information to include',
311 ' blockinfo - Tags if the user is blocked, by whom, and for what reason',
312 ' groups - Lists all the groups the user(s) belongs to',
313 ' implicitgroups - Lists all the groups a user is automatically a member of',
314 ' rights - Lists all the rights the user(s) has',
315 ' editcount - Adds the user\'s edit count',
316 ' registration - Adds the user\'s registration timestamp',
317 ' emailable - Tags if the user can and wants to receive ' .
318 'email through [[Special:Emailuser]]',
319 ' gender - Tags the gender of the user. Returns "male", "female", or "unknown"',
321 'users' => 'A list of users to obtain the same information for',
322 'token' => 'Which tokens to obtain for each user',
326 public function getResultProperties() {
327 $props = array(
328 '' => array(
329 'userid' => array(
330 ApiBase::PROP_TYPE => 'integer',
331 ApiBase::PROP_NULLABLE => true
333 'name' => 'string',
334 'invalid' => 'boolean',
335 'hidden' => 'boolean',
336 'interwiki' => 'boolean',
337 'missing' => 'boolean'
339 'editcount' => array(
340 'editcount' => array(
341 ApiBase::PROP_TYPE => 'integer',
342 ApiBase::PROP_NULLABLE => true
345 'registration' => array(
346 'registration' => array(
347 ApiBase::PROP_TYPE => 'timestamp',
348 ApiBase::PROP_NULLABLE => true
351 'blockinfo' => array(
352 'blockid' => array(
353 ApiBase::PROP_TYPE => 'integer',
354 ApiBase::PROP_NULLABLE => true
356 'blockedby' => array(
357 ApiBase::PROP_TYPE => 'string',
358 ApiBase::PROP_NULLABLE => true
360 'blockedbyid' => array(
361 ApiBase::PROP_TYPE => 'integer',
362 ApiBase::PROP_NULLABLE => true
364 'blockedreason' => array(
365 ApiBase::PROP_TYPE => 'string',
366 ApiBase::PROP_NULLABLE => true
368 'blockedexpiry' => array(
369 ApiBase::PROP_TYPE => 'timestamp',
370 ApiBase::PROP_NULLABLE => true
373 'emailable' => array(
374 'emailable' => 'boolean'
376 'gender' => array(
377 'gender' => array(
378 ApiBase::PROP_TYPE => array(
379 'male',
380 'female',
381 'unknown'
383 ApiBase::PROP_NULLABLE => true
388 self::addTokenProperties( $props, $this->getTokenFunctions() );
390 return $props;
393 public function getDescription() {
394 return 'Get information about a list of users';
397 public function getExamples() {
398 return 'api.php?action=query&list=users&ususers=brion|TimStarling&usprop=groups|editcount|gender';
401 public function getHelpUrls() {
402 return 'https://www.mediawiki.org/wiki/API:Users';