Bsitu has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/154386

Change subject: [4] Code cleanup and miscellneous fix
......................................................................

[4] Code cleanup and miscellneous fix

* cache function result in local variables

* Update the logic of generating notification date header

Change-Id: I04c3ed853076f17c819da8f27bfdb169e99b2a3a
---
M includes/DataOutputFormatter.php
M model/Event.php
2 files changed, 44 insertions(+), 22 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/86/154386/1

diff --git a/includes/DataOutputFormatter.php b/includes/DataOutputFormatter.php
index d7b4f37..c0685b1 100644
--- a/includes/DataOutputFormatter.php
+++ b/includes/DataOutputFormatter.php
@@ -14,6 +14,9 @@
         */
        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 @@
                        $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 @@
                        '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 @@
                        $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' => '' );
@@ -100,6 +107,19 @@
        }
 
        /**
+        * 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
         *
         * @param User
diff --git a/model/Event.php b/model/Event.php
index 7545f75..11e1aaa 100644
--- a/model/Event.php
+++ b/model/Event.php
@@ -487,8 +487,9 @@
         */
        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 '';
        }
@@ -501,8 +502,9 @@
         */
        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 '';
        }

-- 
To view, visit https://gerrit.wikimedia.org/r/154386
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I04c3ed853076f17c819da8f27bfdb169e99b2a3a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Bsitu <bs...@wikimedia.org>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to