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
;
117 function &getInstance() {
118 global $wgDBname, $wgMemc;
119 static $dateFormatter = false;
120 if ( !$dateFormatter ) {
121 $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
122 if ( !$dateFormatter ) {
123 $dateFormatter = new DateFormatter
;
124 $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
127 return $dateFormatter;
134 function reformat( $preference, $text ) {
135 for ( $i=1; $i<=DF_LAST
; $i++
) {
137 if ( @$this->rules
[$preference][$i] ) {
139 $this->mTarget
= $this->rules
[$preference][$i];
140 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
142 $this->mTarget
= $this->rules
[DF_ALL
][$i];
143 } elseif ( $preference ) {
145 $this->mTarget
= $preference;
150 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
158 function replace( $matches ) {
159 # Extract information from $matches
161 $key = $this->keys
[$this->mSource
];
162 for ( $p=0; $p < strlen($key); $p++
) {
163 if ( $key{$p} != ' ' ) {
164 $bits[$key{$p}] = $matches[$p+
1];
168 $format = $this->targets
[$this->mTarget
];
174 for ( $p=0; $p < strlen( $format ); $p++
) {
177 case 'd': # ISO day of month
178 if ( !isset($bits['d']) ) {
179 $text .= sprintf( '%02d', $bits['j'] );
184 case 'm': # ISO month
185 if ( !isset($bits['m']) ) {
186 $m = $this->makeIsoMonth( $bits['F'] );
187 if ( !$m ||
$m == '00' ) {
197 if ( !isset( $bits['y'] ) ) {
198 $text .= $this->makeIsoYear( $bits['Y'] );
203 case 'j': # ordinary day of month
204 if ( !isset($bits['j']) ) {
205 $text .= IntVal( $bits['d'] );
210 case 'F': # long month
211 if ( !isset( $bits['F'] ) ) {
212 $m = IntVal($bits['m']);
213 if ( $m > 12 ||
$m < 1 ) {
217 $text .= $wgContLang->getMonthName( $m );
220 $text .= ucfirst( $bits['F'] );
223 case 'Y': # ordinary (optional BC) year
224 if ( !isset( $bits['Y'] ) ) {
225 $text .= $this->makeNormalYear( $bits['y'] );
243 function getMonthRegex() {
246 for( $i = 1; $i <= 12; $i++
) {
247 $names[] = $wgContLang->getMonthName( $i );
248 $names[] = $wgContLang->getMonthAbbreviation( $i );
250 return implode( '|', $names );
254 * Makes an ISO month, e.g. 02, from a month name
255 * @param string $monthName Month name
256 * @return string ISO month name
258 function makeIsoMonth( $monthName ) {
259 $n = $this->xMonths
[strtolower( $monthName )];
260 return sprintf( '%02d', $n );
265 * @param string $year Year name
266 * @return string ISO year name
268 function makeIsoYear( $year ) {
269 # Assumes the year is in a nice format, as enforced by the regex
270 if ( substr( $year, -2 ) == 'BC' ) {
271 $num = IntVal(substr( $year, 0, -3 )) - 1;
272 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
273 $text = sprintf( '-%04d', $num );
276 $text = sprintf( '%04d', $year );
284 function makeNormalYear( $iso ) {
285 if ( $iso{0} == '-' ) {
286 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
288 $text = IntVal( $iso );
297 function wfMainDateReplace( $matches ) {
298 $df =& DateFormatter
::getInstance();
299 return $df->replace( $matches );