21 define('DF_LASTPREF', 4);
35 * @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 $wgMonthNamesEn, $wgInputEncoding;
52 $this->monthNames
= $this->getMonthRegex();
53 for ( $i=1; $i<=12; $i++
) {
54 $this->xMonths
[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
57 # Attempt at UTF-8 support, untested at the moment
58 if ( $wgInputEncoding == 'UTF-8' ) {
59 $this->regexTrail
= '(?![a-z])/iu';
61 $this->regexTrail
= '(?![a-z])/i';
64 # Partial regular expressions
65 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
66 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
67 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
68 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
69 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
71 # Real regular expressions
72 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
73 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
74 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
75 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
76 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
77 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
78 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
79 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
82 # See the comments in replace() for the meaning of the letters
83 $this->keys
[DF_DMY
] = 'jFY';
84 $this->keys
[DF_YDM
] = 'Y jF';
85 $this->keys
[DF_MDY
] = 'FjY';
86 $this->keys
[DF_YMD
] = 'Y Fj';
87 $this->keys
[DF_DM
] = 'jF';
88 $this->keys
[DF_MD
] = 'Fj';
89 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
90 $this->keys
[DF_ISO2
] = 'ymd';
93 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
94 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
95 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
96 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
97 $this->targets
[DF_DM
] = '[[F j|j F]]';
98 $this->targets
[DF_MD
] = '[[F j]]';
99 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
100 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
104 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
105 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
106 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
107 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
108 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
115 function reformat( $preference, $text ) {
116 for ( $i=1; $i<=DF_LAST
; $i++
) {
118 if ( @$this->rules
[$preference][$i] ) {
120 $this->mTarget
= $this->rules
[$preference][$i];
121 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
123 $this->mTarget
= $this->rules
[DF_ALL
][$i];
124 } elseif ( $preference ) {
126 $this->mTarget
= $preference;
131 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
139 function replace( $matches ) {
140 global $wgMonthNamesEn;
141 # Extract information from $matches
143 $key = $this->keys
[$this->mSource
];
144 for ( $p=0; $p < strlen($key); $p++
) {
145 if ( $key{$p} != ' ' ) {
146 $bits[$key{$p}] = $matches[$p+
1];
150 $format = $this->targets
[$this->mTarget
];
156 for ( $p=0; $p < strlen( $format ); $p++
) {
159 case 'd': # ISO day of month
160 if ( is_null($bits['d']) ) {
161 $text .= sprintf( '%02d', $bits['j'] );
166 case 'm': # ISO month
167 if ( is_null($bits['m']) ) {
168 $m = $this->makeIsoMonth( $bits['F'] );
169 if ( !$m ||
$m == '00' ) {
179 if ( is_null( $bits['y'] ) ) {
180 $text .= $this->makeIsoYear( $bits['Y'] );
185 case 'j': # ordinary day of month
186 if ( is_null($bits['j']) ) {
187 $text .= IntVal( $bits['d'] );
192 case 'F': # long month
193 if ( is_null( $bits['F'] ) ) {
194 $m = IntVal($bits['m']);
195 if ( $m > 12 ||
$m < 1 ) {
198 $text .= $wgMonthNamesEn[$m-1];
201 $text .= ucfirst( $bits['F'] );
204 case 'Y': # ordinary (optional BC) year
205 if ( is_null( $bits['Y'] ) ) {
206 $text .= $this->makeNormalYear( $bits['y'] );
224 function getMonthRegex() {
225 global $wgMonthNamesEn;
226 return implode( '|', $wgMonthNamesEn );
230 * Makes an ISO month, e.g. 02, from a month name
231 * @param string $monthName Month name
232 * @return string ISO month name
234 function makeIsoMonth( $monthName ) {
235 $n = $this->xMonths
[strtolower( $monthName )];
236 return sprintf( '%02d', $n );
241 * @param string $year Year name
242 * @return string ISO year name
244 function makeIsoYear( $year ) {
245 # Assumes the year is in a nice format, as enforced by the regex
246 if ( substr( $year, -2 ) == 'BC' ) {
247 $num = IntVal(substr( $year, 0, -3 )) - 1;
248 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
249 $text = sprintf( '-%04d', $num );
252 $text = sprintf( '%04d', $year );
260 function makeNormalYear( $iso ) {
261 if ( $iso{0} == '-' ) {
262 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
264 $text = IntVal( $iso );
273 function wfMainDateReplace( $matches ) {
274 global $wgDateFormatter;
275 return $wgDateFormatter->replace( $matches );