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
24 use MediaWiki\MediaWikiServices
;
27 * Date formatter, recognises dates in plain text and formats them according to user preferences.
28 * @todo preferences, OutputPage
32 private $mSource, $mTarget;
33 private $monthNames = '';
36 private $rules, $xMonths, $preferences;
38 private $lang, $mLinked;
60 * @param Language $lang In which language to format the date
62 public function __construct( Language
$lang ) {
65 $this->monthNames
= $this->getMonthRegex();
66 for ( $i = 1; $i <= 12; $i++
) {
67 $this->xMonths
[$this->lang
->lc( $this->lang
->getMonthName( $i ) )] = $i;
68 $this->xMonths
[$this->lang
->lc( $this->lang
->getMonthAbbreviation( $i ) )] = $i;
71 $this->regexTrail
= '(?![a-z])/iu';
73 # Partial regular expressions
74 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')\]\]';
75 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})\]\]';
76 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))\]\]';
77 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
78 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
80 # Real regular expressions
81 $this->regexes
[self
::DMY
] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
82 $this->regexes
[self
::YDM
] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
83 $this->regexes
[self
::MDY
] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
84 $this->regexes
[self
::YMD
] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
85 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
86 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
87 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
88 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
91 # See the comments in replace() for the meaning of the letters
92 $this->keys
[self
::DMY
] = 'jFY';
93 $this->keys
[self
::YDM
] = 'Y jF';
94 $this->keys
[self
::MDY
] = 'FjY';
95 $this->keys
[self
::YMD
] = 'Y Fj';
96 $this->keys
[self
::DM
] = 'jF';
97 $this->keys
[self
::MD
] = 'Fj';
98 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
99 $this->keys
[self
::ISO2
] = 'ymd';
101 # Target date formats
102 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
103 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
104 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
105 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
106 $this->targets
[self
::DM
] = '[[F j|j F]]';
107 $this->targets
[self
::MD
] = '[[F j]]';
108 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
109 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
113 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
114 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
115 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
116 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
117 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
119 $this->preferences
= [
120 'default' => self
::NONE
,
124 'ISO 8601' => self
::ISO1
,
129 * Get a DateFormatter object
131 * @param Language|null $lang In which language to format the date
132 * Defaults to the site content language
133 * @return DateFormatter
135 public static function getInstance( Language
$lang = null ) {
136 global $wgMainCacheType;
138 $lang = $lang ?? MediaWikiServices
::getInstance()->getContentLanguage();
139 $cache = ObjectCache
::getLocalServerInstance( $wgMainCacheType );
141 static $dateFormatter = false;
142 if ( !$dateFormatter ) {
143 $dateFormatter = $cache->getWithSetCallback(
144 $cache->makeKey( 'dateformatter', $lang->getCode() ),
146 function () use ( $lang ) {
147 return new DateFormatter( $lang );
152 return $dateFormatter;
156 * @param string $preference User preference
157 * @param string $text Text to reformat
158 * @param array $options Array can contain 'linked' and/or 'match-whole'
162 public function reformat( $preference, $text, $options = [ 'linked' ] ) {
163 $linked = in_array( 'linked', $options );
164 $match_whole = in_array( 'match-whole', $options );
166 if ( isset( $this->preferences
[$preference] ) ) {
167 $preference = $this->preferences
[$preference];
169 $preference = self
::NONE
;
171 for ( $i = 1; $i <= self
::LAST
; $i++
) {
173 if ( isset( $this->rules
[$preference][$i] ) ) {
175 $this->mTarget
= $this->rules
[$preference][$i];
176 } elseif ( isset( $this->rules
[self
::ALL
][$i] ) ) {
178 $this->mTarget
= $this->rules
[self
::ALL
][$i];
179 } elseif ( $preference ) {
181 $this->mTarget
= $preference;
186 $regex = $this->regexes
[$i];
190 $regex = str_replace( [ '\[\[', '\]\]' ], '', $regex );
193 if ( $match_whole ) {
194 // Let's hope this works
195 $regex = preg_replace( '!^/!', '/^', $regex );
196 $regex = str_replace( $this->regexTrail
,
197 '$' . $this->regexTrail
, $regex );
200 // Another horrible hack
201 $this->mLinked
= $linked;
202 $text = preg_replace_callback( $regex, [ $this, 'replace' ], $text );
203 unset( $this->mLinked
);
209 * Regexp replacement callback
211 * @param array $matches
214 private function replace( $matches ) {
215 # Extract information from $matches
217 if ( isset( $this->mLinked
) ) {
218 $linked = $this->mLinked
;
222 $key = $this->keys
[$this->mSource
];
223 $keyLength = strlen( $key );
224 for ( $p = 0; $p < $keyLength; $p++
) {
225 if ( $key[$p] != ' ' ) {
226 $bits[$key[$p]] = $matches[$p +
1];
230 return $this->formatDate( $bits, $matches[0], $linked );
235 * @param string $orig Original input string, to be returned
236 * on formatting failure.
240 private function formatDate( $bits, $orig, $link = true ) {
241 $format = $this->targets
[$this->mTarget
];
245 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
246 // strip remaining links
247 $format = str_replace( [ '[[', ']]' ], '', $format );
254 // Pre-generate y/Y stuff because we need the year for the <span> title.
255 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
256 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
258 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
259 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
262 if ( !isset( $bits['m'] ) ) {
263 $m = $this->makeIsoMonth( $bits['F'] );
264 if ( !$m ||
$m == '00' ) {
271 if ( !isset( $bits['d'] ) ) {
272 $bits['d'] = sprintf( '%02d', $bits['j'] );
275 $formatLength = strlen( $format );
276 for ( $p = 0; $p < $formatLength; $p++
) {
279 case 'd': # ISO day of month
282 case 'm': # ISO month
288 case 'j': # ordinary day of month
289 if ( !isset( $bits['j'] ) ) {
290 $text .= intval( $bits['d'] );
295 case 'F': # long month
296 if ( !isset( $bits['F'] ) ) {
297 $m = intval( $bits['m'] );
298 if ( $m > 12 ||
$m < 1 ) {
301 $text .= $this->lang
->getMonthName( $m );
304 $text .= ucfirst( $bits['F'] );
307 case 'Y': # ordinary (optional BC) year
315 // This occurs when parsing a date with day or month outside the bounds
321 if ( isset( $bits['y'] ) ) {
322 $isoBits[] = $bits['y'];
324 $isoBits[] = $bits['m'];
325 $isoBits[] = $bits['d'];
326 $isoDate = implode( '-', $isoBits );
328 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
329 $text = Html
::rawElement( 'span',
330 [ 'class' => 'mw-formatted-date', 'title' => $isoDate ], $text );
336 * Return a regex that can be used to find month names in string
337 * @return string regex to find the months with
339 private function getMonthRegex() {
341 for ( $i = 1; $i <= 12; $i++
) {
342 $names[] = $this->lang
->getMonthName( $i );
343 $names[] = $this->lang
->getMonthAbbreviation( $i );
345 return implode( '|', $names );
349 * Makes an ISO month, e.g. 02, from a month name
350 * @param string $monthName Month name
351 * @return string ISO month name
353 private function makeIsoMonth( $monthName ) {
354 $n = $this->xMonths
[$this->lang
->lc( $monthName )];
355 return sprintf( '%02d', $n );
359 * Make an ISO year from a year name, for instance: '-1199' from '1200 BC'
360 * @param string $year Year name
361 * @return string ISO year name
363 private function makeIsoYear( $year ) {
364 # Assumes the year is in a nice format, as enforced by the regex
365 if ( substr( $year, -2 ) == 'BC' ) {
366 $num = intval( substr( $year, 0, -3 ) ) - 1;
367 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
368 $text = sprintf( '-%04d', $num );
371 $text = sprintf( '%04d', $year );
377 * Make a year one from an ISO year, for instance: '400 BC' from '-0399'.
378 * @param string $iso ISO year
379 * @return int|string int representing year number in case of AD dates, or string containing
380 * year number and 'BC' at the end otherwise.
382 private function makeNormalYear( $iso ) {
383 if ( $iso[0] == '-' ) {
384 $text = ( intval( substr( $iso, 1 ) ) +
1 ) . ' BC';
386 $text = intval( $iso );