22 define('DF_LASTPREF', 4);
36 * @todo preferences, OutputPage
42 var $mSource, $mTarget;
43 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
45 var $regexes, $pDays, $pMonths, $pYears;
51 function DateFormatter() {
52 global $wgContLang, $wgInputEncoding;
54 $this->monthNames
= $this->getMonthRegex();
55 for ( $i=1; $i<=12; $i++
) {
56 $this->xMonths
[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
58 for ( $i=1; $i<=12; $i++
) {
59 $this->xMonths
[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
62 # Attempt at UTF-8 support, untested at the moment
63 if ( $wgInputEncoding == 'UTF-8' ) {
64 $this->regexTrail
= '(?![a-z])/iu';
66 $this->regexTrail
= '(?![a-z])/i';
69 # Partial regular expressions
70 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
71 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
72 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
73 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
74 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
76 # Real regular expressions
77 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
78 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
79 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
80 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
81 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
82 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
83 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
84 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
87 # See the comments in replace() for the meaning of the letters
88 $this->keys
[DF_DMY
] = 'jFY';
89 $this->keys
[DF_YDM
] = 'Y jF';
90 $this->keys
[DF_MDY
] = 'FjY';
91 $this->keys
[DF_YMD
] = 'Y Fj';
92 $this->keys
[DF_DM
] = 'jF';
93 $this->keys
[DF_MD
] = 'Fj';
94 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
95 $this->keys
[DF_ISO2
] = 'ymd';
98 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
99 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
100 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
101 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
102 $this->targets
[DF_DM
] = '[[F j|j F]]';
103 $this->targets
[DF_MD
] = '[[F j]]';
104 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
105 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
109 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
110 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
111 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
112 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
113 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
119 function &getInstance() {
120 global $wgDBname, $wgMemc;
121 static $dateFormatter = false;
122 if ( !$dateFormatter ) {
123 $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
124 if ( !$dateFormatter ) {
125 $dateFormatter = new DateFormatter
;
126 $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
129 return $dateFormatter;
136 function reformat( $preference, $text ) {
137 if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
138 for ( $i=1; $i<=DF_LAST
; $i++
) {
140 if ( @$this->rules
[$preference][$i] ) {
142 $this->mTarget
= $this->rules
[$preference][$i];
143 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
145 $this->mTarget
= $this->rules
[DF_ALL
][$i];
146 } elseif ( $preference ) {
148 $this->mTarget
= $preference;
153 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
161 function replace( $matches ) {
162 # Extract information from $matches
164 $key = $this->keys
[$this->mSource
];
165 for ( $p=0; $p < strlen($key); $p++
) {
166 if ( $key{$p} != ' ' ) {
167 $bits[$key{$p}] = $matches[$p+
1];
171 $format = $this->targets
[$this->mTarget
];
177 for ( $p=0; $p < strlen( $format ); $p++
) {
180 case 'd': # ISO day of month
181 if ( !isset($bits['d']) ) {
182 $text .= sprintf( '%02d', $bits['j'] );
187 case 'm': # ISO month
188 if ( !isset($bits['m']) ) {
189 $m = $this->makeIsoMonth( $bits['F'] );
190 if ( !$m ||
$m == '00' ) {
200 if ( !isset( $bits['y'] ) ) {
201 $text .= $this->makeIsoYear( $bits['Y'] );
206 case 'j': # ordinary day of month
207 if ( !isset($bits['j']) ) {
208 $text .= IntVal( $bits['d'] );
213 case 'F': # long month
214 if ( !isset( $bits['F'] ) ) {
215 $m = IntVal($bits['m']);
216 if ( $m > 12 ||
$m < 1 ) {
220 $text .= $wgContLang->getMonthName( $m );
223 $text .= ucfirst( $bits['F'] );
226 case 'Y': # ordinary (optional BC) year
227 if ( !isset( $bits['Y'] ) ) {
228 $text .= $this->makeNormalYear( $bits['y'] );
246 function getMonthRegex() {
249 for( $i = 1; $i <= 12; $i++
) {
250 $names[] = $wgContLang->getMonthName( $i );
251 $names[] = $wgContLang->getMonthAbbreviation( $i );
253 return implode( '|', $names );
257 * Makes an ISO month, e.g. 02, from a month name
258 * @param string $monthName Month name
259 * @return string ISO month name
261 function makeIsoMonth( $monthName ) {
262 $n = $this->xMonths
[strtolower( $monthName )];
263 return sprintf( '%02d', $n );
268 * @param string $year Year name
269 * @return string ISO year name
271 function makeIsoYear( $year ) {
272 # Assumes the year is in a nice format, as enforced by the regex
273 if ( substr( $year, -2 ) == 'BC' ) {
274 $num = IntVal(substr( $year, 0, -3 )) - 1;
275 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
276 $text = sprintf( '-%04d', $num );
279 $text = sprintf( '%04d', $year );
287 function makeNormalYear( $iso ) {
288 if ( $iso{0} == '-' ) {
289 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
291 $text = IntVal( $iso );
300 function wfMainDateReplace( $matches ) {
301 $df =& DateFormatter
::getInstance();
302 return $df->replace( $matches );