Merge "[5] Code cleanup and miscellneous fix"

This commit is contained in:
jenkins-bot 2014-08-19 22:20:14 +00:00 committed by Gerrit Code Review
commit 6ae565e1e8
3 changed files with 56 additions and 31 deletions

View file

@ -464,7 +464,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
* $props array * $props array
*/ */
protected function setTitleLink( $event, $message, $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() ); $message->params( $this->getMessage( 'echo-no-title' )->text() );
return; return;
} }
@ -473,7 +474,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
$props['fragment'] = $this->formatSubjectAnchor( $event ); $props['fragment'] = $this->formatSubjectAnchor( $event );
} }
$link = $this->buildLinkParam( $event->getTitle(), $props ); $link = $this->buildLinkParam( $title, $props );
$message->params( $link ); $message->params( $link );
} }
@ -740,7 +741,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
*/ */
protected function getLinkParams( $event, $user, $destination ) { protected function getLinkParams( $event, $user, $destination ) {
$target = null; $target = null;
$query = array(); $query = array();
$title = $event->getTitle();
// Set up link parameters based on the destination // Set up link parameters based on the destination
switch ( $destination ) { switch ( $destination ) {
case 'agent': case 'agent':
@ -749,10 +751,10 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
} }
break; break;
case 'title': case 'title':
$target = $event->getTitle(); $target = $title;
break; break;
case 'section': case 'section':
$target = $event->getTitle(); $target = $title;
if ( $target ) { if ( $target ) {
$fragment = $this->formatSubjectAnchor( $event ); $fragment = $this->formatSubjectAnchor( $event );
if ( $fragment ) { if ( $fragment ) {
@ -762,8 +764,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
break; break;
case 'diff': case 'diff':
$eventData = $event->getExtra(); $eventData = $event->getExtra();
if ( isset( $eventData['revid'] ) && $event->getTitle() ) { if ( isset( $eventData['revid'] ) && $title ) {
$target = $event->getTitle(); $target = $title;
// Explicitly set fragment to empty string for diff links, $title is // Explicitly set fragment to empty string for diff links, $title is
// passed around by reference, it may end up using fragment set from // passed around by reference, it may end up using fragment set from
// other parameters // other parameters
@ -886,7 +888,8 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
} elseif ( $param === 'user' ) { } elseif ( $param === 'user' ) {
$message->params( $user->getName() ); $message->params( $user->getName() );
} elseif ( $param === 'title' ) { } elseif ( $param === 'title' ) {
if ( !$event->getTitle() ) { $title = $event->getTitle();
if ( !$title ) {
$message->params( $this->getMessage( 'echo-no-title' )->text() ); $message->params( $this->getMessage( 'echo-no-title' )->text() );
} else { } else {
if ( $this->outputFormat === 'htmlemail' ) { if ( $this->outputFormat === 'htmlemail' ) {
@ -895,7 +898,7 @@ class EchoBasicFormatter extends EchoNotificationFormatter {
); );
$this->setTitleLink( $event, $message, $props ); $this->setTitleLink( $event, $message, $props );
} else { } else {
$message->params( $this->formatTitle( $event->getTitle() ) ); $message->params( $this->formatTitle( $title ) );
} }
} }
} elseif ( $param === 'titlelink' ) { } elseif ( $param === 'titlelink' ) {

View file

@ -14,6 +14,9 @@ class EchoDataOutputFormatter {
*/ */
public static function formatOutput( EchoNotification $notification, $format = false, User $user = null ) { public static function formatOutput( EchoNotification $notification, $format = false, User $user = null ) {
$event = $notification->getEvent(); $event = $notification->getEvent();
$timestamp = $notification->getTimestamp();
$utcTimestampUnix = wfTimestamp( TS_UNIX, $timestamp );
// Default to notification user if user is not specified // Default to notification user if user is not specified
if ( !$user ) { if ( !$user ) {
$user = $notification->getUser(); $user = $notification->getUser();
@ -23,22 +26,24 @@ class EchoDataOutputFormatter {
$event->setBundleHash( $notification->getBundleDisplayHash() ); $event->setBundleHash( $notification->getBundleDisplayHash() );
} }
$timestampMw = self::getUserLocalTime( $user, $notification->getTimestamp() ); $timestampMw = self::getUserLocalTime( $user, $timestamp );
// Start creating date section header // Start creating date section header
$now = wfTimestamp(); $now = wfTimestamp();
$dateFormat = substr( $timestampMw, 0, 8 ); $dateFormat = substr( $timestampMw, 0, 8 );
if ( substr( self::getUserLocalTime( $user, $now ), 0, 8 ) === $dateFormat ) { $timeDiff = $now - $utcTimestampUnix;
// 'Today' // 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(); $date = wfMessage( 'echo-date-today' )->escaped();
// 'Yesterday'
} elseif ( substr( self::getUserLocalTime( $user, $now - 86400 ), 0, 8 ) === $dateFormat ) { } elseif ( substr( self::getUserLocalTime( $user, $now - 86400 ), 0, 8 ) === $dateFormat ) {
// 'Yesterday'
$date = wfMessage( 'echo-date-yesterday' )->escaped(); $date = wfMessage( 'echo-date-yesterday' )->escaped();
} else { } else {
// 'May 10' or '10 May' (depending on user's date format preference) $date = self::getDateHeader( $user, $timestampMw );
$lang = RequestContext::getMain()->getLanguage();
$dateFormat = $lang->getDateFormatString( 'pretty', $user->getDatePreference() ?: 'default' );
$date = $lang->sprintfDate( $dateFormat, $timestampMw );
} }
// End creating date section header // End creating date section header
@ -48,8 +53,8 @@ class EchoDataOutputFormatter {
'category' => $event->getCategory(), 'category' => $event->getCategory(),
'timestamp' => array( 'timestamp' => array(
// UTC timestamp in UNIX format used for loading more notification // UTC timestamp in UNIX format used for loading more notification
'utcunix' => wfTimestamp( TS_UNIX, $notification->getTimestamp() ), 'utcunix' => $utcTimestampUnix,
'unix' => self::getUserLocalTime( $user, $notification->getTimestamp(), TS_UNIX ), 'unix' => self::getUserLocalTime( $user, $timestamp, TS_UNIX ),
'mw' => $timestampMw, 'mw' => $timestampMw,
'date' => $date 'date' => $date
), ),
@ -59,20 +64,22 @@ class EchoDataOutputFormatter {
$output['variant'] = $event->getVariant(); $output['variant'] = $event->getVariant();
} }
if ( $event->getTitle() ) { $title = $event->getTitle();
if ( $title ) {
$output['title'] = array( $output['title'] = array(
'full' => $event->getTitle()->getPrefixedText(), 'full' => $title->getPrefixedText(),
'namespace' => $event->getTitle()->getNSText(), 'namespace' => $title->getNSText(),
'namespace-key' => $event->getTitle()->getNamespace(), 'namespace-key' =>$title->getNamespace(),
'text' => $event->getTitle()->getText(), 'text' => $title->getText(),
); );
} }
if ( $event->getAgent() ) { $agent = $event->getAgent();
if ( $agent ) {
if ( $event->userCan( Revision::DELETED_USER, $user ) ) { if ( $event->userCan( Revision::DELETED_USER, $user ) ) {
$output['agent'] = array( $output['agent'] = array(
'id' => $event->getAgent()->getId(), 'id' => $agent->getId(),
'name' => $event->getAgent()->getName(), 'name' => $agent->getName(),
); );
} else { } else {
$output['agent'] = array( 'userhidden' => '' ); $output['agent'] = array( 'userhidden' => '' );
@ -99,6 +106,19 @@ class EchoDataOutputFormatter {
return $output; 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 * Helper function for converting UTC timezone to a user's timezone
* *

View file

@ -496,8 +496,9 @@ class EchoEvent extends EchoAbstractEntity{
*/ */
public function getLinkMessage( $rank ) { public function getLinkMessage( $rank ) {
global $wgEchoNotifications; global $wgEchoNotifications;
if ( isset( $wgEchoNotifications[$this->getType()][$rank.'-link']['message'] ) ) { $type = $this->getType();
return $wgEchoNotifications[$this->getType()][$rank.'-link']['message']; if ( isset( $wgEchoNotifications[$type][$rank.'-link']['message'] ) ) {
return $wgEchoNotifications[$type][$rank.'-link']['message'];
} }
return ''; return '';
} }
@ -510,8 +511,9 @@ class EchoEvent extends EchoAbstractEntity{
*/ */
public function getLinkDestination( $rank ) { public function getLinkDestination( $rank ) {
global $wgEchoNotifications; global $wgEchoNotifications;
if ( isset( $wgEchoNotifications[$this->getType()][$rank.'-link']['destination'] ) ) { $type = $this->getType();
return $wgEchoNotifications[$this->getType()][$rank.'-link']['destination']; if ( isset( $wgEchoNotifications[$type][$rank.'-link']['destination'] ) ) {
return $wgEchoNotifications[$type][$rank.'-link']['destination'];
} }
return ''; return '';
} }