2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Utility class that formats a notification in the format specified
|
|
|
|
*/
|
|
|
|
class EchoDataOutputFormatter {
|
|
|
|
|
2015-08-19 20:22:45 +00:00
|
|
|
/**
|
2018-08-13 07:25:22 +00:00
|
|
|
* @var string[] type => class
|
2015-08-19 20:22:45 +00:00
|
|
|
*/
|
2016-12-05 18:51:07 +00:00
|
|
|
protected static $formatters = [
|
2019-02-19 10:42:30 +00:00
|
|
|
'flyout' => EchoFlyoutFormatter::class,
|
|
|
|
'model' => EchoModelFormatter::class,
|
|
|
|
'special' => SpecialNotificationsFormatter::class,
|
|
|
|
'html' => SpecialNotificationsFormatter::class,
|
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
|
|
|
|
*
|
2018-09-14 02:01:44 +00:00
|
|
|
* This method returns an array of data, some of it html
|
|
|
|
* escaped, some of it not. This confuses phan-taint-check,
|
|
|
|
* so mark it as safe for html and safe to be escaped again.
|
|
|
|
* @return-taint onlysafefor_htmlnoent
|
|
|
|
*
|
2015-08-06 22:36:05 +00:00
|
|
|
* @param EchoNotification $notification
|
2018-08-13 07:32:22 +00:00
|
|
|
* @param string|false $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
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return array|false False if it could not be formatted
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2018-08-25 10:51:14 +00:00
|
|
|
public static function formatOutput(
|
|
|
|
EchoNotification $notification,
|
2019-01-02 13:52:14 +00:00
|
|
|
$format,
|
2018-08-25 10:51:14 +00:00
|
|
|
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 );
|
2019-12-21 05:45:14 +00:00
|
|
|
$utcTimestampUnix = (int)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
|
2019-12-21 05:45:14 +00:00
|
|
|
$now = (int)wfTimestamp();
|
2014-07-18 03:58:21 +00:00
|
|
|
$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(),
|
2018-09-03 17:01:06 +00:00
|
|
|
'section' => $event->getSection(),
|
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(),
|
2019-03-01 22:34:38 +00:00
|
|
|
'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 ) {
|
2019-04-17 15:46:06 +00:00
|
|
|
if ( $event->userCan( RevisionRecord::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
|
|
|
|
2018-08-25 10:51:14 +00:00
|
|
|
if ( $notification->getBundledNotifications() &&
|
|
|
|
self::isBundleExpandable( $event->getType() )
|
|
|
|
) {
|
|
|
|
$output['bundledNotifications'] = array_values( array_filter( array_map(
|
|
|
|
function ( EchoNotification $notification ) use ( $format, $user, $lang ) {
|
|
|
|
// remove nested notifications to
|
|
|
|
// - ensure they are formatted as single notifications (not bundled)
|
|
|
|
// - prevent further re-entrance on the current notification
|
|
|
|
$notification->setBundledNotifications( [] );
|
|
|
|
$notification->getEvent()->setBundledEvents( [] );
|
|
|
|
return self::formatOutput( $notification, $format, $user, $lang );
|
|
|
|
},
|
|
|
|
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
|
2018-08-13 07:32:22 +00:00
|
|
|
* @return string[]|string|false False if it could not be formatted
|
2015-10-28 17:15:25 +00:00
|
|
|
*/
|
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 );
|
|
|
|
}
|
2019-03-01 22:34:38 +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
|
2019-12-21 05:45:14 +00:00
|
|
|
* @param string|int $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;
|
2018-08-25 10:51:14 +00:00
|
|
|
return isset( $wgEchoNotifications[$type]['bundle']['expandable'] )
|
|
|
|
&& $wgEchoNotifications[$type]['bundle']['expandable'];
|
2016-06-07 20:08:16 +00:00
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
}
|