| 1 |
<?php |
| 2 |
|
| 3 |
" |
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
namespace MediaWiki\Api; |
| 24 |
|
| 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; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
class ApiQueryAllUsers extends ApiQueryBase { |
| 42 |
use ApiQueryBlockInfoTrait; |
| 43 |
|
| 44 |
private UserFactory $userFactory; |
| 45 |
private UserGroupManager $userGroupManager; |
| 46 |
private GroupPermissionsLookup $groupPermissionsLookup; |
| 47 |
private Language $contentLanguage; |
| 48 |
private TempUserConfig $tempUserConfig; |
| 49 |
|
| 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; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
private function getCanonicalUserName( $name ) { |
| 74 |
$name = $this->contentLanguage->ucfirst( $name ); |
| 75 |
return strtr( $name, '_', ' ' ); |
| 76 |
} |
| 77 |
|
| 78 |
public function execute() { |
| 79 |
$params = $this->extractRequestParams(); |
| 80 |
$activeUserDays = $this->getConfig()->get( MainConfigNames::ActiveUserDays ); |
| 81 |
|
| 82 |
$db = $this->getDB(); |
| 83 |
|
| 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; |
| 97 |
} |
| 98 |
|
| 99 |
$limit = $params['limit']; |
| 100 |
|
| 101 |
$this->addTables( 'user' ); |
| 102 |
|
| 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'] ); |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
$userFieldToSort = $params['activeusers'] ? 'qcc_title' : 'user_name'; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
$maxDuplicateRows = 1; |
| 114 |
|
| 115 |
$this->addWhereRange( $userFieldToSort, $dir, $from, $to ); |
| 116 |
|
| 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() ) |
| 123 |
) |
| 124 |
); |
| 125 |
} |
| 126 |
|
| 127 |
$excludeNamed = $params['excludenamed']; |
| 128 |
$excludeTemp = $params['excludetemp']; |
| 129 |
|
| 130 |
if ( $this->tempUserConfig->isKnown() ) { |
| 131 |
if ( $excludeTemp ) { |
| 132 |
$this->addWhere( |
| 133 |
$this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::NOT_LIKE ) |
| 134 |
); |
| 135 |
} |
| 136 |
if ( $excludeNamed ) { |
| 137 |
$this->addWhere( |
| 138 |
$this->tempUserConfig->getMatchCondition( $db, 'user_name', IExpression::LIKE ) |
| 139 |
); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
if ( $params['rights'] !== null && count( $params['rights'] ) ) { |
| 144 |
$groups = []; |
| 145 |
|
| 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 ) |
| 153 |
); |
| 154 |
} |
| 155 |
} |
| 156 |
|
| 157 |
if ( $groups === [] ) { |
| 158 |
|
| 159 |
$this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], '' ); |
| 160 |
|
| 161 |
return; |
| 162 |
} |
| 163 |
|
| 164 |
$groups = array_unique( $groups ); |
| 165 |
if ( in_array( '*', $groups, true ) || in_array( 'user', $groups, true ) ) { |
| 166 |
|
| 167 |
$groups = []; |
| 168 |
} |
| 169 |
|
| 170 |
if ( $params['group'] === null ) { |
| 171 |
$params['group'] = $groups; |
| 172 |
} else { |
| 173 |
$params['group'] = array_unique( array_merge( $params['group'], $groups ) ); |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
$this->requireMaxOneParameter( $params, 'group', 'excludegroup' ); |
| 178 |
|
| 179 |
if ( $params['group'] !== null && count( $params['group'] ) ) { |
| 180 |
|
| 181 |
|
| 182 |
$this->addTables( 'user_groups', 'ug1' ); |
| 183 |
$this->addJoinConds( [ |
| 184 |
'ug1' => [ |
| 185 |
'JOIN', |
| 186 |
[ |
| 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() ), |
| 190 |
] |
| 191 |
] |
| 192 |
] ); |
| 193 |
$maxDuplicateRows *= count( $params['group'] ); |
| 194 |
} |
| 195 |
|
| 196 |
if ( $params['excludegroup'] !== null && count( $params['excludegroup'] ) ) { |
| 197 |
|
| 198 |
|
| 199 |
$this->addTables( 'user_groups', 'ug1' ); |
| 200 |
|
| 201 |
$this->addJoinConds( [ 'ug1' => [ 'LEFT JOIN', |
| 202 |
[ |
| 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'], |
| 206 |
] |
| 207 |
] ] ); |
| 208 |
$this->addWhere( [ 'ug1.ug_user' => null ] ); |
| 209 |
} |
| 210 |
|
| 211 |
if ( $params['witheditsonly'] ) { |
| 212 |
$this->addWhere( $db->expr( 'user_editcount', '>', 0 ) ); |
| 213 |
} |
| 214 |
|
| 215 |
$this->addDeletedUserFilter(); |
| 216 |
|
| 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() ) |
| 225 |
] ) |
| 226 |
->buildGroupConcatField( '|' ) |
| 227 |
] ); |
| 228 |
} |
| 229 |
|
| 230 |
if ( $params['activeusers'] ) { |
| 231 |
$activeUserSeconds = $activeUserDays * 86400; |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
$this->addTables( 'querycachetwo' ); |
| 236 |
$this->addJoinConds( [ 'querycachetwo' => [ |
| 237 |
'JOIN', [ |
| 238 |
'qcc_type' => 'activeusers', |
| 239 |
'qcc_namespace' => NS_USER, |
| 240 |
'qcc_title=user_name', |
| 241 |
], |
| 242 |
] ] ); |
| 243 |
|
| 244 |
|
| 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 ), |
| 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 |
] ); |
| 260 |
} |
| 261 |
|
| 262 |
$sqlLimit = $limit + $maxDuplicateRows; |
| 263 |
$this->addOption( 'LIMIT', $sqlLimit ); |
| 264 |
|
| 265 |
$this->addFields( [ |
| 266 |
'user_name', |
| 267 |
'user_id' |
| 268 |
] ); |
| 269 |
$this->addFieldsIf( 'user_editcount', $fld_editcount ); |
| 270 |
$this->addFieldsIf( 'user_registration', $fld_registration ); |
| 271 |
|
| 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++; |
| 280 |
|
| 281 |
if ( $lastUser === $row->user_name ) { |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
$countDuplicates++; |
| 286 |
if ( $countDuplicates == $maxDuplicateRows ) { |
| 287 |
ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' ); |
| 288 |
} |
| 289 |
continue; |
| 290 |
} |
| 291 |
|
| 292 |
$countDuplicates = 0; |
| 293 |
$lastUser = $row->user_name; |
| 294 |
|
| 295 |
if ( $count > $limit ) { |
| 296 |
|
| 297 |
|
| 298 |
$this->setContinueEnumParameter( 'from', $row->user_name ); |
| 299 |
break; |
| 300 |
} |
| 301 |
|
| 302 |
if ( $count == $sqlLimit ) { |
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
ApiBase::dieDebug( __METHOD__, 'Saw more duplicate rows than expected' ); |
| 307 |
} |
| 308 |
|
| 309 |
if ( $params['activeusers'] && (int)$row->recentactions === 0 ) { |
| 310 |
|
| 311 |
continue; |
| 312 |
} |
| 313 |
|
| 314 |
$data = [ |
| 315 |
'userid' => (int)$row->user_id, |
| 316 |
'name' => $row->user_name, |
| 317 |
]; |
| 318 |
|
| 319 |
if ( $fld_centralids ) { |
| 320 |
$data += ApiQueryUserInfo::getCentralUserInfo( |
| 321 |
$this->getConfig(), $this->userFactory->newFromId( (int)$row->user_id ), $params['attachedwiki'] |
| 322 |
); |
| 323 |
} |
| 324 |
|
| 325 |
if ( $fld_blockinfo && isset( $blockInfos[$row->user_id] ) ) { |
| 326 |
$data += $blockInfos[$row->user_id]; |
| 327 |
} |
| 328 |
if ( $row->hu_deleted ) { |
| 329 |
$data['hidden'] = true; |
| 330 |
} |
| 331 |
if ( $fld_editcount ) { |
| 332 |
$data['editcount'] = (int)$row->user_editcount; |
| 333 |
} |
| 334 |
if ( $params['activeusers'] ) { |
| 335 |
$data['recentactions'] = (int)$row->recentactions; |
| 336 |
} |
| 337 |
if ( $fld_registration ) { |
| 338 |
$data['registration'] = $row->user_registration ? |
| 339 |
wfTimestamp( TS_ISO_8601, $row->user_registration ) : ''; |
| 340 |
} |
| 341 |
|
| 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; |
| 349 |
} |
| 350 |
|
| 351 |
if ( $fld_groups ) { |
| 352 |
$data['groups'] = $groups; |
| 353 |
ApiResult::setIndexedTagName( $data['groups'], 'g' ); |
| 354 |
ApiResult::setArrayType( $data['groups'], 'array' ); |
| 355 |
} |
| 356 |
|
| 357 |
if ( $fld_implicitgroups ) { |
| 358 |
$data['implicitgroups'] = $implicitGroups; |
| 359 |
ApiResult::setIndexedTagName( $data['implicitgroups'], 'g' ); |
| 360 |
ApiResult::setArrayType( $data['implicitgroups'], 'array' ); |
| 361 |
} |
| 362 |
|
| 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' ); |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
$fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $data ); |
| 372 |
if ( !$fit ) { |
| 373 |
$this->setContinueEnumParameter( 'from', $data['name'] ); |
| 374 |
break; |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
$result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'u' ); |
| 379 |
} |
| 380 |
|
| 381 |
public function getCacheMode( $params ) { |
| 382 |
return 'anon-public-user-private'; |
| 383 |
} |
| 384 |
|
| 385 |
public function getAllowedParams( $flags = 0 ) { |
| 386 |
$userGroups = $this->userGroupManager->listAllGroups(); |
| 387 |
|
| 388 |
if ( $flags & ApiBase::GET_VALUES_FOR_HELP ) { |
| 389 |
sort( $userGroups ); |
| 390 |
} |
| 391 |
|
| 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' |
| 401 |
], |
| 402 |
], |
| 403 |
'group' => [ |
| 404 |
ParamValidator::PARAM_TYPE => $userGroups, |
| 405 |
ParamValidator::PARAM_ISMULTI => true, |
| 406 |
], |
| 407 |
'excludegroup' => [ |
| 408 |
ParamValidator::PARAM_TYPE => $userGroups, |
| 409 |
ParamValidator::PARAM_ISMULTI => true, |
| 410 |
], |
| 411 |
'rights' => [ |
| 412 |
ParamValidator::PARAM_TYPE => array_unique( array_merge( |
| 413 |
$this->getPermissionManager()->getAllPermissions(), |
| 414 |
$this->getPermissionManager()->getImplicitRights() |
| 415 |
) ), |
| 416 |
ParamValidator::PARAM_ISMULTI => true, |
| 417 |
], |
| 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', |
| 428 |
], |
| 429 |
ApiBase::PARAM_HELP_MSG_PER_VALUE => [], |
| 430 |
], |
| 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 |
| 437 |
], |
| 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 ) |
| 444 |
], |
| 445 |
], |
| 446 |
'attachedwiki' => null, |
| 447 |
'excludenamed' => [ |
| 448 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 449 |
], |
| 450 |
'excludetemp' => [ |
| 451 |
ParamValidator::PARAM_TYPE => 'boolean', |
| 452 |
], |
| 453 |
]; |
| 454 |
} |
| 455 |
|
| 456 |
protected function getExamplesMessages() { |
| 457 |
return [ |
| 458 |
'action=query&list=allusers&aufrom=Y' |
| 459 |
=> 'apihelp-query+allusers-example-y', |
| 460 |
]; |
| 461 |
} |
| 462 |
|
| 463 |
public function getHelpUrls() { |
| 464 |
return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allusers'; |
| 465 |
} |
| 466 |
} |
| 467 |
|
| 468 |
|
| 469 |
class_alias( ApiQueryAllUsers::class, 'ApiQueryAllUsers' ); |