Changing comments layout preparing for generated documentation with Phpdocumentor
[lhc/web/wiklou.git] / includes / DateFormatter.php
1 <?php
2 /**
3 * Contain things
4 * @todo document
5 */
6
7 /**
8 *
9 */
10 define('DF_ALL', -1);
11 define('DF_NONE', 0);
12 define('DF_MDY', 1);
13 define('DF_DMY', 2);
14 define('DF_YMD', 3);
15 define('DF_ISO1', 4);
16 define('DF_LASTPREF', 4);
17
18 define('DF_ISO2', 5);
19 define('DF_YDM', 6);
20 define('DF_DM', 7);
21 define('DF_MD', 8);
22 define('DF_LAST', 8);
23
24 /**
25 * @todo preferences, OutputPage
26 */
27 class DateFormatter
28 {
29 var $mSource, $mTarget;
30 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
31
32 var $regexes, $pDays, $pMonths, $pYears;
33 var $rules, $xMonths;
34
35 function DateFormatter()
36 {
37 global $wgMonthNamesEn, $wgInputEncoding;
38
39 $this->monthNames = $this->getMonthRegex();
40 for ( $i=1; $i<=12; $i++ ) {
41 $this->xMonths[strtolower( $wgMonthNamesEn[$i-1] )] = $i;
42 }
43
44 # Attempt at UTF-8 support, untested at the moment
45 if ( $wgInputEncoding == 'UTF-8' ) {
46 $this->regexTrail = '(?![a-z])/iu';
47 } else {
48 $this->regexTrail = '(?![a-z])/i';
49 }
50
51 # Partial regular expressions
52 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')]]';
53 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})]]';
54 $this->prxY = '\[\[(\d{1,4}([ _]BC|))]]';
55 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
56 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
57
58 # Real regular expressions
59 $this->regexes[DF_DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
60 $this->regexes[DF_YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
61 $this->regexes[DF_MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
62 $this->regexes[DF_YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
63 $this->regexes[DF_DM] = "/{$this->prxDM}{$this->regexTrail}";
64 $this->regexes[DF_MD] = "/{$this->prxMD}{$this->regexTrail}";
65 $this->regexes[DF_ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
66 $this->regexes[DF_ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
67
68 # Extraction keys
69 # See the comments in replace() for the meaning of the letters
70 $this->keys[DF_DMY] = 'jFY';
71 $this->keys[DF_YDM] = 'Y jF';
72 $this->keys[DF_MDY] = 'FjY';
73 $this->keys[DF_YMD] = 'Y Fj';
74 $this->keys[DF_DM] = 'jF';
75 $this->keys[DF_MD] = 'Fj';
76 $this->keys[DF_ISO1] = 'ymd'; # y means ISO year
77 $this->keys[DF_ISO2] = 'ymd';
78
79 # Target date formats
80 $this->targets[DF_DMY] = '[[F j|j F]] [[Y]]';
81 $this->targets[DF_YDM] = '[[Y]], [[F j|j F]]';
82 $this->targets[DF_MDY] = '[[F j]], [[Y]]';
83 $this->targets[DF_YMD] = '[[Y]] [[F j]]';
84 $this->targets[DF_DM] = '[[F j|j F]]';
85 $this->targets[DF_MD] = '[[F j]]';
86 $this->targets[DF_ISO1] = '[[Y|y]]-[[F j|m-d]]';
87 $this->targets[DF_ISO2] = '[[y-m-d]]';
88
89 # Rules
90 # pref source target
91 $this->rules[DF_DMY][DF_MD] = DF_DM;
92 $this->rules[DF_ALL][DF_MD] = DF_MD;
93 $this->rules[DF_MDY][DF_DM] = DF_MD;
94 $this->rules[DF_ALL][DF_DM] = DF_DM;
95 $this->rules[DF_NONE][DF_ISO2] = DF_ISO1;
96 }
97
98 function reformat( $preference, $text )
99 {
100 for ( $i=1; $i<=DF_LAST; $i++ ) {
101 $this->mSource = $i;
102 if ( @$this->rules[$preference][$i] ) {
103 # Specific rules
104 $this->mTarget = $this->rules[$preference][$i];
105 } elseif ( @$this->rules[DF_ALL][$i] ) {
106 # General rules
107 $this->mTarget = $this->rules[DF_ALL][$i];
108 } elseif ( $preference ) {
109 # User preference
110 $this->mTarget = $preference;
111 } else {
112 # Default
113 $this->mTarget = $i;
114 }
115 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
116 }
117 return $text;
118 }
119
120 function replace( $matches )
121 {
122 global $wgMonthNamesEn;
123 # Extract information from $matches
124 $bits = array();
125 $key = $this->keys[$this->mSource];
126 for ( $p=0; $p < strlen($key); $p++ ) {
127 if ( $key{$p} != ' ' ) {
128 $bits[$key{$p}] = $matches[$p+1];
129 }
130 }
131
132 $format = $this->targets[$this->mTarget];
133
134 # Construct new date
135 $text = '';
136 $fail = false;
137
138 for ( $p=0; $p < strlen( $format ); $p++ ) {
139 $char = $format{$p};
140 switch ( $char ) {
141 case 'd': # ISO day of month
142 if ( is_null($bits['d']) ) {
143 $text .= sprintf( '%02d', $bits['j'] );
144 } else {
145 $text .= $bits['d'];
146 }
147 break;
148 case 'm': # ISO month
149 if ( is_null($bits['m']) ) {
150 $m = $this->makeIsoMonth( $bits['F'] );
151 if ( !$m || $m == '00' ) {
152 $fail = true;
153 } else {
154 $text .= $m;
155 }
156 } else {
157 $text .= $bits['m'];
158 }
159 break;
160 case 'y': # ISO year
161 if ( is_null( $bits['y'] ) ) {
162 $text .= $this->makeIsoYear( $bits['Y'] );
163 } else {
164 $text .= $bits['y'];
165 }
166 break;
167 case 'j': # ordinary day of month
168 if ( is_null($bits['j']) ) {
169 $text .= IntVal( $bits['d'] );
170 } else {
171 $text .= $bits['j'];
172 }
173 break;
174 case 'F': # long month
175 if ( is_null( $bits['F'] ) ) {
176 $m = IntVal($bits['m']);
177 if ( $m > 12 || $m < 1 ) {
178 $fail = true;
179 } else {
180 $text .= $wgMonthNamesEn[$m-1];
181 }
182 } else {
183 $text .= ucfirst( $bits['F'] );
184 }
185 break;
186 case 'Y': # ordinary (optional BC) year
187 if ( is_null( $bits['Y'] ) ) {
188 $text .= $this->makeNormalYear( $bits['y'] );
189 } else {
190 $text .= $bits['Y'];
191 }
192 break;
193 default:
194 $text .= $char;
195 }
196 }
197 if ( $fail ) {
198 $text = $matches[0];
199 }
200 return $text;
201 }
202
203 function getMonthRegex()
204 {
205 global $wgMonthNamesEn;
206 return implode( '|', $wgMonthNamesEn );
207 }
208
209 # Makes an ISO month, e.g. 02, from a month name
210 function makeIsoMonth( $monthName )
211 {
212 $n = $this->xMonths[strtolower( $monthName )];
213 return sprintf( '%02d', $n );
214 }
215
216 function makeIsoYear( $year )
217 {
218 # Assumes the year is in a nice format, as enforced by the regex
219 if ( substr( $year, -2 ) == 'BC' ) {
220 $num = IntVal(substr( $year, 0, -3 )) - 1;
221 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
222 $text = sprintf( '-%04d', $num );
223
224 } else {
225 $text = sprintf( '%04d', $year );
226 }
227 return $text;
228 }
229
230 function makeNormalYear( $iso )
231 {
232 if ( $iso{0} == '-' ) {
233 $text = (IntVal( substr( $iso, 1 ) ) - 1) . ' BC';
234 } else {
235 $text = IntVal( $iso );
236 }
237 return $text;
238 }
239 }
240
241 function wfMainDateReplace( $matches )
242 {
243 global $wgDateFormatter;
244 return $wgDateFormatter->replace( $matches );
245 }
246 ?>