8 define("DF_LASTPREF", 4);
16 # To do: preferences, OutputPage
20 var $mSource, $mTarget;
21 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
23 var $regexes, $pDays, $pMonths, $pYears;
26 function DateFormatter()
28 global $wgMonthNamesEn, $wgInputEncoding;
30 $this->monthNames
= $this->getMonthRegex();
31 for ( $i=1; $i<=12; $i++
) {
32 $this->xMonths
[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
35 # Attempt at UTF-8 support, untested at the moment
36 if ( $wgInputEncoding == 'UTF-8' ) {
37 $this->regexTrail
= '(?![a-z])/iu';
39 $this->regexTrail
= '(?![a-z])/i';
42 # Partial regular expressions
43 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
44 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
45 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
46 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
47 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
49 # Real regular expressions
50 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
51 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
52 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
53 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
54 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
55 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
56 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
57 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
60 # See the comments in replace() for the meaning of the letters
61 $this->keys
[DF_DMY
] = 'jFY';
62 $this->keys
[DF_YDM
] = 'Y jF';
63 $this->keys
[DF_MDY
] = 'FjY';
64 $this->keys
[DF_YMD
] = 'Y Fj';
65 $this->keys
[DF_DM
] = 'jF';
66 $this->keys
[DF_MD
] = 'Fj';
67 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
68 $this->keys
[DF_ISO2
] = 'ymd';
71 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
72 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
73 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
74 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
75 $this->targets
[DF_DM
] = '[[F j|j F]]';
76 $this->targets
[DF_MD
] = '[[F j]]';
77 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
78 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
82 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
83 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
84 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
85 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
86 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
89 function reformat( $preference, $text )
91 for ( $i=1; $i<=DF_LAST
; $i++
) {
93 if ( @$this->rules
[$preference][$i] ) {
95 $this->mTarget
= $this->rules
[$preference][$i];
96 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
98 $this->mTarget
= $this->rules
[DF_ALL
][$i];
99 } elseif ( $preference ) {
101 $this->mTarget
= $preference;
106 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
111 function replace( $matches )
113 global $wgMonthNamesEn;
114 # Extract information from $matches
116 $key = $this->keys
[$this->mSource
];
117 for ( $p=0; $p < strlen($key); $p++
) {
118 if ( $key{$p} != ' ' ) {
119 $bits[$key{$p}] = $matches[$p+
1];
123 $format = $this->targets
[$this->mTarget
];
129 for ( $p=0; $p < strlen( $format ); $p++
) {
132 case 'd': # ISO day of month
133 if ( is_null($bits['d']) ) {
134 $text .= sprintf( '%02d', $bits['j'] );
139 case 'm': # ISO month
140 if ( is_null($bits['m']) ) {
141 $m = $this->makeIsoMonth( $bits['F'] );
142 if ( !$m ||
$m == '00' ) {
152 if ( is_null( $bits['y'] ) ) {
153 $text .= $this->makeIsoYear( $bits['Y'] );
158 case 'j': # ordinary day of month
159 if ( is_null($bits['j']) ) {
160 $text .= IntVal( $bits['d'] );
165 case 'F': # long month
166 if ( is_null( $bits['F'] ) ) {
167 $m = IntVal($bits['m']);
168 if ( $m > 12 ||
$m < 1 ) {
171 $text .= $wgMonthNamesEn[$m-1];
174 $text .= ucfirst( $bits['F'] );
177 case 'Y': # ordinary (optional BC) year
178 if ( is_null( $bits['Y'] ) ) {
179 $text .= $this->makeNormalYear( $bits['y'] );
194 function getMonthRegex()
196 global $wgMonthNamesEn;
197 return implode( '|', $wgMonthNamesEn );
200 # Makes an ISO month, e.g. 02, from a month name
201 function makeIsoMonth( $monthName )
203 $n = $this->xMonths
[strtolower( $monthName )];
204 return sprintf( '%02d', $n );
207 function makeIsoYear( $year )
209 # Assumes the year is in a nice format, as enforced by the regex
210 if ( substr( $year, -2 ) == 'BC' ) {
211 $num = IntVal(substr( $year, 0, -3 )) - 1;
212 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
213 $text = sprintf( '-%04d', $num );
216 $text = sprintf( '%04d', $year );
221 function makeNormalYear( $iso )
223 if ( $iso{0} == '-' ) {
224 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
226 $text = IntVal( $iso );
232 function wfMainDateReplace( $matches )
234 global $wgDateFormatter;
235 return $wgDateFormatter->replace( $matches );