From 09855af65c4b5c9952d897c22050c52415e9e7a6 Mon Sep 17 00:00:00 2001 From: CosmicAlpha Date: Sun, 3 Mar 2024 15:34:01 -0700 Subject: [PATCH] Inject CentralAuth services again (#81) * Reverts #72 but uses optional_services instead to prevent fataling on non-CentralAuth wikis --- extension.json | 7 +++- includes/SpecialRemovePII.php | 68 +++++++++++++++++++---------------- 2 files changed, 44 insertions(+), 31 deletions(-) diff --git a/extension.json b/extension.json index f128ed0..b1ab4db 100644 --- a/extension.json +++ b/extension.json @@ -8,7 +8,7 @@ "license-name": "GPL-3.0-or-later", "type": "specialpage", "requires": { - "MediaWiki": ">= 1.38.0" + "MediaWiki": ">= 1.41.0" }, "MessagesDirs": { "RemovePII": [ @@ -57,6 +57,11 @@ "HttpRequestFactory", "JobQueueGroupFactory", "UserFactory" + ], + "optional_services": [ + "CentralAuth.CentralAuthAntiSpoofManager", + "CentralAuth.CentralAuthDatabaseManager", + "CentralAuth.GlobalRenameUserValidator" ] } }, diff --git a/includes/SpecialRemovePII.php b/includes/SpecialRemovePII.php index 9557a0f..93b69d8 100644 --- a/includes/SpecialRemovePII.php +++ b/includes/SpecialRemovePII.php @@ -9,18 +9,35 @@ use FormSpecialPage; use Html; use ManualLogEntry; +use MediaWiki\Extension\CentralAuth\CentralAuthDatabaseManager; +use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUser; +use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserDatabaseUpdates; +use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserStatus; +use MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserValidator; +use MediaWiki\Extension\CentralAuth\User\CentralAuthAntiSpoofManager; +use MediaWiki\Extension\CentralAuth\User\CentralAuthUser; +use MediaWiki\Extension\CentralAuth\Widget\HTMLGlobalUserTextField; use MediaWiki\Http\HttpRequestFactory; use MediaWiki\JobQueue\JobQueueGroupFactory; -use MediaWiki\MediaWikiServices; use MediaWiki\User\UserFactory; use SpecialPage; use Status; use WikiMap; class SpecialRemovePII extends FormSpecialPage { + + /** @var CentralAuthAntiSpoofManager|null */ + private $centralAuthAntiSpoofManager; + + /** @var CentralAuthDatabaseManager|null */ + private $centralAuthDatabaseManager; + /** @var Config */ private $config; + /** @var GlobalRenameUserValidator|null */ + private $globalRenameUserValidator; + /** @var HttpRequestFactory */ private $httpRequestFactory; @@ -35,15 +52,23 @@ class SpecialRemovePII extends FormSpecialPage { * @param HttpRequestFactory $httpRequestFactory * @param JobQueueGroupFactory $jobQueueGroupFactory * @param UserFactory $userFactory + * @param ?CentralAuthAntiSpoofManager $centralAuthAntiSpoofManager + * @param ?CentralAuthDatabaseManager $centralAuthDatabaseManager + * @param ?GlobalRenameUserValidator $globalRenameUserValidator */ public function __construct( ConfigFactory $configFactory, HttpRequestFactory $httpRequestFactory, JobQueueGroupFactory $jobQueueGroupFactory, - UserFactory $userFactory + UserFactory $userFactory, + ?CentralAuthAntiSpoofManager $centralAuthAntiSpoofManager, + ?CentralAuthDatabaseManager $centralAuthDatabaseManager, + ?GlobalRenameUserValidator $globalRenameUserValidator ) { parent::__construct( 'RemovePII', 'handle-pii' ); + $this->centralAuthAntiSpoofManager = $centralAuthAntiSpoofManager; + $this->centralAuthDatabaseManager = $centralAuthDatabaseManager; $this->config = $configFactory->makeConfig( 'RemovePII' ); $this->httpRequestFactory = $httpRequestFactory; $this->jobQueueGroupFactory = $jobQueueGroupFactory; @@ -87,7 +112,7 @@ protected function getFormFields() { ]; $formDescriptor['oldname'] = [ - 'class' => \MediaWiki\Extension\CentralAuth\Widget\HTMLGlobalUserTextField::class, + 'class' => HTMLGlobalUserTextField::class, 'required' => true, 'label-message' => 'removepii-oldname-label', ]; @@ -158,18 +183,12 @@ public function validateCentralAuth( array $formData ) { return Status::newFatal( 'removepii-centralauth-notinstalled' ); } - if ( version_compare( MW_VERSION, '1.40', '<' ) && - !ExtensionRegistry::getInstance()->isLoaded( 'Renameuser' ) - ) { - return Status::newFatal( 'centralauth-rename-notinstalled' ); - } - $oldUser = $this->userFactory->newFromName( $formData['oldname'] ); if ( !$oldUser ) { return Status::newFatal( 'centralauth-rename-doesnotexist' ); } - $oldCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $formData['oldname'] ); + $oldCentral = CentralAuthUser::getInstanceByName( $formData['oldname'] ); $canSuppress = $this->getUser() && $this->getUser()->isAllowed( 'centralauth-suppress' ); if ( ( $oldCentral->isSuppressed() || $oldCentral->isHidden() ) && @@ -187,10 +206,7 @@ public function validateCentralAuth( array $formData ) { return Status::newFatal( 'centralauth-rename-badusername' ); } - $globalRenameUserValidator = MediaWikiServices::getInstance()->getService( - 'CentralAuth.GlobalRenameUserValidator' - ); - return $globalRenameUserValidator->validate( $oldUser, $newUser ); + return $this->globalRenameUserValidator->validate( $oldUser, $newUser ); } /** @@ -218,25 +234,17 @@ public function onSubmit( array $formData ) { return Status::newFatal( 'unknown-error' ); } - $caDbManager = MediaWikiServices::getInstance()->getService( - 'CentralAuth.CentralAuthDatabaseManager' - ); - - $caAntiSpoofManager = MediaWikiServices::getInstance()->getService( - 'CentralAuth.CentralAuthAntiSpoofManager' - ); - - $globalRenameUser = new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUser( + $globalRenameUser = new GlobalRenameUser( $this->getUser(), $oldUser, - \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstance( $oldUser ), + CentralAuthUser::getInstance( $oldUser ), $newUser, - \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstance( $newUser ), - new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserStatus( $newUser->getName() ), + CentralAuthUser::getInstance( $newUser ), + new GlobalRenameUserStatus( $newUser->getName() ), $this->jobQueueGroupFactory, - new \MediaWiki\Extension\CentralAuth\GlobalRename\GlobalRenameUserDatabaseUpdates( $caDbManager ), + new GlobalRenameUserDatabaseUpdates( $this->centralAuthDatabaseManager ), new RemovePIIGlobalRenameUserLogger( $this->getUser() ), - $caAntiSpoofManager + $this->centralAuthAntiSpoofManager ); $globalRenameUser->rename( @@ -262,9 +270,9 @@ public function onSubmit( array $formData ) { 'newname' => $newName, ]; - $oldCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $oldName ); + $oldCentral = CentralAuthUser::getInstanceByName( $oldName ); - $newCentral = \MediaWiki\Extension\CentralAuth\User\CentralAuthUser::getInstanceByName( $newName ); + $newCentral = CentralAuthUser::getInstanceByName( $newName ); if ( $oldCentral->renameInProgress() ) { $out->addHTML(