jenkins-bot has submitted this change and it was merged.
Change subject: Added new MWTimestamp::getRelativeTimestamp for pure relative.
......................................................................
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
---
M docs/hooks.txt
M includes/Timestamp.php
M tests/phpunit/includes/TimestampTest.php
3 files changed, 145 insertions(+), 0 deletions(-)
Approvals:
Bartosz Dziewoński: Looks good to me, approved
jenkins-bot: Verified
diff --git a/docs/hooks.txt b/docs/hooks.txt
index a6fa9fe..1a1a454 100644
--- a/docs/hooks.txt
+++ b/docs/hooks.txt
@@ -1205,6 +1205,16 @@
$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 5296122..9952001 100644
--- a/includes/Timestamp.php
+++ b/includes/Timestamp.php
@@ -271,6 +271,44 @@
}
/**
+ * 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
*
* @return string
diff --git a/tests/phpunit/includes/TimestampTest.php
b/tests/phpunit/includes/TimestampTest.php
index 07dcb7d..3668046 100644
--- a/tests/phpunit/includes/TimestampTest.php
+++ b/tests/phpunit/includes/TimestampTest.php
@@ -200,4 +200,101 @@
),
);
}
+
+ /**
+ * @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',
+ ),
+ );
+ }
}
--
To view, visit https://gerrit.wikimedia.org/r/45677
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: merged
Gerrit-Change-Id: I8b3e3fc6eeb4afd58f85fa7d05b5ea1a51b0afb6
Gerrit-PatchSet: 13
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Parent5446 <[email protected]>
Gerrit-Reviewer: Bartosz Dziewoński <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Parent5446 <[email protected]>
Gerrit-Reviewer: jenkins-bot
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits