diff --git a/formatters/BasicFormatter.php b/formatters/BasicFormatter.php index 069fa353c..b9235cfeb 100644 --- a/formatters/BasicFormatter.php +++ b/formatters/BasicFormatter.php @@ -464,7 +464,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter { * $props array */ protected function setTitleLink( $event, $message, $props = array() ) { - if ( !$event->getTitle() ) { + $title = $event->getTitle(); + if ( !$title ) { $message->params( $this->getMessage( 'echo-no-title' )->text() ); return; } @@ -473,7 +474,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter { $props['fragment'] = $this->formatSubjectAnchor( $event ); } - $link = $this->buildLinkParam( $event->getTitle(), $props ); + $link = $this->buildLinkParam( $title, $props ); $message->params( $link ); } @@ -740,7 +741,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter { */ protected function getLinkParams( $event, $user, $destination ) { $target = null; - $query = array(); + $query = array(); + $title = $event->getTitle(); // Set up link parameters based on the destination switch ( $destination ) { case 'agent': @@ -749,10 +751,10 @@ class EchoBasicFormatter extends EchoNotificationFormatter { } break; case 'title': - $target = $event->getTitle(); + $target = $title; break; case 'section': - $target = $event->getTitle(); + $target = $title; if ( $target ) { $fragment = $this->formatSubjectAnchor( $event ); if ( $fragment ) { @@ -762,8 +764,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter { break; case 'diff': $eventData = $event->getExtra(); - if ( isset( $eventData['revid'] ) && $event->getTitle() ) { - $target = $event->getTitle(); + if ( isset( $eventData['revid'] ) && $title ) { + $target = $title; // Explicitly set fragment to empty string for diff links, $title is // passed around by reference, it may end up using fragment set from // other parameters @@ -886,7 +888,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter { } elseif ( $param === 'user' ) { $message->params( $user->getName() ); } elseif ( $param === 'title' ) { - if ( !$event->getTitle() ) { + $title = $event->getTitle(); + if ( !$title ) { $message->params( $this->getMessage( 'echo-no-title' )->text() ); } else { if ( $this->outputFormat === 'htmlemail' ) { @@ -895,7 +898,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter { ); $this->setTitleLink( $event, $message, $props ); } else { - $message->params( $this->formatTitle( $event->getTitle() ) ); + $message->params( $this->formatTitle( $title ) ); } } } elseif ( $param === 'titlelink' ) { diff --git a/includes/DataOutputFormatter.php b/includes/DataOutputFormatter.php index d7b4f3736..68fadf1d9 100644 --- a/includes/DataOutputFormatter.php +++ b/includes/DataOutputFormatter.php @@ -14,6 +14,9 @@ class EchoDataOutputFormatter { */ public static function formatOutput( EchoNotification $notification, $format = false, User $user = null ) { $event = $notification->getEvent(); + $timestamp = $notification->getTimestamp(); + $utcTimestampUnix = wfTimestamp( TS_UNIX, $timestamp ); + // Default to notification user if user is not specified if ( !$user ) { $user = $notification->getUser(); @@ -23,22 +26,24 @@ class EchoDataOutputFormatter { $event->setBundleHash( $notification->getBundleDisplayHash() ); } - $timestampMw = self::getUserLocalTime( $user, $notification->getTimestamp() ); + $timestampMw = self::getUserLocalTime( $user, $timestamp ); // Start creating date section header $now = wfTimestamp(); $dateFormat = substr( $timestampMw, 0, 8 ); - if ( substr( self::getUserLocalTime( $user, $now ), 0, 8 ) === $dateFormat ) { - // 'Today' + $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 ) { $date = wfMessage( 'echo-date-today' )->escaped(); + // 'Yesterday' } elseif ( substr( self::getUserLocalTime( $user, $now - 86400 ), 0, 8 ) === $dateFormat ) { - // 'Yesterday' $date = wfMessage( 'echo-date-yesterday' )->escaped(); } else { - // 'May 10' or '10 May' (depending on user's date format preference) - $lang = RequestContext::getMain()->getLanguage(); - $dateFormat = $lang->getDateFormatString( 'pretty', $user->getDatePreference() ?: 'default' ); - $date = $lang->sprintfDate( $dateFormat, $timestampMw ); + $date = self::getDateHeader( $user, $timestampMw ); } // End creating date section header @@ -48,8 +53,8 @@ class EchoDataOutputFormatter { 'category' => $event->getCategory(), 'timestamp' => array( // UTC timestamp in UNIX format used for loading more notification - 'utcunix' => wfTimestamp( TS_UNIX, $notification->getTimestamp() ), - 'unix' => self::getUserLocalTime( $user, $notification->getTimestamp(), TS_UNIX ), + 'utcunix' => $utcTimestampUnix, + 'unix' => self::getUserLocalTime( $user, $timestamp, TS_UNIX ), 'mw' => $timestampMw, 'date' => $date ), @@ -59,20 +64,22 @@ class EchoDataOutputFormatter { $output['variant'] = $event->getVariant(); } - if ( $event->getTitle() ) { + $title = $event->getTitle(); + if ( $title ) { $output['title'] = array( - 'full' => $event->getTitle()->getPrefixedText(), - 'namespace' => $event->getTitle()->getNSText(), - 'namespace-key' => $event->getTitle()->getNamespace(), - 'text' => $event->getTitle()->getText(), + 'full' => $title->getPrefixedText(), + 'namespace' => $title->getNSText(), + 'namespace-key' =>$title->getNamespace(), + 'text' => $title->getText(), ); } - if ( $event->getAgent() ) { + $agent = $event->getAgent(); + if ( $agent ) { if ( $event->userCan( Revision::DELETED_USER, $user ) ) { $output['agent'] = array( - 'id' => $event->getAgent()->getId(), - 'name' => $event->getAgent()->getName(), + 'id' => $agent->getId(), + 'name' => $agent->getName(), ); } else { $output['agent'] = array( 'userhidden' => '' ); @@ -99,6 +106,19 @@ class EchoDataOutputFormatter { return $output; } + /** + * 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 ); + } + /** * Helper function for converting UTC timezone to a user's timezone * diff --git a/model/Event.php b/model/Event.php index 2d1f85389..af679147e 100644 --- a/model/Event.php +++ b/model/Event.php @@ -496,8 +496,9 @@ class EchoEvent extends EchoAbstractEntity{ */ public function getLinkMessage( $rank ) { global $wgEchoNotifications; - if ( isset( $wgEchoNotifications[$this->getType()][$rank.'-link']['message'] ) ) { - return $wgEchoNotifications[$this->getType()][$rank.'-link']['message']; + $type = $this->getType(); + if ( isset( $wgEchoNotifications[$type][$rank.'-link']['message'] ) ) { + return $wgEchoNotifications[$type][$rank.'-link']['message']; } return ''; } @@ -510,8 +511,9 @@ class EchoEvent extends EchoAbstractEntity{ */ public function getLinkDestination( $rank ) { global $wgEchoNotifications; - if ( isset( $wgEchoNotifications[$this->getType()][$rank.'-link']['destination'] ) ) { - return $wgEchoNotifications[$this->getType()][$rank.'-link']['destination']; + $type = $this->getType(); + if ( isset( $wgEchoNotifications[$type][$rank.'-link']['destination'] ) ) { + return $wgEchoNotifications[$type][$rank.'-link']['destination']; } return ''; }