| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
namespace MediaWiki\Utils; |
| 26 |
|
| 27 |
use DateInterval; |
| 28 |
use MediaWiki\Context\RequestContext; |
| 29 |
use MediaWiki\HookContainer\HookRunner; |
| 30 |
use MediaWiki\Language\Language; |
| 31 |
use MediaWiki\Language\RawMessage; |
| 32 |
use MediaWiki\MainConfigNames; |
| 33 |
use MediaWiki\MediaWikiServices; |
| 34 |
use MediaWiki\Message\Message; |
| 35 |
use MediaWiki\User\User; |
| 36 |
use MediaWiki\User\UserIdentity; |
| 37 |
use MediaWiki\User\UserTimeCorrection; |
| 38 |
use Wikimedia\Timestamp\ConvertibleTimestamp; |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
class MWTimestamp extends ConvertibleTimestamp { |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static function getInstance( $ts = false ) { |
| 56 |
return new static( $ts ); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function offsetForUser( UserIdentity $user ) { |
| 68 |
$option = MediaWikiServices::getInstance()->getUserOptionsLookup()->getOption( $user, 'timecorrection' ); |
| 69 |
|
| 70 |
$value = new UserTimeCorrection( |
| 71 |
$option, |
| 72 |
$this->timestamp, |
| 73 |
MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::LocalTZoffset ) |
| 74 |
); |
| 75 |
$tz = $value->getTimeZone(); |
| 76 |
if ( $tz ) { |
| 77 |
$this->timestamp->setTimezone( $tz ); |
| 78 |
return new DateInterval( 'P0Y' ); |
| 79 |
} |
| 80 |
$interval = $value->getTimeOffsetInterval(); |
| 81 |
$this->timestamp->add( $interval ); |
| 82 |
return $interval; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public function getRelativeTimestamp( |
| 96 |
?MWTimestamp $relativeTo = null, |
| 97 |
?UserIdentity $user = null, |
| 98 |
?Language $lang = null, |
| 99 |
array $chosenIntervals = [] |
| 100 |
) { |
| 101 |
$relativeTo ??= new self(); |
| 102 |
$user ??= RequestContext::getMain()->getUser(); |
| 103 |
$lang ??= RequestContext::getMain()->getLanguage(); |
| 104 |
|
| 105 |
$ts = ''; |
| 106 |
$diff = $this->diff( $relativeTo ); |
| 107 |
|
| 108 |
$user = User::newFromIdentity( $user ); |
| 109 |
if ( ( new HookRunner( MediaWikiServices::getInstance()->getHookContainer() ) )->onGetRelativeTimestamp( |
| 110 |
$ts, |
| 111 |
$diff, |
| 112 |
$this, |
| 113 |
$relativeTo, |
| 114 |
$user, |
| 115 |
$lang |
| 116 |
) ) { |
| 117 |
$seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s ); |
| 118 |
$ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )->inLanguage( $lang )->text(); |
| 119 |
} |
| 120 |
|
| 121 |
return $ts; |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public function getTimezoneMessage() { |
| 134 |
$tzMsg = $this->format( 'T' ); |
| 135 |
$key = 'timezone-' . strtolower( trim( $tzMsg ) ); |
| 136 |
$msg = wfMessage( $key ); |
| 137 |
if ( $msg->exists() ) { |
| 138 |
return $msg; |
| 139 |
} |
| 140 |
|
| 141 |
return new RawMessage( $tzMsg ); |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
public static function getLocalInstance( $ts = false ) { |
| 152 |
$localtimezone = MediaWikiServices::getInstance()->getMainConfig()->get( MainConfigNames::Localtimezone ); |
| 153 |
$timestamp = new self( $ts ); |
| 154 |
$timestamp->setTimezone( $localtimezone ); |
| 155 |
return $timestamp; |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
class_alias( MWTimestamp::class, 'MWTimestamp' ); |