3 * Copyright © 2007 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
23 use MediaWiki\Block\DatabaseBlock
;
26 * Query module to enumerate all registered users.
30 class ApiQueryAllUsers
extends ApiQueryBase
{
31 use ApiQueryBlockInfoTrait
;
33 public function __construct( ApiQuery
$query, $moduleName ) {
34 parent
::__construct( $query, $moduleName, 'au' );
38 * This function converts the user name to a canonical form
39 * which is stored in the database.
43 private function getCanonicalUserName( $name ) {
44 return strtr( $name, '_', ' ' );
47 public function execute() {
48 $params = $this->extractRequestParams();
49 $activeUserDays = $this->getConfig()->get( 'ActiveUserDays' );
52 $commentStore = CommentStore
::getStore();
54 $prop = $params['prop'];
55 if ( !is_null( $prop ) ) {
56 $prop = array_flip( $prop );
57 $fld_blockinfo = isset( $prop['blockinfo'] );
58 $fld_editcount = isset( $prop['editcount'] );
59 $fld_groups = isset( $prop['groups'] );
60 $fld_rights = isset( $prop['rights'] );
61 $fld_registration = isset( $prop['registration'] );
62 $fld_implicitgroups = isset( $prop['implicitgroups'] );
63 $fld_centralids = isset( $prop['centralids'] );
65 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
66 $fld_rights = $fld_implicitgroups = $fld_centralids = false;
69 $limit = $params['limit'];
71 $this->addTables( 'user' );
73 $dir = ( $params['dir'] == 'descending' ?
'older' : 'newer' );
74 $from = is_null( $params['from'] ) ?
null : $this->getCanonicalUserName( $params['from'] );
75 $to = is_null( $params['to'] ) ?
null : $this->getCanonicalUserName( $params['to'] );
77 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
78 # despite the JOIN condition, so manually sort on the correct one.
79 $userFieldToSort = $params['activeusers'] ?
'qcc_title' : 'user_name';
81 # Some of these subtable joins are going to give us duplicate rows, so
82 # calculate the maximum number of duplicates we might see.
83 $maxDuplicateRows = 1;
85 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
87 if ( !is_null( $params['prefix'] ) ) {
88 $this->addWhere( $userFieldToSort .
89 $db->buildLike( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() ) );
92 if ( !is_null( $params['rights'] ) && count( $params['rights'] ) ) {
94 foreach ( $params['rights'] as $r ) {
95 $groups = array_merge( $groups, $this->getPermissionManager()
96 ->getGroupsWithPermission( $r ) );
99 // no group with the given right(s) exists, no need for a query
100 if ( $groups === [] ) {
101 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
106 $groups = array_unique( $groups );
108 if ( is_null( $params['group'] ) ) {
109 $params['group'] = $groups;
111 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
115 $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
117 if ( !is_null( $params['group'] ) && count( $params['group'] ) ) {
118 // Filter only users that belong to a given group. This might
119 // produce as many rows-per-user as there are groups being checked.
120 $this->addTables( 'user_groups', 'ug1' );
121 $this->addJoinConds( [
125 'ug1.ug_user=user_id',
126 'ug1.ug_group' => $params['group'],
127 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
131 $maxDuplicateRows *= count( $params['group'] );
134 if ( !is_null( $params['excludegroup'] ) && count( $params['excludegroup'] ) ) {
135 // Filter only users don't belong to a given group. This can only
136 // produce one row-per-user, because we only keep on "no match".
137 $this->addTables( 'user_groups', 'ug1' );
139 if ( count( $params['excludegroup'] ) == 1 ) {
140 $exclude = [ 'ug1.ug_group' => $params['excludegroup'][0] ];
142 $exclude = [ $db->makeList(
143 [ 'ug1.ug_group' => $params['excludegroup'] ],
147 $this->addJoinConds( [ 'ug1' => [ 'LEFT JOIN',
149 'ug1.ug_user=user_id',
150 'ug1.ug_expiry IS NULL OR ug1.ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
153 $this->addWhere( 'ug1.ug_user IS NULL' );
156 if ( $params['witheditsonly'] ) {
157 $this->addWhere( 'user_editcount > 0' );
160 $this->addBlockInfoToQuery( $fld_blockinfo );
162 if ( $fld_groups ||
$fld_rights ) {
163 $this->addFields( [ 'groups' =>
164 $db->buildGroupConcatField( '|', 'user_groups', 'ug_group', [
166 'ug_expiry IS NULL OR ug_expiry >= ' . $db->addQuotes( $db->timestamp() )
171 if ( $params['activeusers'] ) {
172 $activeUserSeconds = $activeUserDays * 86400;
174 // Filter query to only include users in the active users cache.
175 // There shouldn't be any duplicate rows in querycachetwo here.
176 $this->addTables( 'querycachetwo' );
177 $this->addJoinConds( [ 'querycachetwo' => [
179 'qcc_type' => 'activeusers',
180 'qcc_namespace' => NS_USER
,
181 'qcc_title=user_name',
185 // Actually count the actions using a subquery (T66505 and T66507)
186 $tables = [ 'recentchanges', 'actor' ];
188 'actor' => [ 'JOIN', 'rc_actor = actor_id' ],
190 $timestamp = $db->timestamp( wfTimestamp( TS_UNIX
) - $activeUserSeconds );
192 'recentactions' => '(' . $db->selectSQLText(
196 'actor_user = user_id',
197 'rc_type != ' . $db->addQuotes( RC_EXTERNAL
), // no wikidata
198 'rc_log_type IS NULL OR rc_log_type != ' . $db->addQuotes( 'newusers' ),
199 'rc_timestamp >= ' . $db->addQuotes( $timestamp ),
208 $sqlLimit = $limit +
$maxDuplicateRows;
209 $this->addOption( 'LIMIT', $sqlLimit );
215 $this->addFieldsIf( 'user_editcount', $fld_editcount );
216 $this->addFieldsIf( 'user_registration', $fld_registration );
218 $res = $this->select( __METHOD__
);
220 $countDuplicates = 0;
222 $result = $this->getResult();
223 foreach ( $res as $row ) {
226 if ( $lastUser === $row->user_name
) {
227 // Duplicate row due to one of the needed subtable joins.
228 // Ignore it, but count the number of them to sanely handle
229 // miscalculation of $maxDuplicateRows.
231 if ( $countDuplicates == $maxDuplicateRows ) {
232 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
237 $countDuplicates = 0;
238 $lastUser = $row->user_name
;
240 if ( $count > $limit ) {
241 // We've reached the one extra which shows that there are
242 // additional pages to be had. Stop here...
243 $this->setContinueEnumParameter( 'from', $row->user_name
);
247 if ( $count == $sqlLimit ) {
248 // Should never hit this (either the $countDuplicates check or
249 // the $count > $limit check should hit first), but check it
250 // anyway just in case.
251 ApiBase
::dieDebug( __METHOD__
, 'Saw more duplicate rows than expected' );
254 if ( $params['activeusers'] && $row->recentactions
=== 0 ) {
255 // activeusers cache was out of date
260 'userid' => (int)$row->user_id
,
261 'name' => $row->user_name
,
264 if ( $fld_centralids ) {
265 $data +
= ApiQueryUserInfo
::getCentralUserInfo(
266 $this->getConfig(), User
::newFromId( $row->user_id
), $params['attachedwiki']
270 if ( $fld_blockinfo && !is_null( $row->ipb_id
) ) {
271 $data +
= $this->getBlockDetails( DatabaseBlock
::newFromRow( $row ) );
273 if ( $row->ipb_deleted
) {
274 $data['hidden'] = true;
276 if ( $fld_editcount ) {
277 $data['editcount'] = (int)$row->user_editcount
;
279 if ( $params['activeusers'] ) {
280 $data['recentactions'] = (int)$row->recentactions
;
282 if ( $fld_registration ) {
283 $data['registration'] = $row->user_registration ?
284 wfTimestamp( TS_ISO_8601
, $row->user_registration
) : '';
287 if ( $fld_implicitgroups ||
$fld_groups ||
$fld_rights ) {
288 $implicitGroups = User
::newFromId( $row->user_id
)->getAutomaticGroups();
289 if ( isset( $row->groups
) && $row->groups
!== '' ) {
290 $groups = array_merge( $implicitGroups, explode( '|', $row->groups
) );
292 $groups = $implicitGroups;
296 $data['groups'] = $groups;
297 ApiResult
::setIndexedTagName( $data['groups'], 'g' );
298 ApiResult
::setArrayType( $data['groups'], 'array' );
301 if ( $fld_implicitgroups ) {
302 $data['implicitgroups'] = $implicitGroups;
303 ApiResult
::setIndexedTagName( $data['implicitgroups'], 'g' );
304 ApiResult
::setArrayType( $data['implicitgroups'], 'array' );
308 $data['rights'] = $this->getPermissionManager()->getGroupPermissions( $groups );
309 ApiResult
::setIndexedTagName( $data['rights'], 'r' );
310 ApiResult
::setArrayType( $data['rights'], 'array' );
314 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
316 $this->setContinueEnumParameter( 'from', $data['name'] );
321 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
324 public function getCacheMode( $params ) {
325 return 'anon-public-user-private';
328 public function getAllowedParams() {
329 $userGroups = User
::getAllGroups();
336 ApiBase
::PARAM_DFLT
=> 'ascending',
337 ApiBase
::PARAM_TYPE
=> [
343 ApiBase
::PARAM_TYPE
=> $userGroups,
344 ApiBase
::PARAM_ISMULTI
=> true,
347 ApiBase
::PARAM_TYPE
=> $userGroups,
348 ApiBase
::PARAM_ISMULTI
=> true,
351 ApiBase
::PARAM_TYPE
=> $this->getPermissionManager()->getAllPermissions(),
352 ApiBase
::PARAM_ISMULTI
=> true,
355 ApiBase
::PARAM_ISMULTI
=> true,
356 ApiBase
::PARAM_TYPE
=> [
365 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
368 ApiBase
::PARAM_DFLT
=> 10,
369 ApiBase
::PARAM_TYPE
=> 'limit',
370 ApiBase
::PARAM_MIN
=> 1,
371 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
372 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
374 'witheditsonly' => false,
376 ApiBase
::PARAM_DFLT
=> false,
377 ApiBase
::PARAM_HELP_MSG
=> [
378 'apihelp-query+allusers-param-activeusers',
379 $this->getConfig()->get( 'ActiveUserDays' )
382 'attachedwiki' => null,
386 protected function getExamplesMessages() {
388 'action=query&list=allusers&aufrom=Y'
389 => 'apihelp-query+allusers-example-y',
393 public function getHelpUrls() {
394 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';