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() {
40 global $wgContLang, $wgInputEncoding;
42 $this->monthNames
= $this->getMonthRegex();
43 for ( $i=1; $i<=12; $i++
) {
44 $this->xMonths
[strtolower( $wgContLang->getMonthName( $i ) )] = $i;
46 for ( $i=1; $i<=12; $i++
) {
47 $this->xMonths
[strtolower( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
50 # Attempt at UTF-8 support, untested at the moment
51 if ( $wgInputEncoding == 'UTF-8' ) {
52 $this->regexTrail
= '(?![a-z])/iu';
54 $this->regexTrail
= '(?![a-z])/i';
57 # Partial regular expressions
58 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
59 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
60 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
61 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
62 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
64 # Real regular expressions
65 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
66 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
67 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
68 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
69 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
70 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
71 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
72 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
75 # See the comments in replace() for the meaning of the letters
76 $this->keys
[DF_DMY
] = 'jFY';
77 $this->keys
[DF_YDM
] = 'Y jF';
78 $this->keys
[DF_MDY
] = 'FjY';
79 $this->keys
[DF_YMD
] = 'Y Fj';
80 $this->keys
[DF_DM
] = 'jF';
81 $this->keys
[DF_MD
] = 'Fj';
82 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
83 $this->keys
[DF_ISO2
] = 'ymd';
86 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
87 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
88 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
89 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
90 $this->targets
[DF_DM
] = '[[F j|j F]]';
91 $this->targets
[DF_MD
] = '[[F j]]';
92 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
93 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
97 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
98 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
99 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
100 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
101 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
107 function &getInstance() {
108 global $wgDBname, $wgMemc;
109 static $dateFormatter = false;
110 if ( !$dateFormatter ) {
111 $dateFormatter = $wgMemc->get( "$wgDBname:dateformatter" );
112 if ( !$dateFormatter ) {
113 $dateFormatter = new DateFormatter
;
114 $wgMemc->set( "$wgDBname:dateformatter", $dateFormatter, 3600 );
117 return $dateFormatter;
124 function reformat( $preference, $text ) {
125 if ($preference == 'ISO 8601') $preference = 4; # The ISO 8601 option used to be 4
126 for ( $i=1; $i<=DF_LAST
; $i++
) {
128 if ( @$this->rules
[$preference][$i] ) {
130 $this->mTarget
= $this->rules
[$preference][$i];
131 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
133 $this->mTarget
= $this->rules
[DF_ALL
][$i];
134 } elseif ( $preference ) {
136 $this->mTarget
= $preference;
141 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
149 function replace( $matches ) {
150 # Extract information from $matches
152 $key = $this->keys
[$this->mSource
];
153 for ( $p=0; $p < strlen($key); $p++
) {
154 if ( $key{$p} != ' ' ) {
155 $bits[$key{$p}] = $matches[$p+
1];
159 $format = $this->targets
[$this->mTarget
];
165 for ( $p=0; $p < strlen( $format ); $p++
) {
168 case 'd': # ISO day of month
169 if ( !isset($bits['d']) ) {
170 $text .= sprintf( '%02d', $bits['j'] );
175 case 'm': # ISO month
176 if ( !isset($bits['m']) ) {
177 $m = $this->makeIsoMonth( $bits['F'] );
178 if ( !$m ||
$m == '00' ) {
188 if ( !isset( $bits['y'] ) ) {
189 $text .= $this->makeIsoYear( $bits['Y'] );
194 case 'j': # ordinary day of month
195 if ( !isset($bits['j']) ) {
196 $text .= intval( $bits['d'] );
201 case 'F': # long month
202 if ( !isset( $bits['F'] ) ) {
203 $m = intval($bits['m']);
204 if ( $m > 12 ||
$m < 1 ) {
208 $text .= $wgContLang->getMonthName( $m );
211 $text .= ucfirst( $bits['F'] );
214 case 'Y': # ordinary (optional BC) year
215 if ( !isset( $bits['Y'] ) ) {
216 $text .= $this->makeNormalYear( $bits['y'] );
234 function getMonthRegex() {
237 for( $i = 1; $i <= 12; $i++
) {
238 $names[] = $wgContLang->getMonthName( $i );
239 $names[] = $wgContLang->getMonthAbbreviation( $i );
241 return implode( '|', $names );
245 * Makes an ISO month, e.g. 02, from a month name
246 * @param string $monthName Month name
247 * @return string ISO month name
249 function makeIsoMonth( $monthName ) {
250 $n = $this->xMonths
[strtolower( $monthName )];
251 return sprintf( '%02d', $n );
256 * @param string $year Year name
257 * @return string ISO year name
259 function makeIsoYear( $year ) {
260 # Assumes the year is in a nice format, as enforced by the regex
261 if ( substr( $year, -2 ) == 'BC' ) {
262 $num = intval(substr( $year, 0, -3 )) - 1;
263 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
264 $text = sprintf( '-%04d', $num );
267 $text = sprintf( '%04d', $year );
275 function makeNormalYear( $iso ) {
276 if ( $iso{0} == '-' ) {
277 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
279 $text = intval( $iso );
288 function wfMainDateReplace( $matches ) {
289 $df =& DateFormatter
::getInstance();
290 return $df->replace( $matches );