bf38aa7d5682e29c06eda77511af7d4844b5079e
[lhc/web/wiklou.git] / includes / DateFormatter.php
1 <?php
2 /**
3 * Contain things
4 * @todo document
5 */
6
7 /** */
8 define('DF_ALL', -1);
9 /** */
10 define('DF_NONE', 0);
11 /** */
12 define('DF_MDY', 1);
13 /** */
14 define('DF_DMY', 2);
15 /** */
16 define('DF_YMD', 3);
17 /** */
18 define('DF_ISO1', 4);
19 /** */
20 define('DF_LASTPREF', 4);
21
22 /** */
23 define('DF_ISO2', 5);
24 /** */
25 define('DF_YDM', 6);
26 /** */
27 define('DF_DM', 7);
28 /** */
29 define('DF_MD', 8);
30 /** */
31 define('DF_LAST', 8);
32
33 /**
34 * @todo preferences, OutputPage
35 */
36 class DateFormatter
37 {
38 var $mSource, $mTarget;
39 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
40
41 var $regexes, $pDays, $pMonths, $pYears;
42 var $rules, $xMonths;
43
44 /**
45 * @todo document
46 */
47 function DateFormatter() {
48 global $wgMonthNamesEn, $wgInputEncoding;
49
50 $this->monthNames = $this->getMonthRegex();
51 for ( $i=1; $i<=12; $i++ ) {
52 $this->xMonths[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
53 }
54
55 # Attempt at UTF-8 support, untested at the moment
56 if ( $wgInputEncoding == 'UTF-8' ) {
57 $this->regexTrail = '(?![a-z])/iu';
58 } else {
59 $this->regexTrail = '(?![a-z])/i';
60 }
61
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})]]';
68
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}";
78
79 # Extraction keys
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';
89
90 # Target date formats
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]]';
99
100 # Rules
101 # pref source target
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;
107 }
108
109 /**
110 * @param $preference
111 * @param $text
112 */
113 function reformat( $preference, $text ) {
114 for ( $i=1; $i<=DF_LAST; $i++ ) {
115 $this->mSource = $i;
116 if ( @$this->rules[$preference][$i] ) {
117 # Specific rules
118 $this->mTarget = $this->rules[$preference][$i];
119 } elseif ( @$this->rules[DF_ALL][$i] ) {
120 # General rules
121 $this->mTarget = $this->rules[DF_ALL][$i];
122 } elseif ( $preference ) {
123 # User preference
124 $this->mTarget = $preference;
125 } else {
126 # Default
127 $this->mTarget = $i;
128 }
129 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
130 }
131 return $text;
132 }
133
134 /**
135 * @param $matches
136 */
137 function replace( $matches ) {
138 global $wgMonthNamesEn;
139 # Extract information from $matches
140 $bits = array();
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];
145 }
146 }
147
148 $format = $this->targets[$this->mTarget];
149
150 # Construct new date
151 $text = '';
152 $fail = false;
153
154 for ( $p=0; $p < strlen( $format ); $p++ ) {
155 $char = $format{$p};
156 switch ( $char ) {
157 case 'd': # ISO day of month
158 if ( is_null($bits['d']) ) {
159 $text .= sprintf( '%02d', $bits['j'] );
160 } else {
161 $text .= $bits['d'];
162 }
163 break;
164 case 'm': # ISO month
165 if ( is_null($bits['m']) ) {
166 $m = $this->makeIsoMonth( $bits['F'] );
167 if ( !$m || $m == '00' ) {
168 $fail = true;
169 } else {
170 $text .= $m;
171 }
172 } else {
173 $text .= $bits['m'];
174 }
175 break;
176 case 'y': # ISO year
177 if ( is_null( $bits['y'] ) ) {
178 $text .= $this->makeIsoYear( $bits['Y'] );
179 } else {
180 $text .= $bits['y'];
181 }
182 break;
183 case 'j': # ordinary day of month
184 if ( is_null($bits['j']) ) {
185 $text .= IntVal( $bits['d'] );
186 } else {
187 $text .= $bits['j'];
188 }
189 break;
190 case 'F': # long month
191 if ( is_null( $bits['F'] ) ) {
192 $m = IntVal($bits['m']);
193 if ( $m > 12 || $m < 1 ) {
194 $fail = true;
195 } else {
196 $text .= $wgMonthNamesEn[$m-1];
197 }
198 } else {
199 $text .= ucfirst( $bits['F'] );
200 }
201 break;
202 case 'Y': # ordinary (optional BC) year
203 if ( is_null( $bits['Y'] ) ) {
204 $text .= $this->makeNormalYear( $bits['y'] );
205 } else {
206 $text .= $bits['Y'];
207 }
208 break;
209 default:
210 $text .= $char;
211 }
212 }
213 if ( $fail ) {
214 $text = $matches[0];
215 }
216 return $text;
217 }
218
219 /**
220 * @todo document
221 */
222 function getMonthRegex() {
223 global $wgMonthNamesEn;
224 return implode( '|', $wgMonthNamesEn );
225 }
226
227 /**
228 * Makes an ISO month, e.g. 02, from a month name
229 * @param string $monthName Month name
230 * @return string ISO month name
231 */
232 function makeIsoMonth( $monthName ) {
233 $n = $this->xMonths[strtolower( $monthName )];
234 return sprintf( '%02d', $n );
235 }
236
237 /**
238 * @todo document
239 * @param string $year Year name
240 * @return string ISO year name
241 */
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 );
248
249 } else {
250 $text = sprintf( '%04d', $year );
251 }
252 return $text;
253 }
254
255 /**
256 * @todo document
257 */
258 function makeNormalYear( $iso ) {
259 if ( $iso{0} == '-' ) {
260 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
261 } else {
262 $text = IntVal( $iso );
263 }
264 return $text;
265 }
266 }
267
268 /**
269 * @todo document
270 */
271 function wfMainDateReplace( $matches ) {
272 global $wgDateFormatter;
273 return $wgDateFormatter->replace( $matches );
274 }
275 ?>