2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
abstract class EchoNotificationFormatter {
|
|
|
|
static $formatterClasses = array(
|
|
|
|
'basic' => 'EchoBasicFormatter',
|
|
|
|
'edit' => 'EchoEditFormatter',
|
2012-07-27 22:16:19 +00:00
|
|
|
'comment' => 'EchoCommentFormatter',
|
2012-08-31 23:35:16 +00:00
|
|
|
'welcome' => 'EchoBasicFormatter',
|
2012-04-27 15:14:24 +00:00
|
|
|
);
|
2012-08-30 16:04:39 +00:00
|
|
|
protected $validOutputFormats = array( 'text', 'html', 'email' );
|
2012-04-27 15:14:24 +00:00
|
|
|
protected $outputFormat = 'text';
|
2012-07-31 00:29:49 +00:00
|
|
|
protected $requiredParameters = array();
|
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
|
|
|
*/
|
2012-05-17 15:36:18 +00:00
|
|
|
public function __construct( $parameters ) {
|
|
|
|
$this->parameters = $parameters;
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
$missingParameters =
|
|
|
|
array_diff( $this->requiredParameters, array_keys( $parameters ) );
|
|
|
|
|
|
|
|
if ( count( $missingParameters ) ) {
|
|
|
|
throw new MWException(
|
2012-08-31 21:50:46 +00:00
|
|
|
"Missing required parameters for " .
|
|
|
|
get_class( $this ) . ":" .
|
|
|
|
implode( " ", $missingParameters )
|
2012-07-31 00:29:49 +00:00
|
|
|
);
|
|
|
|
}
|
2012-05-17 15:36:18 +00:00
|
|
|
}
|
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
|
|
|
|
*/
|
|
|
|
public abstract function format( $event, $user, $type );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the output format that the notification will be displayed in.
|
|
|
|
*
|
2012-11-26 22:57:28 +00:00
|
|
|
* @param $format string A valid output format (by default, 'text', 'html', and 'email' are allowed)
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
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 ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
throw new MWException( "Invalid output format $format" );
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->outputFormat = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create an EchoNotificationFormatter from the supplied parameters.
|
2012-05-17 00:29:37 +00:00
|
|
|
* @param $parameters array Associative array.
|
2012-04-27 15:14:24 +00:00
|
|
|
* Select the class of formatter to use with the 'type' or 'class' field.
|
|
|
|
* For other parameters, see the appropriate class' constructor.
|
2012-09-02 09:30:38 +00:00
|
|
|
* @throws MWException
|
2012-04-27 15:14:24 +00:00
|
|
|
* @return EchoNotificationFormatter object.
|
|
|
|
*/
|
|
|
|
public static function factory( $parameters ) {
|
|
|
|
$class = null;
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( $parameters['type'] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$type = $parameters['type'];
|
2012-08-30 16:04:39 +00:00
|
|
|
if ( isset( self::$formatterClasses[$type] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$class = self::$formatterClasses[$type];
|
|
|
|
}
|
2012-08-30 16:04:39 +00:00
|
|
|
} elseif ( isset( $parameters['class'] ) ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
$class = $parameters['class'];
|
|
|
|
}
|
|
|
|
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$class || !class_exists( $class ) ) {
|
|
|
|
throw new MWException( "No valid class ($class) or type ($type) specified for " . __METHOD__ );
|
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
|
|
|
*/
|
|
|
|
protected function formatTitle( $title ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
return $title->getPrefixedText();
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a user link in the appropriate format.
|
|
|
|
*
|
|
|
|
* @param $user User object.
|
2012-05-17 00:29:37 +00:00
|
|
|
* @return string Text suitable for output format.
|
2012-04-27 15:14:24 +00:00
|
|
|
*/
|
|
|
|
protected function formatUser( $user ) {
|
|
|
|
if ( $this->outputFormat === 'html' ) {
|
|
|
|
return Linker::userLink( $user->getId(), $user->getName() );
|
|
|
|
} else {
|
|
|
|
return $user->getName();
|
|
|
|
}
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* 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
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return string Type description
|
2012-07-31 00:29:49 +00:00
|
|
|
*/
|
|
|
|
protected function formatTimestamp( $ts, $user ) {
|
|
|
|
$languageCode = $user->getOption( 'language' );
|
|
|
|
$language = Language::factory( $languageCode );
|
|
|
|
|
|
|
|
if ( MWInit::methodExists( 'Language', 'prettyTimestamp' ) ) {
|
2012-10-25 21:07:21 +00:00
|
|
|
$ts = $language->prettyTimestamp( $ts, false, $user );
|
2012-07-31 00:29:49 +00:00
|
|
|
} else {
|
|
|
|
$ts = $language->userTimeAndDate( $ts, $user );
|
|
|
|
}
|
|
|
|
|
|
|
|
return Xml::element( 'span', array( 'class' => 'mw-echo-timestamp' ), $ts );
|
|
|
|
}
|
2012-05-17 00:29:37 +00:00
|
|
|
}
|