mediawiki-extensions-Echo/includes/formatters/NotificationFormatter.php
Kunal Mehta 1561725130 Remove EchoNotificationFormatter::requiredParameters
Instead, have subclasses implement checking required parameters
manually.

The only subclass that was using this was EchoBasicFormatter and has
been updated.

Change-Id: I23e2fa7044e0d59125530024f8c6c35516d3b90b
2015-06-16 15:32:12 -07:00

140 lines
3.9 KiB
PHP

<?php
/**
* 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.
*/
abstract class EchoNotificationFormatter {
/**
* List of valid output format
* @var array
*/
protected $validOutputFormats = array( 'text', 'flyout', 'html', 'email', 'htmlemail' );
/**
* List of valid distribution type
*/
protected $validDistributionType = array( 'web', 'email', 'emaildigest', 'emailsubject' );
/**
* Current output format, default is 'text'
* @var string
*/
protected $outputFormat = 'text';
/**
* Distribution type, default is 'web'
* @var string
*/
protected $distributionType = 'web';
/**
* List of parameters for constructing messages
* @var array
*/
protected $parameters;
/**
* Creates an instance of the given class with the given parameters.
* @param $parameters array Associative array of parameters
* @throws MWException
*/
public function __construct( array $parameters ) {
$this->parameters = $parameters;
}
/**
* Shows a notification in human-readable format.
* @param $event EchoEvent being notified about.
* @param $user User being notified.
* @param $type string The notification type (e.g. notify, email)
* @return Mixed; depends on output format
* @see EchoNotificationFormatter::setOutputFormat
*/
public abstract function format( $event, $user, $type );
/**
* Set the output format that the notification will be displayed in.
* @param $format string A valid output format (by default, 'text', 'html', 'flyout', and 'email' are allowed)
* @throws InvalidArgumentException
*/
public function setOutputFormat( $format ) {
if ( !in_array( $format, $this->validOutputFormats, true ) ) {
throw new InvalidArgumentException( "Invalid output format $format" );
}
$this->outputFormat = $format;
}
public function setDistributionType( $type ) {
if ( !in_array( $type, $this->validDistributionType, true ) ) {
throw new InvalidArgumentException( "Invalid distribution type $type" );
}
$this->distributionType = $type;
}
/**
* Create an EchoNotificationFormatter from the supplied parameters.
* @param $parameters array Associative array.
* Select the class of formatter to use with the 'formatter-class' field.
* For other parameters, see the appropriate class' constructor.
* @throws RuntimeException
* @return EchoNotificationFormatter object.
*/
public static function factory( $parameters ) {
if ( isset( $parameters['formatter-class'] ) ) {
$class = $parameters['formatter-class'];
} else {
$class = 'EchoBasicFormatter';
}
if ( !class_exists( $class ) ) {
throw new RuntimeException( "Class $class does not exist" );
}
return new $class( $parameters );
}
/**
* Returns a link to a title, or the title itself.
* @param $title Title object
* @return string Text suitable for output format
*/
protected function formatTitle( Title $title ) {
return $title->getPrefixedText();
}
/**
* Formats a timestamp in a human-readable format
* @param $ts string Timestamp in some format compatible with wfTimestamp()
* @return string Human-readable timestamp
*/
protected function formatTimestamp( $ts ) {
$timestamp = new MWTimestamp( $ts );
$ts = $timestamp->getHumanTimestamp();
return $ts;
}
/**
* Returns a revision snippet
* @param EchoEvent $event The event that the notification is for.
* @param User $user The user to format the notification for.
* @return String The revision snippet (or empty string)
*/
public function getRevisionSnippet( $event, $user ) {
$extra = $event->getExtra();
if ( !isset( $extra['section-text'] ) || !$event->userCan( Revision::DELETED_TEXT, $user ) ) {
return '';
}
$snippet = trim( $extra['section-text'] );
return $snippet;
}
}