2016-07-20 14:53:36 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoIcon {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $icon Name of icon as registered in BeforeCreateEchoEvent hook
|
|
|
|
* @param string $dir either 'ltr' or 'rtl'
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getUrl( $icon, $dir ) {
|
|
|
|
global $wgEchoNotificationIcons, $wgExtensionAssetsPath;
|
|
|
|
if ( !isset( $wgEchoNotificationIcons[$icon] ) ) {
|
|
|
|
throw new InvalidArgumentException( "The $icon icon is not registered" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$iconInfo = $wgEchoNotificationIcons[$icon];
|
|
|
|
$needsPrefixing = true;
|
|
|
|
|
|
|
|
// Now we need to check it has a valid url/path
|
|
|
|
if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) {
|
|
|
|
$iconUrl = $iconInfo['url'];
|
|
|
|
$needsPrefixing = false;
|
|
|
|
} elseif ( isset( $iconInfo['path'] ) && $iconInfo['path'] ) {
|
|
|
|
$iconUrl = $iconInfo['path'];
|
|
|
|
} else {
|
|
|
|
// Fallback to hardcoded 'placeholder'. This is used if someone
|
|
|
|
// doesn't configure the 'site' icon for example.
|
|
|
|
$icon = 'placeholder';
|
|
|
|
$iconUrl = $wgEchoNotificationIcons['placeholder']['path'];
|
|
|
|
}
|
|
|
|
|
|
|
|
// Might be an array with different icons for ltr/rtl
|
|
|
|
if ( is_array( $iconUrl ) ) {
|
|
|
|
if ( !isset( $iconUrl[$dir] ) ) {
|
|
|
|
throw new UnexpectedValueException( "Icon type $icon doesn't have an icon for $dir directionality" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$iconUrl = $iconUrl[$dir];
|
|
|
|
}
|
|
|
|
|
|
|
|
// And if it was a 'path', stick the assets path in front
|
|
|
|
if ( $needsPrefixing ) {
|
|
|
|
$iconUrl = "$wgExtensionAssetsPath/$iconUrl";
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iconUrl;
|
|
|
|
}
|
|
|
|
|
2016-08-11 19:22:27 +00:00
|
|
|
/**
|
|
|
|
* Get a link to a rasterized version of the icon
|
|
|
|
*
|
|
|
|
* @param string $icon Icon name
|
2018-06-17 16:56:02 +00:00
|
|
|
* @param string $lang
|
2016-08-11 19:22:27 +00:00
|
|
|
* @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 ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$iconUrl = wfScript( 'load' ) . '?' . wfArrayToCgi( [
|
2016-08-11 19:22:27 +00:00
|
|
|
'modules' => 'ext.echo.emailicons',
|
|
|
|
'image' => $icon,
|
|
|
|
'lang' => $lang,
|
|
|
|
'format' => 'rasterized'
|
2016-12-05 18:51:07 +00:00
|
|
|
] );
|
2016-08-11 19:22:27 +00:00
|
|
|
} else {
|
|
|
|
// For icons that are defined by URL
|
|
|
|
$iconUrl = $wgEchoNotificationIcons[ $icon ][ 'url' ];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $iconUrl;
|
|
|
|
}
|
|
|
|
|
2016-07-20 14:53:36 +00:00
|
|
|
}
|