MediaWiki  1.28.0
MWTimestamp.php
Go to the documentation of this file.
1 <?php
38  public static function getInstance( $ts = false ) {
39  return new static( $ts );
40  }
41 
60  public function getHumanTimestamp(
61  MWTimestamp $relativeTo = null, User $user = null, Language $lang = null
62  ) {
63  if ( $lang === null ) {
64  $lang = RequestContext::getMain()->getLanguage();
65  }
66 
67  return $lang->getHumanTimestamp( $this, $relativeTo, $user );
68  }
69 
78  public function offsetForUser( User $user ) {
80 
81  $option = $user->getOption( 'timecorrection' );
82  $data = explode( '|', $option, 3 );
83 
84  // First handle the case of an actual timezone being specified.
85  if ( $data[0] == 'ZoneInfo' ) {
86  try {
87  $tz = new DateTimeZone( $data[2] );
88  } catch ( Exception $e ) {
89  $tz = false;
90  }
91 
92  if ( $tz ) {
93  $this->timestamp->setTimezone( $tz );
94  return new DateInterval( 'P0Y' );
95  } else {
96  $data[0] = 'Offset';
97  }
98  }
99 
100  $diff = 0;
101  // If $option is in fact a pipe-separated value, check the
102  // first value.
103  if ( $data[0] == 'System' ) {
104  // First value is System, so use the system offset.
105  if ( $wgLocalTZoffset !== null ) {
106  $diff = $wgLocalTZoffset;
107  }
108  } elseif ( $data[0] == 'Offset' ) {
109  // First value is Offset, so use the specified offset
110  $diff = (int)$data[1];
111  } else {
112  // $option actually isn't a pipe separated value, but instead
113  // a comma separated value. Isn't MediaWiki fun?
114  $data = explode( ':', $option );
115  if ( count( $data ) >= 2 ) {
116  // Combination hours and minutes.
117  $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
118  if ( (int)$data[0] < 0 ) {
119  $diff *= -1;
120  }
121  } else {
122  // Just hours.
123  $diff = (int)$data[0] * 60;
124  }
125  }
126 
127  $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
128  if ( $diff < 1 ) {
129  $interval->invert = 1;
130  }
131 
132  $this->timestamp->add( $interval );
133  return $interval;
134  }
135 
146  public function getRelativeTimestamp(
147  MWTimestamp $relativeTo = null,
148  User $user = null,
149  Language $lang = null,
150  array $chosenIntervals = []
151  ) {
152  if ( $relativeTo === null ) {
153  $relativeTo = new self;
154  }
155  if ( $user === null ) {
156  $user = RequestContext::getMain()->getUser();
157  }
158  if ( $lang === null ) {
159  $lang = RequestContext::getMain()->getLanguage();
160  }
161 
162  $ts = '';
163  $diff = $this->diff( $relativeTo );
164  if ( Hooks::run(
165  'GetRelativeTimestamp',
166  [ &$ts, &$diff, $this, $relativeTo, $user, $lang ]
167  ) ) {
168  $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
169  $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
170  ->inLanguage( $lang )->text();
171  }
172 
173  return $ts;
174  }
175 
185  public function getTimezoneMessage() {
186  $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
187  $key = 'timezone-' . strtolower( trim( $tzMsg ) );
188  $msg = wfMessage( $key );
189  if ( $msg->exists() ) {
190  return $msg;
191  } else {
192  return new RawMessage( $tzMsg );
193  }
194  }
195 
203  public static function getLocalInstance( $ts = false ) {
205  $timestamp = new self( $ts );
206  $timestamp->setTimezone( $wgLocaltimezone );
207  return $timestamp;
208  }
209 }
Library for creating, parsing, and converting timestamps.
the array() calling protocol came about after MediaWiki 1.4rc1.
getHumanTimestamp(MWTimestamp $relativeTo=null, User $user=null, Language $lang=null)
Get the timestamp in a human-friendly relative format, e.g., "3 days ago".
Definition: MWTimestamp.php:60
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException'returning false will NOT prevent logging $e
Definition: hooks.txt:2102
offsetForUser(User $user)
Adjust the timestamp depending on the given user's preferences.
Definition: MWTimestamp.php:78
if(!isset($args[0])) $lang
getTimezoneMessage()
Get the localized timezone message, if available.
DateTime $timestamp
The actual timestamp being wrapped (DateTime object).
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
diff(ConvertibleTimestamp $relativeTo)
Calculate the difference between two ConvertibleTimestamp objects.
static getLocalInstance($ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
static getMain()
Static methods.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
static getInstance($ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:38
format($format)
Format the timestamp in a given format.
static run($event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
$wgLocaltimezone
Fake out the timezone that the server thinks it's in.
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
getOption($oname, $defaultOverride=null, $ignoreHidden=false)
Get the user's current setting for a given option.
Definition: User.php:2766
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
Variant of the Message class.
Definition: Message.php:1242
$wgLocalTZoffset
Set an offset from UTC in minutes to use for the default timezone setting for anonymous users and new...
getRelativeTimestamp(MWTimestamp $relativeTo=null, User $user=null, Language $lang=null, array $chosenIntervals=[])
Generate a purely relative timestamp, i.e., represent the time elapsed between the given base timesta...
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:31