jenkins-bot has submitted this change and it was merged. Change subject: Adding hook handler for creating human-friendly relative timestamp ......................................................................
Adding hook handler for creating human-friendly relative timestamp This depends on the GetHumanTimestamp hook (change 45294) in core. Change-Id: I2a4938eee880228cf29388ba51a7c235ff8196b1 --- M Makefile M TimeUnits.body.php M cldr.php A tests/TimeUnitsTest.php 4 files changed, 239 insertions(+), 1 deletion(-) Approvals: Nikerabbit: Looks good to me, approved jenkins-bot: Verified diff --git a/Makefile b/Makefile index 3e54c1f..d57271d 100644 --- a/Makefile +++ b/Makefile @@ -1,9 +1,10 @@ -.PHONY: help all clean +.PHONY: help all clean test CORE=http://www.unicode.org/Public/cldr/23.1/core.zip help: @echo "'make all' to download CLDR data and rebuild files." + @echo "'make test' to run the phpunit tests" @echo "'make clean' to delete the generated LanguageNames*.php files." @echo "'make distclean' to delete the CLDR data." @@ -16,6 +17,9 @@ clean: rm -f CldrNames/CldrNames[A-Z]*.php +test: + php ${MW_INSTALL_PATH}/tests/phpunit/phpunit.php tests + LanguageNames.php: core/ php rebuild.php diff --git a/TimeUnits.body.php b/TimeUnits.body.php index 2f3ffdf..a9bf201 100644 --- a/TimeUnits.body.php +++ b/TimeUnits.body.php @@ -92,4 +92,79 @@ return self::$cache[$code]; } + /** + * Handler for GetHumanTimestamp hook. + * Converts the given time into a human-friendly relative format, for + * example, '6 days ago', 'In 10 months'. + * + * @param string &$output The output timestamp + * @param MWTimestamp $timestamp The current (user-adjusted) timestamp + * @param MWTimestamp $relativeTo The relative (user-adjusted) timestamp + * @param User $user User whose preferences are being used to make timestamp + * @param Language $lang Language that will be used to render the timestamp + * @return bool False means the timestamp was overridden so stop further + * processing. True means the timestamp was not overridden. + */ + public static function onGetHumanTimestamp( &$output, $timestamp, $relativeTo, $user, $lang ) { + + // Map PHP's DateInterval property codes to CLDR unit names. + $units = array( + 's' => 'second', + 'i' => 'minute', + 'h' => 'hour', + 'd' => 'day', + 'm' => 'month', + 'y' => 'year', + ); + + // Get the difference between the two timestamps (as a DateInterval object). + $timeDifference = $timestamp->diff( $relativeTo ); + + // Figure out if the timestamp is in the future or the past. + if ( $timeDifference->invert ) { + $tense = 'future'; + } else { + $tense = 'past'; + } + + // Figure out which unit (days, months, etc.) it makes sense to display + // the timestamp in, and get the number of that unit to use. + $unit = null; + foreach ( $units as $code => $testUnit ) { + $testNumber = $timeDifference->format( '%' . $code ); + if ( intval( $testNumber ) > 0 ) { + $unit = $testUnit; + $number = $testNumber; + } + } + + // If it occurred less than 1 second ago, output 'just now' message. + if ( !$unit ) { + $output = wfMessage( 'just-now' )->inLanguage( $lang )->text(); + return false; + } + + // Get the CLDR time unit strings for the user's language. + // If no strings are returned, abandon the timestamp override. + $timeUnits = TimeUnits::getUnits( $lang->getCode() ); + if ( !$timeUnits ) { + return true; + } + + // Figure out which grammatical number to use. + // If the template doesn't exist, fall back to 'other' as the default. + $grammaticalNumber = $lang->getPluralRuleType( $number ); + $timeUnitKey = "{$unit}-{$tense}-{$grammaticalNumber}"; + if ( !isset( $timeUnits[$timeUnitKey] ) ) { + $timeUnitKey = "{$unit}-{$tense}-other"; + } + + // Select the appropriate template for the timestamp. + $timeUnit = $timeUnits[$timeUnitKey]; + // Replace the placeholder with the number. + $output = str_replace( '{0}', $number, $timeUnit ); + + return false; + } + } diff --git a/cldr.php b/cldr.php index 61b086e..8ec697e 100644 --- a/cldr.php +++ b/cldr.php @@ -28,3 +28,4 @@ $wgAutoloadClasses['CurrencyNames'] = $dir . 'CurrencyNames.body.php'; $wgAutoloadClasses['TimeUnits'] = $dir . 'TimeUnits.body.php'; $wgHooks['LanguageGetTranslatedLanguageNames'][] = 'LanguageNames::coreHook'; +$wgHooks['GetHumanTimestamp'][] = 'TimeUnits::onGetHumanTimestamp'; diff --git a/tests/TimeUnitsTest.php b/tests/TimeUnitsTest.php new file mode 100644 index 0000000..75b7ef9 --- /dev/null +++ b/tests/TimeUnitsTest.php @@ -0,0 +1,158 @@ +<?php +/** + * Tests for TimeUnits + * @author Santhosh Thottingal + * @copyright Copyright © 2007-2013 + * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later + */ +class TimeUnitsTest extends MediaWikiTestCase { + + /** @dataProvider providerTimeUnit */ + function testTimeUnit( + $language, + $tsTime, // The timestamp to format + $currentTime, // The time to consider "now" + $expectedOutput, // The expected output + $desc // Description + ) { + $tsTime = new MWTimestamp( $tsTime ); + $currentTime = new MWTimestamp( $currentTime ); + $this->assertEquals( + $expectedOutput, + $tsTime->getHumanTimestamp( $currentTime, null, Language::factory( $language ) ), + $desc + ); + } + + public static function providerTimeUnit() { + return array( + array( + 'en', + '20111231170000', + '20120101000000', + '7 hours ago', + '"Yesterday" across years', + ), + array( + 'en', + '20120717190900', + '20120717190929', + '29 seconds ago', + '"Just now"', + ), + array( + 'en', + '20120717190900', + '20120717191530', + '6 minutes ago', + 'X minutes ago', + ), + array( + 'en', + '20121006173100', + '20121006173200', + '1 minute ago', + '"1 minute ago"', + ), + array( + 'en', + '20120617190900', + '20120717190900', + '1 month ago', + 'Month difference' + ), + array( + 'en', + '19910130151500', + '20120716193700', + '21 years ago', + 'Different year', + ), + array( + 'en', + '20120714184300', + '20120715040000', + '9 hours ago', + 'Today at another time', + ), + array( + 'en', + '20120617190900', + '20120717190900', + '1 month ago', + 'Another month' + ), + array( + 'en', + '19910130151500', + '20120716193700', + '21 years ago', + 'Different year', + ), + array( + 'ml', + '20111231170000', + '20120101000000', + '7 മണിക്കൂർ മുമ്പ്', + '"Yesterday" across years', + ), + array( + 'ml', + '20120717190900', + '20120717190929', + '29 സെക്കൻറ് മുമ്പ്', + '"Just now"', + ), + array( + 'ml', + '20120717190900', + '20120717191530', + '6 മിനിറ്റ് മുമ്പ്', + 'X minutes ago', + ), + array( + 'ml', + '20121006173100', + '20121006173200', + '1 മിനിറ്റ് മുമ്പ്', + '"1 minute ago"', + ), + array( + 'ml', + '20120617190900', + '20120717190900', + '1 മാസം മുമ്പ്', + 'Month difference' + ), + array( + 'ml', + '19910130151500', + '20120716193700', + '21 വർഷം മുമ്പ്', + 'Different year', + ), + array( + 'ml', + '20120714184300', + '20120715040000', + '9 മണിക്കൂർ മുമ്പ്', + 'Today at another time', + ), + array( + 'ml', + '20120617190900', + '20120717190900', + '1 മാസം മുമ്പ്', + 'Another month' + ), + array( + 'ml', + '19910130151500', + '20120716193700', + '21 വർഷം മുമ്പ്', + 'Different year', + ), + ); + } +} + -- To view, visit https://gerrit.wikimedia.org/r/45499 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: merged Gerrit-Change-Id: I2a4938eee880228cf29388ba51a7c235ff8196b1 Gerrit-PatchSet: 15 Gerrit-Project: mediawiki/extensions/cldr Gerrit-Branch: master Gerrit-Owner: Kaldari <[email protected]> Gerrit-Reviewer: Kaldari <[email protected]> Gerrit-Reviewer: Mormegil <[email protected]> Gerrit-Reviewer: Nikerabbit <[email protected]> Gerrit-Reviewer: Santhosh <[email protected]> Gerrit-Reviewer: jenkins-bot _______________________________________________ MediaWiki-commits mailing list [email protected] https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits
