From 3befbe0a696631718a2b55a7ce661bed564576dc Mon Sep 17 00:00:00 2001 From: ZabeMath Date: Wed, 31 Mar 2021 00:14:16 +0200 Subject: [PATCH] Avoid using User::setOption() User::setOption() is deprecated and should be replaced with UserOptionsManager::setOption() Bug: T277818 Change-Id: I001301fb95635c421a0bbb921fd909c5312dc896 --- extension.json | 14 ++++++++-- includes/EchoHooks.php | 3 ++- includes/EmailBatch.php | 21 ++++++++++++--- includes/api/ApiEchoMute.php | 27 ++++++++++++++++++- ...ecialDisplayNotificationsConfiguration.php | 15 +++++++++-- tests/phpunit/DiscussionParserTest.php | 3 ++- 6 files changed, 72 insertions(+), 11 deletions(-) diff --git a/extension.json b/extension.json index 174f50700..fa5f464ab 100644 --- a/extension.json +++ b/extension.json @@ -25,7 +25,12 @@ "echomarkread": "ApiEchoMarkRead", "echomarkseen": "ApiEchoMarkSeen", "echoarticlereminder": "ApiEchoArticleReminder", - "echomute": "ApiEchoMute" + "echomute": { + "class": "ApiEchoMute", + "services": [ + "UserOptionsManager" + ] + } }, "DefaultUserOptions": { "echo-email-frequency": 0, @@ -41,7 +46,12 @@ }, "SpecialPages": { "Notifications": "SpecialNotifications", - "DisplayNotificationsConfiguration": "SpecialDisplayNotificationsConfiguration", + "DisplayNotificationsConfiguration": { + "class": "SpecialDisplayNotificationsConfiguration", + "services": [ + "UserOptionsManager" + ] + }, "NotificationsMarkRead": "SpecialNotificationsMarkRead" }, "AvailableRights": [ diff --git a/includes/EchoHooks.php b/includes/EchoHooks.php index 1ec908821..8426439f0 100644 --- a/includes/EchoHooks.php +++ b/includes/EchoHooks.php @@ -696,8 +696,9 @@ class EchoHooks implements RecentChange_saveHook { public static function onLocalUserCreated( $user, $autocreated ) { if ( !$autocreated ) { $overrides = self::getNewUserPreferenceOverrides(); + $userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager(); foreach ( $overrides as $prefKey => $value ) { - $user->setOption( $prefKey, $value ); + $userOptionsManager->setOption( $user, $prefKey, $value ); } $user->saveSettings(); EchoEvent::create( [ diff --git a/includes/EmailBatch.php b/includes/EmailBatch.php index 5075b738e..aa404ec5b 100644 --- a/includes/EmailBatch.php +++ b/includes/EmailBatch.php @@ -1,6 +1,7 @@ mUser = $user; $this->language = Language::factory( $this->mUser->getOption( 'language' ) ); + $this->userOptionsManager = $userOptionsManager; } /** @@ -67,12 +75,13 @@ class MWEchoEmailBatch { */ public static function newFromUserId( $userId, $enforceFrequency = true ) { $user = User::newFromId( (int)$userId ); + $userOptionsManager = MediaWikiServices::getInstance()->getUserOptionsManager(); $userEmailSetting = (int)$user->getOption( 'echo-email-frequency' ); // clear all existing events if user decides not to receive emails if ( $userEmailSetting == -1 ) { - $emailBatch = new self( $user ); + $emailBatch = new self( $user, $userOptionsManager ); $emailBatch->clearProcessedEvent(); return false; @@ -103,7 +112,7 @@ class MWEchoEmailBatch { } } - return new self( $user ); + return new self( $user, $userOptionsManager ); } /** @@ -170,7 +179,11 @@ class MWEchoEmailBatch { * Update the user's last batch timestamp after a successful batch */ protected function updateUserLastBatchTimestamp() { - $this->mUser->setOption( 'echo-email-last-batch', wfTimestampNow() ); + $this->userOptionsManager->setOption( + $this->mUser, + 'echo-email-last-batch', + wfTimestampNow() + ); $this->mUser->saveSettings(); $this->mUser->invalidateCache(); } diff --git a/includes/api/ApiEchoMute.php b/includes/api/ApiEchoMute.php index 3f6268536..733720a96 100644 --- a/includes/api/ApiEchoMute.php +++ b/includes/api/ApiEchoMute.php @@ -1,6 +1,7 @@ userOptionsManager = $userOptionsManager; + } + public function execute() { $user = $this->getUser()->getInstanceForUpdate(); if ( !$user || !$user->isRegistered() ) { @@ -55,7 +74,13 @@ class ApiEchoMute extends ApiBase { } if ( $changed ) { - $user->setOption( $mutelistInfo['pref'], $this->serializePref( $ids, $mutelistInfo['type'] ) ); + $this->userOptionsManager->setOption( + // Phan does not understand dieWithError() - T240141 + // @phan-suppress-next-line PhanTypeMismatchArgumentNullable + $user, + $mutelistInfo['pref'], + $this->serializePref( $ids, $mutelistInfo['type'] ) + ); $user->saveSettings(); } diff --git a/includes/special/SpecialDisplayNotificationsConfiguration.php b/includes/special/SpecialDisplayNotificationsConfiguration.php index c4717e49d..f2f770f6a 100644 --- a/includes/special/SpecialDisplayNotificationsConfiguration.php +++ b/includes/special/SpecialDisplayNotificationsConfiguration.php @@ -1,5 +1,7 @@ attributeManager = EchoServices::getInstance()->getAttributeManager(); $this->notificationController = new EchoNotificationController(); + $this->userOptionsManager = $userOptionsManager; } public function execute( $subPage ) { @@ -288,7 +299,7 @@ class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage { // We can't run the actual hook, to avoid side effects. $overrides = EchoHooks::getNewUserPreferenceOverrides(); foreach ( $overrides as $prefKey => $value ) { - $loggedInUser->setOption( $prefKey, $value ); + $this->userOptionsManager->setOption( $loggedInUser, $prefKey, $value ); } $byCategoryValueNew = []; diff --git a/tests/phpunit/DiscussionParserTest.php b/tests/phpunit/DiscussionParserTest.php index 6c6cdfb30..dae7405de 100644 --- a/tests/phpunit/DiscussionParserTest.php +++ b/tests/phpunit/DiscussionParserTest.php @@ -161,8 +161,9 @@ class EchoDiscussionParserTest extends MediaWikiTestCase { // Set preferences if ( $user ) { + $userOptionsManager = $this->getServiceContainer()->getUserOptionsManager(); foreach ( $preferences as $option => $value ) { - $user->setOption( $option, $value ); + $userOptionsManager->setOption( $user, $option, $value ); } $user->saveSettings(); }