22 define('DF_LASTPREF', 4);
36 * @todo preferences, OutputPage
40 var $mSource, $mTarget;
41 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
43 var $regexes, $pDays, $pMonths, $pYears;
49 function DateFormatter() {
50 global $wgContLang, $wgInputEncoding;
52 $this->monthNames
= $this->getMonthRegex();
53 for ( $i=1; $i<=12; $i++
) {
54 $this->xMonths
[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
56 for ( $i=1; $i<=12; $i++
) {
57 $this->xMonths
[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
60 # Attempt at UTF-8 support, untested at the moment
61 if ( $wgInputEncoding == 'UTF-8' ) {
62 $this->regexTrail
= '(?![a-z])/iu';
64 $this->regexTrail
= '(?![a-z])/i';
67 # Partial regular expressions
68 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
69 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
70 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
71 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
72 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
74 # Real regular expressions
75 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
76 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
77 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
78 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
79 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
80 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
81 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
82 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
85 # See the comments in replace() for the meaning of the letters
86 $this->keys
[DF_DMY
] = 'jFY';
87 $this->keys
[DF_YDM
] = 'Y jF';
88 $this->keys
[DF_MDY
] = 'FjY';
89 $this->keys
[DF_YMD
] = 'Y Fj';
90 $this->keys
[DF_DM
] = 'jF';
91 $this->keys
[DF_MD
] = 'Fj';
92 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
93 $this->keys
[DF_ISO2
] = 'ymd';
96 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
97 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
98 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
99 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
100 $this->targets
[DF_DM
] = '[[F j|j F]]';
101 $this->targets
[DF_MD
] = '[[F j]]';
102 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
103 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
107 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
108 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
109 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
110 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
111 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
118 function reformat( $preference, $text ) {
119 for ( $i=1; $i<=DF_LAST
; $i++
) {
121 if ( @$this->rules
[$preference][$i] ) {
123 $this->mTarget
= $this->rules
[$preference][$i];
124 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
126 $this->mTarget
= $this->rules
[DF_ALL
][$i];
127 } elseif ( $preference ) {
129 $this->mTarget
= $preference;
134 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
142 function replace( $matches ) {
143 # Extract information from $matches
145 $key = $this->keys
[$this->mSource
];
146 for ( $p=0; $p < strlen($key); $p++
) {
147 if ( $key{$p} != ' ' ) {
148 $bits[$key{$p}] = $matches[$p+
1];
152 $format = $this->targets
[$this->mTarget
];
158 for ( $p=0; $p < strlen( $format ); $p++
) {
161 case 'd': # ISO day of month
162 if ( !isset($bits['d']) ) {
163 $text .= sprintf( '%02d', $bits['j'] );
168 case 'm': # ISO month
169 if ( !isset($bits['m']) ) {
170 $m = $this->makeIsoMonth( $bits['F'] );
171 if ( !$m ||
$m == '00' ) {
181 if ( !isset( $bits['y'] ) ) {
182 $text .= $this->makeIsoYear( $bits['Y'] );
187 case 'j': # ordinary day of month
188 if ( !isset($bits['j']) ) {
189 $text .= IntVal( $bits['d'] );
194 case 'F': # long month
195 if ( !isset( $bits['F'] ) ) {
196 $m = IntVal($bits['m']);
197 if ( $m > 12 ||
$m < 1 ) {
201 $text .= $wgContLang->getMonthName( $m );
204 $text .= ucfirst( $bits['F'] );
207 case 'Y': # ordinary (optional BC) year
208 if ( !isset( $bits['Y'] ) ) {
209 $text .= $this->makeNormalYear( $bits['y'] );
227 function getMonthRegex() {
230 for( $i = 1; $i <= 12; $i++
) {
231 $names[] = $wgContLang->getMonthName( $i );
232 $names[] = $wgContLang->getMonthAbbreviation( $i );
234 return implode( '|', $names );
238 * Makes an ISO month, e.g. 02, from a month name
239 * @param string $monthName Month name
240 * @return string ISO month name
242 function makeIsoMonth( $monthName ) {
243 $n = $this->xMonths
[strtolower( $monthName )];
244 return sprintf( '%02d', $n );
249 * @param string $year Year name
250 * @return string ISO year name
252 function makeIsoYear( $year ) {
253 # Assumes the year is in a nice format, as enforced by the regex
254 if ( substr( $year, -2 ) == 'BC' ) {
255 $num = IntVal(substr( $year, 0, -3 )) - 1;
256 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
257 $text = sprintf( '-%04d', $num );
260 $text = sprintf( '%04d', $year );
268 function makeNormalYear( $iso ) {
269 if ( $iso{0} == '-' ) {
270 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
272 $text = IntVal( $iso );
281 function wfMainDateReplace( $matches ) {
282 global $wgDateFormatter;
283 return $wgDateFormatter->replace( $matches );