Add a rasterized email icons to Echo modules for email

Make sure we send rasterized png emails instead of SVGs in the
email formatter.

Bug: T127794
Change-Id: Ia28757bda521ec182f75458c8e52d54847d15681
This commit is contained in:
Moriel Schottlender 2016-08-11 12:22:27 -07:00 committed by Mooeypoo
parent c4121e7ff8
commit 5fefbe4b03
6 changed files with 107 additions and 3 deletions

View file

@ -353,6 +353,11 @@ $wgResourceModules += array(
'selectorWithoutVariant' => '.oo-ui-icon-{name}',
'selectorWithVariant' => '.oo-ui-image-{variant}.oo-ui-icon-{name}',
),
'ext.echo.emailicons' => array(
'class' => 'ResourceLoaderEchoImageModule',
'selector' => '.mw-echo-icon-{name}',
'localBasePath' => $wgExtensionDirectory, // Extension path
) + $echoResourceTemplate,
);
unset( $echoResourceTemplate );

View file

@ -113,6 +113,7 @@ $wgAutoloadClasses += [
'RemoveInvalidNotification' => __DIR__ . '/maintenance/removeInvalidNotification.php',
'RemoveInvalidTargetPage' => __DIR__ . '/maintenance/removeInvalidTargetPage.php',
'RemoveOrphanedEvents' => __DIR__ . '/maintenance/removeOrphanedEvents.php',
'ResourceLoaderEchoImageModule' => __DIR__ . '/includes/ResourceLoaderEchoImageModule.php',
'SpecialDisplayNotificationsConfiguration' => __DIR__ . '/includes/special/SpecialDisplayNotificationsConfiguration.php',
'SpecialNotifications' => __DIR__ . '/includes/special/SpecialNotifications.php',
'SpecialNotificationsFormatter' => __DIR__ . '/includes/formatters/SpecialNotificationsFormatter.php',

View file

@ -0,0 +1,59 @@
<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
/**
* A sibling of secret special sauce.
* @see ResourceLoaderOOUIImageModule for familial resemblence
*/
class ResourceLoaderEchoImageModule extends ResourceLoaderImageModule {
protected function loadFromDefinition() {
global $wgEchoNotificationIcons;
if ( $this->definition === null ) {
return;
}
$images = array();
foreach ( $wgEchoNotificationIcons as $iconName => $definition ) {
// FIXME: We also have a 'site' icon which is "magical"
// and uses witchcraft and should be handled specifically
$paths = '';
if ( isset( $definition[ 'path' ] ) ) {
if ( is_array( $definition[ 'path' ] ) ) {
foreach ( $definition[ 'path' ] as $dir => $p ) {
// Has both rtl and ltr definitions
$paths[ $dir ] = $p;
}
} else {
$paths = $definition[ 'path' ];
}
if ( !empty( $paths ) ) {
$images[ $iconName ][ 'file' ] = $paths;
}
}
}
$this->definition[ 'images' ] = $images;
$this->definition[ 'selector' ] = '.oo-ui-icon-{name}';
// Parent
parent::loadFromDefinition();
}
}

View file

@ -175,9 +175,10 @@ EOF;
*/
protected function applyStyleToEvent( EchoEventPresentationModel $model ) {
$iconUrl = wfExpandUrl(
EchoIcon::getUrl( $model->getIconType(), $this->language->getDir() ),
EchoIcon::getRasterizedUrl( $model->getIconType(), $this->language->getCode() ),
PROTO_CANONICAL
);
$imgSrc = Sanitizer::encodeAttribute( $iconUrl );
// notification text

View file

@ -6,7 +6,6 @@ class EchoHtmlEmailFormatter extends EchoEventFormatter {
const SECONDARY_LINK_STYLE = 'text-decoration: none;font-size: 10px;font-family: Arial, Helvetica, sans-serif; color: #808184;';
protected function formatModel( EchoEventPresentationModel $model ) {
$subject = $model->getSubjectMessage()->parse();
$intro = $model->getHeaderMessage()->parse();
@ -26,7 +25,7 @@ class EchoHtmlEmailFormatter extends EchoEventFormatter {
}
$iconUrl = wfExpandUrl(
EchoIcon::getUrl( $model->getIconType(), $this->language->getDir() ),
EchoIcon::getRasterizedUrl( $model->getIconType(), $this->language->getCode() ),
PROTO_CANONICAL
);

View file

@ -46,4 +46,43 @@ class EchoIcon {
return $iconUrl;
}
/**
* Get a link to a rasterized version of the icon
*
* @param string $icon Icon name
* @param string $lang Language
* @return string URL to the rasterized version of the icon
*/
public static function getRasterizedUrl( $icon, $lang ) {
global $wgEchoNotificationIcons;
if ( !isset( $wgEchoNotificationIcons[$icon] ) ) {
throw new InvalidArgumentException( "The $icon icon is not registered" );
}
$url = isset( $wgEchoNotificationIcons[ $icon ][ 'url' ] ) ?
$wgEchoNotificationIcons[ $icon ][ 'url' ] :
null;
// If the defined URL is explicitly false, use placeholder
if ( $url === false ) {
$icon = 'placeholder';
}
// If the URL is null or false call the resource loader
// rasterizing module
if ( $url === false || $url === null ) {
$iconUrl = wfScript( 'load' ) . '?' . wfArrayToCgi( array(
'modules' => 'ext.echo.emailicons',
'image' => $icon,
'lang' => $lang,
'format' => 'rasterized'
) );
} else {
// For icons that are defined by URL
$iconUrl = $wgEchoNotificationIcons[ $icon ][ 'url' ];
}
return $iconUrl;
}
}