2012-04-27 15:14:24 +00:00
|
|
|
<?php
|
|
|
|
|
2013-03-07 03:08:12 +00:00
|
|
|
/**
|
|
|
|
* @Todo - Consider having $event/$user as class properties since the formatter is
|
|
|
|
* always tied to these two entities, in this case, we won't have to pass it around
|
|
|
|
* in all the internal method
|
|
|
|
*/
|
2012-04-27 15:14:24 +00:00
|
|
|
class EchoBasicFormatter extends EchoNotificationFormatter {
|
2013-04-04 21:56:05 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Required parameters
|
|
|
|
* @param array
|
|
|
|
*/
|
2012-07-31 00:29:49 +00:00
|
|
|
protected $requiredParameters = array(
|
|
|
|
'title-message',
|
2013-04-04 21:56:05 +00:00
|
|
|
'title-params'
|
2012-07-31 00:29:49 +00:00
|
|
|
);
|
|
|
|
|
2013-04-04 21:56:05 +00:00
|
|
|
/**
|
|
|
|
* Notification title data for archive page
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
protected $title;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification title data for flyout
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
protected $flyoutTitle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification title data for bundling ( flyout and archive page )
|
|
|
|
*/
|
|
|
|
protected $bundleTitle;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @Todo Check if this varaible can be removed
|
|
|
|
*/
|
|
|
|
protected $content;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification email data
|
|
|
|
* @param array
|
|
|
|
*/
|
|
|
|
protected $email;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Notification icon for each type
|
|
|
|
* @param string
|
|
|
|
*/
|
|
|
|
protected $icon;
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Data for constructing bundle message, data in this array
|
|
|
|
* should be used in function processParams()
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $bundleData = array (
|
|
|
|
'use-bundle' => false
|
|
|
|
);
|
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
|
|
|
|
2013-04-04 21:56:05 +00:00
|
|
|
// Title for archive page
|
|
|
|
$this->title = array(
|
|
|
|
'message' => $params['title-message'],
|
|
|
|
'params' => $params['title-params']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Set up default params if one is missing
|
|
|
|
$params += $this->getDefaultParams();
|
2013-05-13 22:00:16 +00:00
|
|
|
|
2013-04-04 21:56:05 +00:00
|
|
|
// Title for the flyout
|
|
|
|
$this->flyoutTitle = array(
|
|
|
|
'message' => $params['flyout-message'],
|
|
|
|
'params' => $params['flyout-params']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Bundle title for both archive page and flyout
|
|
|
|
$this->bundleTitle = array(
|
|
|
|
'message' => $params['bundle-message'],
|
|
|
|
'params' => $params['bundle-params']
|
|
|
|
);
|
|
|
|
|
|
|
|
// Notification payload data, eg, summary
|
|
|
|
$this->payload = $params['payload'];
|
|
|
|
|
|
|
|
// Notification email subject and body
|
|
|
|
$this->email = array(
|
|
|
|
'subject' => array(
|
|
|
|
'message' => $params['email-subject-message'],
|
|
|
|
'params' => $params['email-subject-params']
|
|
|
|
),
|
|
|
|
'body' => array(
|
|
|
|
'message' => $params['email-body-message'],
|
|
|
|
'params' => $params['email-body-params']
|
|
|
|
),
|
|
|
|
'batch-body' => array(
|
|
|
|
'message' => $params['email-body-batch-message'],
|
|
|
|
'params' => $params['email-body-batch-params']
|
2013-03-06 00:04:48 +00:00
|
|
|
),
|
|
|
|
'batch-bundle-body' => array(
|
|
|
|
'message' => $params['email-body-batch-bundle-message'],
|
|
|
|
'params' => $params['email-body-batch-bundle-params']
|
2013-04-04 21:56:05 +00:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
// Notification icon for the event type
|
|
|
|
$this->icon = $params['icon'];
|
|
|
|
}
|
2012-10-25 20:39:41 +00:00
|
|
|
|
2013-04-04 21:56:05 +00:00
|
|
|
/**
|
|
|
|
* Internal function that returns notification default params
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
protected function getDefaultParams() {
|
|
|
|
return array(
|
|
|
|
'flyout-message' => $this->title['message'],
|
|
|
|
'flyout-params' => $this->title['params'],
|
|
|
|
'bundle-message' => '',
|
|
|
|
'bundle-params' => array(),
|
|
|
|
'payload' => array(),
|
|
|
|
'email-subject-message' => 'echo-email-subject-default',
|
|
|
|
'email-subject-params' => array(),
|
|
|
|
'email-body-message' => 'echo-email-body-default',
|
|
|
|
'email-body-params' => array( 'text-notification' ),
|
|
|
|
'email-body-batch-message' => 'echo-email-batch-body-default',
|
|
|
|
'email-body-batch-params' => array(),
|
2013-03-06 00:04:48 +00:00
|
|
|
'email-body-batch-bundle-message' => '',
|
|
|
|
'email-body-batch-bundle-params' => array(),
|
2013-04-04 21:56:05 +00:00
|
|
|
'icon' => 'placeholder'
|
|
|
|
);
|
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.
|
2013-02-14 20:02:45 +00:00
|
|
|
* @param $type string The type of notification being distributed (e.g. email, web)
|
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-06-17 21:44:46 +00:00
|
|
|
global $wgExtensionAssetsPath, $wgEchoNotificationIcons;
|
2013-01-14 23:52:46 +00:00
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
// Use the bundle message if use-bundle is true and there is a bundle message
|
2013-03-06 00:04:48 +00:00
|
|
|
$this->generateBundleData( $event, $user, $type );
|
2013-01-15 23:21:39 +00:00
|
|
|
if ( $this->bundleData['use-bundle'] && isset( $this->bundleTitle['message'] ) ) {
|
|
|
|
$this->title = $this->flyoutTitle = $this->bundleTitle;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2013-04-29 03:40:56 +00:00
|
|
|
$iconInfo = $wgEchoNotificationIcons[$this->icon];
|
|
|
|
if ( isset( $iconInfo['url'] ) && $iconInfo['url'] ) {
|
|
|
|
$iconUrl = $iconInfo['url'];
|
|
|
|
} else {
|
|
|
|
if ( !isset( $iconInfo['path'] ) || !$iconInfo['path'] ) {
|
|
|
|
// Fallback in case icon is not configured; mainly intended for 'site'
|
|
|
|
$iconInfo = $wgEchoNotificationIcons['placeholder'];
|
|
|
|
}
|
|
|
|
$iconUrl = "$wgExtensionAssetsPath/{$iconInfo['path']}";
|
|
|
|
}
|
|
|
|
|
2012-11-26 22:57:28 +00:00
|
|
|
// Assume html as the format for the notification
|
2013-04-29 03:40:56 +00:00
|
|
|
$output = Html::element(
|
|
|
|
'img',
|
|
|
|
array(
|
|
|
|
'class' => "mw-echo-icon",
|
|
|
|
'src' => $iconUrl,
|
|
|
|
)
|
2013-04-02 23:47:54 +00:00
|
|
|
);
|
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
|
2013-04-10 23:24:34 +00:00
|
|
|
/* Disabling dismiss interface until there is consensus on how it should be implemented
|
2013-04-02 23:47:54 +00:00
|
|
|
if ( $event->isDismissable( 'web' ) ) {
|
2013-01-14 23:52:46 +00:00
|
|
|
$output .= $this->formatDismissInterface( $event, $user );
|
|
|
|
}
|
2013-04-10 23:24:34 +00:00
|
|
|
*/
|
2013-01-14 23:52:46 +00:00
|
|
|
|
2012-11-26 22:57:28 +00:00
|
|
|
// Build the notification title
|
2013-04-02 23:47:54 +00:00
|
|
|
$content = Xml::tags(
|
|
|
|
'div',
|
|
|
|
array( 'class' => 'mw-echo-title' ),
|
|
|
|
$this->formatNotificationTitle( $event, $user )->parse()
|
|
|
|
) . "\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 ) {
|
2013-03-07 03:08:12 +00:00
|
|
|
$payload .= $this->formatPayload( $payloadComponent, $event, $user );
|
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
|
2013-03-05 19:26:26 +00:00
|
|
|
$content .= $this->formatTimestamp( $event->getTimestamp() );
|
2013-02-28 23:52:12 +00:00
|
|
|
|
2012-11-30 23:58:54 +00:00
|
|
|
$output .= Xml::tags( 'div', array( 'class' => 'mw-echo-content' ), $content ) . "\n";
|
|
|
|
|
2013-02-13 02:08:36 +00:00
|
|
|
// The state div is used to visually indicate read or unread status. This is
|
|
|
|
// handled in a separate element than the notification element so that things
|
|
|
|
// like the close box won't inherit the greyed out opacity (which can't be reset).
|
|
|
|
$output = Xml::tags( 'div', array( 'class' => 'mw-echo-state' ), $output ) . "\n";
|
|
|
|
|
2012-07-31 00:29:49 +00:00
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:52:12 +00:00
|
|
|
/**
|
2013-04-02 23:47:54 +00:00
|
|
|
* Format notification dismiss interface
|
|
|
|
*
|
2013-02-28 23:52:12 +00:00
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
|
|
|
* @return string
|
|
|
|
*/
|
2013-01-14 23:52:46 +00:00
|
|
|
protected function formatDismissInterface( $event, $user ) {
|
2013-04-16 22:40:45 +00:00
|
|
|
$userLanguage = $user->getOption( 'language' );
|
|
|
|
|
2013-03-20 22:49:20 +00:00
|
|
|
$dismissTitle = wfMessage( 'echo-category-title-' . $event->getCategory() )
|
2013-04-16 22:40:45 +00:00
|
|
|
->inLanguage( $userLanguage )
|
|
|
|
->numParams( 1 )
|
2013-02-16 02:20:34 +00:00
|
|
|
->text();
|
2013-04-16 22:40:45 +00:00
|
|
|
$dismissMessage = wfMessage( 'echo-dismiss-message', $dismissTitle )
|
|
|
|
->inLanguage( $userLanguage )
|
|
|
|
->escaped();
|
2013-02-16 02:20:34 +00:00
|
|
|
$dismiss = Xml::tags( 'div', array( 'class' => 'mw-echo-dismiss-message' ), $dismissMessage ) . "\n";
|
|
|
|
$prefsMessage = wfMessage( 'echo-dismiss-prefs-message' )
|
2013-04-16 22:40:45 +00:00
|
|
|
->inLanguage( $userLanguage )
|
|
|
|
->escaped();
|
2013-02-16 02:20:34 +00:00
|
|
|
$dismiss .= Xml::tags( 'div', array( 'class' => 'mw-echo-prefs-dismiss-message' ), $prefsMessage ) . "\n";
|
2013-01-14 23:52:46 +00:00
|
|
|
$dismiss = Xml::tags( 'div', array( 'class' => 'mw-echo-dismiss', 'style' => 'display:none;' ), $dismiss ) . "\n";
|
|
|
|
return $dismiss;
|
|
|
|
}
|
|
|
|
|
2013-02-28 23:52:12 +00:00
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
|
|
|
* @return string
|
|
|
|
*/
|
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
|
|
|
}
|
|
|
|
|
2013-02-28 23:52:12 +00:00
|
|
|
/**
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
|
|
|
* @param $type
|
2013-06-17 21:44:46 +00:00
|
|
|
* @return array
|
2013-02-28 23:52:12 +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
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
if ( $this->bundleData['use-bundle'] && $this->email['batch-bundle-body'] ) {
|
|
|
|
$bodyKey = $this->email['batch-bundle-body'];
|
|
|
|
} else {
|
|
|
|
$bodyKey = $this->email['batch-body'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$batchBody = preg_replace( "/\n{3,}/", "\n\n", $this->formatFragment( $bodyKey, $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;
|
|
|
|
}
|
|
|
|
|
2013-03-07 03:08:12 +00:00
|
|
|
/**
|
|
|
|
* Formats the payload of a notification, child method overwriting this method should
|
|
|
|
* always call this method in default case so they can use the payload defined in this
|
|
|
|
* function as well
|
|
|
|
* @param $payload string
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function formatPayload( $payload, $event, $user ) {
|
|
|
|
switch ( $payload ) {
|
|
|
|
case 'summary':
|
|
|
|
return $this->formatSummary( $event, $user );
|
|
|
|
break;
|
2013-05-13 22:00:16 +00:00
|
|
|
case 'comment-text':
|
|
|
|
return $this->formatCommentText( $event, $user );
|
|
|
|
break;
|
2013-03-07 03:08:12 +00:00
|
|
|
default:
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-13 22:00:16 +00:00
|
|
|
/**
|
|
|
|
* Extract the comment left by a user on a talk page from the event.
|
|
|
|
* @param $event EchoEvent The event to format the comment of
|
|
|
|
* @param $user User The user to format content for
|
|
|
|
* @return string Up to the first 200 characters of the comment
|
|
|
|
*/
|
|
|
|
protected function formatCommentText( EchoEvent $event, $user ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
if ( !$event->userCan( Revision::DELETED_TEXT, $user ) ) {
|
|
|
|
return wfMessage( 'echo-rev-deleted-text-view' )->text();
|
|
|
|
}
|
2013-05-13 22:00:16 +00:00
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( !isset( $extra['content'] ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$content = EchoDiscussionParser::stripHeader( $extra['content'] );
|
|
|
|
$content = EchoDiscussionParser::stripSignature( $content );
|
|
|
|
$content = EchoDiscussionParser::stripIndents( $content );
|
|
|
|
return EchoDiscussionParser::getTextSnippet( $content, 200 );
|
|
|
|
}
|
|
|
|
|
2013-05-14 22:22:52 +00:00
|
|
|
/**
|
|
|
|
* Extract the subject anchor (linkable portion of the edited page) from
|
|
|
|
* the event.
|
|
|
|
*
|
|
|
|
* @param $event EchoEvent The event to format the subject anchor of
|
|
|
|
* @return string The anchor on page, or an empty string
|
|
|
|
*/
|
|
|
|
protected function formatSubjectAnchor( EchoEvent $event ) {
|
2013-05-08 18:11:44 +00:00
|
|
|
global $wgParser, $wgUser;
|
2013-05-14 22:22:52 +00:00
|
|
|
|
2013-05-08 18:11:44 +00:00
|
|
|
if ( !$event->userCan( Revision::DELETED_TEXT, $wgUser ) ) {
|
|
|
|
return wfMessage( 'echo-rev-deleted-text-view' )->text();
|
|
|
|
}
|
2013-05-14 22:22:52 +00:00
|
|
|
$extra = $event->getExtra();
|
|
|
|
if ( empty( $extra['section-title'] ) ) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
|
|
|
// Strip out #, keeping # in the i18n message makes it look more clear
|
|
|
|
return substr( $wgParser->guessLegacySectionNameFromWikiText( $extra['section-title'] ), 1 );
|
|
|
|
}
|
|
|
|
|
2012-10-28 16:47:41 +00:00
|
|
|
/**
|
|
|
|
* Generate links based on output format and passed properties
|
|
|
|
* $event EchoEvent
|
|
|
|
* $message Message
|
|
|
|
* $props array
|
|
|
|
*/
|
|
|
|
protected function setTitleLink( $event, $message, $props = array() ) {
|
|
|
|
if ( !$event->getTitle() ) {
|
|
|
|
$message->params( wfMessage( 'echo-no-title' )->text() );
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$title = $event->getTitle();
|
|
|
|
|
|
|
|
$param = array();
|
|
|
|
if ( isset( $props['param'] ) ) {
|
|
|
|
$param = (array)$props['param'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $props['fragment'] ) ) {
|
2013-05-14 22:22:52 +00:00
|
|
|
$fragment = $props['fragment'];
|
|
|
|
} else {
|
|
|
|
$fragment = $this->formatSubjectAnchor( $event );
|
2012-10-28 16:47:41 +00:00
|
|
|
}
|
2013-05-14 22:22:52 +00:00
|
|
|
$title->setFragment( "#$fragment" );
|
2012-10-28 16:47:41 +00:00
|
|
|
|
2013-03-12 20:53:09 +00:00
|
|
|
if ( $this->outputFormat === 'html' || $this->outputFormat === 'flyout' ) {
|
2012-10-28 16:47:41 +00:00
|
|
|
$class = array();
|
|
|
|
if ( isset( $props['class'] ) ) {
|
|
|
|
$class['class'] = $props['class'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $props['linkText'] ) ) {
|
|
|
|
$linkText = $props['linkText'];
|
|
|
|
} else {
|
|
|
|
$linkText = htmlspecialchars( $title->getPrefixedText() );
|
|
|
|
}
|
|
|
|
|
|
|
|
$message->rawParams( Linker::link( $title, $linkText, $class, $param ) );
|
|
|
|
} elseif ( $this->outputFormat === 'email' ) {
|
2013-05-15 19:34:10 +00:00
|
|
|
// plain text email in some mail client is messing with
|
|
|
|
// ending punctuation in links, it is better to encode them
|
2013-05-09 18:40:05 +00:00
|
|
|
$url = $title->getCanonicalURL( $param );
|
|
|
|
// $url should contain all ascii characters now, it's safe to use substr()
|
|
|
|
$lastChar = substr( $url, -1 );
|
|
|
|
if ( $lastChar && !ctype_alnum( $lastChar ) ) {
|
2013-05-15 19:34:10 +00:00
|
|
|
$lastChar = str_replace(
|
|
|
|
array( '.', '-', '(', ';', '!', ':', ',' ),
|
|
|
|
array( '%2E', '%2D', '%28', '%3B', '%21', '%3A', '%2C' ),
|
|
|
|
$lastChar
|
|
|
|
);
|
2013-05-09 18:40:05 +00:00
|
|
|
$url = substr( $url, 0, -1 ) . $lastChar;
|
|
|
|
}
|
|
|
|
$message->params( $url );
|
2012-10-28 16:47:41 +00:00
|
|
|
} else {
|
|
|
|
$message->params( $title->getFullURL( $param ) );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-15 23:21:39 +00:00
|
|
|
/**
|
|
|
|
* Get raw bundle data for an event so it can be manipulated
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
2013-03-06 00:04:48 +00:00
|
|
|
* @param $type string Notification distribution type: web/email
|
2013-01-15 23:21:39 +00:00
|
|
|
* @return ResultWrapper|bool
|
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
protected function getRawBundleData( $event, $user, $type ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoBackend;
|
|
|
|
|
|
|
|
// We should keep bundling for events as long as it has bundle
|
|
|
|
// hash event for bundle-turned-off events as well, this is
|
|
|
|
// mainly for historical data
|
|
|
|
if ( !$event->getBundleHash() ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
return $wgEchoBackend->getRawBundleData( $user, $event->getBundleHash(), $type );
|
2013-01-15 23:21:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Construct the bundle data for an event, by default, the group iterator
|
|
|
|
* is agent, eg, by user A and x others. custom formatter can overwrite
|
|
|
|
* this function to use a differnt group iterator such as title, namespace
|
|
|
|
* @param $event EchoEvent
|
|
|
|
* @param $user User
|
2013-03-06 00:04:48 +00:00
|
|
|
* @param $type string Notification distribution type
|
2013-01-15 23:21:39 +00:00
|
|
|
*/
|
2013-03-06 00:04:48 +00:00
|
|
|
protected function generateBundleData( $event, $user, $type ) {
|
2013-01-15 23:21:39 +00:00
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
2013-03-06 00:04:48 +00:00
|
|
|
$data = $this->getRawBundleData( $event, $user, $type );
|
2013-01-15 23:21:39 +00:00
|
|
|
|
|
|
|
if ( !$data ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$agents = array();
|
|
|
|
$agent = $event->getAgent();
|
|
|
|
if ( $agent ) {
|
|
|
|
if ( $agent->isAnon() ) {
|
|
|
|
$agents[$agent->getName()] = $agent->getName();
|
|
|
|
} else {
|
|
|
|
$agents[$agent->getId()] = $agent->getId();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
throw new MWException( "Agent is required for bundling notification!" );
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize with 1 for the agent of current event
|
|
|
|
$count = 1;
|
|
|
|
foreach ( $data as $row ) {
|
|
|
|
$key = $row->event_agent_id ? 'event_agent_id' : 'event_agent_ip';
|
|
|
|
if ( !isset( $agents[$row->$key] ) ) {
|
|
|
|
$agents[$row->$key] = $row->$key;
|
|
|
|
$count++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $count > $wgEchoMaxNotificationCount + 1 ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->bundleData['agent-other-count'] = $count - 1;
|
|
|
|
if ( $count > 1 ) {
|
|
|
|
$this->bundleData['use-bundle'] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getBundleData() {
|
|
|
|
return $this->bundleData;
|
|
|
|
}
|
|
|
|
|
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' ) {
|
2012-08-31 21:50:46 +00:00
|
|
|
if ( !$event->getAgent() ) {
|
2013-03-28 00:17:35 +00:00
|
|
|
$message->params( wfMessage( 'echo-no-agent' )->text() );
|
2013-05-08 18:11:44 +00:00
|
|
|
} elseif ( !$event->userCan( Revision::DELETED_USER, $user ) ) {
|
|
|
|
$message->params( wfMessage( 'rev-deleted-user' )->text() );
|
2012-04-27 15:14:24 +00:00
|
|
|
} else {
|
2013-04-02 23:47:54 +00:00
|
|
|
$message->params( $event->getAgent()->getName() );
|
2012-04-27 15:14:24 +00:00
|
|
|
}
|
2013-01-15 23:21:39 +00:00
|
|
|
// example: {7} others, {99+} others
|
|
|
|
} elseif ( $param === 'agent-other-display') {
|
|
|
|
global $wgEchoMaxNotificationCount;
|
|
|
|
|
|
|
|
if ( $this->bundleData['agent-other-count'] > $wgEchoMaxNotificationCount ) {
|
|
|
|
$message->params(
|
|
|
|
wfMessage( 'echo-notification-count' )
|
|
|
|
->inLanguage( $user->getOption( 'language' ) )
|
|
|
|
->params( $wgEchoMaxNotificationCount )
|
|
|
|
->text()
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$message->params( $this->bundleData['agent-other-count'] );
|
|
|
|
}
|
|
|
|
// the number used for plural support
|
|
|
|
} elseif ( $param === 'agent-other-count') {
|
|
|
|
$message->params( $this->bundleData['agent-other-count'] );
|
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
|
|
|
}
|
2013-03-27 22:47:54 +00:00
|
|
|
} elseif ( $param === 'titlelink' ) {
|
|
|
|
$this->setTitleLink( $event, $message );
|
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 );
|
2013-03-06 00:04:48 +00:00
|
|
|
} elseif ( $param === 'email-intro' ) {
|
|
|
|
if ( $this->bundleData['use-bundle'] && isset( $this->email['batch-bundle-body']['message'] ) ) {
|
|
|
|
$detail = array(
|
|
|
|
'message' => $this->email['batch-bundle-body']['message'],
|
|
|
|
'params' => $this->email['batch-bundle-body']['params']
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$detail = array(
|
|
|
|
'message' => $this->email['batch-body']['message'],
|
|
|
|
'params' => $this->email['batch-body']['params']
|
|
|
|
);
|
|
|
|
}
|
|
|
|
$message->params( $this->formatFragment( $detail, $event, $user )->text() );
|
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
|
|
|
}
|