Move MWTimestamp::getHumanTimestamp() to Language
authorBrian Wolff <bawolff+wn@gmail.com>
Sun, 24 May 2015 19:49:59 +0000 (13:49 -0600)
committerKunal Mehta <legoktm@gmail.com>
Sun, 31 May 2015 08:53:54 +0000 (01:53 -0700)
We're trying to remove MediaWiki dependencies from MWTimestamp so it can
be split out into a separate library. In addition to getting rid of a
dependency on RequestContext, Language, and User, it makes more logical
sense there anyways.

Bug: T100924
Change-Id: If46eaea42d8a5a808c10f0dc353e181714295a44

includes/MWTimestamp.php
languages/Language.php

index ea91470..f2bd6ba 100644 (file)
@@ -199,42 +199,19 @@ class MWTimestamp {
         *
         * @since 1.20
         * @since 1.22 Uses Language::getHumanTimestamp to produce the timestamp
+        * @deprecated since 1.26 Use Language::getHumanTimestamp directly
         *
-        * @param MWTimestamp|null $relativeTo The base timestamp to compare to
-        *   (defaults to now).
-        * @param User|null $user User the timestamp is being generated for (or null
-        *   to use main context's user).
-        * @param Language|null $lang Language to use to make the human timestamp
-        *   (or null to use main context's language).
+        * @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now)
+        * @param User|null $user User the timestamp is being generated for (or null to use main context's user)
+        * @param Language|null $lang Language to use to make the human timestamp (or null to use main context's language)
         * @return string Formatted timestamp
         */
-       public function getHumanTimestamp( MWTimestamp $relativeTo = null,
-               User $user = null, Language $lang = null
-       ) {
-               if ( $relativeTo === null ) {
-                       $relativeTo = new self();
-               }
-               if ( $user === null ) {
-                       $user = RequestContext::getMain()->getUser();
-               }
+       public function getHumanTimestamp( MWTimestamp $relativeTo = null, User $user = null, Language $lang = null ) {
                if ( $lang === null ) {
                        $lang = RequestContext::getMain()->getLanguage();
                }
 
-               // Adjust for the user's timezone.
-               $offsetThis = $this->offsetForUser( $user );
-               $offsetRel = $relativeTo->offsetForUser( $user );
-
-               $ts = '';
-               if ( Hooks::run( 'GetHumanTimestamp', array( &$ts, $this, $relativeTo, $user, $lang ) ) ) {
-                       $ts = $lang->getHumanTimestamp( $this, $relativeTo, $user );
-               }
-
-               // Reset the timezone on the objects.
-               $this->timestamp->sub( $offsetThis );
-               $relativeTo->timestamp->sub( $offsetRel );
-
-               return $ts;
+               return $lang->getHumanTimestamp( $this, $relativeTo, $user );
        }
 
        /**
index e1a2047..18f4594 100644 (file)
@@ -2462,22 +2462,56 @@ class Language {
                return $this->internalUserTimeAndDate( 'both', $ts, $user, $options );
        }
 
+       /**
+        * Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
+        *
+        * Determine the difference between the timestamp and the current time, and
+        * generate a readable timestamp by returning "<N> <units> ago", where the
+        * largest possible unit is used.
+        *
+        * @since 1.26 (Prior to 1.26 method existed but was not meant to be used directly)
+        *
+        * @param MWTimestamp $time
+        * @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now)
+        * @param User|null $user User the timestamp is being generated for (or null to use main context's user)
+        * @return string Formatted timestamp
+        */
+       public function getHumanTimestamp( MWTimestamp $time, MWTimestamp $relativeTo = null, User $user = null ) {
+               if ( $relativeTo === null ) {
+                       $relativeTo = new MWTimestamp();
+               }
+               if ( $user === null ) {
+                       $user = RequestContext::getMain()->getUser();
+               }
+
+               // Adjust for the user's timezone.
+               $offsetThis = $time->offsetForUser( $user );
+               $offsetRel = $relativeTo->offsetForUser( $user );
+
+               $ts = '';
+               if ( Hooks::run( 'GetHumanTimestamp', array( &$ts, $time, $relativeTo, $user, $this ) ) ) {
+                       $ts = $this->getHumanTimestampInternal( $time, $relativeTo, $user );
+               }
+
+               // Reset the timezone on the objects.
+               $time->timestamp->sub( $offsetThis );
+               $relativeTo->timestamp->sub( $offsetRel );
+
+               return $ts;
+       }
+
        /**
         * Convert an MWTimestamp into a pretty human-readable timestamp using
         * the given user preferences and relative base time.
         *
-        * DO NOT USE THIS FUNCTION DIRECTLY. Instead, call MWTimestamp::getHumanTimestamp
-        * on your timestamp object, which will then call this function. Calling
-        * this function directly will cause hooks to be skipped over.
-        *
-        * @see MWTimestamp::getHumanTimestamp
+        * @see Language::getHumanTimestamp
         * @param MWTimestamp $ts Timestamp to prettify
         * @param MWTimestamp $relativeTo Base timestamp
         * @param User $user User preferences to use
         * @return string Human timestamp
-        * @since 1.22
+        * @since 1.26
         */
-       public function getHumanTimestamp( MWTimestamp $ts, MWTimestamp $relativeTo, User $user ) {
+       private function getHumanTimestampInternal( MWTimestamp $ts, MWTimestamp $relativeTo, User $user ) {
                $diff = $ts->diff( $relativeTo );
                $diffDay = (bool)( (int)$ts->timestamp->format( 'w' ) -
                        (int)$relativeTo->timestamp->format( 'w' ) );