mediawiki-extensions-Echo/formatters/NotificationFormatter.php
Andrew Garrett 0699d8f402 Formatting updates for Echo.
First implementation of "two line" formatting.
Messages have a title and optional content.

Distinguishing writing on "your talk page" from another talk page in messages.

Change-Id: I9051e4bfb66d1c25c1bf68ec092b52fd90544336
2012-08-01 10:37:17 -07:00

125 lines
3.5 KiB
PHP

<?php
abstract class EchoNotificationFormatter {
static $formatterClasses = array(
'basic' => 'EchoBasicFormatter',
'edit' => 'EchoEditFormatter',
'comment' => 'EchoCommentFormatter',
);
protected $validOutputFormats = array('text', 'html', 'email');
protected $outputFormat = 'text';
protected $requiredParameters = array();
/**
* Creates an instance of the given class with the given parameters.
*
* @param $parameters Associative array of parameters
*/
public function __construct( $parameters ) {
$this->parameters = $parameters;
$missingParameters =
array_diff( $this->requiredParameters, array_keys( $parameters ) );
if ( count( $missingParameters ) ) {
throw new MWException(
"Missing required parameters for ".
get_class( $this ).":".
implode( " ", $missingParameters )
);
}
}
/**
* Shows a notification in human-readable format.
*
* @param $event The event being notified about.
* @param $user The user being notified.
* @param $type 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 A valid output format (by default, 'text' and 'html' are allowed)
*/
public function setOutputFormat( $format ) {
if ( ! in_array( $format, $this->validOutputFormats, true ) ) {
throw new MWException( "Invalid output format $format" );
}
$this->outputFormat = $format;
}
/**
* Create an EchoNotificationFormatter from the supplied parameters.
* @param $parameters Associative array.
* Select the class of formatter to use with the 'type' or 'class' field.
* For other parameters, see the appropriate class' constructor.
* @return EchoNotificationFormatter object.
*/
public static function factory( $parameters ) {
$class = null;
if ( isset($parameters['type']) ) {
$type = $parameters['type'];
if ( isset(self::$formatterClasses[$type]) ) {
$class = self::$formatterClasses[$type];
}
} elseif ( isset($parameters['class']) ) {
$class = $parameters['class'];
}
if ( ! $class || !class_exists($class) ) {
throw new MWException( "No valid class ($class) or type ($type) specified for ".__METHOD__);
}
return new $class( $parameters );
}
/**
* Returns a link to a title, or the title itself.
* @param $title Title object
* @return Text suitable for output format
*/
protected function formatTitle( $title ) {
return $title->getPrefixedText();
}
/**
* Returns a user link in the appropriate format.
*
* @param $user User object.
* @return Text suitable for output format.
*/
protected function formatUser( $user ) {
if ( $this->outputFormat === 'html' ) {
return Linker::userLink( $user->getId(), $user->getName() );
} else {
return $user->getName();
}
}
/**
* Formats a timestamp (in a human-readable format if supported by
* MediaWiki)
*
* @param $ts Timestamp in some format compatible with wfTimestamp()
* @param $user User to format for. false to detect
* @return type description
*/
protected function formatTimestamp( $ts, $user ) {
$languageCode = $user->getOption( 'language' );
$language = Language::factory( $languageCode );
if ( MWInit::methodExists( 'Language', 'prettyTimestamp' ) ) {
$ts = $language->prettyTimestamp( $ts );
} else {
$ts = $language->userTimeAndDate( $ts, $user );
}
return Xml::element( 'span', array( 'class' => 'mw-echo-timestamp' ), $ts );
}
}