Avoid using User::setOption()

User::setOption() is deprecated and should be replaced with UserOptionsManager::setOption()

Bug: T277818
Change-Id: I001301fb95635c421a0bbb921fd909c5312dc896
This commit is contained in:
ZabeMath 2021-03-31 00:14:16 +02:00 committed by Zabe
parent 39afffb297
commit 3befbe0a69
6 changed files with 72 additions and 11 deletions

View file

@ -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": [

View file

@ -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( [

View file

@ -1,6 +1,7 @@
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserOptionsManager;
use Wikimedia\Rdbms\IResultWrapper;
/**
@ -18,6 +19,11 @@ class MWEchoEmailBatch {
*/
protected $language;
/**
* @var UserOptionsManager
*/
protected $userOptionsManager;
/**
* @var EchoEvent[] events included in this email
*/
@ -41,10 +47,12 @@ class MWEchoEmailBatch {
/**
* @param User $user
* @param UserOptionsManager $userOptionsManager
*/
public function __construct( User $user ) {
public function __construct( User $user, UserOptionsManager $userOptionsManager ) {
$this->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();
}

View file

@ -1,6 +1,7 @@
<?php
use MediaWiki\MediaWikiServices;
use MediaWiki\User\UserOptionsManager;
class ApiEchoMute extends ApiBase {
@ -19,6 +20,24 @@ class ApiEchoMute extends ApiBase {
],
];
/** @var UserOptionsManager */
private $userOptionsManager;
/**
* @param ApiMain $main
* @param string $action
* @param UserOptionsManager $userOptionsManager
*/
public function __construct(
ApiMain $main,
$action,
UserOptionsManager $userOptionsManager
) {
parent::__construct( $main, $action );
$this->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();
}

View file

@ -1,5 +1,7 @@
<?php
use MediaWiki\User\UserOptionsManager;
class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
/**
* EchoAttributeManager to access notification configuration
@ -53,11 +55,20 @@ class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
*/
protected $flippedNotifyTypes;
public function __construct() {
/**
* @var UserOptionsManager
*/
private $userOptionsManager;
/**
* @param UserOptionsManager $userOptionsManager
*/
public function __construct( UserOptionsManager $userOptionsManager ) {
parent::__construct( 'DisplayNotificationsConfiguration' );
$this->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 = [];

View file

@ -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();
}