From fa84729e54d2256c5b352e096f8a23c049a307d5 Mon Sep 17 00:00:00 2001 From: Tyler Anthony Romeo Date: Thu, 24 Jan 2013 17:38:36 -0500 Subject: [PATCH] Added new MWTimestamp::getRelativeTimestamp for pure relative. 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 | 10 +++ includes/Timestamp.php | 38 ++++++++++ tests/phpunit/includes/TimestampTest.php | 97 ++++++++++++++++++++++++ 3 files changed, 145 insertions(+) diff --git a/docs/hooks.txt b/docs/hooks.txt index a6fa9fed76..1a1a454b9a 100644 --- a/docs/hooks.txt +++ b/docs/hooks.txt @@ -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 diff --git a/includes/Timestamp.php b/includes/Timestamp.php index 5296122ef1..9952001e73 100644 --- a/includes/Timestamp.php +++ b/includes/Timestamp.php @@ -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 * diff --git a/tests/phpunit/includes/TimestampTest.php b/tests/phpunit/includes/TimestampTest.php index 07dcb7d81b..3668046200 100644 --- a/tests/phpunit/includes/TimestampTest.php +++ b/tests/phpunit/includes/TimestampTest.php @@ -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', + ), + ); + } } -- 2.20.1