Merge "Make AttributeManager a service"

This commit is contained in:
jenkins-bot 2021-02-26 23:45:26 +00:00 committed by Gerrit Code Review
commit ac8d21d2a1
15 changed files with 40 additions and 61 deletions

View file

@ -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' );

View file

@ -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
*

View file

@ -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',

View file

@ -37,4 +37,9 @@ class EchoServices {
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()
->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 ];

View file

@ -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 ) ) {

View file

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

View file

@ -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 ) {

View file

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

View file

@ -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 ) {

View file

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

View file

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

View file

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

View file

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

View file

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