bf38aa7d5682e29c06eda77511af7d4844b5079e
20 define('DF_LASTPREF', 4);
34 * @todo preferences, OutputPage
38 var $mSource, $mTarget;
39 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
41 var $regexes, $pDays, $pMonths, $pYears;
47 function DateFormatter() {
48 global $wgMonthNamesEn, $wgInputEncoding;
50 $this->monthNames
= $this->getMonthRegex();
51 for ( $i=1; $i<=12; $i++
) {
52 $this->xMonths
[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
55 # Attempt at UTF-8 support, untested at the moment
56 if ( $wgInputEncoding == 'UTF-8' ) {
57 $this->regexTrail
= '(?![a-z])/iu';
59 $this->regexTrail
= '(?![a-z])/i';
62 # Partial regular expressions
63 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
64 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
65 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
66 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
67 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
69 # Real regular expressions
70 $this->regexes
[DF_DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
71 $this->regexes
[DF_YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
72 $this->regexes
[DF_MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
73 $this->regexes
[DF_YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
74 $this->regexes
[DF_DM
] = "/{$this->prxDM}{$this->regexTrail}";
75 $this->regexes
[DF_MD
] = "/{$this->prxMD}{$this->regexTrail}";
76 $this->regexes
[DF_ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
77 $this->regexes
[DF_ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
80 # See the comments in replace() for the meaning of the letters
81 $this->keys
[DF_DMY
] = 'jFY';
82 $this->keys
[DF_YDM
] = 'Y jF';
83 $this->keys
[DF_MDY
] = 'FjY';
84 $this->keys
[DF_YMD
] = 'Y Fj';
85 $this->keys
[DF_DM
] = 'jF';
86 $this->keys
[DF_MD
] = 'Fj';
87 $this->keys
[DF_ISO1
] = 'ymd'; # y means ISO year
88 $this->keys
[DF_ISO2
] = 'ymd';
91 $this->targets
[DF_DMY
] = '[[F j|j F]] [[Y]]';
92 $this->targets
[DF_YDM
] = '[[Y]], [[F j|j F]]';
93 $this->targets
[DF_MDY
] = '[[F j]], [[Y]]';
94 $this->targets
[DF_YMD
] = '[[Y]] [[F j]]';
95 $this->targets
[DF_DM
] = '[[F j|j F]]';
96 $this->targets
[DF_MD
] = '[[F j]]';
97 $this->targets
[DF_ISO1
] = '[[Y|y]]-[[F j|m-d]]';
98 $this->targets
[DF_ISO2
] = '[[y-m-d]]';
102 $this->rules
[DF_DMY
][DF_MD
] = DF_DM
;
103 $this->rules
[DF_ALL
][DF_MD
] = DF_MD
;
104 $this->rules
[DF_MDY
][DF_DM
] = DF_MD
;
105 $this->rules
[DF_ALL
][DF_DM
] = DF_DM
;
106 $this->rules
[DF_NONE
][DF_ISO2
] = DF_ISO1
;
113 function reformat( $preference, $text ) {
114 for ( $i=1; $i<=DF_LAST
; $i++
) {
116 if ( @$this->rules
[$preference][$i] ) {
118 $this->mTarget
= $this->rules
[$preference][$i];
119 } elseif ( @$this->rules
[DF_ALL
][$i] ) {
121 $this->mTarget
= $this->rules
[DF_ALL
][$i];
122 } elseif ( $preference ) {
124 $this->mTarget
= $preference;
129 $text = preg_replace_callback( $this->regexes
[$i], 'wfMainDateReplace', $text );
137 function replace( $matches ) {
138 global $wgMonthNamesEn;
139 # Extract information from $matches
141 $key = $this->keys
[$this->mSource
];
142 for ( $p=0; $p < strlen($key); $p++
) {
143 if ( $key{$p} != ' ' ) {
144 $bits[$key{$p}] = $matches[$p+
1];
148 $format = $this->targets
[$this->mTarget
];
154 for ( $p=0; $p < strlen( $format ); $p++
) {
157 case 'd': # ISO day of month
158 if ( is_null($bits['d']) ) {
159 $text .= sprintf( '%02d', $bits['j'] );
164 case 'm': # ISO month
165 if ( is_null($bits['m']) ) {
166 $m = $this->makeIsoMonth( $bits['F'] );
167 if ( !$m ||
$m == '00' ) {
177 if ( is_null( $bits['y'] ) ) {
178 $text .= $this->makeIsoYear( $bits['Y'] );
183 case 'j': # ordinary day of month
184 if ( is_null($bits['j']) ) {
185 $text .= IntVal( $bits['d'] );
190 case 'F': # long month
191 if ( is_null( $bits['F'] ) ) {
192 $m = IntVal($bits['m']);
193 if ( $m > 12 ||
$m < 1 ) {
196 $text .= $wgMonthNamesEn[$m-1];
199 $text .= ucfirst( $bits['F'] );
202 case 'Y': # ordinary (optional BC) year
203 if ( is_null( $bits['Y'] ) ) {
204 $text .= $this->makeNormalYear( $bits['y'] );
222 function getMonthRegex() {
223 global $wgMonthNamesEn;
224 return implode( '|', $wgMonthNamesEn );
228 * Makes an ISO month, e.g. 02, from a month name
229 * @param string $monthName Month name
230 * @return string ISO month name
232 function makeIsoMonth( $monthName ) {
233 $n = $this->xMonths
[strtolower( $monthName )];
234 return sprintf( '%02d', $n );
239 * @param string $year Year name
240 * @return string ISO year name
242 function makeIsoYear( $year ) {
243 # Assumes the year is in a nice format, as enforced by the regex
244 if ( substr( $year, -2 ) == 'BC' ) {
245 $num = IntVal(substr( $year, 0, -3 )) - 1;
246 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
247 $text = sprintf( '-%04d', $num );
250 $text = sprintf( '%04d', $year );
258 function makeNormalYear( $iso ) {
259 if ( $iso{0} == '-' ) {
260 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
262 $text = IntVal( $iso );
271 function wfMainDateReplace( $matches ) {
272 global $wgDateFormatter;
273 return $wgDateFormatter->replace( $matches );