MediaWiki  1.29.2
MWTimestamp.php
Go to the documentation of this file.
1 <?php
24 use Wikimedia\Timestamp\ConvertibleTimestamp;
25 
32 class MWTimestamp extends ConvertibleTimestamp {
39  public static function getInstance( $ts = false ) {
40  return new static( $ts );
41  }
42 
61  public function getHumanTimestamp(
62  MWTimestamp $relativeTo = null, User $user = null, Language $lang = null
63  ) {
64  if ( $lang === null ) {
65  $lang = RequestContext::getMain()->getLanguage();
66  }
67 
68  return $lang->getHumanTimestamp( $this, $relativeTo, $user );
69  }
70 
79  public function offsetForUser( User $user ) {
80  global $wgLocalTZoffset;
81 
82  $option = $user->getOption( 'timecorrection' );
83  $data = explode( '|', $option, 3 );
84 
85  // First handle the case of an actual timezone being specified.
86  if ( $data[0] == 'ZoneInfo' ) {
87  try {
88  $tz = new DateTimeZone( $data[2] );
89  } catch ( Exception $e ) {
90  $tz = false;
91  }
92 
93  if ( $tz ) {
94  $this->timestamp->setTimezone( $tz );
95  return new DateInterval( 'P0Y' );
96  } else {
97  $data[0] = 'Offset';
98  }
99  }
100 
101  $diff = 0;
102  // If $option is in fact a pipe-separated value, check the
103  // first value.
104  if ( $data[0] == 'System' ) {
105  // First value is System, so use the system offset.
106  if ( $wgLocalTZoffset !== null ) {
107  $diff = $wgLocalTZoffset;
108  }
109  } elseif ( $data[0] == 'Offset' ) {
110  // First value is Offset, so use the specified offset
111  $diff = (int)$data[1];
112  } else {
113  // $option actually isn't a pipe separated value, but instead
114  // a comma separated value. Isn't MediaWiki fun?
115  $data = explode( ':', $option );
116  if ( count( $data ) >= 2 ) {
117  // Combination hours and minutes.
118  $diff = abs( (int)$data[0] ) * 60 + (int)$data[1];
119  if ( (int)$data[0] < 0 ) {
120  $diff *= -1;
121  }
122  } else {
123  // Just hours.
124  $diff = (int)$data[0] * 60;
125  }
126  }
127 
128  $interval = new DateInterval( 'PT' . abs( $diff ) . 'M' );
129  if ( $diff < 1 ) {
130  $interval->invert = 1;
131  }
132 
133  $this->timestamp->add( $interval );
134  return $interval;
135  }
136 
147  public function getRelativeTimestamp(
148  MWTimestamp $relativeTo = null,
149  User $user = null,
150  Language $lang = null,
151  array $chosenIntervals = []
152  ) {
153  if ( $relativeTo === null ) {
154  $relativeTo = new self;
155  }
156  if ( $user === null ) {
157  $user = RequestContext::getMain()->getUser();
158  }
159  if ( $lang === null ) {
160  $lang = RequestContext::getMain()->getLanguage();
161  }
162 
163  $ts = '';
164  $diff = $this->diff( $relativeTo );
165  if ( Hooks::run(
166  'GetRelativeTimestamp',
167  [ &$ts, &$diff, $this, $relativeTo, $user, $lang ]
168  ) ) {
169  $seconds = ( ( ( $diff->days * 24 + $diff->h ) * 60 + $diff->i ) * 60 + $diff->s );
170  $ts = wfMessage( 'ago', $lang->formatDuration( $seconds, $chosenIntervals ) )
171  ->inLanguage( $lang )->text();
172  }
173 
174  return $ts;
175  }
176 
186  public function getTimezoneMessage() {
187  $tzMsg = $this->format( 'T' ); // might vary on DST changeover!
188  $key = 'timezone-' . strtolower( trim( $tzMsg ) );
189  $msg = wfMessage( $key );
190  if ( $msg->exists() ) {
191  return $msg;
192  } else {
193  return new RawMessage( $tzMsg );
194  }
195  }
196 
204  public static function getLocalInstance( $ts = false ) {
205  global $wgLocaltimezone;
206  $timestamp = new self( $ts );
207  $timestamp->setTimezone( $wgLocaltimezone );
208  return $timestamp;
209  }
210 }
MWTimestamp\getTimezoneMessage
getTimezoneMessage()
Get the localized timezone message, if available.
Definition: MWTimestamp.php:186
MWTimestamp
Library for creating and parsing MW-style timestamps.
Definition: MWTimestamp.php:32
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
captcha-old.count
count
Definition: captcha-old.py:225
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
$user
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 account $user
Definition: hooks.txt:246
diff
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content as context as context the output can only depend on parameters provided to this hook not on global state indicating whether full HTML should be generated If generation of HTML may be but other information should still be present in the ParserOutput object to manipulate or replace but no entry for that model exists in $wgContentHandlers please use GetContentModels hook to make them known to core if desired whether it is OK to use $contentModel on $title Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok inclusive false for true for descending in case the handler function wants to provide a converted Content object Note that $result getContentModel() must return $toModel. 'CustomEditor' also included in $newHeader if any indicating whether we should show just the diff
Definition: hooks.txt:1167
php
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
MWTimestamp\offsetForUser
offsetForUser(User $user)
Adjust the timestamp depending on the given user's preferences.
Definition: MWTimestamp.php:79
MWTimestamp\getInstance
static getInstance( $ts=false)
Get a timestamp instance in GMT.
Definition: MWTimestamp.php:39
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
MWTimestamp\getRelativeTimestamp
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...
Definition: MWTimestamp.php:147
$e
div flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException' returning false will NOT prevent logging $e
Definition: hooks.txt:2122
RequestContext\getMain
static getMain()
Static methods.
Definition: RequestContext.php:468
format
if the prop value should be in the metadata multi language array format
Definition: hooks.txt:1630
wfMessage
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 unset offset - wrap String Wrap the message in html(usually something like "&lt
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:204
MWTimestamp\getHumanTimestamp
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:61
Language
Internationalisation code.
Definition: Language.php:35
array
the array() calling protocol came about after MediaWiki 1.4rc1.