2014-07-18 03:58:21 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility class that formats a notification in the format specified
|
|
|
|
*/
|
|
|
|
class EchoDataOutputFormatter {
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format a notification for a user in the format specified
|
|
|
|
*
|
|
|
|
* @param string|bool specifify output format, false to not format any notifications
|
|
|
|
* @param User|null the target user viewing the notification
|
|
|
|
* @return array
|
|
|
|
*/
|
2014-07-31 20:10:35 +00:00
|
|
|
public static function formatOutput( EchoNotification $notification, $format = false, User $user = null ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$event = $notification->getEvent();
|
2014-08-16 06:41:39 +00:00
|
|
|
$timestamp = $notification->getTimestamp();
|
|
|
|
$utcTimestampUnix = wfTimestamp( TS_UNIX, $timestamp );
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
// Default to notification user if user is not specified
|
|
|
|
if ( !$user ) {
|
|
|
|
$user = $notification->getUser();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $notification->getBundleBase() && $notification->getBundleDisplayHash() ) {
|
|
|
|
$event->setBundleHash( $notification->getBundleDisplayHash() );
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
|
|
|
$output = array(
|
|
|
|
'id' => $event->getId(),
|
|
|
|
'type' => $event->getType(),
|
|
|
|
'category' => $event->getCategory(),
|
|
|
|
'timestamp' => array(
|
|
|
|
// 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 ),
|
2014-07-18 03:58:21 +00:00
|
|
|
'mw' => $timestampMw,
|
|
|
|
'date' => $date
|
|
|
|
),
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $event->getVariant() ) {
|
|
|
|
$output['variant'] = $event->getVariant();
|
|
|
|
}
|
|
|
|
|
2014-08-16 06:41:39 +00:00
|
|
|
$title = $event->getTitle();
|
|
|
|
if ( $title ) {
|
2014-07-18 03:58:21 +00:00
|
|
|
$output['title'] = array(
|
2014-08-16 06:41:39 +00:00
|
|
|
'full' => $title->getPrefixedText(),
|
|
|
|
'namespace' => $title->getNSText(),
|
|
|
|
'namespace-key' =>$title->getNamespace(),
|
|
|
|
'text' => $title->getText(),
|
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 ) ) {
|
|
|
|
$output['agent'] = array(
|
2014-08-16 06:41:39 +00:00
|
|
|
'id' => $agent->getId(),
|
|
|
|
'name' => $agent->getName(),
|
2014-07-18 03:58:21 +00:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
$output['agent'] = array( 'userhidden' => '' );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
// the target page or a user marks it as read manully ( coming soon )
|
|
|
|
if ( $notification->getTargetPage() ) {
|
|
|
|
$output['targetpage'] = $notification->getTargetPage()->getPageId();
|
|
|
|
} else {
|
|
|
|
$output['targetpage'] = '';
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
if ( $format ) {
|
|
|
|
$output['*'] = EchoNotificationController::formatNotification( $event, $user, $format );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
|
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' );
|
|
|
|
return $lang->sprintfDate( $dateFormat, $timestampMw );
|
|
|
|
}
|
|
|
|
|
2014-07-18 03:58:21 +00:00
|
|
|
/**
|
|
|
|
* Helper function for converting UTC timezone to a user's timezone
|
|
|
|
*
|
|
|
|
* @param User
|
|
|
|
* @param string
|
|
|
|
* @param int output format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getUserLocalTime( User $user, $ts, $format = TS_MW ) {
|
|
|
|
$timestamp = new MWTimestamp( $ts );
|
|
|
|
$timestamp->offsetForUser( $user );
|
|
|
|
return $timestamp->getTimestamp( $format );
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|