MediaWiki  1.27.3
DateFormatter.php
Go to the documentation of this file.
1 <?php
30  public $mSource, $mTarget;
32 
35 
36  protected $lang, $mLinked;
37 
38  const ALL = -1;
39  const NONE = 0;
40  const MDY = 1;
41  const DMY = 2;
42  const YMD = 3;
43  const ISO1 = 4;
44  const LASTPREF = 4;
45  const ISO2 = 5;
46  const YDM = 6;
47  const DM = 7;
48  const MD = 8;
49  const LAST = 8;
50 
54  public function __construct( Language $lang ) {
55  $this->lang = $lang;
56 
57  $this->monthNames = $this->getMonthRegex();
58  for ( $i = 1; $i <= 12; $i++ ) {
59  $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
60  $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
61  }
62 
63  $this->regexTrail = '(?![a-z])/iu';
64 
65  # Partial regular expressions
66  $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
67  $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
68  $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
69  $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
70  $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
71 
72  # Real regular expressions
73  $this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
74  $this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
75  $this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
76  $this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
77  $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
78  $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
79  $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
80  $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
81 
82  # Extraction keys
83  # See the comments in replace() for the meaning of the letters
84  $this->keys[self::DMY] = 'jFY';
85  $this->keys[self::YDM] = 'Y jF';
86  $this->keys[self::MDY] = 'FjY';
87  $this->keys[self::YMD] = 'Y Fj';
88  $this->keys[self::DM] = 'jF';
89  $this->keys[self::MD] = 'Fj';
90  $this->keys[self::ISO1] = 'ymd'; # y means ISO year
91  $this->keys[self::ISO2] = 'ymd';
92 
93  # Target date formats
94  $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
95  $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
96  $this->targets[self::MDY] = '[[F j]], [[Y]]';
97  $this->targets[self::YMD] = '[[Y]] [[F j]]';
98  $this->targets[self::DM] = '[[F j|j F]]';
99  $this->targets[self::MD] = '[[F j]]';
100  $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
101  $this->targets[self::ISO2] = '[[y-m-d]]';
102 
103  # Rules
104  # pref source target
105  $this->rules[self::DMY][self::MD] = self::DM;
106  $this->rules[self::ALL][self::MD] = self::MD;
107  $this->rules[self::MDY][self::DM] = self::MD;
108  $this->rules[self::ALL][self::DM] = self::DM;
109  $this->rules[self::NONE][self::ISO2] = self::ISO1;
110 
111  $this->preferences = [
112  'default' => self::NONE,
113  'dmy' => self::DMY,
114  'mdy' => self::MDY,
115  'ymd' => self::YMD,
116  'ISO 8601' => self::ISO1,
117  ];
118  }
119 
127  public static function getInstance( $lang = null ) {
129 
131  $cache = ObjectCache::getLocalServerInstance( $wgMainCacheType );
132 
133  static $dateFormatter = false;
134  if ( !$dateFormatter ) {
135  $dateFormatter = $cache->getWithSetCallback(
136  $cache->makeKey( 'dateformatter', $lang->getCode() ),
137  $cache::TTL_HOUR,
138  function () use ( $lang ) {
139  return new DateFormatter( $lang );
140  }
141  );
142  }
143 
144  return $dateFormatter;
145  }
146 
154  public function reformat( $preference, $text, $options = [ 'linked' ] ) {
155  $linked = in_array( 'linked', $options );
156  $match_whole = in_array( 'match-whole', $options );
157 
158  if ( isset( $this->preferences[$preference] ) ) {
159  $preference = $this->preferences[$preference];
160  } else {
161  $preference = self::NONE;
162  }
163  for ( $i = 1; $i <= self::LAST; $i++ ) {
164  $this->mSource = $i;
165  if ( isset( $this->rules[$preference][$i] ) ) {
166  # Specific rules
167  $this->mTarget = $this->rules[$preference][$i];
168  } elseif ( isset( $this->rules[self::ALL][$i] ) ) {
169  # General rules
170  $this->mTarget = $this->rules[self::ALL][$i];
171  } elseif ( $preference ) {
172  # User preference
173  $this->mTarget = $preference;
174  } else {
175  # Default
176  $this->mTarget = $i;
177  }
178  $regex = $this->regexes[$i];
179 
180  // Horrible hack
181  if ( !$linked ) {
182  $regex = str_replace( [ '\[\[', '\]\]' ], '', $regex );
183  }
184 
185  if ( $match_whole ) {
186  // Let's hope this works
187  $regex = preg_replace( '!^/!', '/^', $regex );
188  $regex = str_replace( $this->regexTrail,
189  '$' . $this->regexTrail, $regex );
190  }
191 
192  // Another horrible hack
193  $this->mLinked = $linked;
194  $text = preg_replace_callback( $regex, [ $this, 'replace' ], $text );
195  unset( $this->mLinked );
196  }
197  return $text;
198  }
199 
204  public function replace( $matches ) {
205  # Extract information from $matches
206  $linked = true;
207  if ( isset( $this->mLinked ) ) {
208  $linked = $this->mLinked;
209  }
210 
211  $bits = [];
212  $key = $this->keys[$this->mSource];
213  $keyLength = strlen( $key );
214  for ( $p = 0; $p < $keyLength; $p++ ) {
215  if ( $key[$p] != ' ' ) {
216  $bits[$key[$p]] = $matches[$p + 1];
217  }
218  }
219 
220  return $this->formatDate( $bits, $linked );
221  }
222 
228  public function formatDate( $bits, $link = true ) {
229  $format = $this->targets[$this->mTarget];
230 
231  if ( !$link ) {
232  // strip piped links
233  $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
234  // strip remaining links
235  $format = str_replace( [ '[[', ']]' ], '', $format );
236  }
237 
238  # Construct new date
239  $text = '';
240  $fail = false;
241 
242  // Pre-generate y/Y stuff because we need the year for the <span> title.
243  if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
244  $bits['y'] = $this->makeIsoYear( $bits['Y'] );
245  }
246  if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
247  $bits['Y'] = $this->makeNormalYear( $bits['y'] );
248  }
249 
250  if ( !isset( $bits['m'] ) ) {
251  $m = $this->makeIsoMonth( $bits['F'] );
252  if ( !$m || $m == '00' ) {
253  $fail = true;
254  } else {
255  $bits['m'] = $m;
256  }
257  }
258 
259  if ( !isset( $bits['d'] ) ) {
260  $bits['d'] = sprintf( '%02d', $bits['j'] );
261  }
262 
263  $formatLength = strlen( $format );
264  for ( $p = 0; $p < $formatLength; $p++ ) {
265  $char = $format[$p];
266  switch ( $char ) {
267  case 'd': # ISO day of month
268  $text .= $bits['d'];
269  break;
270  case 'm': # ISO month
271  $text .= $bits['m'];
272  break;
273  case 'y': # ISO year
274  $text .= $bits['y'];
275  break;
276  case 'j': # ordinary day of month
277  if ( !isset( $bits['j'] ) ) {
278  $text .= intval( $bits['d'] );
279  } else {
280  $text .= $bits['j'];
281  }
282  break;
283  case 'F': # long month
284  if ( !isset( $bits['F'] ) ) {
285  $m = intval( $bits['m'] );
286  if ( $m > 12 || $m < 1 ) {
287  $fail = true;
288  } else {
289  $text .= $this->lang->getMonthName( $m );
290  }
291  } else {
292  $text .= ucfirst( $bits['F'] );
293  }
294  break;
295  case 'Y': # ordinary (optional BC) year
296  $text .= $bits['Y'];
297  break;
298  default:
299  $text .= $char;
300  }
301  }
302  if ( $fail ) {
304  $text = $matches[0];
305  }
306 
307  $isoBits = [];
308  if ( isset( $bits['y'] ) ) {
309  $isoBits[] = $bits['y'];
310  }
311  $isoBits[] = $bits['m'];
312  $isoBits[] = $bits['d'];
313  $isoDate = implode( '-', $isoBits );
314 
315  // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
316  $text = Html::rawElement( 'span',
317  [ 'class' => 'mw-formatted-date', 'title' => $isoDate ], $text );
318 
319  return $text;
320  }
321 
326  public function getMonthRegex() {
327  $names = [];
328  for ( $i = 1; $i <= 12; $i++ ) {
329  $names[] = $this->lang->getMonthName( $i );
330  $names[] = $this->lang->getMonthAbbreviation( $i );
331  }
332  return implode( '|', $names );
333  }
334 
340  public function makeIsoMonth( $monthName ) {
341  $n = $this->xMonths[$this->lang->lc( $monthName )];
342  return sprintf( '%02d', $n );
343  }
344 
350  public function makeIsoYear( $year ) {
351  # Assumes the year is in a nice format, as enforced by the regex
352  if ( substr( $year, -2 ) == 'BC' ) {
353  $num = intval( substr( $year, 0, -3 ) ) - 1;
354  # PHP bug note: sprintf( "%04d", -1 ) fails poorly
355  $text = sprintf( '-%04d', $num );
356 
357  } else {
358  $text = sprintf( '%04d', $year );
359  }
360  return $text;
361  }
362 
369  public function makeNormalYear( $iso ) {
370  if ( $iso[0] == '-' ) {
371  $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
372  } else {
373  $text = intval( $iso );
374  }
375  return $text;
376  }
377 }
makeIsoYear($year)
Make an ISO year from a year name, for instance: '-1199' from '1200 BC'.
magic word the default is to use $key to get the and $key value or $key value text $key value html to format the value $key
Definition: hooks.txt:2325
reformat($preference, $text, $options=[ 'linked'])
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
static rawElement($element, $attribs=[], $contents= '')
Returns an HTML element in a string.
Definition: Html.php:210
globals txt Globals are evil The original MediaWiki code relied on globals for processing context far too often MediaWiki development since then has been a story of slowly moving context out of global variables and into objects Storing processing context in object member variables allows those objects to be reused in a much more flexible way Consider the elegance of
database rows
Definition: globals.txt:10
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for and distribution as defined by Sections through of this document Licensor shall mean the copyright owner or entity authorized by the copyright owner that is granting the License Legal Entity shall mean the union of the acting entity and all other entities that control are controlled by or are under common control with that entity For the purposes of this definition control means(i) the power
getMonthRegex()
Return a regex that can be used to find month names in string.
static getInstance($lang=null)
Get a DateFormatter object.
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
__construct(Language $lang)
the value to return A Title object or null for latest to be modified or replaced by the hook handler or if authentication is not possible after cache objects are set for highlighting & $link
Definition: hooks.txt:2585
replace($matches)
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist 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 $options
Definition: hooks.txt:1008
$cache
Definition: mcc.php:33
CACHE_MEMCACHED $wgMainCacheType
Definition: memcached.txt:63
formatDate($bits, $link=true)
makeNormalYear($iso)
Make a year one from an ISO year, for instance: '400 BC' from '-0399'.
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
static getLocalServerInstance($fallback=CACHE_NONE)
Factory function for CACHE_ACCEL (referenced from DefaultSettings.php)
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the local content language as $wgContLang
Definition: design.txt:56
makeIsoMonth($monthName)
Makes an ISO month, e.g.
Date formatter, recognises dates in plain text and formats them according to user preferences...
wfGetLangObj($langcode=false)
Return a Language object from $langcode.
$matches