16 define('DF_LASTPREF', 4);
24 * @todo preferences, OutputPage
30 var $mSource, $mTarget;
31 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
33 var $regexes, $pDays, $pMonths, $pYears;
39 function DateFormatter() {
42 $this->monthNames
= $this->getMonthRegex();
43 for ( $i=1; $i<=12; $i++
) {
44 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
45 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
48 $this->regexTrail
= '(?![a-z])/iu';
50 # Partial regular expressions
51 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
52 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
53 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
54 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
55 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
57 # Real regular expressions
58 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
59 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
60 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
61 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
62 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
63 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
64 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
65 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
68 # See the comments in replace() for the meaning of the letters
69 $this->keys
[DF_DMY
] = 'jFY';
70 $this->keys
[DF_YDM
] = 'Y jF';
71 $this->keys
[DF_MDY
] = 'FjY';
72 $this->keys
[DF_YMD
] = 'Y Fj';
73 $this->keys
[DF_DM
] = 'jF';
74 $this->keys
[DF_MD
] = 'Fj';
75 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
76 $this->keys
[DF_ISO2
] = 'ymd';
79 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
80 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
81 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
82 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
83 $this->targets
[DF_DM
] = '[[F j|j F]]';
84 $this->targets
[DF_MD
] = '[[F j]]';
85 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
86 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
90 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
91 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
92 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
93 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
94 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
100 function &getInstance() {
101 global $wgDBname, $wgMemc;
102 static $dateFormatter = false;
103 if ( !$dateFormatter ) {
104 $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
105 if ( !$dateFormatter ) {
106 $dateFormatter = new DateFormatter
;
107 $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
110 return $dateFormatter;
117 function reformat( $preference, $text ) {
118 if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
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 $monthName String: month name
240 * @return string ISO month name
242 function makeIsoMonth( $monthName ) {
245 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
246 return sprintf( '%02d', $n );
251 * @param $year String: Year name
252 * @return string ISO year name
254 function makeIsoYear( $year ) {
255 # Assumes the year is in a nice format, as enforced by the regex
256 if ( substr( $year, -2 ) == 'BC' ) {
257 $num = intval(substr( $year, 0, -3 )) - 1;
258 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
259 $text = sprintf( '-%04d', $num );
262 $text = sprintf( '%04d', $year );
270 function makeNormalYear( $iso ) {
271 if ( $iso{0} == '-' ) {
272 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
274 $text = intval( $iso );
283 function wfMainDateReplace( $matches ) {
284 $df =& DateFormatter
::getInstance();
285 return $df->replace( $matches );