Bsitu has uploaded a new change for review.

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


Change subject: Add HTML email support to email digest
......................................................................

Add HTML email support to email digest

This is merely an initial draft and needs a lot of improvement/tests

Change-Id: I3b881acbcf4b18fc0401364ea0a6bc993d2c2246
---
M Echo.i18n.php
M formatters/BasicFormatter.php
M includes/EmailBatch.php
M includes/EmailFormatter.php
4 files changed, 289 insertions(+), 45 deletions(-)


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

diff --git a/Echo.i18n.php b/Echo.i18n.php
index 353c899..e0ddb34 100644
--- a/Echo.i18n.php
+++ b/Echo.i18n.php
@@ -19,8 +19,7 @@
        'prefs-newmessageindicator' => 'New message indicator',
        'echo-pref-send-me' => 'Send me:',
        'echo-pref-send-to' => 'Send to:',
-       // Update this to 'Email format' when HTML email is ready for email 
digest
-       'echo-pref-email-format' => 'Individual email format:',
+       'echo-pref-email-format' => 'Email format:',
        'echo-pref-web' => 'Web',
        'echo-pref-email' => 'Email',
        'echo-pref-email-frequency-never' => 'Do not send me any email 
notifications',
@@ -159,6 +158,19 @@
 
 $5',
 
+
+
+
+       'echo-email-batch-body-intro-daily' => 'Hi $1,
+Here\'s a summary of today\'s activity on {{SITENAME}} for you
+',
+       'echo-email-batch-body-intro-weekly' => 'Hi $1,
+Here\'s a summary of this week\'s activity on {{SITENAME}} for you
+',
+       'echo-email-batch-link-text-read-them-on-sitename' => 'Read them on 
{{SITENAME}}',
+
+
+
        // Supressed Revisions
        'echo-rev-deleted-text-view' => 'This page revision has been 
suppressed',
 );
diff --git a/formatters/BasicFormatter.php b/formatters/BasicFormatter.php
index 876432d..f8a22dc 100644
--- a/formatters/BasicFormatter.php
+++ b/formatters/BasicFormatter.php
@@ -260,10 +260,9 @@
        protected function formatEmail( $event, $user, $type ) {
                global $wgEchoNotifications;
 
-               if ( $this->bundleData['use-bundle'] && 
$this->email['batch-bundle-body'] ) {
-                       $key = $this->email['batch-bundle-body'];
-               } else {
-                       $key = $this->email['batch-body'];
+               // Email digest
+               if ( $type === 'emaildigest' ) {
+                       return $this->formatEmailDigest( $event, $user );
                }
 
                // Echo single email
@@ -276,8 +275,6 @@
                        'subject' => $this->formatFragment( 
$this->email['subject'], $event, $user )->text(),
                        // Single email text body
                        'body' => $textEmailFormatter->formatEmail(),
-                       // Email digest text body
-                       'batch-body' => $this->formatFragment( $key, $event, 
$user )->text()
                );
 
                $format = MWEchoNotifUser::newFromUser( $user 
)->getEmailFormat();
@@ -297,6 +294,32 @@
        }
 
        /**
+        * Format text and/or html verion of email digest
+        * @param $event EchoEvent
+        * @param $user User
+        * @return array
+        */
+       protected function formatEmailDigest( $event, $user ) {
+               if ( $this->bundleData['use-bundle'] && 
$this->email['batch-bundle-body'] ) {
+                       $key = $this->email['batch-bundle-body'];
+               } else {
+                       $key = $this->email['batch-body'];
+               }
+
+               // Email digest text body
+               $content = array( 'batch-body' => $this->formatFragment( $key, 
$event, $user )->text() );
+               $format = MWEchoNotifUser::newFromUser( $user 
)->getEmailFormat();
+               if ( $format == EchoHooks::EMAIL_FORMAT_HTML ) {
+                       $outputFormat = $this->outputFormat;
+                       $this->setOutputFormat( 'htmlemail' );
+                       $content['batch-body-html'] = $this->formatFragment( 
$key, $event, $user )->parse();
+                       $content['icon'] = $this->icon;
+                       $this->setOutputFormat( $outputFormat );
+               }
+               return $content;
+       }
+
+       /**
         * Creates a notification fragment based on a message and parameters
         *
         * @param $details array An i18n message and parameters to pass to the 
message
diff --git a/includes/EmailBatch.php b/includes/EmailBatch.php
index cd6686d..2425587 100644
--- a/includes/EmailBatch.php
+++ b/includes/EmailBatch.php
@@ -159,12 +159,12 @@
                // get the category for this event
                $category = $event->getCategory();
                $event->setBundleHash( $hash );
-               $email = EchoNotificationController::formatNotification( 
$event, $this->mUser, 'email', 'email' );
+               $email = EchoNotificationController::formatNotification( 
$event, $this->mUser, 'email', 'emaildigest' );
 
                if ( !isset( $this->content[$category] ) ) {
                        $this->content[$category] = array();
                }
-               $this->content[$category][] = $email['batch-body'];
+               $this->content[$category][] = $email;
        }
 
        /**
@@ -218,12 +218,6 @@
        public function sendEmail() {
                global $wgNotificationSender, $wgNotificationSenderName, 
$wgNotificationReplyName, $wgEchoEmailFooterAddress;
 
-               // global email footer
-               $footer = wfMessage( 'echo-email-footer-default' )
-                               ->inLanguage( $this->mUser->getOption( 
'language' ) )
-                               ->params( $wgEchoEmailFooterAddress, '' )
-                               ->text();
-
                // @Todo - replace them with the CONSTANT in 33810 once it is 
merged 
                if ( $this->mUser->getOption( 'echo-email-frequency' ) == 7 ) {
                        $frequency = 'weekly';
@@ -233,6 +227,22 @@
                        $emailDeliveryMode = 'daily_digest';
                }
 
+               // Echo digest email mode
+               $emailDigest = new EchoEmailDigest( $user, $this->content, 
$frequency );
+               
+               $textEmailFormatter = new EchoTextEmailFormatter( $emailDigest 
);
+               
+               $body = $textEmailFormatter->formatEmail();
+               
+               $format = MWEchoNotifUser::newFromUser( $this->mUser 
)->getEmailFormat();
+               if ( $format == EchoHooks::EMAIL_FORMAT_HTML ) {
+                       $htmlEmailFormatter = new EchoHTMLEmailFormatter( 
$emailDigest );
+                       $body = array(
+                               'text' => $body,
+                               'html' => $htmlEmailFormatter->formatEmail()
+                       );
+               }
+               
                // email subject
                if ( $this->count > self::$displaySize ) {
                        $count = wfMessage( 'echo-notification-count' 
)->params( self::$displaySize )->text();
@@ -240,13 +250,6 @@
                        $count = $this->count;
                }
                $subject = wfMessage( 'echo-email-batch-subject-' . $frequency 
)->params( $count, $this->count )->text();
-               $body = wfMessage( 'echo-email-batch-body-' . $frequency 
)->params(
-                               $this->mUser->getName(),
-                               $count,
-                               $this->count,
-                               $this->listToText(),
-                               $footer
-                       )->text();
 
                $toAddress = new MailAddress( $this->mUser );
                $fromAddress = new MailAddress( $wgNotificationSender, 
$wgNotificationSenderName );
diff --git a/includes/EmailFormatter.php b/includes/EmailFormatter.php
index 45a5c85..6d92717 100644
--- a/includes/EmailFormatter.php
+++ b/includes/EmailFormatter.php
@@ -108,8 +108,6 @@
        protected $user;
 
        /**
-        * @param $notifFormatter EchoBasicFormatter
-        * @param $event EchoEvent
         * @param $user User
         */
        public function __construct( User $user, array $component ) {
@@ -179,6 +177,27 @@
        protected function getSecondaryLinkCSS() {
                return 'text-decoration: none;font-size: 10px;font-family: 
arial;
                        color: #808184';
+       }
+
+       /**
+        * Get the notification icon path
+        * @param $icon string
+        * @return string
+        */
+       protected function getNotifIcon( $icon ) {
+               global $wgEchoNotificationIcons, $wgExtensionAssetsPath;
+
+               $iconInfo = $wgEchoNotificationIcons[$icon];
+               if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) {
+                       $iconUrl = $iconInfo['url'];
+               } else {
+                       if ( !isset( $iconInfo['path'] ) || !$iconInfo['path'] 
) {
+                               $iconInfo = 
$wgEchoNotificationIcons['placeholder'];
+                       }
+                       $iconUrl = "$wgExtensionAssetsPath/{$iconInfo['path']}";
+               }
+
+               return wfExpandUrl( $iconUrl ); 
        }
 }
 
@@ -297,19 +316,7 @@
         * @return string
         */
        public function buildEmailIcon( $format ) {
-               global  $wgEchoNotificationIcons, $wgExtensionAssetsPath;
-
-               $iconInfo = 
$wgEchoNotificationIcons[$this->notifFormatter->getValue( 'icon' )];
-               if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) {
-                       $iconUrl = $iconInfo['url'];
-               } else {
-                       if ( !isset( $iconInfo['path'] ) || !$iconInfo['path'] 
) {
-                               $iconInfo = 
$wgEchoNotificationIcons['placeholder'];
-                       }
-                       $iconUrl = "$wgExtensionAssetsPath/{$iconInfo['path']}";
-               }
-
-               return wfExpandUrl( $iconUrl );
+               return $this->getNotifIcon( $this->notifFormatter->getValue( 
'icon' ) );
        }
 
        /**
@@ -398,15 +405,214 @@
  */
 class EchoEmailDigest extends EchoEmailMode {
 
-       public function __construct( $user ) {
-               parent::__construct( $user, array( 'header', 'intro', 
'digestList' ) );
+       /**
+        * Email digest mode
+        */
+       protected $digestMode;
+
+       /**
+        * Raw email digest list
+        */
+       protected $rawDigestList;
+
+       public function __construct( User $user, array $rawDigestList, 
$digestMode = 'daily' ) {
+               parent::__construct( $user, array( 'emailIcon', 'intro', 
'digestList', 'action' ) );
+               // Some data validation
+               if ( !in_array( $digestMode, array( 'daily', 'weekly' ) ) ) {
+                       $digestMode = 'daily';
+               }
+               $this->digestMode = $digestMode;
+               $this->rawDigestList = $rawDigestList;
        }
 
-       public function buildHeader() {}
-       public function buildIntro() {}
-       public function buildDigestList() {}
+       /**
+        * Build the email icon component
+        * @param $format string 'text'/'html'
+        * @return string
+        */
+       public function buildEmailIcon( $format ) {
+               global $wgLogo;
 
-       public function getTextTemplate() {}
-       public function getHTMLTemplate() {}
+               if ( $wgLogo === false ) {
+                       $logo = "{$wgStylePath}/common/images/wiki.png";
+               } else {
+                       $logo = $wgLogo;
+               }
+
+               return wfExpandUrl( $logo );
+       }
+
+       /**
+        * Build the intro component
+        * @param $format string 'text'/'html'
+        * @return string
+        */
+       public function buildIntro( $format ) {
+               $message = wfMessage( 'echo-email-batch-body-intro-' . 
$this->digestMode );
+
+               if ( $format === 'text' ) {
+                       return $message->text();
+               } else {
+                       return $message->parse();
+               }
+       }
+
+       /**
+        * Build the digestList component
+        * @param $format 'text'/'html'
+        * @return string
+        */
+       public function buildDigestList( $format ) {
+               if ( !$this->rawDigestList ) {
+                       return '';
+               }
+
+               if ( $format === 'text' ) {
+                       $result = array();
+                       // build the text section for each category
+                       foreach( $this->rawDigestList as $category => $notifs ) 
{
+                               // Messages that can be used here:
+                               // * echo-category-title-system
+                               // * echo-category-title-other
+                               // * echo-category-title-edit-user-talk
+                               // * echo-category-title-reverted
+                               // * echo-category-title-article-linked
+                               // * echo-category-title-mention
+                               $output = wfMessage( 'echo-category-title-' . 
$category )->numParams( count( $notifs ) )->text()
+                                       . wfMessage( 'colon-separator' 
)->text() . "\n";
+       
+                               foreach( $notifs as $notif ) {
+                                       $output .= "\n " . wfMessage( 
'echo-email-batch-bullet' )->text() . ' ' . $notif['batch-body'];
+                               }
+                               $result[] = $output;
+                       }
+       
+                       // for prepending and appending 
'echo-email-batch-separator'
+                       $result = array_merge( array( '' ), $result, array( '' 
) );
+
+                       return trim(
+                               implode(
+                                       "\n\n" . wfMessage( 
'echo-email-batch-separator' )->text() . "\n\n",
+                                       $result
+                               )
+                       );
+               } else {
+                       $result = array();
+                       // build the html section for each category
+                       foreach( $this->rawDigestList as $category => $notifs ) 
{
+                               // Messages that can be used here:
+                               // * echo-category-title-system
+                               // * echo-category-title-other
+                               // * echo-category-title-edit-user-talk
+                               // * echo-category-title-reverted
+                               // * echo-category-title-article-linked
+                               // * echo-category-title-mention
+                               $output = wfMessage( 'echo-category-title-' . 
$category )->numParams( count( $notifs ) )->escaped()
+                                       . wfMessage( 'colon-separator' 
)->escaped() . "<br /><hr /><br />";
+       
+                               foreach( $notifs as $notif ) {
+                                       $output .= "<br />" . 
$this->$this->getNotifIcon( $notif['icon'] ) . '' . $notif['batch-body-html'];
+                               }
+                               $result[] = $output;
+                       }
+       
+                       return trim( implode( '<br /></br />', $result ) );
+               }
+       }
+
+       /**
+        * Build the action component
+        * @param $format string 'html'/'text'
+        * @return string
+        */
+       public function buildAction( $format ) {
+               $title = Special::getTitleFor( 'Notifications' );
+               if ( $format === 'text' ) {
+                       return wfMessage( 
'echo-email-batch-link-text-read-them-on-sitename' )->text()
+                                       . wfMessage( 'colon-separator' )->text()
+                                       . '<'
+                                       . $title->getCanonicalURL()
+                                       . '>';
+               } else {
+                       return Linker::link(
+                               $title,
+                               wfMessage( 
'echo-email-batch-link-text-read-them-on-sitename' )->escaped(),
+                               array( 'style' => $this->getPrimaryLinkCSS() ),
+                               array(),
+                               array( 'http' )
+                       );
+               }
+       }
+
+       public function getTextTemplate() {
+               return <<< EOF
+%%intro%%
+
+%%digestList%%
+
+%%action%%
+
+%footer%
+
+EOF;
+       }
+
+       public function getHTMLTemplate() {
+               return <<< EOF
+<html><head></head><body>
+<table cellspacing="0" cellpadding="0" border="0" width="100%" align="center">
+<tr>
+       <td bgcolor="#E6E7E8"><center>
+               <br /><br />
+               <table cellspacing="0" cellpadding="0" border="0" width="600">
+                       <tr>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "61">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "469" style = 
"line-height:50px;">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                       </tr><tr>
+                               <td bgcolor="#FFFFFF" width = "35" 
rowspan="2">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "61" align = 
"center" valign="top" rowspan="2"><img src="%%emailIcon%%" alt = "" height = 
"100" width = "100"></td>
+                               <td bgcolor="#FFFFFF" width = "369" style = 
"font-family:sans-serif; font-size:15px; line-height:19px; 
color:#C9C9C9;">%%intro%%</td>
+                               <td bgcolor="#FFFFFF" width = "35" rowspan = 
"2">&nbsp;</td>
+                       </tr><tr>
+                               <td bgcolor="#FFFFFF" width = "61" style = 
"font-family: sans-serif; font-size:16px; line-height: 20px; font-weight: 600;">
+                                       <table cellspacing="0" cellpadding="0" 
border="0" style="margin-top: 12px;">
+                                               <tr>
+                                                       <td bgcolor="#FFFFFF" 
style="font-family: sans-serif; font-size:16px; font-weight: 600;">
+                                                               %%digestList%%
+                                                       </td>
+                                               </tr>
+                                       </table>
+                                       <table cellspacing="0" cellpadding="0" 
border="0" style="margin-top: 25px;">
+                                               <tr>
+                                                       <td bgcolor="#F8F8F8" 
style="font-family: sans-serif; font-size:14px;">
+                                                               %%action%%
+                                                       </td>
+                                               </tr>
+                                       </table>
+                               </td>
+                       </tr><tr>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "61">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "369" style = 
"line-height:30px;">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                       </tr><tr>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "61">&nbsp;</td>
+                               <td bgcolor="#FFFFFF" width = "369" style = 
"font-family:sans-serif; font-size:12px; line-height:15px; color:#C9C9C9"><br />
+                                       %%footer%%
+                                       <br /><br />
+                               </td>
+                               <td bgcolor="#FFFFFF" width = "35">&nbsp;</td>
+                       </tr>
+               </table>
+               <br><br></center>
+       </td>
+</tr>
+</table>
+</body></html>
+EOF;
+       }
 
 }

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

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

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

Reply via email to