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
|
|
|
|
*/
|
2013-01-07 22:44:58 +00:00
|
|
|
protected $validOutputFormats = array( 'text', 'flyout', 'html', 'email' );
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* List of parameters for constructing messages
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $parameters;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* List of parameters that must exist in $this->$parameters
|
|
|
|
*/
|
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
|
|
|
*/
|
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-07-31 00:29:49 +00:00
|
|
|
|
2013-03-05 19:26:26 +00:00
|
|
|
$missingParameters = array_diff( $this->requiredParameters, array_keys( $parameters ) );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2013-03-05 19:26:26 +00:00
|
|
|
if ( $missingParameters ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
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.
|
2013-05-14 22:22:52 +00:00
|
|
|
* @param $format string A valid output format (by default, 'text', 'html', 'flyout', 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.
|
2013-02-15 23:34:47 +00:00
|
|
|
* Select the class of formatter to use with the 'class' field.
|
2012-04-27 15:14:24 +00:00
|
|
|
* 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;
|
2013-02-15 23:34:47 +00:00
|
|
|
if ( isset( $parameters['formatter-class'] ) ) {
|
|
|
|
$class = $parameters['formatter-class'];
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2013-02-15 23:34:47 +00:00
|
|
|
// Default to basic formatter
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$class || !class_exists( $class ) ) {
|
2013-02-15 23:34:47 +00:00
|
|
|
$class = 'EchoBasicFormatter';
|
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 ) {
|
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();
|
2013-06-12 23:18:26 +00:00
|
|
|
return $ts;
|
2012-11-28 02:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2013-06-12 23:18:26 +00:00
|
|
|
* Formats a revision comment (i.e. edit summary)
|
|
|
|
* @param EchoEvent $event The event that the notification is for.
|
|
|
|
* @param User $user The user to format the notification for.
|
|
|
|
* @return String The revision comment (or empty string)
|
2012-11-28 02:17:52 +00:00
|
|
|
*/
|
2013-06-12 23:18:26 +00:00
|
|
|
protected function formatRevisionComment( $event, $user ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
$revision = $event->getRevision();
|
|
|
|
if ( $revision === null ) {
|
2012-11-28 02:17:52 +00:00
|
|
|
return '';
|
2013-05-08 18:11:44 +00:00
|
|
|
} elseif( !$event->userCan( Revision::DELETED_COMMENT, $user ) ) {
|
|
|
|
return wfMessage( 'rev-deleted-comment' )->text();
|
|
|
|
} else {
|
2013-06-12 23:18:26 +00:00
|
|
|
$comment = $revision->getComment( Revision::FOR_THIS_USER, $user );
|
2013-01-07 22:44:58 +00:00
|
|
|
if ( $this->outputFormat === 'html' || $this->outputFormat === 'flyout' ) {
|
2013-06-12 23:18:26 +00:00
|
|
|
if ( $this->outputFormat === 'html' ) {
|
|
|
|
// Parse the revision comment
|
|
|
|
$comment = Linker::formatComment( $comment, $revision->getTitle() );
|
|
|
|
} else {
|
|
|
|
$comment = $this->customFormatRevisionComment( $comment );
|
|
|
|
}
|
|
|
|
if ( $comment ) {
|
|
|
|
// No quotation marks for now, but this might need to be reverted.
|
|
|
|
// $comment = wfMessage( 'echo-quotation-marks', $comment )->inContentLanguage()->plain();
|
|
|
|
$comment = Xml::tags( 'span', array( 'class' => 'comment' ), $comment );
|
|
|
|
$comment = Xml::tags( 'div', array( 'class' => 'mw-echo-edit-summary' ), $comment );
|
2013-04-05 00:47:04 +00:00
|
|
|
}
|
2012-11-28 02:17:52 +00:00
|
|
|
}
|
2013-06-12 23:18:26 +00:00
|
|
|
return $comment;
|
|
|
|
}
|
|
|
|
}
|
2012-12-19 00:50:28 +00:00
|
|
|
|
2013-06-12 23:18:26 +00:00
|
|
|
/**
|
|
|
|
* Formats a revision comment (i.e. edit summary) for use in the flyout (and
|
|
|
|
* possibly HTML email). This is a helper function for formatRevisionComment.
|
|
|
|
* @param String $comment The raw revision comment
|
|
|
|
* @return String The formatted revision comment (or empty string)
|
|
|
|
*/
|
|
|
|
private function customFormatRevisionComment( $comment ) {
|
|
|
|
// Strip wikitext from the revision comment and manually convert autocomments.
|
|
|
|
// This bypasses the creation of the arrow section links (→) and turns
|
|
|
|
// any other links into plain text.
|
|
|
|
$comment = FeedItem::stripComment( $comment );
|
|
|
|
$comment = trim( htmlspecialchars( $comment ) );
|
|
|
|
// Convert autocomments (e.g. section titles) from raw form
|
|
|
|
// Example input: '/* Foobar */ My changes'
|
|
|
|
// Output: '<span class='autocomment'>Foobar:</span> My changes'
|
|
|
|
preg_match( "!(.*)/\*\s*(.*?)\s*\*/(.*)!", $comment, $matches );
|
|
|
|
if ( $matches ) {
|
|
|
|
$section = $matches[2];
|
|
|
|
if ( $matches[3] ) {
|
|
|
|
// Add a colon after the section name
|
|
|
|
$section .= wfMessage( 'colon-separator' )->inContentLanguage()->escaped();
|
|
|
|
}
|
|
|
|
// Add standard span tag for autocomment
|
|
|
|
$comment = $matches[1] . "<span class='autocomment'>" . $section . "</span>" . $matches[3];
|
2012-11-28 02:17:52 +00:00
|
|
|
}
|
2013-06-12 23:18:26 +00:00
|
|
|
return $comment;
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
2012-12-18 03:48:42 +00:00
|
|
|
|
2012-05-17 00:29:37 +00:00
|
|
|
}
|