Fixed spacing in db/debug/diff/externalstore/objectcache folder
[mediawiki.git] / includes / api / ApiQueryAllUsers.php
blob1948a51af837b2db5fe6d9f8901d9758ada3656c
1 <?php
2 /**
5 * Created on July 7, 2007
7 * Copyright © 2007 Yuri Astrakhan "<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 enumerate all registered users.
30 * @ingroup API
32 class ApiQueryAllUsers extends ApiQueryBase {
33 public function __construct( $query, $moduleName ) {
34 parent::__construct( $query, $moduleName, 'au' );
37 /**
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
40 * @param string $name
41 * @return String
43 private function getCanonicalUserName( $name ) {
44 return str_replace( '_', ' ', $name );
47 public function execute() {
48 $db = $this->getDB();
49 $params = $this->extractRequestParams();
51 $prop = $params['prop'];
52 if ( !is_null( $prop ) ) {
53 $prop = array_flip( $prop );
54 $fld_blockinfo = isset( $prop['blockinfo'] );
55 $fld_editcount = isset( $prop['editcount'] );
56 $fld_groups = isset( $prop['groups'] );
57 $fld_rights = isset( $prop['rights'] );
58 $fld_registration = isset( $prop['registration'] );
59 $fld_implicitgroups = isset( $prop['implicitgroups'] );
60 } else {
61 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration = $fld_rights = $fld_implicitgroups = false;
64 $limit = $params['limit'];
66 $this->addTables( 'user' );
67 $useIndex = true;
69 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
70 $from = is_null( $params['from'] ) ? null : $this->getCanonicalUserName( $params['from'] );
71 $to = is_null( $params['to'] ) ? null : $this->getCanonicalUserName( $params['to'] );
73 # MySQL doesn't seem to use 'equality propagation' here, so like the
74 # ActiveUsers special page, we have to use rc_user_text for some cases.
75 $userFieldToSort = $params['activeusers'] ? 'rc_user_text' : 'user_name';
77 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
79 if ( !is_null( $params['prefix'] ) ) {
80 $this->addWhere( $userFieldToSort .
81 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
84 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
85 $groups = array();
86 foreach ( $params['rights'] as $r ) {
87 $groups = array_merge( $groups, User::getGroupsWithPermission( $r ) );
90 // no group with the given right(s) exists, no need for a query
91 if ( !count( $groups ) ) {
92 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), '' );
93 return;
96 $groups = array_unique( $groups );
98 if ( is_null( $params['group'] ) ) {
99 $params['group'] = $groups;
100 } else {
101 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
105 if ( !is_null( $params['group'] ) && !is_null( $params['excludegroup'] ) ) {
106 $this->dieUsage( 'group and excludegroup cannot be used together', 'group-excludegroup' );
109 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
110 $useIndex = false;
111 // Filter only users that belong to a given group
112 $this->addTables( 'user_groups', 'ug1' );
113 $this->addJoinConds( array( 'ug1' => array( 'INNER JOIN', array( 'ug1.ug_user=user_id',
114 'ug1.ug_group' => $params['group'] ) ) ) );
117 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
118 $useIndex = false;
119 // Filter only users don't belong to a given group
120 $this->addTables( 'user_groups', 'ug1' );
122 if ( count( $params['excludegroup'] ) == 1 ) {
123 $exclude = array( 'ug1.ug_group' => $params['excludegroup'][0] );
124 } else {
125 $exclude = array( $db->makeList( array( 'ug1.ug_group' => $params['excludegroup'] ), LIST_OR ) );
127 $this->addJoinConds( array( 'ug1' => array( 'LEFT OUTER JOIN',
128 array_merge( array( 'ug1.ug_user=user_id' ), $exclude )
130 ) );
131 $this->addWhere( 'ug1.ug_user IS NULL' );
134 if ( $params['witheditsonly'] ) {
135 $this->addWhere( 'user_editcount > 0' );
138 $this->showHiddenUsersAddBlockInfo( $fld_blockinfo );
140 if ( $fld_groups || $fld_rights ) {
141 // Show the groups the given users belong to
142 // request more than needed to avoid not getting all rows that belong to one user
143 $groupCount = count( User::getAllGroups() );
144 $sqlLimit = $limit + $groupCount + 1;
146 $this->addTables( 'user_groups', 'ug2' );
147 $this->addJoinConds( array( 'ug2' => array( 'LEFT JOIN', 'ug2.ug_user=user_id' ) ) );
148 $this->addFields( 'ug2.ug_group ug_group2' );
149 } else {
150 $sqlLimit = $limit + 1;
153 if ( $params['activeusers'] ) {
154 global $wgActiveUserDays;
155 $this->addTables( 'recentchanges' );
157 $this->addJoinConds( array( 'recentchanges' => array(
158 'INNER JOIN', 'rc_user_text=user_name'
159 ) ) );
161 $this->addFields( array( 'recentedits' => 'COUNT(*)' ) );
163 $this->addWhere( 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ) );
164 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX ) - $wgActiveUserDays * 24 * 3600 );
165 $this->addWhere( 'rc_timestamp >= ' . $db->addQuotes( $timestamp ) );
167 $this->addOption( 'GROUP BY', $userFieldToSort );
170 $this->addOption( 'LIMIT', $sqlLimit );
172 $this->addFields( array(
173 'user_name',
174 'user_id'
175 ) );
176 $this->addFieldsIf( 'user_editcount', $fld_editcount );
177 $this->addFieldsIf( 'user_registration', $fld_registration );
179 if ( $useIndex ) {
180 $this->addOption( 'USE INDEX', array( 'user' => 'user_name' ) );
183 $res = $this->select( __METHOD__ );
185 $count = 0;
186 $lastUserData = false;
187 $lastUser = false;
188 $result = $this->getResult();
191 // This loop keeps track of the last entry.
192 // For each new row, if the new row is for different user then the last, the last entry is added to results.
193 // Otherwise, the group of the new row is appended to the last entry.
194 // The setContinue... is more complex because of this, and takes into account the higher sql limit
195 // to make sure all rows that belong to the same user are received.
197 foreach ( $res as $row ) {
198 $count++;
200 if ( $lastUser !== $row->user_name ) {
201 // Save the last pass's user data
202 if ( is_array( $lastUserData ) ) {
203 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
204 null, $lastUserData );
206 $lastUserData = null;
208 if ( !$fit ) {
209 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
210 break;
214 if ( $count > $limit ) {
215 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
216 $this->setContinueEnumParameter( 'from', $row->user_name );
217 break;
220 // Record new user's data
221 $lastUser = $row->user_name;
222 $lastUserData = array(
223 'userid' => $row->user_id,
224 'name' => $lastUser,
226 if ( $fld_blockinfo && !is_null( $row->ipb_by_text ) ) {
227 $lastUserData['blockid'] = $row->ipb_id;
228 $lastUserData['blockedby'] = $row->ipb_by_text;
229 $lastUserData['blockedbyid'] = $row->ipb_by;
230 $lastUserData['blockreason'] = $row->ipb_reason;
231 $lastUserData['blockexpiry'] = $row->ipb_expiry;
233 if ( $row->ipb_deleted ) {
234 $lastUserData['hidden'] = '';
236 if ( $fld_editcount ) {
237 $lastUserData['editcount'] = intval( $row->user_editcount );
239 if ( $params['activeusers'] ) {
240 $lastUserData['recenteditcount'] = intval( $row->recentedits );
242 if ( $fld_registration ) {
243 $lastUserData['registration'] = $row->user_registration ?
244 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
248 if ( $sqlLimit == $count ) {
249 // BUG! database contains group name that User::getAllGroups() does not return
250 // TODO: should handle this more gracefully
251 ApiBase::dieDebug( __METHOD__,
252 'MediaWiki configuration error: the database contains more user groups than known to User::getAllGroups() function' );
255 $lastUserObj = User::newFromId( $row->user_id );
257 // Add user's group info
258 if ( $fld_groups ) {
259 if ( !isset( $lastUserData['groups'] ) ) {
260 if ( $lastUserObj ) {
261 $lastUserData['groups'] = $lastUserObj->getAutomaticGroups();
262 } else {
263 // This should not normally happen
264 $lastUserData['groups'] = array();
268 if ( !is_null( $row->ug_group2 ) ) {
269 $lastUserData['groups'][] = $row->ug_group2;
272 $result->setIndexedTagName( $lastUserData['groups'], 'g' );
275 if ( $fld_implicitgroups && !isset( $lastUserData['implicitgroups'] ) && $lastUserObj ) {
276 $lastUserData['implicitgroups'] = $lastUserObj->getAutomaticGroups();
277 $result->setIndexedTagName( $lastUserData['implicitgroups'], 'g' );
279 if ( $fld_rights ) {
280 if ( !isset( $lastUserData['rights'] ) ) {
281 if ( $lastUserObj ) {
282 $lastUserData['rights'] = User::getGroupPermissions( $lastUserObj->getAutomaticGroups() );
283 } else {
284 // This should not normally happen
285 $lastUserData['rights'] = array();
289 if ( !is_null( $row->ug_group2 ) ) {
290 $lastUserData['rights'] = array_unique( array_merge( $lastUserData['rights'],
291 User::getGroupPermissions( array( $row->ug_group2 ) ) ) );
294 $result->setIndexedTagName( $lastUserData['rights'], 'r' );
298 if ( is_array( $lastUserData ) ) {
299 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
300 null, $lastUserData );
301 if ( !$fit ) {
302 $this->setContinueEnumParameter( 'from', $lastUserData['name'] );
306 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'u' );
309 public function getCacheMode( $params ) {
310 return 'anon-public-user-private';
313 public function getAllowedParams() {
314 $userGroups = User::getAllGroups();
315 return array(
316 'from' => null,
317 'to' => null,
318 'prefix' => null,
319 'dir' => array(
320 ApiBase::PARAM_DFLT => 'ascending',
321 ApiBase::PARAM_TYPE => array(
322 'ascending',
323 'descending'
326 'group' => array(
327 ApiBase::PARAM_TYPE => $userGroups,
328 ApiBase::PARAM_ISMULTI => true,
330 'excludegroup' => array(
331 ApiBase::PARAM_TYPE => $userGroups,
332 ApiBase::PARAM_ISMULTI => true,
334 'rights' => array(
335 ApiBase::PARAM_TYPE => User::getAllRights(),
336 ApiBase::PARAM_ISMULTI => true,
338 'prop' => array(
339 ApiBase::PARAM_ISMULTI => true,
340 ApiBase::PARAM_TYPE => array(
341 'blockinfo',
342 'groups',
343 'implicitgroups',
344 'rights',
345 'editcount',
346 'registration'
349 'limit' => array(
350 ApiBase::PARAM_DFLT => 10,
351 ApiBase::PARAM_TYPE => 'limit',
352 ApiBase::PARAM_MIN => 1,
353 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
354 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
356 'witheditsonly' => false,
357 'activeusers' => false,
361 public function getParamDescription() {
362 global $wgActiveUserDays;
363 return array(
364 'from' => 'The user name to start enumerating from',
365 'to' => 'The user name to stop enumerating at',
366 'prefix' => 'Search for all users that begin with this value',
367 'dir' => 'Direction to sort in',
368 'group' => 'Limit users to given group name(s)',
369 'excludegroup' => 'Exclude users in given group name(s)',
370 'rights' => 'Limit users to given right(s) (does not include rights granted by implicit or auto-promoted groups like *, user, or autoconfirmed)',
371 'prop' => array(
372 'What pieces of information to include.',
373 ' blockinfo - Adds the information about a current block on the user',
374 ' groups - Lists groups that the user is in. This uses more server resources and may return fewer results than the limit',
375 ' implicitgroups - Lists all the groups the user is automatically in',
376 ' rights - Lists rights that the user has',
377 ' editcount - Adds the edit count of the user',
378 ' registration - Adds the timestamp of when the user registered if available (may be blank)',
380 'limit' => 'How many total user names to return',
381 'witheditsonly' => 'Only list users who have made edits',
382 'activeusers' => "Only list users active in the last {$wgActiveUserDays} days(s)"
386 public function getResultProperties() {
387 return array(
388 '' => array(
389 'userid' => 'integer',
390 'name' => 'string',
391 'recenteditcount' => array(
392 ApiBase::PROP_TYPE => 'integer',
393 ApiBase::PROP_NULLABLE => true
396 'blockinfo' => array(
397 'blockid' => array(
398 ApiBase::PROP_TYPE => 'integer',
399 ApiBase::PROP_NULLABLE => true
401 'blockedby' => array(
402 ApiBase::PROP_TYPE => 'string',
403 ApiBase::PROP_NULLABLE => true
405 'blockedbyid' => array(
406 ApiBase::PROP_TYPE => 'integer',
407 ApiBase::PROP_NULLABLE => true
409 'blockedreason' => array(
410 ApiBase::PROP_TYPE => 'string',
411 ApiBase::PROP_NULLABLE => true
413 'blockedexpiry' => array(
414 ApiBase::PROP_TYPE => 'string',
415 ApiBase::PROP_NULLABLE => true
417 'hidden' => 'boolean'
419 'editcount' => array(
420 'editcount' => 'integer'
422 'registration' => array(
423 'registration' => 'string'
428 public function getDescription() {
429 return 'Enumerate all registered users';
432 public function getPossibleErrors() {
433 return array_merge( parent::getPossibleErrors(), array(
434 array( 'code' => 'group-excludegroup', 'info' => 'group and excludegroup cannot be used together' ),
435 ) );
438 public function getExamples() {
439 return array(
440 'api.php?action=query&list=allusers&aufrom=Y',
444 public function getHelpUrls() {
445 return 'https://www.mediawiki.org/wiki/API:Allusers';