messageLocalizer = $messageLocalizer; $this->userGroupManager = $userGroupManager; $this->logger = $logger; } /** * @todo Core should provide an alternative way to create system users, possibly returning just UserIdentity. * Or can we just use `new UserIdentityValue` here? * @return UserIdentity */ public function getUser(): UserIdentity { $username = $this->messageLocalizer->msg( 'abusefilter-blocker' )->inContentLanguage()->text(); $user = User::newSystemUser( $username, [ 'steal' => true ] ); if ( !$user ) { // User name is invalid. Don't throw because this is a system message, easy // to change and make wrong either by mistake or intentionally to break the site. $this->logger->warning( 'The AbuseFilter user\'s name is invalid. Please change it in ' . 'MediaWiki:abusefilter-blocker' ); // Use the default name to avoid breaking other stuff. This should have no harm, // aside from blocks temporarily attributed to another user. // Don't use the database in case the English onwiki message is broken, T284364 $defaultName = $this->messageLocalizer->msg( 'abusefilter-blocker' ) ->inLanguage( 'en' ) ->useDatabase( false ) ->text(); $user = User::newSystemUser( $defaultName, [ 'steal' => true ] ); } '@phan-var User $user'; // Promote user to 'sysop' so it doesn't look // like an unprivileged account is blocking users if ( !in_array( 'sysop', $this->userGroupManager->getUserGroups( $user ) ) ) { $this->userGroupManager->addUserToGroup( $user, 'sysop' ); } return $user; } }