Remove yet more stray executable bits. The last, I hope.
[lhc/web/wiklou.git] / includes / DateFormatter.php
1 <?php
2 /**
3 * Contain things
4 * @todo document
5 * @package MediaWiki
6 * @subpackage Parser
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 define('DF_ISO2', 5);
18 define('DF_YDM', 6);
19 define('DF_DM', 7);
20 define('DF_MD', 8);
21 define('DF_LAST', 8);
22
23 /**
24 * @todo preferences, OutputPage
25 * @package MediaWiki
26 * @subpackage Parser
27 */
28 class DateFormatter
29 {
30 var $mSource, $mTarget;
31 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
32
33 var $regexes, $pDays, $pMonths, $pYears;
34 var $rules, $xMonths;
35
36 /**
37 * @todo document
38 */
39 function DateFormatter() {
40 global $wgContLang;
41
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;
46 }
47
48 $this->regexTrail = '(?![a-z])/iu';
49
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})]]';
56
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}";
66
67 # Extraction keys
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';
77
78 # Target date formats
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]]';
87
88 # Rules
89 # pref source target
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;
95 }
96
97 /**
98 * @static
99 */
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 );
108 }
109 }
110 return $dateFormatter;
111 }
112
113 /**
114 * @param $preference
115 * @param $text
116 */
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++ ) {
120 $this->mSource = $i;
121 if ( @$this->rules[$preference][$i] ) {
122 # Specific rules
123 $this->mTarget = $this->rules[$preference][$i];
124 } elseif ( @$this->rules[DF_ALL][$i] ) {
125 # General rules
126 $this->mTarget = $this->rules[DF_ALL][$i];
127 } elseif ( $preference ) {
128 # User preference
129 $this->mTarget = $preference;
130 } else {
131 # Default
132 $this->mTarget = $i;
133 }
134 $text = preg_replace_callback( $this->regexes[$i], 'wfMainDateReplace', $text );
135 }
136 return $text;
137 }
138
139 /**
140 * @param $matches
141 */
142 function replace( $matches ) {
143 # Extract information from $matches
144 $bits = array();
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];
149 }
150 }
151
152 $format = $this->targets[$this->mTarget];
153
154 # Construct new date
155 $text = '';
156 $fail = false;
157
158 for ( $p=0; $p < strlen( $format ); $p++ ) {
159 $char = $format{$p};
160 switch ( $char ) {
161 case 'd': # ISO day of month
162 if ( !isset($bits['d']) ) {
163 $text .= sprintf( '%02d', $bits['j'] );
164 } else {
165 $text .= $bits['d'];
166 }
167 break;
168 case 'm': # ISO month
169 if ( !isset($bits['m']) ) {
170 $m = $this->makeIsoMonth( $bits['F'] );
171 if ( !$m || $m == '00' ) {
172 $fail = true;
173 } else {
174 $text .= $m;
175 }
176 } else {
177 $text .= $bits['m'];
178 }
179 break;
180 case 'y': # ISO year
181 if ( !isset( $bits['y'] ) ) {
182 $text .= $this->makeIsoYear( $bits['Y'] );
183 } else {
184 $text .= $bits['y'];
185 }
186 break;
187 case 'j': # ordinary day of month
188 if ( !isset($bits['j']) ) {
189 $text .= intval( $bits['d'] );
190 } else {
191 $text .= $bits['j'];
192 }
193 break;
194 case 'F': # long month
195 if ( !isset( $bits['F'] ) ) {
196 $m = intval($bits['m']);
197 if ( $m > 12 || $m < 1 ) {
198 $fail = true;
199 } else {
200 global $wgContLang;
201 $text .= $wgContLang->getMonthName( $m );
202 }
203 } else {
204 $text .= ucfirst( $bits['F'] );
205 }
206 break;
207 case 'Y': # ordinary (optional BC) year
208 if ( !isset( $bits['Y'] ) ) {
209 $text .= $this->makeNormalYear( $bits['y'] );
210 } else {
211 $text .= $bits['Y'];
212 }
213 break;
214 default:
215 $text .= $char;
216 }
217 }
218 if ( $fail ) {
219 $text = $matches[0];
220 }
221 return $text;
222 }
223
224 /**
225 * @todo document
226 */
227 function getMonthRegex() {
228 global $wgContLang;
229 $names = array();
230 for( $i = 1; $i <= 12; $i++ ) {
231 $names[] = $wgContLang->getMonthName( $i );
232 $names[] = $wgContLang->getMonthAbbreviation( $i );
233 }
234 return implode( '|', $names );
235 }
236
237 /**
238 * Makes an ISO month, e.g. 02, from a month name
239 * @param string $monthName Month name
240 * @return string ISO month name
241 */
242 function makeIsoMonth( $monthName ) {
243 global $wgContLang;
244
245 $n = $this->xMonths[$wgContLang->lc( $monthName )];
246 return sprintf( '%02d', $n );
247 }
248
249 /**
250 * @todo document
251 * @param string $year Year name
252 * @return string ISO year name
253 */
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 );
260
261 } else {
262 $text = sprintf( '%04d', $year );
263 }
264 return $text;
265 }
266
267 /**
268 * @todo document
269 */
270 function makeNormalYear( $iso ) {
271 if ( $iso{0} == '-' ) {
272 $text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
273 } else {
274 $text = intval( $iso );
275 }
276 return $text;
277 }
278 }
279
280 /**
281 * @todo document
282 */
283 function wfMainDateReplace( $matches ) {
284 $df =& DateFormatter::getInstance();
285 return $df->replace( $matches );
286 }
287
288 ?>