2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class EchoBasicFormatter extends EchoNotificationFormatter {
|
|
|
|
protected $messageKey = false;
|
|
|
|
protected $messageParams = false;
|
2012-07-31 00:29:49 +00:00
|
|
|
protected $requiredParameters = array(
|
|
|
|
'title-message',
|
|
|
|
'title-params',
|
|
|
|
);
|
2012-11-28 02:17:52 +00:00
|
|
|
protected $validPayloadComponents = array( 'summary', 'snippet', 'welcome' );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
2013-01-07 22:44:58 +00:00
|
|
|
protected $title, $flyoutTitle, $content, $email, $icon;
|
2012-04-27 15:14:24 +00:00
|
|
|
|
|
|
|
public function __construct( $params ) {
|
2012-08-30 16:04:39 +00:00
|
|
|
parent::__construct( $params );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
$this->title = array();
|
2013-01-07 22:44:58 +00:00
|
|
|
$this->flyoutTitle = array();
|
|
|
|
if ( isset( $params['flyout-message'] ) && isset( $params['flyout-params'] ) ) {
|
|
|
|
$this->flyoutTitle['message'] = $params['flyout-message'];
|
|
|
|
$this->flyoutTitle['params'] = $params['flyout-params'];
|
|
|
|
} else {
|
|
|
|
$this->flyoutTitle['message'] = $params['title-message'];
|
|
|
|
$this->flyoutTitle['params'] = $params['title-params'];
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
$this->title['message'] = $params['title-message'];
|
|
|
|
$this->title['params'] = $params['title-params'];
|
2012-11-28 02:17:52 +00:00
|
|
|
$this->payload = array();
|
|
|
|
|
|
|
|
if ( isset( $params['payload'] ) ) {
|
|
|
|
$this->payload = $params['payload'];
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->email = array();
|
|
|
|
if ( isset( $params['email-subject-message'] ) ) {
|
|
|
|
$this->email['subject'] = array();
|
|
|
|
$this->email['subject']['message'] = $params['email-subject-message'];
|
|
|
|
|
|
|
|
if ( isset( $params['email-subject-params'] ) ) {
|
|
|
|
$this->email['subject']['params'] = $params['email-subject-params'];
|
|
|
|
} else {
|
|
|
|
$this->email['subject']['params'] = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->email = array(
|
|
|
|
'subject' => array(
|
|
|
|
'message' => 'echo-email-subject-default',
|
|
|
|
'params' => array(),
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'message' => 'echo-email-body-default',
|
|
|
|
'params' => array(
|
|
|
|
'text-notification',
|
|
|
|
),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $params['email-body-message'] ) ) {
|
|
|
|
$this->email['body'] = array();
|
|
|
|
$this->email['body']['message'] = $params['email-body-message'];
|
|
|
|
|
|
|
|
if ( isset( $params['email-body-params'] ) ) {
|
2012-10-24 18:12:06 +00:00
|
|
|
$this->email['body']['params'] = $params['email-body-params'];
|
2012-07-31 00:29:49 +00:00
|
|
|
} else {
|
|
|
|
$this->email['body']['params'] = array( 'text-notification' );
|
|
|
|
}
|
2012-11-27 01:53:35 +00:00
|
|
|
} else {
|
|
|
|
$this->email['body'] = array(
|
|
|
|
'message' => 'echo-email-body-default',
|
|
|
|
'params' => array(
|
|
|
|
'text-notification',
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->email['batch-body'] = array();
|
|
|
|
if ( isset( $params['email-body-batch-message'] ) ) {
|
|
|
|
$this->email['batch-body']['message'] = $params['email-body-batch-message'];
|
|
|
|
if ( isset( $params['email-body-batch-params'] ) ) {
|
|
|
|
$this->email['batch-body']['params'] = $params['email-body-batch-params'];
|
|
|
|
} else {
|
|
|
|
$this->email['batch-body']['params'] = array();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->email['batch-body'] = $this->email['body'];
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( isset( $params['icon'] ) ) {
|
|
|
|
$this->icon = $params['icon'];
|
|
|
|
}
|
2012-10-25 20:39:41 +00:00
|
|
|
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-09-02 09:30:38 +00:00
|
|
|
/**
|
2012-11-26 22:57:28 +00:00
|
|
|
* Formats a notification
|
|
|
|
*
|
|
|
|
* @param $event EchoEvent that the notification is for.
|
|
|
|
* @param $user User to format the notification for.
|
|
|
|
* @param $type string The type of notification being distributed (e.g. email, notify)
|
2012-09-02 09:30:38 +00:00
|
|
|
* @return array|string
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
public function format( $event, $user, $type ) {
|
2013-01-14 23:52:46 +00:00
|
|
|
global $wgEchoEventDetails;
|
|
|
|
|
2012-05-17 15:36:18 +00:00
|
|
|
if ( $this->outputFormat === 'email' ) {
|
|
|
|
return $this->formatEmail( $event, $user, $type );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
if ( $this->outputFormat === 'text' ) {
|
|
|
|
return $this->formatNotificationTitle( $event, $user )->text();
|
|
|
|
}
|
2012-05-17 15:36:18 +00:00
|
|
|
|
2012-11-26 22:57:28 +00:00
|
|
|
// Assume html as the format for the notification
|
|
|
|
|
2012-11-30 23:58:54 +00:00
|
|
|
$output = '';
|
|
|
|
|
|
|
|
// Add the notification icon
|
2012-12-05 00:55:12 +00:00
|
|
|
if ( !$this->icon ) {
|
|
|
|
$this->icon = 'placeholder';
|
2012-11-30 23:58:54 +00:00
|
|
|
}
|
2012-12-05 00:55:12 +00:00
|
|
|
$output = Xml::tags( 'div',
|
|
|
|
array( 'class' => "mw-echo-icon mw-echo-icon-{$this->icon}" ),
|
|
|
|
' '
|
|
|
|
) . $output;
|
2012-11-30 23:58:54 +00:00
|
|
|
|
2013-01-14 23:52:46 +00:00
|
|
|
// Add the hidden dismiss interface if the notification is dismissable
|
|
|
|
if ( !isset( $wgEchoEventDetails[$event->type]['nodismiss'] )
|
|
|
|
|| !in_array( 'web', $wgEchoEventDetails[$event->type]['nodismiss'] ) )
|
|
|
|
{
|
|
|
|
$output .= $this->formatDismissInterface( $event, $user );
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:57:28 +00:00
|
|
|
// Build the notification title
|
2012-07-31 00:29:49 +00:00
|
|
|
$title = $this->formatNotificationTitle( $event, $user )->parse();
|
2012-11-30 23:58:54 +00:00
|
|
|
$content = Xml::tags( 'div', array( 'class' => 'mw-echo-title' ), $title ) . "\n";
|
2012-11-26 22:57:28 +00:00
|
|
|
|
2012-11-30 23:58:54 +00:00
|
|
|
// Build the notification payload
|
|
|
|
$payload = '';
|
2012-11-28 02:17:52 +00:00
|
|
|
foreach ( $this->payload as $payloadComponent ) {
|
|
|
|
if ( in_array( $payloadComponent, $this->validPayloadComponents ) ) {
|
|
|
|
switch ( $payloadComponent ) {
|
|
|
|
case 'summary':
|
2012-12-28 08:41:01 +00:00
|
|
|
$payload .= $this->formatSummary( $event, $user );
|
2012-11-28 02:17:52 +00:00
|
|
|
break;
|
|
|
|
case 'snippet':
|
|
|
|
// TODO: build this
|
|
|
|
break;
|
|
|
|
case 'welcome':
|
|
|
|
$details = array(
|
|
|
|
'message' => 'notification-new-user-content',
|
|
|
|
'params' => array( 'agent' )
|
|
|
|
);
|
2012-11-30 23:58:54 +00:00
|
|
|
$payload .= $this->formatFragment( $details, $event, $user )->parse();
|
2012-11-28 02:17:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new MWException( "Unrecognised payload component $payloadComponent" );
|
|
|
|
}
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-11-28 02:17:52 +00:00
|
|
|
|
2012-11-30 23:58:54 +00:00
|
|
|
if ( $payload !== '' ) {
|
|
|
|
$content .= Xml::tags( 'div', array( 'class' => 'mw-echo-payload' ), $payload ) . "\n";
|
2012-05-17 15:36:18 +00:00
|
|
|
}
|
|
|
|
|
2012-11-30 23:58:54 +00:00
|
|
|
// Add timestamp
|
|
|
|
$content .= $this->formatTimestamp( $event->getTimestamp(), $user );
|
|
|
|
|
|
|
|
$output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n";
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2013-01-14 23:52:46 +00:00
|
|
|
protected function formatDismissInterface( $event, $user ) {
|
|
|
|
$dismissTitle = wfMessage( 'echo-dismiss-title-' . $event->type )
|
|
|
|
->inLanguage( $user->getOption( 'language' ) )
|
|
|
|
->escaped();
|
|
|
|
$dismiss = wfMessage( 'echo-dismiss-message', $dismissTitle )
|
|
|
|
->inLanguage( $user->getOption( 'language' ) )
|
|
|
|
->text();
|
|
|
|
$dismiss = Xml::tags( 'div', array( 'class' => 'mw-echo-dismiss-message' ), $dismiss ) . "\n";
|
|
|
|
$dismiss = Xml::tags( 'div', array( 'class' => 'mw-echo-dismiss', 'style' => 'display:none;' ), $dismiss ) . "\n";
|
|
|
|
return $dismiss;
|
|
|
|
}
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
protected function formatNotificationTitle( $event, $user ) {
|
2013-01-07 22:44:58 +00:00
|
|
|
if ( $this->outputFormat === 'flyout' ) {
|
|
|
|
return $this->formatFragment( $this->flyoutTitle, $event, $user );
|
|
|
|
} else {
|
|
|
|
return $this->formatFragment( $this->title, $event, $user );
|
|
|
|
}
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 02:17:52 +00:00
|
|
|
protected function formatEmail( $event, $user, $type ) {
|
|
|
|
$subject = $this->formatFragment( $this->email['subject'], $event, $user )->text();
|
|
|
|
|
2012-12-18 01:50:00 +00:00
|
|
|
$body = preg_replace( "/\n{3,}/", "\n\n", $this->formatFragment( $this->email['body'], $event, $user )->text() );
|
2012-05-17 15:36:18 +00:00
|
|
|
|
2012-12-18 01:50:00 +00:00
|
|
|
$batchBody = preg_replace( "/\n{3,}/", "\n\n", $this->formatFragment( $this->email['batch-body'], $event, $user )->text() );
|
2012-11-27 01:53:35 +00:00
|
|
|
|
|
|
|
return array( 'subject' => $subject, 'body' => $body, 'batch-body' => $batchBody );
|
2012-07-31 00:29:49 +00:00
|
|
|
}
|
|
|
|
|
2012-11-28 02:17:52 +00:00
|
|
|
/**
|
|
|
|
* Creates a notification fragment based on a message and parameters
|
|
|
|
*
|
|
|
|
* @param $details array An i18n message and parameters to pass to the message
|
|
|
|
* @param $event EchoEvent that the notification is for.
|
|
|
|
* @param $user User to format the notification for.
|
|
|
|
* @return string
|
|
|
|
*/
|
2012-07-31 00:29:49 +00:00
|
|
|
protected function formatFragment( $details, $event, $user ) {
|
2012-11-30 23:36:46 +00:00
|
|
|
$message = wfMessage( $details['message'] )
|
|
|
|
->inLanguage( $user->getOption( 'language' ) );
|
2012-07-31 00:29:49 +00:00
|
|
|
|
|
|
|
$this->processParams( $details['params'], $event, $message, $user );
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
2012-11-28 02:17:52 +00:00
|
|
|
/**
|
|
|
|
* Convert the parameters into real values and pass them into the message
|
|
|
|
*
|
|
|
|
* @param $params array
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $message Message
|
|
|
|
* @param $user User
|
|
|
|
*/
|
2012-07-31 00:29:49 +00:00
|
|
|
protected function processParams( $params, $event, $message, $user ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
foreach ( $params as $param ) {
|
2012-05-17 15:36:18 +00:00
|
|
|
$this->processParam( $event, $param, $message, $user );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-26 05:09:43 +00:00
|
|
|
/**
|
2012-11-28 02:17:52 +00:00
|
|
|
* Helper function for processParams()
|
|
|
|
*
|
2012-09-26 05:09:43 +00:00
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $param
|
|
|
|
* @param $message Message
|
|
|
|
* @param $user User
|
|
|
|
* @throws MWException
|
|
|
|
*/
|
2012-10-23 21:57:22 +00:00
|
|
|
protected function processParam( $event, $param, $message, $user ) {
|
2012-04-27 15:14:24 +00:00
|
|
|
if ( $param === 'agent' ) {
|
|
|
|
// Actually converts to two parameters for gender support
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$event->getAgent() ) {
|
2012-07-26 17:23:18 +00:00
|
|
|
$message->params( '', wfMessage( 'echo-no-agent' )->text() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
|
|
|
$agent = $event->getAgent();
|
|
|
|
$message->params( $agent->getName() );
|
|
|
|
}
|
2012-05-17 15:36:18 +00:00
|
|
|
} elseif ( $param === 'user' ) {
|
|
|
|
$message->params( $user->getName() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} elseif ( $param === 'title' ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$event->getTitle() ) {
|
2012-07-26 17:23:18 +00:00
|
|
|
$message->params( wfMessage( 'echo-no-title' )->text() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
2012-07-31 00:29:49 +00:00
|
|
|
$message->params( $this->formatTitle( $event->getTitle() ) );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2012-10-25 20:39:41 +00:00
|
|
|
} elseif ( $param === 'text-notification' ) {
|
2012-07-31 00:29:49 +00:00
|
|
|
$oldOutputFormat = $this->outputFormat;
|
2012-08-30 16:04:39 +00:00
|
|
|
$this->setOutputFormat( 'text' );
|
2012-07-31 00:29:49 +00:00
|
|
|
// $type is ignored in this class
|
|
|
|
$textNotification = $this->format( $event, $user, '' );
|
|
|
|
$this->setOutputFormat( $oldOutputFormat );
|
|
|
|
|
|
|
|
$message->params( $textNotification );
|
2012-10-25 20:39:41 +00:00
|
|
|
} elseif ( $param === 'email-footer' ) {
|
|
|
|
global $wgEchoEmailFooterAddress;
|
|
|
|
$message->params(
|
|
|
|
wfMessage( 'echo-email-footer-default' )
|
|
|
|
->inLanguage( $user->getOption( 'language' ) )
|
2012-12-18 01:50:00 +00:00
|
|
|
->params( $wgEchoEmailFooterAddress, wfMessage( 'echo-email-batch-separator' )->text() )
|
2012-10-25 20:39:41 +00:00
|
|
|
->text()
|
|
|
|
);
|
2012-05-17 15:36:18 +00:00
|
|
|
} else {
|
|
|
|
throw new MWException( "Unrecognised parameter $param" );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
|
|
|
}
|
2012-07-26 17:23:18 +00:00
|
|
|
}
|