Kaldari has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/49846


Change subject: Implementing new prefs system for Echo
......................................................................

Implementing new prefs system for Echo

Change-Id: Id745ed6cf1c92695569597fab6ea662bac8c76c0
---
M Echo.i18n.php
M Echo.php
M Hooks.php
M Notifier.php
M controller/NotificationController.php
M formatters/BasicFormatter.php
M includes/EmailBatch.php
M model/Event.php
8 files changed, 154 insertions(+), 120 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/46/49846/1

diff --git a/Echo.i18n.php b/Echo.i18n.php
index c6edd5d..d5e247b 100644
--- a/Echo.i18n.php
+++ b/Echo.i18n.php
@@ -19,7 +19,7 @@
        'echo-pref-web' => 'Web',
        'echo-pref-email' => 'E-mail',
        'echo-pref-subscription-edit-user-talk' => 'Posts on my talk page',
-       'echo-pref-subscription-article-linked' => 'Creates a link to a page I 
created',
+       'echo-pref-subscription-page-linked' => 'Creates a link to a page I 
created',
        'echo-pref-subscription-reverted' => 'Reverts my edit',
        'echo-pref-email-frequency-never' => 'Do not send me any e-mail 
notifications',
        'echo-pref-email-frequency-immediately' => 'Individual notifications as 
they come in',
diff --git a/Echo.php b/Echo.php
index c0795f4..074ed37 100644
--- a/Echo.php
+++ b/Echo.php
@@ -228,47 +228,55 @@
        'edit-user-talk', // User talk page is edited
        'reverted', // An edit is undone or rolled-back
        'article-linked', // A page a user created is linked from another page
-       // These aren't ready yet, specifically they have no means of 
subscription
-#      'add-comment', // A signed comment is added to an existing section
-#      'add-talkpage-topic', // A new section is added to a talk page
 );
 
-// This array stores the configuration info for enabled events.
-// Possible parameters include category, priority, and usergroups.
-// If an event is not specified, it means the event belongs to the
-// 'other' category with priority 10, which is the lowest,
-// priority is ranging from 1 to 10.
+// Define the categories that notifications can belong to. Categories can be
+// assigned the following parameters: priority, nodismiss, and usergroups
+// If a notifications type doesn't have a category parameter, it is
+// automatically assigned to the 'other' category which is lowest priority and
+// has no preferences or dismissibility.
+// The priority parameter controls the order in which notifications are
+// displayed in preferences and batch emails. Priority ranges from 1 to 10. If
+// the priority is not specified, it defaults to 10, which is the lowest.
 // The usergroups param specifies an array of usergroups eligible to recieve 
the
-// notification type. If no usergroups parameter exists, all groups are 
eligible.
-$wgEchoEventDetails = array(
-       'welcome' => array(
-               'nodismiss' => array( 'web', 'email' ),
+// notifications in the category. If no usergroups parameter is specified, all
+// groups are eligible.
+// The nodismiss parameter disables the dismissability of notifications in the
+// category. It can either be set to true (to disable dismissability entirely),
+// or to an array of output formats (see $wgEchoNotifiers).
+$wgEchoNotificationCategories = array(
+       'system' => array(
+               'priority' => 9,
+               'nodismiss' => true,
+       ),
+       'other' => array(
+               'nodismiss' => true,
        ),
        'edit-user-talk' => array(
-               'category' => 'edit-user-talk',
                'priority' => 1,
                'nodismiss' => array( 'web' ),
        ),
        'reverted' => array(
-               'category' => 'edit-revert',
-               'priority' => 9
+               'priority' => 9,
        ),
-       'article-linked' => array(
-               'category' => 'cross-reference',
-               'priority' => 5
+       'page-linked' => array(
+               'priority' => 5,
+               'usergroups' => array( 'sysop' ),
        ),
 );
 
 // Definitions of the notification event types built into Echo.
 // If formatter-class isn't specified, defaults to EchoBasicFormatter.
-$wgEchoNotificationFormatters = array(
+$wgEchoNotificationDetails = array(
        'welcome' => array(
+               'category' => 'system',
                'title-message' => 'notification-new-user',
                'title-params' => array( 'agent' ),
                'payload' => array( 'welcome' ),
                'icon' => 'w',
        ),
        'edit-user-talk' => array(
+               'category' => 'edit-user-talk',
                'formatter-class' => 'EchoEditFormatter',
                'title-message' => 'notification-edit-talk-page2',
                'title-params' => array( 'agent', 'user' ),
@@ -282,23 +290,8 @@
                'email-body-batch-params' => array( 'agent', 'difflink', 
'summary' ),
                'icon' => 'chat',
        ),
-       'add-comment' => array(
-               'formatter-class' => 'EchoCommentFormatter',
-               'title-message' => 'notification-add-comment2',
-               'title-message-yours' => 'notification-add-comment-yours2',
-               'title-params' => array( 'agent', 'subject', 'title', 
'content-page' ),
-               'payload' => array( 'snippet' ),
-               'icon' => 'chat',
-       ),
-       'add-talkpage-topic' => array(
-               'formatter-class' => 'EchoCommentFormatter',
-               'title-message' => 'notification-add-talkpage-topic2',
-               'title-message-yours' => 
'notification-add-talkpage-topic-yours2',
-               'title-params' => array( 'agent', 'subject', 'title', 
'content-page' ),
-               'payload' => array( 'snippet' ),
-               'icon' => 'chat',
-       ),
        'reverted' => array(
+               'category' => 'edit-revert',
                'formatter-class' => 'EchoEditFormatter',
                'title-message' => 'notification-reverted2',
                'title-params' => array( 'agent', 'title', 'difflink', 'number' 
),
@@ -313,7 +306,8 @@
                'email-body-batch-params' => array( 'agent', 'title', 'number' 
),
                'icon' => 'revert',
        ),
-       'article-linked' => array(
+       'page-linked' => array(
+               'category' => 'page-linked',
                'formatter-class' => 'EchoArticleLinkedFormatter',
                'title-message' => 'notification-article-linked2',
                'title-params' => array( 'agent', 'title',  'title-linked' ),
diff --git a/Hooks.php b/Hooks.php
index a7ba701..aad9c20 100644
--- a/Hooks.php
+++ b/Hooks.php
@@ -185,7 +185,8 @@
         * @return bool true in all cases
         */
        public static function getPreferences( $user, &$preferences ) {
-               global $wgEchoDefaultNotificationTypes, $wgAuth, 
$wgEchoEnableEmailBatch, $wgEchoEventDetails;
+               global $wgEchoDefaultNotificationTypes, $wgAuth, 
$wgEchoEnableEmailBatch,
+                       $wgEchoNotifiers, $wgEchoNotificationDetails, 
$wgEchoNotificationCategories;
 
                // Show email frequency options
                $never = wfMessage( 'echo-pref-email-frequency-never' 
)->plain();
@@ -235,63 +236,55 @@
                        'default' => $emailContent,
                        'section' => 'echo/emailfrequency'
                );
+               
+               // Gather all the events and categories from other extensions
+               EchoEvent::gatherValidEchoEvents();
 
-               // Sort events by priority
-               $eventsAndPriorities = array();
-               foreach ( EchoEvent::gatherValidEchoEvents() as $enabledEvent ) 
{
-                       if ( isset( 
$wgEchoEventDetails[$enabledEvent]['priority'] ) ) {
-                               $eventsAndPriorities[$enabledEvent] = 
$wgEchoEventDetails[$enabledEvent]['priority'];
-                       } else {
-                               $eventsAndPriorities[$enabledEvent] = 10;
+               // Sort notification categories by priority
+               $categoriesAndPriorities = array();
+               foreach ( $wgEchoNotificationCategories as $category => 
$categoryData ) {
+                       // See if the category is not dismissable at all. Must 
do strict
+                       // comparison to true since nodismiss can also be an 
array
+                       if ( isset( $categoryData['nodismiss'] ) && 
$categoryData['nodismiss'] === true ) {
+                               continue;
+                       }
+                       // See if user is eligible to recieve this notification 
(per user group restrictions)
+                       if ( 
EchoNotificationController::getCategoryEligibility( $user, $category ) ) {
+                               $categoriesAndPriorities[$category] = 
EchoNotificationController::getCategoryPriority( $category );
                        }
                }
-               asort( $eventsAndPriorities );
-               $eventsAndPriorities = array_keys( $eventsAndPriorities );
+               asort( $categoriesAndPriorities );
+               $validSortedCategories = array_keys( $categoriesAndPriorities );
 
                // Show subscription options
+
+               // $oldPrefs are prefs that we are replacing
                $oldPrefs['email']['edit-user-talk'] = 'enotifusertalkpages';
-               $subTypes = array( 'web', 'email' );
-               foreach ( $subTypes as $subType ) {
-                       $options = array();
-                       foreach ( $eventsAndPriorities as $enabledEvent ) {
-                               if ( isset( 
$wgEchoEventDetails[$enabledEvent]['nodismiss'] )
-                                       && in_array( $subType, 
$wgEchoEventDetails[$enabledEvent]['nodismiss'] ) )
-                               {
-                                       continue;
-                               }
-                               // Make sure notifications of this subtype are 
possible for this event
-                               if ( isset( 
$wgEchoDefaultNotificationTypes[$enabledEvent] ) ) {
-                                       if ( 
!$wgEchoDefaultNotificationTypes[$enabledEvent][$subType] ) {
-                                               continue;
-                                       }
-                               // Make sure this subtype is an available 
notification type
-                               } elseif ( 
!$wgEchoDefaultNotificationTypes['all'][$subType] ) {
-                                       continue;
-                               }
-                               // Make sure the user is eligible to recieve 
this type of notification
-                               if ( 
!EchoNotificationController::getNotificationEligibility( $user, $enabledEvent ) 
) {
-                                       continue;
-                               }
-                               $eventMessage = wfMessage( 
'echo-pref-subscription-' . $enabledEvent )->plain();
-                               $options[$eventMessage] = $enabledEvent;
-                               // overwrite other preferences, if necessary
-                               if ( isset( $oldPrefs[$subType][$enabledEvent] 
) ) {
-                                       unset( 
$preferences[$oldPrefs[$subType][$enabledEvent]] );
-                               }
-                       }
-                       $preferences['echo-' . $subType . '-notifications'] = 
array(
-                               'type' => 'multiselect',
-                               'label-message' => 'echo-pref-' . $subType,
-                               'section' => 'echo/echosubscriptions',
-                               'options' => $options,
-                       );
+
+               // Build the columns (output formats)
+               $columns = array();
+               foreach ( $wgEchoNotifiers as $notifierType => $notifierData ) {
+                       $formatMessage = wfMessage( 'echo-pref-' . 
$notifierType )->plain();
+                       $columns[$formatMessage] = $notifierType;
                }
 
-               // Show fly-out display prefs
-               $preferences['echo-notify-hide-link'] = array(
-                       'type' => 'toggle',
-                       'label-message' => 'echo-pref-notify-hide-link',
-                       'section' => 'echo/displaynotifications',
+               // Build the rows (notification categories)
+               $rows = array();
+               foreach ( $validSortedCategories as $category ) {
+                       $categoryMessage = wfMessage( 'echo-pref-subscription-' 
. $category )->plain();
+                       $rows[$categoryMessage] = $category;
+               }
+
+               // TODO: Figure out the individual exceptions in the matrix and 
make them disabled.
+               // Depends on additional functionality being added to 
HTMLForm::HTMLCheckMatrix.
+               // TODO: Unset redundant prefs while we're cycling through the 
matrix to find exceptions.
+
+               $preferences['echo-subscriptions'] = array(
+                       'class' => 'HTMLCheckMatrix',
+                       'section' => 'echo/echosubscriptions',
+                       'rows' => $rows,
+                       'columns' => $columns,
+                       'default' => array( 'web-edit-user-talk', 
'email-reverted' ),
                );
                return true;
        }
diff --git a/Notifier.php b/Notifier.php
index 57836c8..a71a46a 100644
--- a/Notifier.php
+++ b/Notifier.php
@@ -27,16 +27,12 @@
                        return false;
                }
                // See if the user wants to receive emails for this type of 
event
-               if ( $user->getOption( 'echo-email-notifications' . 
$event->getType() ) ) {
-                       global $wgEchoEnableEmailBatch, $wgEchoEventDetails, 
$wgPasswordSender, $wgPasswordSenderName;
+               if ( $user->getOption( 'echo-subscriptionsemail-' . 
$event->getType() ) ) {
+                       global $wgEchoEnableEmailBatch, $wgPasswordSender, 
$wgPasswordSenderName;
        
                        // batched email notification
                        if ( $wgEchoEnableEmailBatch && $user->getOption( 
'echo-email-frequency' ) > 0 ) {
-                               // default priority is 10
-                               $priority = 10;
-                               if ( isset( 
$wgEchoEventDetails[$event->getType()]['priority'] ) ) {
-                                       $priority = 
$wgEchoEventDetails[$event->getType()]['priority'];
-                               }
+                               $priority = 
EchoNotificationController::getNotificationPriority( $event->getType() );
                                MWEchoEmailBatch::addToQueue( $user->getId(), 
$event->getId(), $priority );
                                return true;
                        }
diff --git a/controller/NotificationController.php 
b/controller/NotificationController.php
index 973c859..7662559 100644
--- a/controller/NotificationController.php
+++ b/controller/NotificationController.php
@@ -40,19 +40,81 @@
         * (based on user groups, not user preferences)
         *
         * @param $user User object
-        * @param $notificationType string A notification type defined in 
$wgEchoEventDetails
+        * @param $notificationType string A notification type defined in 
$wgEchoNotificationDetails
         * @return boolean
         */
        public static function getNotificationEligibility( $user, 
$notificationType ) {
-               global $wgEchoEventDetails;
+               $category = 
EchoNotificationController::getNotificationCategory( $notificationType );
+               return EchoNotificationController::getCategoryEligibility( 
$user, $category );
+       }
+
+       /**
+        * See if a user is eligible to recieve a certain type of notification
+        * (based on user groups, not user preferences)
+        *
+        * @param $user User object
+        * @param $category string A notification category defined in 
$wgEchoNotificationCategories
+        * @return boolean
+        */
+       public static function getCategoryEligibility( $user, $category ) {
+               global $wgEchoNotificationCategories;
                $usersGroups = $user->getGroups();
-               if ( isset( 
$wgEchoEventDetails[$notificationType]['usergroups'] ) ) {
-                       $allowedGroups = 
$wgEchoEventDetails[$notificationType]['usergroups'];
+               if ( isset( 
$wgEchoNotificationCategories[$category]['usergroups'] ) ) {
+                       $allowedGroups = 
$wgEchoNotificationCategories[$category]['usergroups'];
                        if ( !array_intersect( $usersGroups, $allowedGroups ) ) 
{
                                return false;
                        }
                }
                return true;
+       }
+
+       /**
+        * Get the priority for a specific notification type
+        *
+        * @param $notificationType string A notification type defined in 
$wgEchoNotificationDetails
+        * @return integer From 1 to 10 (10 is default)
+        */
+       public static function getNotificationPriority( $notificationType ) {
+               $category = $this->getNotificationCategory( $notificationType );
+               return EchoNotificationController::getCategoryPriority( 
$category );
+       }
+
+       /**
+        * Get the priority for a notification category
+        *
+        * @param $category string A notification category defined in 
$wgEchoNotificationCategories
+        * @return integer From 1 to 10 (10 is default)
+        */
+       public static function getCategoryPriority( $category ) {
+               global $wgEchoNotificationCategories;
+               if ( isset( 
$wgEchoNotificationCategories[$category]['priority'] ) ) {
+                       $priority = 
$wgEchoNotificationCategories[$category]['priority'];
+                       if ( $priority >= 1 && $priority <= 10 ) {
+                               return $priority;
+                       }
+               }
+               return 10;
+       }
+
+       /**
+        * Get the notification category for a notification type
+        *
+        * @param $notificationType string A notification type defined in 
$wgEchoNotificationDetails
+        * @return String The name of the notification category or 'other' if no
+        *     category is explicitly assigned.
+        */
+       public static function getNotificationCategory( $notificationType ) {
+               global $wgEchoNotificationDetails, 
$wgEchoNotificationCategories;
+               if ( isset( 
$wgEchoNotificationDetails[$notificationType]['category'] ) ) {
+                       $category = 
$wgEchoNotificationDetails[$notificationType]['category'];
+                       if ( isset( $wgEchoNotificationCategories[$category] ) 
) {
+                               return $category;
+                       } else {
+                               return 'other';
+                       }
+               } else {
+                       return 'other';
+               }
        }
 
        /**
@@ -240,7 +302,7 @@
         *     and body (for emails), or an error message
         */
        public static function formatNotification( $event, $user, $format = 
'text', $type = 'web' ) {
-               global $wgEchoNotificationFormatters;
+               global $wgEchoNotificationDetails;
 
                $eventType = $event->getType();
 
@@ -248,11 +310,11 @@
                // this hook should only be executed once to gather valid 
formatter
                if ( $runHook ) {
                        // allow extensions to define their own notification 
formatter
-                       wfRunHooks( 'BeforeFormatEchoNotification', array( 
&$wgEchoNotificationFormatters ) );
+                       wfRunHooks( 'BeforeFormatEchoNotification', array( 
&$wgEchoNotificationDetails ) );
                        $runHook = false;
                }
-               if ( isset( $wgEchoNotificationFormatters[$eventType] ) ) {
-                       $params = $wgEchoNotificationFormatters[$eventType];
+               if ( isset( $wgEchoNotificationDetails[$eventType] ) ) {
+                       $params = $wgEchoNotificationDetails[$eventType];
                        $notifier = EchoNotificationFormatter::factory( $params 
);
                        $notifier->setOutputFormat( $format );
 
diff --git a/formatters/BasicFormatter.php b/formatters/BasicFormatter.php
index 8f6172f..2c53313 100644
--- a/formatters/BasicFormatter.php
+++ b/formatters/BasicFormatter.php
@@ -101,7 +101,7 @@
         * @return array|string
         */
        public function format( $event, $user, $type ) {
-               global $wgEchoEventDetails;
+               global $wgEchoNotificationCategories;
 
                if ( $this->outputFormat === 'email' ) {
                        return $this->formatEmail( $event, $user, $type );
@@ -125,8 +125,9 @@
                ) . $output;
 
                // Add the hidden dismiss interface if the notification is 
dismissable
-               if ( !isset( $wgEchoEventDetails[$event->type]['nodismiss'] )
-                       || !in_array( 'web', 
$wgEchoEventDetails[$event->type]['nodismiss'] ) )
+               $category = 
EchoNotificationController::getNotificationCategory( $event->type );
+               if ( !isset( 
$wgEchoNotificationCategories[$category]['nodismiss'] )
+                       || !in_array( 'web', 
$wgEchoNotificationCategories[$category]['nodismiss'] ) )
                {
                        $output .= $this->formatDismissInterface( $event, $user 
);
                }
diff --git a/includes/EmailBatch.php b/includes/EmailBatch.php
index d86e2d9..cd57979 100644
--- a/includes/EmailBatch.php
+++ b/includes/EmailBatch.php
@@ -145,13 +145,8 @@
         * Add individual event template to the big email content
         */
        protected function appendContent( $event ) {
-               global $wgEchoEventDetails;
-
                // get the category for this event
-               $category = 'other';
-               if ( isset( $wgEchoEventDetails[$event->getType()]['category'] 
) ) {
-                       $category = 
$wgEchoEventDetails[$event->getType()]['category'];
-               }
+               $category = 
EchoNotificationController::getNotificationCategory( $event->getType() );
 
                $email = EchoNotificationController::formatNotification( 
$event, $this->mUser, 'email' );
                if ( !isset( $this->content[$category] ) ) {
diff --git a/model/Event.php b/model/Event.php
index 8fc709c..522ae97 100644
--- a/model/Event.php
+++ b/model/Event.php
@@ -104,20 +104,13 @@
         * Get a list of enabled Echo events, allow extensions to create their 
own events
         */
        public static function gatherValidEchoEvents() {
-               global $wgEchoEnabledEvents, $wgEchoEventDetails;
+               global $wgEchoEnabledEvents, $wgEchoNotificationDetails, 
$wgEchoNotificationCategories;
 
                static $runHook = true;
                // this hook should only be executed once to gather valid echo 
events
                if ( $runHook ) {
                        // allow extensions to define their own event
-                       wfRunHooks( 'BeforeCreateEchoEvent', array( 
&$wgEchoEnabledEvents, &$wgEchoEventDetails ) );
-
-                       foreach ( $wgEchoEventDetails as $event => $detail ) {
-                               if ( isset( $detail['priority'] ) && 
$detail['priority'] < 1 && $detail['priority'] > 10 ) {
-                                       throw new MWException( "$event: Valid 
event priority is ranging from 1 to 10" );
-                               }
-                       }
-
+                       wfRunHooks( 'BeforeCreateEchoEvent', array( 
&$wgEchoEnabledEvents, &$wgEchoNotificationDetails, 
&$wgEchoNotificationCategories ) );
                        $runHook = false;
                }
 

-- 
To view, visit https://gerrit.wikimedia.org/r/49846
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Id745ed6cf1c92695569597fab6ea662bac8c76c0
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Kaldari <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to