2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class that formats a notification in the format specified
|
|
|
|
*/
|
|
|
|
class EchoDataOutputFormatter {
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
|
|
|
* @var array type => class
|
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected static $formatters = [
|
2015-11-16 16:05:07 +00:00
|
|
|
'flyout' => 'EchoFlyoutFormatter',
|
|
|
|
'model' => 'EchoModelFormatter',
|
2015-11-11 02:23:03 +00:00
|
|
|
'special' => 'SpecialNotificationsFormatter',
|
2016-01-13 00:53:01 +00:00
|
|
|
'html' => 'SpecialNotificationsFormatter',
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2015-11-11 02:23:03 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Format a notification for a user in the format specified
|
|
|
|
*
|
2015-08-06 22:36:05 +00:00
|
|
|
* @param EchoNotification $notification
|
2016-03-04 19:23:02 +00:00
|
|
|
* @param string|bool $format output format, false to not format any notifications
|
2015-10-26 15:34:36 +00:00
|
|
|
* @param User $user the target user viewing the notification
|
2015-10-26 15:37:57 +00:00
|
|
|
* @param Language $lang Language to format the notification in
|
2015-10-28 17:15:25 +00:00
|
|
|
* @return array|bool false if it could not be formatted
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2015-10-26 15:37:57 +00:00
|
|
|
public static function formatOutput( EchoNotification $notification, $format = false, User $user, Language $lang ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$event = $notification->getEvent();
|
2014-08-16 06:41:39 +00:00
|
|
|
$timestamp = $notification->getTimestamp();
|
2016-07-22 18:59:10 +00:00
|
|
|
$utcTimestampIso8601 = wfTimestamp( TS_ISO_8601, $timestamp );
|
2014-08-16 06:41:39 +00:00
|
|
|
$utcTimestampUnix = wfTimestamp( TS_UNIX, $timestamp );
|
2015-12-22 16:07:30 +00:00
|
|
|
$utcTimestampMW = wfTimestamp( TS_MW, $timestamp );
|
2016-03-04 19:23:02 +00:00
|
|
|
$bundledIds = null;
|
|
|
|
|
|
|
|
$bundledNotifs = $notification->getBundledNotifications();
|
|
|
|
if ( $bundledNotifs ) {
|
2016-06-07 20:08:16 +00:00
|
|
|
$bundledEvents = array_map( function ( EchoNotification $notif ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
return $notif->getEvent();
|
|
|
|
}, $bundledNotifs );
|
|
|
|
$event->setBundledEvents( $bundledEvents );
|
|
|
|
|
|
|
|
$bundledIds = array_map( function ( $event ) {
|
|
|
|
return (int)$event->getId();
|
|
|
|
}, $bundledEvents );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
$timestampMw = self::getUserLocalTime( $user, $timestamp );
|
2014-07-18 03:58:21 +00:00
|
|
|
|
|
|
|
// Start creating date section header
|
|
|
|
$now = wfTimestamp();
|
|
|
|
$dateFormat = substr( $timestampMw, 0, 8 );
|
2014-08-16 06:41:39 +00:00
|
|
|
$timeDiff = $now - $utcTimestampUnix;
|
|
|
|
// Most notifications would be more than two days ago, check this
|
|
|
|
// first instead of checking 'today' then 'yesterday'
|
|
|
|
if ( $timeDiff > 172800 ) {
|
|
|
|
$date = self::getDateHeader( $user, $timestampMw );
|
|
|
|
// 'Today'
|
|
|
|
} elseif ( substr( self::getUserLocalTime( $user, $now ), 0, 8 ) === $dateFormat ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$date = wfMessage( 'echo-date-today' )->escaped();
|
2014-08-16 06:41:39 +00:00
|
|
|
// 'Yesterday'
|
2014-07-18 03:58:21 +00:00
|
|
|
} elseif ( substr( self::getUserLocalTime( $user, $now - 86400 ), 0, 8 ) === $dateFormat ) {
|
|
|
|
$date = wfMessage( 'echo-date-yesterday' )->escaped();
|
|
|
|
} else {
|
2014-08-16 06:41:39 +00:00
|
|
|
$date = self::getDateHeader( $user, $timestampMw );
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
// End creating date section header
|
|
|
|
|
2016-12-05 18:51:07 +00:00
|
|
|
$output = [
|
2016-04-21 11:16:21 +00:00
|
|
|
'wiki' => wfWikiID(),
|
2014-07-18 03:58:21 +00:00
|
|
|
'id' => $event->getId(),
|
|
|
|
'type' => $event->getType(),
|
|
|
|
'category' => $event->getCategory(),
|
2016-12-05 18:51:07 +00:00
|
|
|
'timestamp' => [
|
2016-07-22 18:59:10 +00:00
|
|
|
// ISO 8601 is supposed to be the *only* format used for
|
|
|
|
// date output, but back-compat...
|
|
|
|
'utciso8601' => $utcTimestampIso8601,
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
// UTC timestamp in UNIX format used for loading more notification
|
2014-08-16 06:41:39 +00:00
|
|
|
'utcunix' => $utcTimestampUnix,
|
|
|
|
'unix' => self::getUserLocalTime( $user, $timestamp, TS_UNIX ),
|
2015-12-22 16:07:30 +00:00
|
|
|
'utcmw' => $utcTimestampMW,
|
2014-07-18 03:58:21 +00:00
|
|
|
'mw' => $timestampMw,
|
|
|
|
'date' => $date
|
2016-12-05 18:51:07 +00:00
|
|
|
],
|
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
|
2016-03-04 19:23:02 +00:00
|
|
|
if ( $bundledIds ) {
|
|
|
|
$output['bundledIds'] = $bundledIds;
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $event->getVariant() ) {
|
|
|
|
$output['variant'] = $event->getVariant();
|
|
|
|
}
|
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
if ( $title ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$output['title'] = [
|
2014-08-16 06:41:39 +00:00
|
|
|
'full' => $title->getPrefixedText(),
|
|
|
|
'namespace' => $title->getNSText(),
|
2015-10-01 13:48:52 +00:00
|
|
|
'namespace-key' => $title->getNamespace(),
|
2014-08-16 06:41:39 +00:00
|
|
|
'text' => $title->getText(),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
$agent = $event->getAgent();
|
|
|
|
if ( $agent ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $event->userCan( Revision::DELETED_USER, $user ) ) {
|
2016-12-05 18:51:07 +00:00
|
|
|
$output['agent'] = [
|
2014-08-16 06:41:39 +00:00
|
|
|
'id' => $agent->getId(),
|
|
|
|
'name' => $agent->getName(),
|
2016-12-05 18:51:07 +00:00
|
|
|
];
|
2014-07-18 03:58:21 +00:00
|
|
|
} else {
|
2016-12-05 18:51:07 +00:00
|
|
|
$output['agent'] = [ 'userhidden' => '' ];
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-19 04:44:03 +00:00
|
|
|
if ( $event->getRevision() ) {
|
|
|
|
$output['revid'] = $event->getRevision()->getId();
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $notification->getReadTimestamp() ) {
|
|
|
|
$output['read'] = $notification->getReadTimestamp();
|
|
|
|
}
|
|
|
|
|
2014-08-07 00:55:24 +00:00
|
|
|
// This is only meant for unread notifications, if a notification has a target
|
|
|
|
// page, then it shouldn't be auto marked as read unless the user visits
|
2015-11-16 16:05:07 +00:00
|
|
|
// the target page or a user marks it as read manually ( coming soon )
|
2016-12-05 18:51:07 +00:00
|
|
|
$output['targetpages'] = [];
|
2015-03-16 15:47:13 +00:00
|
|
|
if ( $notification->getTargetPages() ) {
|
|
|
|
foreach ( $notification->getTargetPages() as $targetPage ) {
|
|
|
|
$output['targetpages'][] = $targetPage->getPageId();
|
|
|
|
}
|
2014-08-07 00:55:24 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $format ) {
|
2015-10-28 17:15:25 +00:00
|
|
|
$formatted = self::formatNotification( $event, $user, $format, $lang );
|
|
|
|
if ( $formatted === false ) {
|
|
|
|
// Can't display it, so mark it as read
|
2016-07-27 14:52:18 +00:00
|
|
|
EchoDeferredMarkAsDeletedUpdate::add( $event );
|
2015-10-28 17:15:25 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$output['*'] = $formatted;
|
2016-06-07 20:08:16 +00:00
|
|
|
|
|
|
|
if ( $notification->getBundledNotifications() && self::isBundleExpandable( $event->getType() ) ) {
|
2016-09-19 22:53:01 +00:00
|
|
|
$output['bundledNotifications'] = array_values( array_filter( array_map( function ( EchoNotification $notification ) use ( $format, $user, $lang ) {
|
2016-06-07 20:08:16 +00:00
|
|
|
// remove nested notifications to
|
2017-06-20 02:41:30 +00:00
|
|
|
// - ensure they are formatted as single notifications (not bundled)
|
|
|
|
// - prevent further re-entrance on the current notification
|
2016-12-05 18:51:07 +00:00
|
|
|
$notification->setBundledNotifications( [] );
|
|
|
|
$notification->getEvent()->setBundledEvents( [] );
|
2016-06-07 20:08:16 +00:00
|
|
|
return self::formatOutput( $notification, $format, $user, $lang );
|
2016-12-05 18:51:07 +00:00
|
|
|
}, array_merge( [ $notification ], $notification->getBundledNotifications() ) ) ) );
|
2016-06-07 20:08:16 +00:00
|
|
|
}
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
2015-10-28 17:15:25 +00:00
|
|
|
/**
|
|
|
|
* @param EchoEvent $event
|
|
|
|
* @param User $user
|
2016-03-04 19:23:02 +00:00
|
|
|
* @param string $format
|
2015-10-28 17:15:25 +00:00
|
|
|
* @param Language $lang
|
|
|
|
* @return string|bool false if it could not be formatted
|
|
|
|
*/
|
2015-10-26 15:37:57 +00:00
|
|
|
protected static function formatNotification( EchoEvent $event, User $user, $format, $lang ) {
|
2016-07-20 14:53:36 +00:00
|
|
|
if ( isset( self::$formatters[$format] ) ) {
|
2017-01-02 14:05:49 +00:00
|
|
|
$class = self::$formatters[$format];
|
2015-08-19 20:22:45 +00:00
|
|
|
/** @var EchoEventFormatter $formatter */
|
2017-01-02 14:05:49 +00:00
|
|
|
$formatter = new $class( $user, $lang );
|
2015-08-19 20:22:45 +00:00
|
|
|
return $formatter->format( $event );
|
|
|
|
} else {
|
2016-07-20 14:53:36 +00:00
|
|
|
return false;
|
2015-08-19 20:22:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
/**
|
|
|
|
* Get the date header in user's format, 'May 10' or '10 May', depending
|
|
|
|
* on user's date format preference
|
|
|
|
* @param User $user
|
|
|
|
* @param string $timestampMw
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected static function getDateHeader( User $user, $timestampMw ) {
|
|
|
|
$lang = RequestContext::getMain()->getLanguage();
|
|
|
|
$dateFormat = $lang->getDateFormatString( 'pretty', $user->getDatePreference() ?: 'default' );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
return $lang->sprintfDate( $dateFormat, $timestampMw );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Helper function for converting UTC timezone to a user's timezone
|
|
|
|
*
|
2017-08-23 14:38:58 +00:00
|
|
|
* @param User $user
|
|
|
|
* @param string $ts
|
2015-12-09 17:00:15 +00:00
|
|
|
* @param int $format output format
|
2014-07-18 03:58:21 +00:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getUserLocalTime( User $user, $ts, $format = TS_MW ) {
|
|
|
|
$timestamp = new MWTimestamp( $ts );
|
|
|
|
$timestamp->offsetForUser( $user );
|
2015-10-01 13:48:52 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
return $timestamp->getTimestamp( $format );
|
|
|
|
}
|
|
|
|
|
2016-06-07 20:08:16 +00:00
|
|
|
/**
|
|
|
|
* @param string $type
|
|
|
|
* @return bool Whether a notification type can be an expandable bundle
|
|
|
|
*/
|
|
|
|
public static function isBundleExpandable( $type ) {
|
|
|
|
global $wgEchoNotifications;
|
|
|
|
return isset( $wgEchoNotifications[$type]['bundle']['expandable'] ) && $wgEchoNotifications[$type]['bundle']['expandable'];
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|