2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
2022-11-13 07:48:43 +00:00
|
|
|
namespace MediaWiki\Extension\Notifications;
|
|
|
|
|
|
|
|
use Language;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoEventFormatter;
|
2022-11-01 22:01:23 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoFlyoutFormatter;
|
|
|
|
use MediaWiki\Extension\Notifications\Formatters\EchoModelFormatter;
|
|
|
|
use MediaWiki\Extension\Notifications\Formatters\SpecialNotificationsFormatter;
|
2022-11-02 21:34:17 +00:00
|
|
|
use MediaWiki\Extension\Notifications\Model\Event;
|
|
|
|
use MediaWiki\Extension\Notifications\Model\Notification;
|
2019-04-17 15:46:06 +00:00
|
|
|
use MediaWiki\Revision\RevisionRecord;
|
2023-04-25 09:53:21 +00:00
|
|
|
use MediaWiki\WikiMap\WikiMap;
|
2022-11-13 07:48:43 +00:00
|
|
|
use MWTimestamp;
|
|
|
|
use RequestContext;
|
|
|
|
use User;
|
2019-04-17 15:46:06 +00:00
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Utility class that formats a notification in the format specified
|
2021-08-11 09:22:49 +00:00
|
|
|
* @todo Make this a service with DI
|
2014-07-18 03:58:21 +00:00
|
|
|
*/
|
2022-11-13 07:48:43 +00:00
|
|
|
class DataOutputFormatter {
|
2014-07-18 03:58:21 +00:00
|
|
|
|
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
|
|
|
|
*
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Notification $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(
|
2022-11-02 21:34:17 +00:00
|
|
|
Notification $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 ) {
|
2022-11-02 21:34:17 +00:00
|
|
|
$bundledEvents = array_map( static function ( Notification $notif ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
return $notif->getEvent();
|
|
|
|
}, $bundledNotifs );
|
|
|
|
$event->setBundledEvents( $bundledEvents );
|
|
|
|
|
2021-05-04 16:06:42 +00:00
|
|
|
$bundledIds = array_map( static function ( $event ) {
|
2016-03-04 19:23:02 +00:00
|
|
|
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 = [
|
2021-12-21 00:47:31 +00:00
|
|
|
'wiki' => WikiMap::getCurrentWikiId(),
|
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
|
2022-11-13 07:48:43 +00:00
|
|
|
DeferredMarkAsDeletedUpdate::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() &&
|
2022-11-13 07:53:42 +00:00
|
|
|
Services::getInstance()->getAttributeManager()->isBundleExpandable( $event->getType() )
|
2018-08-25 10:51:14 +00:00
|
|
|
) {
|
|
|
|
$output['bundledNotifications'] = array_values( array_filter( array_map(
|
2022-11-02 21:34:17 +00:00
|
|
|
static function ( Notification $notification ) use ( $format, $user, $lang ) {
|
2018-08-25 10:51:14 +00:00
|
|
|
// 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
|
|
|
/**
|
2022-11-02 21:34:17 +00:00
|
|
|
* @param Event $event
|
2015-10-28 17:15:25 +00:00
|
|
|
* @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
|
|
|
*/
|
2022-11-02 21:34:17 +00:00
|
|
|
protected static function formatNotification( Event $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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|