15 define('DF_LASTPREF', 4);
23 * @todo preferences, OutputPage
29 var $mSource, $mTarget;
30 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
32 var $regexes, $pDays, $pMonths, $pYears;
38 function DateFormatter() {
39 global $wgContLang, $wgInputEncoding;
41 $this->monthNames
= $this->getMonthRegex();
42 for ( $i=1; $i<=12; $i++
) {
43 $this->xMonths
[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
45 for ( $i=1; $i<=12; $i++
) {
46 $this->xMonths
[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
49 # Attempt at UTF-8 support, untested at the moment
50 if ( $wgInputEncoding == 'UTF-8' ) {
51 $this->regexTrail
= '(?![a-z])/iu';
53 $this->regexTrail
= '(?![a-z])/i';
56 # Partial regular expressions
57 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
58 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
59 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
60 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
61 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
63 # Real regular expressions
64 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
65 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
66 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
67 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
68 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
69 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
70 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
71 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
74 # See the comments in replace() for the meaning of the letters
75 $this->keys
[DF_DMY
] = 'jFY';
76 $this->keys
[DF_YDM
] = 'Y jF';
77 $this->keys
[DF_MDY
] = 'FjY';
78 $this->keys
[DF_YMD
] = 'Y Fj';
79 $this->keys
[DF_DM
] = 'jF';
80 $this->keys
[DF_MD
] = 'Fj';
81 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
82 $this->keys
[DF_ISO2
] = 'ymd';
85 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
86 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
87 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
88 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
89 $this->targets
[DF_DM
] = '[[F j|j F]]';
90 $this->targets
[DF_MD
] = '[[F j]]';
91 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
92 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
96 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
97 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
98 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
99 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
100 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
106 function &getInstance() {
107 global $wgDBname, $wgMemc;
108 static $dateFormatter = false;
109 if ( !$dateFormatter ) {
110 $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
111 if ( !$dateFormatter ) {
112 $dateFormatter = new DateFormatter
;
113 $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
116 return $dateFormatter;
123 function reformat( $preference, $text ) {
124 if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
125 for ( $i=1; $i<=DF_LAST
; $i++
) {
127 if ( @$this->rules
[$preference][$i] ) {
129 $this->mTarget
= $this->rules
[$preference][$i];
130 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
132 $this->mTarget
= $this->rules
[DF_ALL
][$i];
133 } elseif ( $preference ) {
135 $this->mTarget
= $preference;
140 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
148 function replace( $matches ) {
149 # Extract information from $matches
151 $key = $this->keys
[$this->mSource
];
152 for ( $p=0; $p < strlen($key); $p++
) {
153 if ( $key{$p} != ' ' ) {
154 $bits[$key{$p}] = $matches[$p+
1];
158 $format = $this->targets
[$this->mTarget
];
164 for ( $p=0; $p < strlen( $format ); $p++
) {
167 case 'd': # ISO day of month
168 if ( !isset($bits['d']) ) {
169 $text .= sprintf( '%02d', $bits['j'] );
174 case 'm': # ISO month
175 if ( !isset($bits['m']) ) {
176 $m = $this->makeIsoMonth( $bits['F'] );
177 if ( !$m ||
$m == '00' ) {
187 if ( !isset( $bits['y'] ) ) {
188 $text .= $this->makeIsoYear( $bits['Y'] );
193 case 'j': # ordinary day of month
194 if ( !isset($bits['j']) ) {
195 $text .= IntVal( $bits['d'] );
200 case 'F': # long month
201 if ( !isset( $bits['F'] ) ) {
202 $m = IntVal($bits['m']);
203 if ( $m > 12 ||
$m < 1 ) {
207 $text .= $wgContLang->getMonthName( $m );
210 $text .= ucfirst( $bits['F'] );
213 case 'Y': # ordinary (optional BC) year
214 if ( !isset( $bits['Y'] ) ) {
215 $text .= $this->makeNormalYear( $bits['y'] );
233 function getMonthRegex() {
236 for( $i = 1; $i <= 12; $i++
) {
237 $names[] = $wgContLang->getMonthName( $i );
238 $names[] = $wgContLang->getMonthAbbreviation( $i );
240 return implode( '|', $names );
244 * Makes an ISO month, e.g. 02, from a month name
245 * @param string $monthName Month name
246 * @return string ISO month name
248 function makeIsoMonth( $monthName ) {
249 $n = $this->xMonths
[strtolower( $monthName )];
250 return sprintf( '%02d', $n );
255 * @param string $year Year name
256 * @return string ISO year name
258 function makeIsoYear( $year ) {
259 # Assumes the year is in a nice format, as enforced by the regex
260 if ( substr( $year, -2 ) == 'BC' ) {
261 $num = IntVal(substr( $year, 0, -3 )) - 1;
262 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
263 $text = sprintf( '-%04d', $num );
266 $text = sprintf( '%04d', $year );
274 function makeNormalYear( $iso ) {
275 if ( $iso{0} == '-' ) {
276 $text = (IntVal( substr( $iso, 1 ) ) +
1) . ' BC';
278 $text = IntVal( $iso );
287 function wfMainDateReplace( $matches ) {
288 $df =& DateFormatter
::getInstance();
289 return $df->replace( $matches );