2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
2013-03-05 19:26:26 +00:00
|
|
|
|
2012-11-28 02:17:52 +00:00
|
|
|
/**
|
2013-03-05 19:26:26 +00:00
|
|
|
* Abstract class for constructing a notification message, this class includes
|
|
|
|
* only the most generic formatting functionality as it may be extended by
|
|
|
|
* notification formatters for other extensions with unique content or
|
|
|
|
* requirements.
|
2012-11-28 02:17:52 +00:00
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
abstract class EchoNotificationFormatter {
|
2013-03-05 19:26:26 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* List of valid output format
|
|
|
|
* @var array
|
|
|
|
*/
|
2016-01-13 00:53:01 +00:00
|
|
|
protected $validOutputFormats = array( 'text', 'flyout', 'email', 'htmlemail' );
|
2013-03-05 19:26:26 +00:00
|
|
|
|
2013-09-26 23:25:46 +00:00
|
|
|
/**
|
|
|
|
* List of valid distribution type
|
|
|
|
*/
|
2014-09-11 21:02:28 +00:00
|
|
|
protected $validDistributionType = array( 'web', 'email', 'emaildigest', 'emailsubject' );
|
2013-09-26 23:25:46 +00:00
|
|
|
|
2013-03-05 19:26:26 +00:00
|
|
|
/**
|
|
|
|
* Current output format, default is 'text'
|
|
|
|
* @var string
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $outputFormat = 'text';
|
2013-03-05 19:26:26 +00:00
|
|
|
|
2013-09-26 23:25:46 +00:00
|
|
|
/**
|
|
|
|
* Distribution type, default is 'web'
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $distributionType = 'web';
|
|
|
|
|
2013-03-05 19:26:26 +00:00
|
|
|
/**
|
|
|
|
* List of parameters for constructing messages
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $parameters;
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
|
|
|
* Creates an instance of the given class with the given parameters.
|
2012-05-17 00:29:37 +00:00
|
|
|
* @param $parameters array Associative array of parameters
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2013-03-05 19:26:26 +00:00
|
|
|
public function __construct( array $parameters ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
$this->parameters = $parameters;
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Shows a notification in human-readable format.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @param $event EchoEvent being notified about.
|
|
|
|
* @param $user User being notified.
|
|
|
|
* @param $type string The notification type (e.g. notify, email)
|
2012-04-27 15:14:24 +00:00
|
|
|
* @return Mixed; depends on output format
|
|
|
|
* @see EchoNotificationFormatter::setOutputFormat
|
|
|
|
*/
|
2015-10-29 11:24:29 +00:00
|
|
|
abstract public function format( $event, $user, $type );
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the output format that the notification will be displayed in.
|
2016-01-13 00:53:01 +00:00
|
|
|
* @param $format string A valid output format (by default, 'text', 'flyout', and 'email' are allowed)
|
2015-06-16 02:28:48 +00:00
|
|
|
* @throws InvalidArgumentException
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
public function setOutputFormat( $format ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !in_array( $format, $this->validOutputFormats, true ) ) {
|
2015-06-16 02:28:48 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid output format $format" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputFormat = $format;
|
|
|
|
}
|
|
|
|
|
2013-09-26 23:25:46 +00:00
|
|
|
public function setDistributionType( $type ) {
|
|
|
|
if ( !in_array( $type, $this->validDistributionType, true ) ) {
|
2015-06-16 02:28:48 +00:00
|
|
|
throw new InvalidArgumentException( "Invalid distribution type $type" );
|
2013-09-26 23:25:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->distributionType = $type;
|
|
|
|
}
|
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
/**
|
2015-08-10 18:27:52 +00:00
|
|
|
* Create an EchoNotificationFormatter for the given type.
|
|
|
|
* @param string $type
|
2015-06-16 02:21:01 +00:00
|
|
|
* Select the class of formatter to use with the 'formatter-class' field.
|
2012-04-27 15:14:24 +00:00
|
|
|
* For other parameters, see the appropriate class' constructor.
|
2015-06-16 02:21:01 +00:00
|
|
|
* @throws RuntimeException
|
2012-04-27 15:14:24 +00:00
|
|
|
* @return EchoNotificationFormatter object.
|
|
|
|
*/
|
2015-08-10 18:27:52 +00:00
|
|
|
public static function factory( $type ) {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
if ( !isset( $wgEchoNotifications[$type] ) ) {
|
|
|
|
throw new InvalidArgumentException( "The notification type '$type' is not registered" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$parameters = $wgEchoNotifications[$type];
|
2013-02-15 23:34:47 +00:00
|
|
|
if ( isset( $parameters['formatter-class'] ) ) {
|
|
|
|
$class = $parameters['formatter-class'];
|
2015-06-16 02:21:01 +00:00
|
|
|
} else {
|
|
|
|
$class = 'EchoBasicFormatter';
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2015-06-16 02:21:01 +00:00
|
|
|
if ( !class_exists( $class ) ) {
|
|
|
|
throw new RuntimeException( "Class $class does not exist" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return new $class( $parameters );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a link to a title, or the title itself.
|
|
|
|
* @param $title Title object
|
2012-05-17 00:29:37 +00:00
|
|
|
* @return string Text suitable for output format
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
2014-02-06 21:00:25 +00:00
|
|
|
protected function formatTitle( Title $title ) {
|
2013-01-07 22:44:58 +00:00
|
|
|
return $title->getPrefixedText();
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
/**
|
2013-04-23 18:05:54 +00:00
|
|
|
* Formats a timestamp in a human-readable format
|
2013-02-28 23:52:12 +00:00
|
|
|
* @param $ts string Timestamp in some format compatible with wfTimestamp()
|
2013-04-23 18:05:54 +00:00
|
|
|
* @return string Human-readable timestamp
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
2013-03-05 19:26:26 +00:00
|
|
|
protected function formatTimestamp( $ts ) {
|
2013-01-24 00:39:42 +00:00
|
|
|
$timestamp = new MWTimestamp( $ts );
|
|
|
|
$ts = $timestamp->getHumanTimestamp();
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2013-06-12 23:18:26 +00:00
|
|
|
return $ts;
|
2012-11-28 02:17:52 +00:00
|
|
|
}
|
|
|
|
|
2015-08-05 19:41:12 +00:00
|
|
|
/**
|
|
|
|
* @todo this shouldn't be static
|
|
|
|
* @param string $icon Name of icon as registered in BeforeCreateEchoEvent hook
|
|
|
|
* @param string $dir either 'ltr' or 'rtl'
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getIconUrl( $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;
|
|
|
|
}
|
|
|
|
|
2012-11-28 02:17:52 +00:00
|
|
|
/**
|
2013-07-24 03:50:43 +00:00
|
|
|
* Returns a revision snippet
|
2013-06-12 23:18:26 +00:00
|
|
|
* @param EchoEvent $event The event that the notification is for.
|
|
|
|
* @param User $user The user to format the notification for.
|
2013-07-24 03:50:43 +00:00
|
|
|
* @return String The revision snippet (or empty string)
|
2012-11-28 02:17:52 +00:00
|
|
|
*/
|
2013-07-24 03:50:43 +00:00
|
|
|
public function getRevisionSnippet( $event, $user ) {
|
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( !isset( $extra['section-text'] ) || !$event->userCan( Revision::DELETED_TEXT, $user ) ) {
|
2012-11-28 02:17:52 +00:00
|
|
|
return '';
|
2013-06-12 23:18:26 +00:00
|
|
|
}
|
2012-12-19 00:50:28 +00:00
|
|
|
|
2013-07-24 03:50:43 +00:00
|
|
|
$snippet = trim( $extra['section-text'] );
|
|
|
|
|
|
|
|
return $snippet;
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
2012-12-18 03:48:42 +00:00
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
}
|