Added new MWTimestamp::getRelativeTimestamp for pure relative.
authorTyler Anthony Romeo <tylerromeo@gmail.com>
Thu, 24 Jan 2013 22:38:36 +0000 (17:38 -0500)
committerTyler Anthony Romeo <tylerromeo@gmail.com>
Tue, 24 Sep 2013 23:40:48 +0000 (19:40 -0400)
Since MWTimestamp::getHumanTimestamp just makes a pretty
timestamp, and not a purely relative one, this commit adds
MWTimestamp::getRelativeTimestamp, which does just that.

Change-Id: I8b3e3fc6eeb4afd58f85fa7d05b5ea1a51b0afb6

docs/hooks.txt
includes/Timestamp.php
tests/phpunit/includes/TimestampTest.php

index a6fa9fe..1a1a454 100644 (file)
@@ -1205,6 +1205,16 @@ $out: OutputPage object (to check what type of page the user is on)
 $user: User whose preferences are being modified.
 &$preferences: Preferences description array, to be fed to an HTMLForm object
 
+'GetRelativeTimestamp': Pre-emptively override the relative timestamp generated
+by MWTimestamp::getRelativeTimestamp(). Return false in this hook to use the custom
+output.
+&$output: string for the output timestamp
+&$diff: DateInterval representing the difference between the timestamps
+$timestamp: MWTimestamp object of the current (user-adjusted) timestamp
+$relativeTo: MWTimestamp object of the relative (user-adjusted) timestamp
+$user: User whose preferences are being used to make timestamp
+$lang: Language that will be used to render the timestamp
+
 'getUserPermissionsErrors': Add a permissions error when permissions errors are
 checked for. Use instead of userCan for most cases. Return false if the user
 can't do it, and populate $result with the reason in the form of
index 5296122..9952001 100644 (file)
@@ -270,6 +270,44 @@ class MWTimestamp {
                return $interval;
        }
 
+       /**
+        * Generate a purely relative timestamp, i.e., represent the time elapsed between
+        * the given base timestamp and this object.
+        *
+        * @param MWTimestamp $relativeTo Relative base timestamp (defaults to now)
+        * @param User $user Use to use offset for
+        * @param Language $lang Language to use
+        * @param array $chosenIntervals Intervals to use to represent it
+        * @return string Relative timestamp
+        */
+       public function getRelativeTimestamp(
+               MWTimestamp $relativeTo = null,
+               User $user = null,
+               Language $lang = null,
+               array $chosenIntervals = array()
+       ) {
+               if ( $relativeTo === null ) {
+                       $relativeTo = new self;
+               }
+               if ( $user === null ) {
+                       $user = RequestContext::getMain()->getUser();
+               }
+               if ( $lang === null ) {
+                       $lang = RequestContext::getMain()->getLanguage();
+               }
+
+               $ts = '';
+               $diff = $this->diff( $relativeTo );
+               if ( wfRunHooks( 'GetRelativeTimestamp', array( &$ts, &$diff, $this, $relativeTo, $user, $lang ) ) ) {
+                       $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
+                       $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
+                               ->inLanguage( $lang )
+                               ->text();
+               }
+
+               return $ts;
+       }
+
        /**
         * @since 1.20
         *
index 07dcb7d..3668046 100644 (file)
@@ -200,4 +200,101 @@ class TimestampTest extends MediaWikiLangTestCase {
                        ),
                );
        }
+
+       /**
+        * @test
+        * @dataProvider provideRelativeTimestampTests
+        */
+       public function testRelativeTimestamp(
+               $tsTime, // The timestamp to format
+               $currentTime, // The time to consider "now"
+               $timeCorrection, // The time offset to use
+               $dateFormat, // The date preference to use
+               $expectedOutput, // The expected output
+               $desc // Description
+       ) {
+               $user = $this->getMock( 'User' );
+               $user->expects( $this->any() )
+                       ->method( 'getOption' )
+                       ->with( 'timecorrection' )
+                       ->will( $this->returnValue( $timeCorrection ) );
+
+               $tsTime = new MWTimestamp( $tsTime );
+               $currentTime = new MWTimestamp( $currentTime );
+
+               $this->assertEquals(
+                       $expectedOutput,
+                       $tsTime->getRelativeTimestamp( $currentTime, $user ),
+                       $desc
+               );
+       }
+
+       public static function provideRelativeTimestampTests() {
+               return array(
+                       array(
+                               '20111231170000',
+                               '20120101000000',
+                               'Offset|0',
+                               'mdy',
+                               '7 hours ago',
+                               '"Yesterday" across years',
+                       ),
+                       array(
+                               '20120717190900',
+                               '20120717190929',
+                               'Offset|0',
+                               'mdy',
+                               '29 seconds ago',
+                               '"Just now"',
+                       ),
+                       array(
+                               '20120717190900',
+                               '20120717191530',
+                               'Offset|0',
+                               'mdy',
+                               '6 minutes and 30 seconds ago',
+                               'Combination of multiple units',
+                       ),
+                       array(
+                               '20121006173100',
+                               '20121006173200',
+                               'Offset|0',
+                               'mdy',
+                               '1 minute ago',
+                               '"1 minute ago"',
+                       ),
+                       array(
+                               '19910130151500',
+                               '20120716193700',
+                               'Offset|0',
+                               'mdy',
+                               '2 decades, 1 year, 168 days, 2 hours, 8 minutes and 48 seconds ago',
+                               'A long time ago',
+                       ),
+                       array(
+                               '20120101050000',
+                               '20120101080000',
+                               'Offset|-360',
+                               'mdy',
+                               '3 hours ago',
+                               '"Yesterday" across years with time correction',
+                       ),
+                       array(
+                               '20120714184300',
+                               '20120716184300',
+                               'Offset|-420',
+                               'mdy',
+                               '2 days ago',
+                               'Recent weekday with time correction',
+                       ),
+                       array(
+                               '20120714184300',
+                               '20120715040000',
+                               'Offset|-420',
+                               'mdy',
+                               '9 hours and 17 minutes ago',
+                               'Today at another time with time correction',
+                       ),
+               );
+       }
 }