64 for ( $i = 1; $i <= 12; $i++ ) {
65 $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
66 $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
69 $this->regexTrail =
'(?![a-z])/iu';
71 # Partial regular expressions
72 $this->prxDM =
'\[\[(\d{1,2})[ _](' . $this->monthNames .
')\]\]';
73 $this->prxMD =
'\[\[(' . $this->monthNames .
')[ _](\d{1,2})\]\]';
74 $this->prxY =
'\[\[(\d{1,4}([ _]BC|))\]\]';
75 $this->prxISO1 =
'\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
76 $this->prxISO2 =
'\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
78 # Real regular expressions
79 $this->regexes[
self::DMY] =
"/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
80 $this->regexes[
self::YDM] =
"/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
81 $this->regexes[
self::MDY] =
"/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
82 $this->regexes[
self::YMD] =
"/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
83 $this->regexes[
self::DM] =
"/{$this->prxDM}{$this->regexTrail}";
84 $this->regexes[
self::MD] =
"/{$this->prxMD}{$this->regexTrail}";
85 $this->regexes[
self::ISO1] =
"/{$this->prxISO1}{$this->regexTrail}";
86 $this->regexes[
self::ISO2] =
"/{$this->prxISO2}{$this->regexTrail}";
89 # See the comments in replace() for the meaning of the letters
100 $this->targets[
self::DMY] =
'[[F j|j F]] [[Y]]';
101 $this->targets[
self::YDM] =
'[[Y]], [[F j|j F]]';
102 $this->targets[
self::MDY] =
'[[F j]], [[Y]]';
103 $this->targets[
self::YMD] =
'[[Y]] [[F j]]';
104 $this->targets[
self::DM] =
'[[F j|j F]]';
105 $this->targets[
self::MD] =
'[[F j]]';
106 $this->targets[
self::ISO1] =
'[[Y|y]]-[[F j|m-d]]';
117 $this->preferences = [
139 static $dateFormatter =
false;
140 if ( !$dateFormatter ) {
141 $dateFormatter =
$cache->getWithSetCallback(
142 $cache->makeKey(
'dateformatter',
$lang->getCode() ),
150 return $dateFormatter;
161 $linked = in_array(
'linked',
$options );
162 $match_whole = in_array(
'match-whole',
$options );
164 if ( isset( $this->preferences[$preference] ) ) {
165 $preference = $this->preferences[$preference];
171 if ( isset( $this->rules[$preference][$i] ) ) {
173 $this->mTarget = $this->rules[$preference][$i];
174 } elseif ( isset( $this->rules[self::ALL][$i] ) ) {
176 $this->mTarget = $this->rules[
self::ALL][$i];
177 } elseif ( $preference ) {
179 $this->mTarget = $preference;
184 $regex = $this->regexes[$i];
188 $regex = str_replace( [
'\[\[',
'\]\]' ],
'', $regex );
191 if ( $match_whole ) {
193 $regex = preg_replace(
'!^/!',
'/^', $regex );
194 $regex = str_replace( $this->regexTrail,
195 '$' . $this->regexTrail, $regex );
199 $this->mLinked = $linked;
200 $text = preg_replace_callback( $regex, [ $this,
'replace' ], $text );
201 unset( $this->mLinked );
213 # Extract information from $matches
215 if ( isset( $this->mLinked ) ) {
221 $keyLength = strlen( $key );
222 for ( $p = 0; $p < $keyLength; $p++ ) {
223 if ( $key[$p] !=
' ' ) {
243 $format = preg_replace(
'/\[\[[^|]+\|([^\]]+)\]\]/',
'$1', $format );
245 $format = str_replace( [
'[[',
']]' ],
'', $format );
253 if ( !isset( $bits[
'y'] ) && isset( $bits[
'Y'] ) ) {
256 if ( !isset( $bits[
'Y'] ) && isset( $bits[
'y'] ) ) {
260 if ( !isset( $bits[
'm'] ) ) {
262 if ( !$m || $m ==
'00' ) {
269 if ( !isset( $bits[
'd'] ) ) {
270 $bits[
'd'] = sprintf(
'%02d', $bits[
'j'] );
273 $formatLength = strlen( $format );
274 for ( $p = 0; $p < $formatLength; $p++ ) {
277 case 'd': # ISO day
of month
280 case 'm': # ISO month
286 case 'j': # ordinary day
of month
287 if ( !isset( $bits[
'j'] ) ) {
288 $text .= intval( $bits[
'd'] );
293 case 'F': #
long month
294 if ( !isset( $bits[
'F'] ) ) {
295 $m = intval( $bits[
'm'] );
296 if ( $m > 12 || $m < 1 ) {
299 $text .= $this->lang->getMonthName( $m );
302 $text .= ucfirst( $bits[
'F'] );
305 case 'Y': # ordinary (optional BC) year
319 if ( isset( $bits[
'y'] ) ) {
320 $isoBits[] = $bits[
'y'];
322 $isoBits[] = $bits[
'm'];
323 $isoBits[] = $bits[
'd'];
324 $isoDate = implode(
'-', $isoBits );
328 [
'class' =>
'mw-formatted-date',
'title' => $isoDate ], $text );
339 for ( $i = 1; $i <= 12; $i++ ) {
340 $names[] = $this->lang->getMonthName( $i );
341 $names[] = $this->lang->getMonthAbbreviation( $i );
343 return implode(
'|', $names );
352 $n = $this->xMonths[$this->lang->lc( $monthName )];
353 return sprintf(
'%02d', $n );
362 # Assumes the year is in a nice format, as enforced by the regex
363 if ( substr( $year, -2 ) ==
'BC' ) {
364 $num = intval( substr( $year, 0, -3 ) ) - 1;
365 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
366 $text = sprintf(
'-%04d', $num );
369 $text = sprintf(
'%04d', $year );
381 if ( $iso[0] ==
'-' ) {
382 $text = ( intval( substr( $iso, 1 ) ) + 1 ) .
' BC';
384 $text = intval( $iso );