| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\Notifications\Formatters; |
| 4 |
|
| 5 |
use InvalidArgumentException; |
| 6 |
use UnexpectedValueException; |
| 7 |
|
| 8 |
class EchoIcon { |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
public static function getUrl( $icon, $dir ) { |
| 16 |
global $wgEchoNotificationIcons, $wgExtensionAssetsPath; |
| 17 |
if ( !isset( $wgEchoNotificationIcons[$icon] ) ) { |
| 18 |
throw new InvalidArgumentException( "The $icon icon is not registered" ); |
| 19 |
} |
| 20 |
|
| 21 |
$iconInfo = $wgEchoNotificationIcons[$icon]; |
| 22 |
$needsPrefixing = true; |
| 23 |
|
| 24 |
|
| 25 |
if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) { |
| 26 |
$iconUrl = $iconInfo['url']; |
| 27 |
$needsPrefixing = false; |
| 28 |
} elseif ( isset( $iconInfo['path'] ) && $iconInfo['path'] ) { |
| 29 |
$iconUrl = $iconInfo['path']; |
| 30 |
} else { |
| 31 |
|
| 32 |
|
| 33 |
$icon = 'placeholder'; |
| 34 |
$iconUrl = $wgEchoNotificationIcons['placeholder']['path']; |
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
if ( is_array( $iconUrl ) ) { |
| 39 |
if ( !isset( $iconUrl[$dir] ) ) { |
| 40 |
throw new UnexpectedValueException( "Icon type $icon doesn't have an icon for $dir directionality" ); |
| 41 |
} |
| 42 |
|
| 43 |
$iconUrl = $iconUrl[$dir]; |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
if ( $needsPrefixing ) { |
| 48 |
$iconUrl = "$wgExtensionAssetsPath/$iconUrl"; |
| 49 |
} |
| 50 |
|
| 51 |
return $iconUrl; |
| 52 |
} |
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public static function getRasterizedUrl( $icon, $lang ) { |
| 62 |
global $wgEchoNotificationIcons; |
| 63 |
if ( !isset( $wgEchoNotificationIcons[$icon] ) ) { |
| 64 |
throw new InvalidArgumentException( "The $icon icon is not registered" ); |
| 65 |
} |
| 66 |
|
| 67 |
$url = $wgEchoNotificationIcons[ $icon ][ 'url' ] ?? null; |
| 68 |
|
| 69 |
|
| 70 |
if ( $url === false ) { |
| 71 |
$icon = 'placeholder'; |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
if ( $url === false || $url === null ) { |
| 77 |
$iconUrl = wfScript( 'load' ) . '?' . wfArrayToCgi( [ |
| 78 |
'modules' => 'ext.echo.emailicons', |
| 79 |
'image' => $icon, |
| 80 |
'lang' => $lang, |
| 81 |
'format' => 'rasterized' |
| 82 |
] ); |
| 83 |
} else { |
| 84 |
|
| 85 |
$iconUrl = $wgEchoNotificationIcons[ $icon ][ 'url' ]; |
| 86 |
} |
| 87 |
|
| 88 |
return $iconUrl; |
| 89 |
} |
| 90 |
|
| 91 |
} |