Make AttributeManager a service

Adds AttributeManager to EchoServices so that dependencies of
AttributeManager can be injected.

Bug: T275148
Change-Id: I4fa5084d72914d16b6d218e7dd3521f5a1919b80
This commit is contained in:
Jeena Huneidi 2021-02-24 17:55:40 -08:00
parent 0805daf1c2
commit 596729d852
15 changed files with 40 additions and 61 deletions

View file

@ -8,6 +8,15 @@ use MediaWiki\Storage\NameTableStore;
return [ 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 ): 'EchoPushNotificationServiceClient' => function ( MediaWikiServices $services ):
NotificationServiceClient { NotificationServiceClient {
$echoConfig = $services->getConfigFactory()->makeConfig( 'Echo' ); $echoConfig = $services->getConfigFactory()->makeConfig( 'Echo' );

View file

@ -51,12 +51,6 @@ class EchoAttributeManager {
public const ATTR_LOCATORS = 'user-locators'; public const ATTR_LOCATORS = 'user-locators';
public const ATTR_FILTERS = 'user-filters'; 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[] $notifications Notification attributes
* @param array[] $categories Notification categories * @param array[] $categories Notification categories
@ -80,36 +74,6 @@ class EchoAttributeManager {
$this->notifyTypeAvailabilityByCategory = $notifyTypeAvailabilityByCategory; $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 * Get the user-locators|user-filters related to the provided event type
* *

View file

@ -296,7 +296,7 @@ class EchoHooks implements RecentChange_saveHook {
$wgEchoCrossWikiNotifications, $wgEchoPerUserBlacklist, $wgEchoCrossWikiNotifications, $wgEchoPerUserBlacklist,
$wgEchoWatchlistNotifications; $wgEchoWatchlistNotifications;
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
// Show email frequency options // Show email frequency options
$freqOptions = [ $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 // 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 // email notification if the user is receiving Echo talk page notification
if ( $title->isTalkPage() && $targetUser->getTalkPage()->equals( $title ) ) { if ( $title->isTalkPage() && $targetUser->getTalkPage()->equals( $title ) ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$events = $attributeManager->getUserEnabledEvents( $targetUser, 'email' ); $events = $attributeManager->getUserEnabledEvents( $targetUser, 'email' );
if ( in_array( 'edit-user-talk', $events ) ) { if ( in_array( 'edit-user-talk', $events ) ) {
// Do not send watchlist email notification, the user will receive an Echo notification // 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() ) { if ( $newUser->isRegistered() ) {
// Select notifications that are now sent to the same user // Select notifications that are now sent to the same user
$dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_MASTER ); $dbw = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_MASTER );
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$selfIds = $dbw->selectFieldValues( $selfIds = $dbw->selectFieldValues(
[ 'echo_notification', 'echo_event' ], [ 'echo_notification', 'echo_event' ],
'event_id', 'event_id',

View file

@ -37,4 +37,9 @@ class EchoServices {
return $this->services->getService( 'EchoPushSubscriptionManager' ); return $this->services->getService( 'EchoPushSubscriptionManager' );
} }
/** @return EchoAttributeManager */
public function getAttributeManager(): EchoAttributeManager {
return $this->services->getService( 'EchoAttributeManager' );
}
} }

View file

@ -259,7 +259,7 @@ class MWEchoNotifUser {
$talkPageNotificationManager = MediaWikiServices::getInstance() $talkPageNotificationManager = MediaWikiServices::getInstance()
->getTalkPageNotificationManager(); ->getTalkPageNotificationManager();
if ( $talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) { if ( $talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$categoryMap = $attributeManager->getEventsByCategory(); $categoryMap = $attributeManager->getEventsByCategory();
$usertalkTypes = $categoryMap['edit-user-talk']; $usertalkTypes = $categoryMap['edit-user-talk'];
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser(
@ -301,7 +301,7 @@ class MWEchoNotifUser {
$talkPageNotificationManager = MediaWikiServices::getInstance() $talkPageNotificationManager = MediaWikiServices::getInstance()
->getTalkPageNotificationManager(); ->getTalkPageNotificationManager();
if ( !$talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) { if ( !$talkPageNotificationManager->userHasNewMessages( $this->mUser ) ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$categoryMap = $attributeManager->getEventsByCategory(); $categoryMap = $attributeManager->getEventsByCategory();
$usertalkTypes = $categoryMap['edit-user-talk']; $usertalkTypes = $categoryMap['edit-user-talk'];
$unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser( $unreadEditUserTalk = $this->notifMapper->fetchUnreadByUser(
@ -343,7 +343,7 @@ class MWEchoNotifUser {
$sections = EchoAttributeManager::$sections; $sections = EchoAttributeManager::$sections;
} }
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$eventTypes = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', $sections ); $eventTypes = $attributeManager->getUserEnabledEventsbySections( $this->mUser, 'web', $sections );
$notifs = $this->notifMapper->fetchUnreadByUser( $this->mUser, $wgEchoMaxUpdateCount, null, $eventTypes ); $notifs = $this->notifMapper->fetchUnreadByUser( $this->mUser, $wgEchoMaxUpdateCount, null, $eventTypes );
@ -557,7 +557,7 @@ class MWEchoNotifUser {
* @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ] * @return array[] [ 'alert' => [ 'count' => N, 'timestamp' => TS ], ... ]
*/ */
protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) { protected function computeLocalCountsAndTimestamps( $dbSource = DB_REPLICA ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$result = []; $result = [];
$totals = [ 'count' => 0, 'timestamp' => -1 ]; $totals = [ 'count' => 0, 'timestamp' => -1 ];

View file

@ -12,7 +12,7 @@ class EchoNotifier {
public static function notifyWithNotification( $user, $event ) { public static function notifyWithNotification( $user, $event ) {
// Only create the notification if the user wants to receive that type // Only create the notification if the user wants to receive that type
// of notification and they are eligible to receive it. See bug 47664. // of notification and they are eligible to receive it. See bug 47664.
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' ); $userWebNotifications = $attributeManager->getUserEnabledEvents( $user, 'web' );
if ( !in_array( $event->getType(), $userWebNotifications ) ) { if ( !in_array( $event->getType(), $userWebNotifications ) ) {
return; return;
@ -51,7 +51,7 @@ class EchoNotifier {
return false; return false;
} }
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$userEmailNotifications = $attributeManager->getUserEnabledEvents( $user, 'email' ); $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 // 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 ) ) { if ( in_array( $event->getType(), $userEmailNotifications ) ) {

View file

@ -2,8 +2,8 @@
namespace EchoPush; namespace EchoPush;
use EchoAttributeManager;
use EchoEvent; use EchoEvent;
use EchoServices;
use JobQueueGroup; use JobQueueGroup;
use User; use User;
@ -16,7 +16,7 @@ class PushNotifier {
* @param EchoEvent $event * @param EchoEvent $event
*/ */
public static function notifyWithPush( User $user, EchoEvent $event ): void { public static function notifyWithPush( User $user, EchoEvent $event ): void {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$userEnabledEvents = $attributeManager->getUserEnabledEvents( $user, 'push' ); $userEnabledEvents = $attributeManager->getUserEnabledEvents( $user, 'push' );
if ( in_array( $event->getType(), $userEnabledEvents ) ) { if ( in_array( $event->getType(), $userEnabledEvents ) ) {
JobQueueGroup::singleton()->push( self::createJob( $user, $event ) ); JobQueueGroup::singleton()->push( self::createJob( $user, $event ) );

View file

@ -100,7 +100,7 @@ class ApiEchoNotifications extends ApiQueryBase {
} }
} }
} else { } else {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$result = $this->getPropList( $result = $this->getPropList(
$user, $user,
$attributeManager->getUserEnabledEventsbySections( $user, 'web', $params['sections'] ), $attributeManager->getUserEnabledEventsbySections( $user, 'web', $params['sections'] ),
@ -163,7 +163,7 @@ class ApiEchoNotifications extends ApiQueryBase {
$unreadFirst = false, $unreadFirst = false,
$bundle = false $bundle = false
) { ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$sectionEvents = $attributeManager->getUserEnabledEventsbySections( $user, 'web', [ $section ] ); $sectionEvents = $attributeManager->getUserEnabledEventsbySections( $user, 'web', [ $section ] );
if ( !$sectionEvents ) { if ( !$sectionEvents ) {

View file

@ -56,7 +56,7 @@ class ApiEchoUnreadNotificationPages extends ApiQueryBase {
* @phan-return array{pages:array[],totalCount:int} * @phan-return array{pages:array[],totalCount:int}
*/ */
protected function getFromLocal( $limit, $groupPages ) { protected function getFromLocal( $limit, $groupPages ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$enabledTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' ); $enabledTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' );
$dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA ); $dbr = MWEchoDbFactory::newFromDefault()->getEchoDb( DB_REPLICA );

View file

@ -208,7 +208,7 @@ class EchoNotificationController {
* this event type * this event type
*/ */
public static function getEventNotifyTypes( $eventType ) { public static function getEventNotifyTypes( $eventType ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$category = $attributeManager->getNotificationCategory( $eventType ); $category = $attributeManager->getNotificationCategory( $eventType );
@ -432,7 +432,7 @@ class EchoNotificationController {
* @return array * @return array
*/ */
public static function evaluateUserCallable( EchoEvent $event, $locator = EchoAttributeManager::ATTR_LOCATORS ) { public static function evaluateUserCallable( EchoEvent $event, $locator = EchoAttributeManager::ATTR_LOCATORS ) {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$type = $event->getType(); $type = $event->getType();
$result = []; $result = [];
foreach ( $attributeManager->getUserCallable( $type, $locator ) as $callable ) { foreach ( $attributeManager->getUserCallable( $type, $locator ) as $callable ) {

View file

@ -593,7 +593,7 @@ class EchoEvent extends EchoAbstractEntity implements Bundleable {
* @return string * @return string
*/ */
public function getCategory() { public function getCategory() {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
return $attributeManager->getNotificationCategory( $this->type ); return $attributeManager->getNotificationCategory( $this->type );
} }
@ -603,7 +603,7 @@ class EchoEvent extends EchoAbstractEntity implements Bundleable {
* @return string * @return string
*/ */
public function getSection() { public function getSection() {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
return $attributeManager->getNotificationSection( $this->type ); return $attributeManager->getNotificationSection( $this->type );
} }

View file

@ -22,7 +22,7 @@ class NotificationPager extends ReverseChronologicalPager {
} }
public function getQueryInfo() { public function getQueryInfo() {
$attributeManager = EchoAttributeManager::newFromGlobalVars(); $attributeManager = EchoServices::getInstance()->getAttributeManager();
$eventTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' ); $eventTypes = $attributeManager->getUserEnabledEvents( $this->getUser(), 'web' );
return [ return [

View file

@ -56,7 +56,7 @@ class SpecialDisplayNotificationsConfiguration extends UnlistedSpecialPage {
public function __construct() { public function __construct() {
parent::__construct( 'DisplayNotificationsConfiguration' ); parent::__construct( 'DisplayNotificationsConfiguration' );
$this->attributeManager = EchoAttributeManager::newFromGlobalVars(); $this->attributeManager = EchoServices::getInstance()->getAttributeManager();
$this->notificationController = new EchoNotificationController(); $this->notificationController = new EchoNotificationController();
} }

View file

@ -30,4 +30,9 @@ class EchoServicesTest extends MediaWikiIntegrationTestCase {
$this->assertInstanceOf( SubscriptionManager::class, $subscriptionManager ); $this->assertInstanceOf( SubscriptionManager::class, $subscriptionManager );
} }
public function testGetAttributeManager(): void {
$attributeManager = $this->echoServices->getAttributeManager();
$this->assertInstanceOf( EchoAttributeManager::class, $attributeManager );
}
} }

View file

@ -3,11 +3,7 @@
/** /**
* @covers \EchoAttributeManager * @covers \EchoAttributeManager
*/ */
class EchoAttributeManagerTest extends MediaWikiTestCase { class EchoAttributeManagerTest extends MediaWikiUnitTestCase {
public function testNewFromGlobalVars() {
$this->assertInstanceOf( EchoAttributeManager::class, EchoAttributeManager::newFromGlobalVars() );
}
public static function getUserLocatorsProvider() { public static function getUserLocatorsProvider() {
return [ return [