Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiQueryAllUsers.php
blob340c0485996c064d227b00b09d557b33ebb190cf
1 <?php
2 /**
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
20 * @file
23 namespace MediaWiki\Api;
25 use MediaWiki\Language\Language;
26 use MediaWiki\MainConfigNames;
27 use MediaWiki\Permissions\GroupPermissionsLookup;
28 use MediaWiki\User\TempUser\TempUserConfig;
29 use MediaWiki\User\UserFactory;
30 use MediaWiki\User\UserGroupManager;
31 use Wikimedia\ParamValidator\ParamValidator;
32 use Wikimedia\ParamValidator\TypeDef\IntegerDef;
33 use Wikimedia\Rdbms\IExpression;
34 use Wikimedia\Rdbms\LikeValue;
36 /**
37 * Query module to enumerate all registered users.
39 * @ingroup API
41 class ApiQueryAllUsers extends ApiQueryBase {
42 use ApiQueryBlockInfoTrait;
44 private UserFactory $userFactory;
45 private UserGroupManager $userGroupManager;
46 private GroupPermissionsLookup $groupPermissionsLookup;
47 private Language $contentLanguage;
48 private TempUserConfig $tempUserConfig;
50 public function __construct(
51 ApiQuery $query,
52 string $moduleName,
53 UserFactory $userFactory,
54 UserGroupManager $userGroupManager,
55 GroupPermissionsLookup $groupPermissionsLookup,
56 Language $contentLanguage,
57 TempUserConfig $tempUserConfig
58 ) {
59 parent::__construct( $query, $moduleName, 'au' );
60 $this->userFactory = $userFactory;
61 $this->userGroupManager = $userGroupManager;
62 $this->groupPermissionsLookup = $groupPermissionsLookup;
63 $this->contentLanguage = $contentLanguage;
64 $this->tempUserConfig = $tempUserConfig;
67 /**
68 * This function converts the user name to a canonical form
69 * which is stored in the database.
70 * @param string $name
71 * @return string
73 private function getCanonicalUserName( $name ) {
74 $name = $this->contentLanguage->ucfirst( $name );
75 return strtr( $name, '_', ' ' );
78 public function execute() {
79 $params = $this->extractRequestParams();
80 $activeUserDays = $this->getConfig()->get( MainConfigNames::ActiveUserDays );
82 $db = $this->getDB();
84 $prop = $params['prop'];
85 if ( $prop !== null ) {
86 $prop = array_fill_keys( $prop, true );
87 $fld_blockinfo = isset( $prop['blockinfo'] );
88 $fld_editcount = isset( $prop['editcount'] );
89 $fld_groups = isset( $prop['groups'] );
90 $fld_rights = isset( $prop['rights'] );
91 $fld_registration = isset( $prop['registration'] );
92 $fld_implicitgroups = isset( $prop['implicitgroups'] );
93 $fld_centralids = isset( $prop['centralids'] );
94 } else {
95 $fld_blockinfo = $fld_editcount = $fld_groups = $fld_registration =
96 $fld_rights = $fld_implicitgroups = $fld_centralids = false;
99 $limit = $params['limit'];
101 $this->addTables( 'user' );
103 $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
104 $from = $params['from'] === null ? null : $this->getCanonicalUserName( $params['from'] );
105 $to = $params['to'] === null ? null : $this->getCanonicalUserName( $params['to'] );
107 # MySQL can't figure out that 'user_name' and 'qcc_title' are the same
108 # despite the JOIN condition, so manually sort on the correct one.
109 $userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name';
111 # Some of these subtable joins are going to give us duplicate rows, so
112 # calculate the maximum number of duplicates we might see.
113 $maxDuplicateRows = 1;
115 $this->addWhereRange( $userFieldToSort, $dir, $from, $to );
117 if ( $params['prefix'] !== null ) {
118 $this->addWhere(
119 $db->expr(
120 $userFieldToSort,
121 IExpression::LIKE,
122 new LikeValue( $this->getCanonicalUserName( $params['prefix'] ), $db->anyString() )
127 $excludeNamed = $params['excludenamed'];
128 $excludeTemp = $params['excludetemp'];
130 if ( $this->tempUserConfig->isKnown() ) {
131 if ( $excludeTemp ) {
132 $this->addWhere(
133 $this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::NOT_LIKE )
136 if ( $excludeNamed ) {
137 $this->addWhere(
138 $this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::LIKE )
143 if ( $params['rights'] !== null && count( $params['rights'] ) ) {
144 $groups = [];
145 // TODO: this does not properly account for $wgRevokePermissions
146 foreach ( $params['rights'] as $r ) {
147 if ( in_array( $r, $this->getPermissionManager()->getImplicitRights(), true ) ) {
148 $groups[] = '*';
149 } else {
150 $groups = array_merge(
151 $groups,
152 $this->groupPermissionsLookup->getGroupsWithPermission( $r )
157 if ( $groups === [] ) {
158 // No group with the given right(s) exists, no need for a query
159 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' );
161 return;
164 $groups = array_unique( $groups );
165 if ( in_array( '*', $groups, true ) || in_array( 'user', $groups, true ) ) {
166 // All user rows logically match but there are no "*"/"user" user_groups rows
167 $groups = [];
170 if ( $params['group'] === null ) {
171 $params['group'] = $groups;
172 } else {
173 $params['group'] = array_unique( array_merge( $params['group'], $groups ) );
177 $this->requireMaxOneParameter( $params, 'group', 'excludegroup' );
179 if ( $params['group'] !== null && count( $params['group'] ) ) {
180 // Filter only users that belong to a given group. This might
181 // produce as many rows-per-user as there are groups being checked.
182 $this->addTables( 'user_groups', 'ug1' );
183 $this->addJoinConds( [
184 'ug1' => [
185 'JOIN',
187 'ug1.ug_user=user_id',
188 'ug1.ug_group' => $params['group'],
189 $db->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $db->timestamp() ),
192 ] );
193 $maxDuplicateRows *= count( $params['group'] );
196 if ( $params['excludegroup'] !== null && count( $params['excludegroup'] ) ) {
197 // Filter only users don't belong to a given group. This can only
198 // produce one row-per-user, because we only keep on "no match".
199 $this->addTables( 'user_groups', 'ug1' );
201 $this->addJoinConds( [ 'ug1' => [ 'LEFT JOIN',
203 'ug1.ug_user=user_id',
204 $db->expr( 'ug1.ug_expiry', '=', null )->or( 'ug1.ug_expiry', '>=', $db->timestamp() ),
205 'ug1.ug_group' => $params['excludegroup'],
207 ] ] );
208 $this->addWhere( [ 'ug1.ug_user' => null ] );
211 if ( $params['witheditsonly'] ) {
212 $this->addWhere( $db->expr( 'user_editcount', '>', 0 ) );
215 $this->addDeletedUserFilter();
217 if ( $fld_groups || $fld_rights ) {
218 $this->addFields( [ 'groups' =>
219 $db->newSelectQueryBuilder()
220 ->table( 'user_groups' )
221 ->field( 'ug_group' )
222 ->where( [
223 'ug_user=user_id',
224 $db->expr( 'ug_expiry', '=', null )->or( 'ug_expiry', '>=', $db->timestamp() )
226 ->buildGroupConcatField( '|' )
227 ] );
230 if ( $params['activeusers'] ) {
231 $activeUserSeconds = $activeUserDays * 86400;
233 // Filter query to only include users in the active users cache.
234 // There shouldn't be any duplicate rows in querycachetwo here.
235 $this->addTables( 'querycachetwo' );
236 $this->addJoinConds( [ 'querycachetwo' => [
237 'JOIN', [
238 'qcc_type' => 'activeusers',
239 'qcc_namespace' => NS_USER,
240 'qcc_title=user_name',
242 ] ] );
244 // Actually count the actions using a subquery (T66505 and T66507)
245 $timestamp = $db->timestamp( (int)wfTimestamp( TS_UNIX ) - $activeUserSeconds );
246 $subqueryBuilder = $db->newSelectQueryBuilder()
247 ->select( 'COUNT(*)' )
248 ->from( 'recentchanges' )
249 ->join( 'actor', null, 'rc_actor = actor_id' )
250 ->where( [
251 'actor_user = user_id',
252 $db->expr( 'rc_type', '!=', RC_EXTERNAL ), // no wikidata
253 $db->expr( 'rc_log_type', '=', null )
254 ->or( 'rc_log_type', '!=', 'newusers' ),
255 $db->expr( 'rc_timestamp', '>=', $timestamp ),
256 ] );
257 $this->addFields( [
258 'recentactions' => '(' . $subqueryBuilder->caller( __METHOD__ )->getSQL() . ')'
259 ] );
262 $sqlLimit = $limit + $maxDuplicateRows;
263 $this->addOption( 'LIMIT', $sqlLimit );
265 $this->addFields( [
266 'user_name',
267 'user_id'
268 ] );
269 $this->addFieldsIf( 'user_editcount', $fld_editcount );
270 $this->addFieldsIf( 'user_registration', $fld_registration );
272 $res = $this->select( __METHOD__ );
273 $count = 0;
274 $countDuplicates = 0;
275 $lastUser = false;
276 $result = $this->getResult();
277 $blockInfos = $fld_blockinfo ? $this->getBlockDetailsForRows( $res ) : null;
278 foreach ( $res as $row ) {
279 $count++;
281 if ( $lastUser === $row->user_name ) {
282 // Duplicate row due to one of the needed subtable joins.
283 // Ignore it, but count the number of them to sensibly handle
284 // miscalculation of $maxDuplicateRows.
285 $countDuplicates++;
286 if ( $countDuplicates == $maxDuplicateRows ) {
287 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
289 continue;
292 $countDuplicates = 0;
293 $lastUser = $row->user_name;
295 if ( $count > $limit ) {
296 // We've reached the one extra which shows that there are
297 // additional pages to be had. Stop here...
298 $this->setContinueEnumParameter( 'from', $row->user_name );
299 break;
302 if ( $count == $sqlLimit ) {
303 // Should never hit this (either the $countDuplicates check or
304 // the $count > $limit check should hit first), but check it
305 // anyway just in case.
306 ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' );
309 if ( $params['activeusers'] && (int)$row->recentactions === 0 ) {
310 // activeusers cache was out of date
311 continue;
314 $data = [
315 'userid' => (int)$row->user_id,
316 'name' => $row->user_name,
319 if ( $fld_centralids ) {
320 $data += ApiQueryUserInfo::getCentralUserInfo(
321 $this->getConfig(), $this->userFactory->newFromId( (int)$row->user_id ), $params['attachedwiki']
325 if ( $fld_blockinfo && isset( $blockInfos[$row->user_id] ) ) {
326 $data += $blockInfos[$row->user_id];
328 if ( $row->hu_deleted ) {
329 $data['hidden'] = true;
331 if ( $fld_editcount ) {
332 $data['editcount'] = (int)$row->user_editcount;
334 if ( $params['activeusers'] ) {
335 $data['recentactions'] = (int)$row->recentactions;
337 if ( $fld_registration ) {
338 $data['registration'] = $row->user_registration ?
339 wfTimestamp( TS_ISO_8601, $row->user_registration ) : '';
342 if ( $fld_implicitgroups || $fld_groups || $fld_rights ) {
343 $implicitGroups = $this->userGroupManager
344 ->getUserImplicitGroups( $this->userFactory->newFromId( (int)$row->user_id ) );
345 if ( isset( $row->groups ) && $row->groups !== '' ) {
346 $groups = array_merge( $implicitGroups, explode( '|', $row->groups ) );
347 } else {
348 $groups = $implicitGroups;
351 if ( $fld_groups ) {
352 $data['groups'] = $groups;
353 ApiResult::setIndexedTagName( $data['groups'], 'g' );
354 ApiResult::setArrayType( $data['groups'], 'array' );
357 if ( $fld_implicitgroups ) {
358 $data['implicitgroups'] = $implicitGroups;
359 ApiResult::setIndexedTagName( $data['implicitgroups'], 'g' );
360 ApiResult::setArrayType( $data['implicitgroups'], 'array' );
363 if ( $fld_rights ) {
364 $user = $this->userFactory->newFromId( (int)$row->user_id );
365 $data['rights'] = $this->getPermissionManager()->getUserPermissions( $user );
366 ApiResult::setIndexedTagName( $data['rights'], 'r' );
367 ApiResult::setArrayType( $data['rights'], 'array' );
371 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data );
372 if ( !$fit ) {
373 $this->setContinueEnumParameter( 'from', $data['name'] );
374 break;
378 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' );
381 public function getCacheMode( $params ) {
382 return 'anon-public-user-private';
385 public function getAllowedParams( $flags = 0 ) {
386 $userGroups = $this->userGroupManager->listAllGroups();
388 if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) {
389 sort( $userGroups );
392 return [
393 'from' => null,
394 'to' => null,
395 'prefix' => null,
396 'dir' => [
397 ParamValidator::PARAM_DEFAULT => 'ascending',
398 ParamValidator::PARAM_TYPE => [
399 'ascending',
400 'descending'
403 'group' => [
404 ParamValidator::PARAM_TYPE => $userGroups,
405 ParamValidator::PARAM_ISMULTI => true,
407 'excludegroup' => [
408 ParamValidator::PARAM_TYPE => $userGroups,
409 ParamValidator::PARAM_ISMULTI => true,
411 'rights' => [
412 ParamValidator::PARAM_TYPE => array_unique( array_merge(
413 $this->getPermissionManager()->getAllPermissions(),
414 $this->getPermissionManager()->getImplicitRights()
415 ) ),
416 ParamValidator::PARAM_ISMULTI => true,
418 'prop' => [
419 ParamValidator::PARAM_ISMULTI => true,
420 ParamValidator::PARAM_TYPE => [
421 'blockinfo',
422 'groups',
423 'implicitgroups',
424 'rights',
425 'editcount',
426 'registration',
427 'centralids',
429 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
431 'limit' => [
432 ParamValidator::PARAM_DEFAULT => 10,
433 ParamValidator::PARAM_TYPE => 'limit',
434 IntegerDef::PARAM_MIN => 1,
435 IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
436 IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
438 'witheditsonly' => false,
439 'activeusers' => [
440 ParamValidator::PARAM_DEFAULT => false,
441 ApiBase::PARAM_HELP_MSG => [
442 'apihelp-query+allusers-param-activeusers',
443 $this->getConfig()->get( MainConfigNames::ActiveUserDays )
446 'attachedwiki' => null,
447 'excludenamed' => [
448 ParamValidator::PARAM_TYPE => 'boolean',
450 'excludetemp' => [
451 ParamValidator::PARAM_TYPE => 'boolean',
456 protected function getExamplesMessages() {
457 return [
458 'action=query&list=allusers&aufrom=Y'
459 => 'apihelp-query+allusers-example-y',
463 public function getHelpUrls() {
464 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers';
468 /** @deprecated class alias since 1.43 */
469 class_alias( ApiQueryAllUsers::class, 'ApiQueryAllUsers' );