2012-07-31 19:37:01 +00:00
|
|
|
<?php
|
2012-09-01 19:56:38 +00:00
|
|
|
/**
|
|
|
|
|
* Creation and parsing of MW-style timestamps.
|
|
|
|
|
*
|
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
|
|
|
|
* @file
|
|
|
|
|
* @since 1.20
|
|
|
|
|
* @author Tyler Romeo, 2012
|
|
|
|
|
*/
|
2021-03-10 21:43:55 +00:00
|
|
|
|
|
|
|
|
use MediaWiki\MediaWikiServices;
|
|
|
|
|
use MediaWiki\User\UserIdentity;
|
2021-04-15 20:54:58 +00:00
|
|
|
use MediaWiki\User\UserTimeCorrection;
|
2016-10-02 04:51:51 +00:00
|
|
|
use Wikimedia\Timestamp\ConvertibleTimestamp;
|
2012-07-31 19:37:01 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Library for creating and parsing MW-style timestamps. Based on the JS
|
|
|
|
|
* library that does the same thing.
|
|
|
|
|
*
|
2020-06-26 12:56:03 +00:00
|
|
|
* @newable
|
|
|
|
|
*
|
2012-07-31 19:37:01 +00:00
|
|
|
* @since 1.20
|
|
|
|
|
*/
|
2016-09-22 04:43:32 +00:00
|
|
|
class MWTimestamp extends ConvertibleTimestamp {
|
2012-07-31 19:37:01 +00:00
|
|
|
/**
|
2016-09-15 22:46:01 +00:00
|
|
|
* Get a timestamp instance in GMT
|
2012-09-21 19:00:34 +00:00
|
|
|
*
|
2016-09-15 22:46:01 +00:00
|
|
|
* @param bool|string $ts Timestamp to set, or false for current time
|
|
|
|
|
* @return MWTimestamp The instance
|
2012-07-31 19:37:01 +00:00
|
|
|
*/
|
2016-09-15 22:46:01 +00:00
|
|
|
public static function getInstance( $ts = false ) {
|
|
|
|
|
return new static( $ts );
|
2012-07-31 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 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.
|
|
|
|
|
*
|
2012-09-21 19:00:34 +00:00
|
|
|
* @since 1.20
|
2013-01-24 21:14:21 +00:00
|
|
|
* @since 1.22 Uses Language::getHumanTimestamp to produce the timestamp
|
2015-05-24 19:49:59 +00:00
|
|
|
* @deprecated since 1.26 Use Language::getHumanTimestamp directly
|
2012-09-21 19:00:34 +00:00
|
|
|
*
|
2015-05-24 19:49:59 +00:00
|
|
|
* @param MWTimestamp|null $relativeTo The base timestamp to compare to (defaults to now)
|
2021-03-10 21:43:55 +00:00
|
|
|
* @param UserIdentity|null $user User the timestamp is being generated for
|
2015-10-03 12:52:08 +00:00
|
|
|
* (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)
|
2013-01-24 21:14:21 +00:00
|
|
|
* @return string Formatted timestamp
|
2012-07-31 19:37:01 +00:00
|
|
|
*/
|
2015-10-03 12:52:08 +00:00
|
|
|
public function getHumanTimestamp(
|
2021-03-10 21:43:55 +00:00
|
|
|
MWTimestamp $relativeTo = null, UserIdentity $user = null, Language $lang = null
|
2015-10-03 12:52:08 +00:00
|
|
|
) {
|
2013-01-24 21:14:21 +00:00
|
|
|
if ( $lang === null ) {
|
|
|
|
|
$lang = RequestContext::getMain()->getLanguage();
|
|
|
|
|
}
|
|
|
|
|
|
2015-05-24 19:49:59 +00:00
|
|
|
return $lang->getHumanTimestamp( $this, $relativeTo, $user );
|
2013-01-24 21:14:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Adjust the timestamp depending on the given user's preferences.
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22
|
|
|
|
|
*
|
2021-03-10 21:43:55 +00:00
|
|
|
* @param UserIdentity $user User to take preferences from
|
2013-01-24 21:14:21 +00:00
|
|
|
* @return DateInterval Offset that was applied to the timestamp
|
|
|
|
|
*/
|
2021-03-10 21:43:55 +00:00
|
|
|
public function offsetForUser( UserIdentity $user ) {
|
|
|
|
|
$option = MediaWikiServices::getInstance()
|
|
|
|
|
->getUserOptionsLookup()
|
|
|
|
|
->getOption( $user, 'timecorrection' );
|
|
|
|
|
|
2021-04-15 20:54:58 +00:00
|
|
|
$value = new UserTimeCorrection(
|
|
|
|
|
$option,
|
|
|
|
|
$this->timestamp,
|
|
|
|
|
MediaWikiServices::getInstance()->getMainConfig()->get( 'LocalTZoffset' )
|
|
|
|
|
);
|
|
|
|
|
$tz = $value->getTimeZone();
|
|
|
|
|
if ( $tz ) {
|
|
|
|
|
$this->timestamp->setTimezone( $tz );
|
|
|
|
|
return new DateInterval( 'P0Y' );
|
2012-07-31 19:37:01 +00:00
|
|
|
}
|
2021-04-15 20:54:58 +00:00
|
|
|
$interval = $value->getTimeOffsetInterval();
|
2013-01-24 21:14:21 +00:00
|
|
|
$this->timestamp->add( $interval );
|
|
|
|
|
return $interval;
|
2012-07-31 19:37:01 +00:00
|
|
|
}
|
|
|
|
|
|
2013-01-24 22:38:36 +00:00
|
|
|
/**
|
|
|
|
|
* Generate a purely relative timestamp, i.e., represent the time elapsed between
|
|
|
|
|
* the given base timestamp and this object.
|
|
|
|
|
*
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param MWTimestamp|null $relativeTo Relative base timestamp (defaults to now)
|
2021-03-10 21:43:55 +00:00
|
|
|
* @param UserIdentity|null $user Use to use offset for
|
2018-06-26 21:14:43 +00:00
|
|
|
* @param Language|null $lang Language to use
|
2013-01-24 22:38:36 +00:00
|
|
|
* @param array $chosenIntervals Intervals to use to represent it
|
|
|
|
|
* @return string Relative timestamp
|
|
|
|
|
*/
|
|
|
|
|
public function getRelativeTimestamp(
|
|
|
|
|
MWTimestamp $relativeTo = null,
|
2021-03-10 21:43:55 +00:00
|
|
|
UserIdentity $user = null,
|
2013-01-24 22:38:36 +00:00
|
|
|
Language $lang = null,
|
2016-02-17 09:09:32 +00:00
|
|
|
array $chosenIntervals = []
|
2013-01-24 22:38:36 +00:00
|
|
|
) {
|
|
|
|
|
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 );
|
2021-03-10 21:43:55 +00:00
|
|
|
|
|
|
|
|
$user = User::newFromIdentity( $user ); // For compatibility with the hook signature
|
Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.
General principles:
* Use DI if it is already used. We're not changing the way state is
managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
is a service, it's a more generic interface, it is the only
thing that provides isRegistered() which is needed in some cases,
and a HookRunner can be efficiently constructed from it
(confirmed by benchmark). Because HookContainer is needed
for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
SpecialPage and ApiBase have getHookContainer() and getHookRunner()
methods in the base class, and classes that extend that base class
are not expected to know or care where the base class gets its
HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
getHookRunner() methods, getting them from the global service
container. The point of this is to ease migration to DI by ensuring
that call sites ask their local friendly base class rather than
getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
methods did not seem warranted, there is a private HookRunner property
which is accessed directly. Very rarely (two cases), there is a
protected property, for consistency with code that conventionally
assumes protected=private, but in cases where the class might actually
be overridden, a protected accessor is preferred over a protected
property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
global code. In a few cases it was used for objects with broken
construction schemes, out of horror or laziness.
Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore
Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router
setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine
Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-03-19 02:42:09 +00:00
|
|
|
if ( Hooks::runner()->onGetRelativeTimestamp(
|
|
|
|
|
$ts, $diff, $this, $relativeTo, $user, $lang )
|
|
|
|
|
) {
|
2013-01-24 22:38:36 +00:00
|
|
|
$seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
|
|
|
|
|
$ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
|
2014-05-12 14:42:51 +00:00
|
|
|
->inLanguage( $lang )->text();
|
2013-01-24 22:38:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return $ts;
|
|
|
|
|
}
|
|
|
|
|
|
2014-12-26 01:57:14 +00:00
|
|
|
/**
|
|
|
|
|
* Get the localized timezone message, if available.
|
|
|
|
|
*
|
|
|
|
|
* Premade translations are not shipped as format() may return whatever the
|
|
|
|
|
* system uses, localized or not, so translation must be done through wiki.
|
|
|
|
|
*
|
2015-11-20 22:43:01 +00:00
|
|
|
* @since 1.27
|
2014-12-26 01:57:14 +00:00
|
|
|
* @return Message The localized timezone message
|
|
|
|
|
*/
|
|
|
|
|
public function getTimezoneMessage() {
|
|
|
|
|
$tzMsg = $this->format( 'T' ); // might vary on DST changeover!
|
|
|
|
|
$key = 'timezone-' . strtolower( trim( $tzMsg ) );
|
|
|
|
|
$msg = wfMessage( $key );
|
|
|
|
|
if ( $msg->exists() ) {
|
|
|
|
|
return $msg;
|
|
|
|
|
}
|
2018-09-24 13:08:31 +00:00
|
|
|
|
|
|
|
|
return new RawMessage( $tzMsg );
|
2014-12-26 01:57:14 +00:00
|
|
|
}
|
|
|
|
|
|
2013-07-06 19:59:35 +00:00
|
|
|
/**
|
|
|
|
|
* Get a timestamp instance in the server local timezone ($wgLocaltimezone)
|
|
|
|
|
*
|
|
|
|
|
* @since 1.22
|
|
|
|
|
* @param bool|string $ts Timestamp to set, or false for current time
|
2014-07-24 17:42:24 +00:00
|
|
|
* @return MWTimestamp The local instance
|
2013-07-06 19:59:35 +00:00
|
|
|
*/
|
|
|
|
|
public static function getLocalInstance( $ts = false ) {
|
|
|
|
|
global $wgLocaltimezone;
|
|
|
|
|
$timestamp = new self( $ts );
|
|
|
|
|
$timestamp->setTimezone( $wgLocaltimezone );
|
|
|
|
|
return $timestamp;
|
|
|
|
|
}
|
2012-07-31 19:37:01 +00:00
|
|
|
}
|