Merge "SpecialNuke: inject services"

This commit is contained in:
jenkins-bot 2023-04-28 17:46:31 +00:00 committed by Gerrit Code Review
commit 139c485cea
2 changed files with 54 additions and 13 deletions

View file

@ -62,8 +62,12 @@
"class": "MediaWiki\\Extension\\Nuke\\SpecialNuke", "class": "MediaWiki\\Extension\\Nuke\\SpecialNuke",
"services": [ "services": [
"JobQueueGroup", "JobQueueGroup",
"DBLoadBalancer",
"PermissionManager", "PermissionManager",
"RepoGroup" "RepoGroup",
"UserFactory",
"UserNamePrefixSearch",
"UserNameUtils"
] ]
} }
}, },

View file

@ -11,6 +11,9 @@ use ListToggle;
use MediaWiki\Extension\Nuke\Hooks\NukeHookRunner; use MediaWiki\Extension\Nuke\Hooks\NukeHookRunner;
use Mediawiki\Page\File\FileDeleteForm; use Mediawiki\Page\File\FileDeleteForm;
use MediaWiki\Permissions\PermissionManager; use MediaWiki\Permissions\PermissionManager;
use MediaWiki\User\UserFactory;
use MediaWiki\User\UserNamePrefixSearch;
use MediaWiki\User\UserNameUtils;
use OOUI\DropdownInputWidget; use OOUI\DropdownInputWidget;
use OOUI\FieldLayout; use OOUI\FieldLayout;
use OOUI\TextInputWidget; use OOUI\TextInputWidget;
@ -18,41 +21,63 @@ use PermissionsError;
use RepoGroup; use RepoGroup;
use SpecialPage; use SpecialPage;
use Title; use Title;
use User;
use UserBlockedError; use UserBlockedError;
use UserNamePrefixSearch;
use WebRequest; use WebRequest;
use Wikimedia\Rdbms\ILoadBalancer;
use Xml; use Xml;
class SpecialNuke extends SpecialPage { class SpecialNuke extends SpecialPage {
/** @var NukeHookRunner */ /** @var NukeHookRunner|null */
private $hookRunner; private $hookRunner;
/** @var JobQueueGroup */ /** @var JobQueueGroup */
private $jobQueueGroup; private $jobQueueGroup;
/** @var ILoadBalancer */
private $loadBalancer;
/** @var PermissionManager */ /** @var PermissionManager */
private $permissionManager; private $permissionManager;
/** @var RepoGroup */ /** @var RepoGroup */
private $repoGroup; private $repoGroup;
/** @var UserFactory */
private $userFactory;
/** @var UserNamePrefixSearch */
private $userNamePrefixSearch;
/** @var UserNameUtils */
private $userNameUtils;
/** /**
* @param JobQueueGroup $jobQueueGroup * @param JobQueueGroup $jobQueueGroup
* @param ILoadBalancer $loadBalancer
* @param PermissionManager $permissionManager * @param PermissionManager $permissionManager
* @param RepoGroup $repoGroup * @param RepoGroup $repoGroup
* @param UserFactory $userFactory
* @param UserNamePrefixSearch $userNamePrefixSearch
* @param UserNameUtils $userNameUtils
*/ */
public function __construct( public function __construct(
JobQueueGroup $jobQueueGroup, JobQueueGroup $jobQueueGroup,
ILoadBalancer $loadBalancer,
PermissionManager $permissionManager, PermissionManager $permissionManager,
RepoGroup $repoGroup RepoGroup $repoGroup,
UserFactory $userFactory,
UserNamePrefixSearch $userNamePrefixSearch,
UserNameUtils $userNameUtils
) { ) {
parent::__construct( 'Nuke', 'nuke' ); parent::__construct( 'Nuke', 'nuke' );
$this->hookRunner = new NukeHookRunner( $this->getHookContainer() );
$this->jobQueueGroup = $jobQueueGroup; $this->jobQueueGroup = $jobQueueGroup;
$this->loadBalancer = $loadBalancer;
$this->permissionManager = $permissionManager; $this->permissionManager = $permissionManager;
$this->repoGroup = $repoGroup; $this->repoGroup = $repoGroup;
$this->userFactory = $userFactory;
$this->userNamePrefixSearch = $userNamePrefixSearch;
$this->userNameUtils = $userNameUtils;
} }
public function doesWrites() { public function doesWrites() {
@ -86,7 +111,7 @@ class SpecialNuke extends SpecialPage {
// Normalise name // Normalise name
if ( $target !== '' ) { if ( $target !== '' ) {
$user = User::newFromName( $target ); $user = $this->userFactory->newFromName( $target );
if ( $user ) { if ( $user ) {
$target = $user->getName(); $target = $user->getName();
} }
@ -318,7 +343,7 @@ class SpecialNuke extends SpecialPage {
* @return array * @return array
*/ */
protected function getNewPages( $username, $limit, $namespace = null ) { protected function getNewPages( $username, $limit, $namespace = null ) {
$dbr = wfGetDB( DB_REPLICA ); $dbr = $this->loadBalancer->getConnectionRef( DB_REPLICA );
$what = [ $what = [
'rc_namespace', 'rc_namespace',
@ -375,7 +400,7 @@ class SpecialNuke extends SpecialPage {
// Allows other extensions to provide pages to be nuked that don't use // Allows other extensions to provide pages to be nuked that don't use
// the recentchanges table the way mediawiki-core does // the recentchanges table the way mediawiki-core does
$this->hookRunner->onNukeGetNewPages( $username, $pattern, $namespace, $limit, $pages ); $this->getNukeHookRunner()->onNukeGetNewPages( $username, $pattern, $namespace, $limit, $pages );
// Re-enforcing the limit *after* the hook because other extensions // Re-enforcing the limit *after* the hook because other extensions
// may add and/or remove pages. We need to make sure we don't end up // may add and/or remove pages. We need to make sure we don't end up
@ -404,7 +429,7 @@ class SpecialNuke extends SpecialPage {
$title = Title::newFromText( $page ); $title = Title::newFromText( $page );
$deletionResult = false; $deletionResult = false;
if ( !$this->hookRunner->onNukeDeletePage( $title, $reason, $deletionResult ) ) { if ( !$this->getNukeHookRunner()->onNukeDeletePage( $title, $reason, $deletionResult ) ) {
if ( $deletionResult ) { if ( $deletionResult ) {
$res[] = $this->msg( 'nuke-deleted' ) $res[] = $this->msg( 'nuke-deleted' )
->plaintextParams( $title->getPrefixedText() ) ->plaintextParams( $title->getPrefixedText() )
@ -486,14 +511,15 @@ class SpecialNuke extends SpecialPage {
* @return string[] Matching subpages * @return string[] Matching subpages
*/ */
public function prefixSearchSubpages( $search, $limit, $offset ) { public function prefixSearchSubpages( $search, $limit, $offset ) {
$user = User::newFromName( $search ); $search = $this->userNameUtils->getCanonical( $search );
if ( !$user ) { if ( !$search ) {
// No prefix suggestion for invalid user // No prefix suggestion for invalid user
return []; return [];
} }
// Autocomplete subpage as user list - public to allow caching // Autocomplete subpage as user list - public to allow caching
return UserNamePrefixSearch::search( 'public', $search, $limit, $offset ); return $this->userNamePrefixSearch
->search( UserNamePrefixSearch::AUDIENCE_PUBLIC, $search, $limit, $offset );
} }
/** /**
@ -528,4 +554,15 @@ class SpecialNuke extends SpecialPage {
return $dropdownSelection; return $dropdownSelection;
} }
} }
/**
* @return NukeHookRunner
*/
private function getNukeHookRunner() {
if ( !$this->hookRunner ) {
$this->hookRunner = new NukeHookRunner( $this->getHookContainer() );
}
return $this->hookRunner;
}
} }