BlockLogFormatter: Durations are relative to block's timestamp, not Unix epoch
authorBartosz Dziewoński <matma.rex@gmail.com>
Wed, 18 Jan 2017 13:08:28 +0000 (14:08 +0100)
committerBartosz Dziewoński <matma.rex@gmail.com>
Wed, 18 Jan 2017 13:21:56 +0000 (13:21 +0000)
Also fixed legacy code in LogFormatter producing messages for IRC feed.

Bug: T55907
Change-Id: I0df19574f74210a91ce72c79188b6618f04ef9a2

includes/logging/BlockLogFormatter.php
includes/logging/LogFormatter.php
languages/Language.php
languages/classes/LanguageFi.php

index c390232..a0bfb59 100644 (file)
@@ -59,9 +59,15 @@ class BlockLogFormatter extends LogFormatter {
                        // The lrm is needed to make sure that the number
                        // is shown on the correct side of the tooltip text.
                        $durationTooltip = '&lrm;' . htmlspecialchars( $params[4] );
-                       $params[4] = Message::rawParam( "<span class='blockExpiry' title='$durationTooltip'>" .
-                               $this->context->getLanguage()->translateBlockExpiry( $params[4],
-                                       $this->context->getUser() ) . '</span>' );
+                       $params[4] = Message::rawParam(
+                               "<span class='blockExpiry' title='$durationTooltip'>" .
+                               $this->context->getLanguage()->translateBlockExpiry(
+                                       $params[4],
+                                       $this->context->getUser(),
+                                       wfTimestamp( TS_UNIX, $this->entry->getTimestamp() )
+                               ) .
+                               '</span>'
+                       );
                        $params[5] = isset( $params[5] ) ?
                                self::formatBlockFlags( $params[5], $this->context->getLanguage() ) : '';
                }
index a64fee1..b5af783 100644 (file)
@@ -353,7 +353,11 @@ class LogFormatter {
                                                        $rawDuration = $parameters['5::duration'];
                                                        $rawFlags = $parameters['6::flags'];
                                                }
-                                               $duration = $wgContLang->translateBlockExpiry( $rawDuration );
+                                               $duration = $wgContLang->translateBlockExpiry(
+                                                       $rawDuration,
+                                                       null,
+                                                       wfTimestamp( TS_UNIX, $entry->getTimestamp() )
+                                               );
                                                $flags = BlockLogFormatter::formatBlockFlags( $rawFlags, $wgContLang );
                                                $text = wfMessage( 'blocklogentry' )
                                                        ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
@@ -363,7 +367,11 @@ class LogFormatter {
                                                        ->rawParams( $target )->inContentLanguage()->escaped();
                                                break;
                                        case 'reblock':
-                                               $duration = $wgContLang->translateBlockExpiry( $parameters['5::duration'] );
+                                               $duration = $wgContLang->translateBlockExpiry(
+                                                       $parameters['5::duration'],
+                                                       null,
+                                                       wfTimestamp( TS_UNIX, $entry->getTimestamp() )
+                                               );
                                                $flags = BlockLogFormatter::formatBlockFlags( $parameters['6::flags'], $wgContLang );
                                                $text = wfMessage( 'reblock-logentry' )
                                                        ->rawParams( $target, $duration, $flags )->inContentLanguage()->escaped();
index 5bce76b..8c7ce31 100644 (file)
@@ -3975,10 +3975,11 @@ class Language {
         *
         * @param string $str The validated block duration in English
         * @param User $user User object to use timezone from or null for $wgUser
+        * @param int $now Current timestamp, for formatting relative block durations
         * @return string Somehow translated block duration
         * @see LanguageFi.php for example implementation
         */
-       function translateBlockExpiry( $str, User $user = null ) {
+       function translateBlockExpiry( $str, User $user = null, $now = 0 ) {
                $duration = SpecialBlock::getSuggestedDurations( $this );
                foreach ( $duration as $show => $value ) {
                        if ( strcmp( $str, $value ) == 0 ) {
@@ -3995,11 +3996,11 @@ class Language {
                }
 
                // If all else fails, return a standard duration or timestamp description.
-               $time = strtotime( $str, 0 );
+               $time = strtotime( $str, $now );
                if ( $time === false ) { // Unknown format. Return it as-is in case.
                        return $str;
-               } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
-                       // $time is relative to 0 so it's a duration length.
+               } elseif ( $time !== strtotime( $str, $now + 1 ) ) { // It's a relative timestamp.
+                       // The result differs based on current time, so it's a duration length.
                        return $this->formatDuration( $time );
                } else { // It's an absolute timestamp.
                        if ( $time === 0 ) {
index 3388536..54ff421 100644 (file)
@@ -85,9 +85,10 @@ class LanguageFi extends Language {
        /**
         * @param string $str
         * @param User $user User object to use timezone from or null for $wgUser
+        * @param int $now Current timestamp, for formatting relative block durations
         * @return string
         */
-       function translateBlockExpiry( $str, User $user = null ) {
+       function translateBlockExpiry( $str, User $user = null, $now = 0 ) {
                /*
                        'ago', 'now', 'today', 'this', 'next',
                        'first', 'third', 'fourth', 'fifth', 'sixth', 'seventh', 'eighth', 'ninth',