5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Date formatter, recognises dates in plain text and formats them according to user preferences.
26 * @todo preferences, OutputPage
30 public $mSource, $mTarget;
31 public $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
33 public $regexes, $pDays, $pMonths, $pYears;
34 public $rules, $xMonths, $preferences;
36 protected $lang, $mLinked;
52 * @param Language $lang In which language to format the date
54 public function __construct( Language
$lang ) {
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;
63 $this->regexTrail
= '(?![a-z])/iu';
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})\]\]';
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}";
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';
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]]';
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
;
111 $this->preferences
= [
112 'default' => self
::NONE
,
116 'ISO 8601' => self
::ISO1
,
121 * Get a DateFormatter object
123 * @param Language|string|null $lang In which language to format the date
124 * Defaults to the site content language
125 * @return DateFormatter
127 public static function getInstance( $lang = null ) {
128 global $wgContLang, $wgMainCacheType;
130 $lang = $lang ?
wfGetLangObj( $lang ) : $wgContLang;
131 $cache = ObjectCache
::getLocalServerInstance( $wgMainCacheType );
133 static $dateFormatter = false;
134 if ( !$dateFormatter ) {
135 $dateFormatter = $cache->getWithSetCallback(
136 $cache->makeKey( 'dateformatter', $lang->getCode() ),
138 function () use ( $lang ) {
139 return new DateFormatter( $lang );
144 return $dateFormatter;
148 * @param string $preference User preference
149 * @param string $text Text to reformat
150 * @param array $options Array can contain 'linked' and/or 'match-whole'
154 public function reformat( $preference, $text, $options = [ 'linked' ] ) {
155 $linked = in_array( 'linked', $options );
156 $match_whole = in_array( 'match-whole', $options );
158 if ( isset( $this->preferences
[$preference] ) ) {
159 $preference = $this->preferences
[$preference];
161 $preference = self
::NONE
;
163 for ( $i = 1; $i <= self
::LAST
; $i++
) {
165 if ( isset( $this->rules
[$preference][$i] ) ) {
167 $this->mTarget
= $this->rules
[$preference][$i];
168 } elseif ( isset( $this->rules
[self
::ALL
][$i] ) ) {
170 $this->mTarget
= $this->rules
[self
::ALL
][$i];
171 } elseif ( $preference ) {
173 $this->mTarget
= $preference;
178 $regex = $this->regexes
[$i];
182 $regex = str_replace( [ '\[\[', '\]\]' ], '', $regex );
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 );
192 // Another horrible hack
193 $this->mLinked
= $linked;
194 $text = preg_replace_callback( $regex, [ &$this, 'replace' ], $text );
195 unset( $this->mLinked
);
201 * @param array $matches
204 public function replace( $matches ) {
205 # Extract information from $matches
207 if ( isset( $this->mLinked
) ) {
208 $linked = $this->mLinked
;
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];
220 return $this->formatDate( $bits, $linked );
228 public function formatDate( $bits, $link = true ) {
229 $format = $this->targets
[$this->mTarget
];
233 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
234 // strip remaining links
235 $format = str_replace( [ '[[', ']]' ], '', $format );
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'] );
246 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
247 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
250 if ( !isset( $bits['m'] ) ) {
251 $m = $this->makeIsoMonth( $bits['F'] );
252 if ( !$m ||
$m == '00' ) {
259 if ( !isset( $bits['d'] ) ) {
260 $bits['d'] = sprintf( '%02d', $bits['j'] );
263 $formatLength = strlen( $format );
264 for ( $p = 0; $p < $formatLength; $p++
) {
267 case 'd': # ISO day of month
270 case 'm': # ISO month
276 case 'j': # ordinary day of month
277 if ( !isset( $bits['j'] ) ) {
278 $text .= intval( $bits['d'] );
283 case 'F': # long month
284 if ( !isset( $bits['F'] ) ) {
285 $m = intval( $bits['m'] );
286 if ( $m > 12 ||
$m < 1 ) {
289 $text .= $this->lang
->getMonthName( $m );
292 $text .= ucfirst( $bits['F'] );
295 case 'Y': # ordinary (optional BC) year
303 /** @todo FIXME: $matches doesn't exist here, what's expected? */
308 if ( isset( $bits['y'] ) ) {
309 $isoBits[] = $bits['y'];
311 $isoBits[] = $bits['m'];
312 $isoBits[] = $bits['d'];
313 $isoDate = implode( '-', $isoBits );
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 );
323 * Return a regex that can be used to find month names in string
324 * @return string regex to find the months with
326 public function getMonthRegex() {
328 for ( $i = 1; $i <= 12; $i++
) {
329 $names[] = $this->lang
->getMonthName( $i );
330 $names[] = $this->lang
->getMonthAbbreviation( $i );
332 return implode( '|', $names );
336 * Makes an ISO month, e.g. 02, from a month name
337 * @param string $monthName Month name
338 * @return string ISO month name
340 public function makeIsoMonth( $monthName ) {
341 $n = $this->xMonths
[$this->lang
->lc( $monthName )];
342 return sprintf( '%02d', $n );
346 * Make an ISO year from a year name, for instance: '-1199' from '1200 BC'
347 * @param string $year Year name
348 * @return string ISO year name
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 );
358 $text = sprintf( '%04d', $year );
364 * Make a year one from an ISO year, for instance: '400 BC' from '-0399'.
365 * @param string $iso ISO year
366 * @return int|string int representing year number in case of AD dates, or string containing
367 * year number and 'BC' at the end otherwise.
369 public function makeNormalYear( $iso ) {
370 if ( $iso[0] == '-' ) {
371 $text = ( intval( substr( $iso, 1 ) ) +
1 ) . ' BC';
373 $text = intval( $iso );