diff --git a/ServiceWiring.php b/ServiceWiring.php index 98bffc63f..bb710c03a 100644 --- a/ServiceWiring.php +++ b/ServiceWiring.php @@ -8,6 +8,15 @@ use MediaWiki\Storage\NameTableStore; return [ + 'EchoAttributeManager' => function ( MediaWikiServices $services ): EchoAttributeManager { + $echoConfig = $services->getConfigFactory()->makeConfig( 'Echo' ); + $notifications = $echoConfig->get( 'EchoNotifications' ); + $categories = $echoConfig->get( 'EchoNotificationCategories' ); + $typeAvailability = $echoConfig->get( 'DefaultNotifyTypeAvailability' ); + $typeAvailabilityByCategory = $echoConfig->get( 'NotifyTypeAvailabilityByCategory' ); + return new EchoAttributeManager( $notifications, $categories, $typeAvailability, $typeAvailabilityByCategory ); + }, + 'EchoPushNotificationServiceClient' => function ( MediaWikiServices $services ): NotificationServiceClient { $echoConfig = $services->getConfigFactory()->makeConfig( 'Echo' ); diff --git a/includes/AttributeManager.php b/includes/AttributeManager.php index 91b9a391e..1b37a9671 100644 --- a/includes/AttributeManager.php +++ b/includes/AttributeManager.php @@ -51,12 +51,6 @@ class EchoAttributeManager { public const ATTR_LOCATORS = 'user-locators'; public const ATTR_FILTERS = 'user-filters'; - /** - * An EchoAttributeManager instance created from global variables - * @var self - */ - protected static $globalVarInstance; - /** * @param array[] $notifications Notification attributes * @param array[] $categories Notification categories @@ -80,36 +74,6 @@ class EchoAttributeManager { $this->notifyTypeAvailabilityByCategory = $notifyTypeAvailabilityByCategory; } - /** - * Create an instance from global variables - * @return EchoAttributeManager - */ - public static function newFromGlobalVars() { - global $wgEchoNotifications, $wgEchoNotificationCategories, - $wgDefaultNotifyTypeAvailability, $wgNotifyTypeAvailabilityByCategory; - - // Unit test may alter the global data for test purpose - if ( defined( 'MW_PHPUNIT_TEST' ) ) { - return new self( - $wgEchoNotifications, - $wgEchoNotificationCategories, - $wgDefaultNotifyTypeAvailability, - $wgNotifyTypeAvailabilityByCategory - ); - } - - if ( self::$globalVarInstance === null ) { - self::$globalVarInstance = new self( - $wgEchoNotifications, - $wgEchoNotificationCategories, - $wgDefaultNotifyTypeAvailability, - $wgNotifyTypeAvailabilityByCategory - ); - } - - return self::$globalVarInstance; - } - /** * Get the user-locators|user-filters related to the provided event type * diff --git a/includes/EchoHooks.php b/includes/EchoHooks.php index 1581b0c34..edf365630 100644 --- a/includes/EchoHooks.php +++ b/includes/EchoHooks.php @@ -296,7 +296,7 @@ class EchoHooks implements RecentChange_saveHook { $wgEchoCrossWikiNotifications, $wgEchoPerUserBlacklist, $wgEchoWatchlistNotifications; - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); // Show email frequency options $freqOptions = [ @@ -1215,7 +1215,7 @@ class EchoHooks implements RecentChange_saveHook { // If a user is watching his/her own talk page, do not send talk page watchlist // email notification if the user is receiving Echo talk page notification if ( $title->isTalkPage() && $targetUser->getTalkPage()->equals( $title ) ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $events = $attributeManager->getUserEnabledEvents( $targetUser, 'email' ); if ( in_array( 'edit-user-talk', $events ) ) { // Do not send watchlist email notification, the user will receive an Echo notification @@ -1482,7 +1482,7 @@ class EchoHooks implements RecentChange_saveHook { if ( $newUser->isRegistered() ) { // Select notifications that are now sent to the same user $dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_MASTER ); - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $selfIds = $dbw->selectFieldValues( [ 'echo_notification', 'echo_event' ], 'event_id', diff --git a/includes/EchoServices.php b/includes/EchoServices.php index 7633d07bc..0c13c25ec 100644 --- a/includes/EchoServices.php +++ b/includes/EchoServices.php @@ -37,4 +37,9 @@ class EchoServices { return $this->services->getService( 'EchoPushSubscriptionManager' ); } + /** @return EchoAttributeManager */ + public function getAttributeManager(): EchoAttributeManager { + return $this->services->getService( 'EchoAttributeManager' ); + } + } diff --git a/includes/NotifUser.php b/includes/NotifUser.php index b7cffc0ce..42083a9ce 100644 --- a/includes/NotifUser.php +++ b/includes/NotifUser.php @@ -259,7 +259,7 @@ class MWEchoNotifUser { $talkPageNotificationManager = MediaWikiServices::getInstance() ->getTalkPageNotificationManager(); if ( $talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $categoryMap = $attributeManager->getEventsByCategory(); $usertalkTypes = $categoryMap['edit-user-talk']; $unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( @@ -301,7 +301,7 @@ class MWEchoNotifUser { $talkPageNotificationManager = MediaWikiServices::getInstance() ->getTalkPageNotificationManager(); if ( !$talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $categoryMap = $attributeManager->getEventsByCategory(); $usertalkTypes = $categoryMap['edit-user-talk']; $unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( @@ -343,7 +343,7 @@ class MWEchoNotifUser { $sections = EchoAttributeManager::$sections; } - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $eventTypes = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', $sections ); $notifs = $this->notifMapper->fetchUnreadByUser( $this->mUser, $wgEchoMaxUpdateCount, null, $eventTypes ); @@ -557,7 +557,7 @@ class MWEchoNotifUser { * @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ] */ protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $result = []; $totals = [ 'count' => 0, 'timestamp' => -1 ]; diff --git a/includes/Notifier.php b/includes/Notifier.php index c2a6a72eb..4c88f2078 100644 --- a/includes/Notifier.php +++ b/includes/Notifier.php @@ -12,7 +12,7 @@ class EchoNotifier { public static function notifyWithNotification( $user, $event ) { // Only create the notification if the user wants to receive that type // of notification and they are eligible to receive it. See bug 47664. - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' ); if ( !in_array( $event->getType(), $userWebNotifications ) ) { return; @@ -51,7 +51,7 @@ class EchoNotifier { return false; } - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' ); // See if the user wants to receive emails for this category or the user is eligible to receive this email if ( in_array( $event->getType(), $userEmailNotifications ) ) { diff --git a/includes/Push/PushNotifier.php b/includes/Push/PushNotifier.php index 37f5096a6..e44f73648 100644 --- a/includes/Push/PushNotifier.php +++ b/includes/Push/PushNotifier.php @@ -2,8 +2,8 @@ namespace EchoPush; -use EchoAttributeManager; use EchoEvent; +use EchoServices; use JobQueueGroup; use User; @@ -16,7 +16,7 @@ class PushNotifier { * @param EchoEvent $event */ public static function notifyWithPush( User $user, EchoEvent $event ): void { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $userEnabledEvents = $attributeManager->getUserEnabledEvents( $user, 'push' ); if ( in_array( $event->getType(), $userEnabledEvents ) ) { JobQueueGroup::singleton()->push( self::createJob( $user, $event ) ); diff --git a/includes/api/ApiEchoNotifications.php b/includes/api/ApiEchoNotifications.php index 2a1d9b988..4ed293f22 100644 --- a/includes/api/ApiEchoNotifications.php +++ b/includes/api/ApiEchoNotifications.php @@ -100,7 +100,7 @@ class ApiEchoNotifications extends ApiQueryBase { } } } else { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $result = $this->getPropList( $user, $attributeManager->getUserEnabledEventsbySections( $user, 'web', $params['sections'] ), @@ -163,7 +163,7 @@ class ApiEchoNotifications extends ApiQueryBase { $unreadFirst = false, $bundle = false ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $sectionEvents = $attributeManager->getUserEnabledEventsbySections( $user, 'web', [ $section ] ); if ( !$sectionEvents ) { diff --git a/includes/api/ApiEchoUnreadNotificationPages.php b/includes/api/ApiEchoUnreadNotificationPages.php index c5a07f73b..0d39b18ff 100644 --- a/includes/api/ApiEchoUnreadNotificationPages.php +++ b/includes/api/ApiEchoUnreadNotificationPages.php @@ -56,7 +56,7 @@ class ApiEchoUnreadNotificationPages extends ApiQueryBase { * @phan-return array{pages:array[],totalCount:int} */ protected function getFromLocal( $limit, $groupPages ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $enabledTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' ); $dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA ); diff --git a/includes/controller/NotificationController.php b/includes/controller/NotificationController.php index c90c6529f..f919bb7e1 100644 --- a/includes/controller/NotificationController.php +++ b/includes/controller/NotificationController.php @@ -208,7 +208,7 @@ class EchoNotificationController { * this event type */ public static function getEventNotifyTypes( $eventType ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $category = $attributeManager->getNotificationCategory( $eventType ); @@ -432,7 +432,7 @@ class EchoNotificationController { * @return array */ public static function evaluateUserCallable( EchoEvent $event, $locator = EchoAttributeManager::ATTR_LOCATORS ) { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $type = $event->getType(); $result = []; foreach ( $attributeManager->getUserCallable( $type, $locator ) as $callable ) { diff --git a/includes/model/Event.php b/includes/model/Event.php index cd990c7e0..6656c8e5e 100644 --- a/includes/model/Event.php +++ b/includes/model/Event.php @@ -593,7 +593,7 @@ class EchoEvent extends EchoAbstractEntity implements Bundleable { * @return string */ public function getCategory() { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); return $attributeManager->getNotificationCategory( $this->type ); } @@ -603,7 +603,7 @@ class EchoEvent extends EchoAbstractEntity implements Bundleable { * @return string */ public function getSection() { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); return $attributeManager->getNotificationSection( $this->type ); } diff --git a/includes/special/NotificationPager.php b/includes/special/NotificationPager.php index 2dfc12384..11b59a5dc 100644 --- a/includes/special/NotificationPager.php +++ b/includes/special/NotificationPager.php @@ -22,7 +22,7 @@ class NotificationPager extends ReverseChronologicalPager { } public function getQueryInfo() { - $attributeManager = EchoAttributeManager::newFromGlobalVars(); + $attributeManager = EchoServices::getInstance()->getAttributeManager(); $eventTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' ); return [ diff --git a/includes/special/SpecialDisplayNotificationsConfiguration.php b/includes/special/SpecialDisplayNotificationsConfiguration.php index a39c817ce..c4717e49d 100644 --- a/includes/special/SpecialDisplayNotificationsConfiguration.php +++ b/includes/special/SpecialDisplayNotificationsConfiguration.php @@ -56,7 +56,7 @@ class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage { public function __construct() { parent::__construct( 'DisplayNotificationsConfiguration' ); - $this->attributeManager = EchoAttributeManager::newFromGlobalVars(); + $this->attributeManager = EchoServices::getInstance()->getAttributeManager(); $this->notificationController = new EchoNotificationController(); } diff --git a/tests/phpunit/integration/EchoServicesTest.php b/tests/phpunit/integration/EchoServicesTest.php index 60c46855b..ef9405b2d 100644 --- a/tests/phpunit/integration/EchoServicesTest.php +++ b/tests/phpunit/integration/EchoServicesTest.php @@ -30,4 +30,9 @@ class EchoServicesTest extends MediaWikiIntegrationTestCase { $this->assertInstanceOf( SubscriptionManager::class, $subscriptionManager ); } + public function testGetAttributeManager(): void { + $attributeManager = $this->echoServices->getAttributeManager(); + $this->assertInstanceOf( EchoAttributeManager::class, $attributeManager ); + } + } diff --git a/tests/phpunit/AttributeManagerTest.php b/tests/phpunit/unit/AttributeManagerTest.php similarity index 98% rename from tests/phpunit/AttributeManagerTest.php rename to tests/phpunit/unit/AttributeManagerTest.php index 4dd749c8e..7475aa878 100644 --- a/tests/phpunit/AttributeManagerTest.php +++ b/tests/phpunit/unit/AttributeManagerTest.php @@ -3,11 +3,7 @@ /** * @covers \EchoAttributeManager */ -class EchoAttributeManagerTest extends MediaWikiTestCase { - - public function testNewFromGlobalVars() { - $this->assertInstanceOf( EchoAttributeManager::class, EchoAttributeManager::newFromGlobalVars() ); - } +class EchoAttributeManagerTest extends MediaWikiUnitTestCase { public static function getUserLocatorsProvider() { return [