Merge "Added new MWTimestamp::getRelativeTimestamp for pure relative."

This commit is contained in:
jenkins-bot 2013-09-27 17:45:26 +00:00 committed by Gerrit Code Review
commit 6d0a4f463e
3 changed files with 145 additions and 0 deletions

View file

@ -1222,6 +1222,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

View 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
*

View 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',
),
);
}
}