3 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
9 * @todo preferences, OutputPage
14 var $mSource, $mTarget;
15 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
17 var $regexes, $pDays, $pMonths, $pYears;
18 var $rules, $xMonths, $preferences;
36 function DateFormatter() {
39 $this->monthNames
= $this->getMonthRegex();
40 for ( $i=1; $i<=12; $i++
) {
41 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
42 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
45 $this->regexTrail
= '(?![a-z])/iu';
47 # Partial regular expressions
48 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
49 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
50 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
51 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
52 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
54 # Real regular expressions
55 $this->regexes
[self
::DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
56 $this->regexes
[self
::YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
57 $this->regexes
[self
::MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
58 $this->regexes
[self
::YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
59 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
60 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
61 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
62 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
65 # See the comments in replace() for the meaning of the letters
66 $this->keys
[self
::DMY
] = 'jFY';
67 $this->keys
[self
::YDM
] = 'Y jF';
68 $this->keys
[self
::MDY
] = 'FjY';
69 $this->keys
[self
::YMD
] = 'Y Fj';
70 $this->keys
[self
::DM
] = 'jF';
71 $this->keys
[self
::MD
] = 'Fj';
72 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
73 $this->keys
[self
::ISO2
] = 'ymd';
76 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
77 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
78 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
79 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
80 $this->targets
[self
::DM
] = '[[F j|j F]]';
81 $this->targets
[self
::MD
] = '[[F j]]';
82 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
83 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
87 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
88 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
89 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
90 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
91 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
93 $this->preferences
= array(
94 'default' => self
::NONE
,
98 'ISO 8601' => self
::ISO1
,
105 function &getInstance() {
107 static $dateFormatter = false;
108 if ( !$dateFormatter ) {
109 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
110 if ( !$dateFormatter ) {
111 $dateFormatter = new DateFormatter
;
112 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
115 return $dateFormatter;
119 * @param string $preference User preference
120 * @param string $text Text to reformat
122 function reformat( $preference, $text ) {
123 if ( isset( $this->preferences
[$preference] ) ) {
124 $preference = $this->preferences
[$preference];
126 $preference = self
::NONE
;
128 for ( $i=1; $i<=self
::LAST
; $i++
) {
130 if ( isset ( $this->rules
[$preference][$i] ) ) {
132 $this->mTarget
= $this->rules
[$preference][$i];
133 } elseif ( isset ( $this->rules
[self
::ALL
][$i] ) ) {
135 $this->mTarget
= $this->rules
[self
::ALL
][$i];
136 } elseif ( $preference ) {
138 $this->mTarget
= $preference;
143 $text = preg_replace_callback( $this->regexes
[$i], array( &$this, 'replace' ), $text );
151 function replace( $matches ) {
152 # Extract information from $matches
154 $key = $this->keys
[$this->mSource
];
155 for ( $p=0; $p < strlen($key); $p++
) {
156 if ( $key{$p} != ' ' ) {
157 $bits[$key{$p}] = $matches[$p+
1];
161 $format = $this->targets
[$this->mTarget
];
167 for ( $p=0; $p < strlen( $format ); $p++
) {
170 case 'd': # ISO day of month
171 if ( !isset($bits['d']) ) {
172 $text .= sprintf( '%02d', $bits['j'] );
177 case 'm': # ISO month
178 if ( !isset($bits['m']) ) {
179 $m = $this->makeIsoMonth( $bits['F'] );
180 if ( !$m ||
$m == '00' ) {
190 if ( !isset( $bits['y'] ) ) {
191 $text .= $this->makeIsoYear( $bits['Y'] );
196 case 'j': # ordinary day of month
197 if ( !isset($bits['j']) ) {
198 $text .= intval( $bits['d'] );
203 case 'F': # long month
204 if ( !isset( $bits['F'] ) ) {
205 $m = intval($bits['m']);
206 if ( $m > 12 ||
$m < 1 ) {
210 $text .= $wgContLang->getMonthName( $m );
213 $text .= ucfirst( $bits['F'] );
216 case 'Y': # ordinary (optional BC) year
217 if ( !isset( $bits['Y'] ) ) {
218 $text .= $this->makeNormalYear( $bits['y'] );
236 function getMonthRegex() {
239 for( $i = 1; $i <= 12; $i++
) {
240 $names[] = $wgContLang->getMonthName( $i );
241 $names[] = $wgContLang->getMonthAbbreviation( $i );
243 return implode( '|', $names );
247 * Makes an ISO month, e.g. 02, from a month name
248 * @param $monthName String: month name
249 * @return string ISO month name
251 function makeIsoMonth( $monthName ) {
254 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
255 return sprintf( '%02d', $n );
260 * @param $year String: Year name
261 * @return string ISO year name
263 function makeIsoYear( $year ) {
264 # Assumes the year is in a nice format, as enforced by the regex
265 if ( substr( $year, -2 ) == 'BC' ) {
266 $num = intval(substr( $year, 0, -3 )) - 1;
267 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
268 $text = sprintf( '-%04d', $num );
271 $text = sprintf( '%04d', $year );
279 function makeNormalYear( $iso ) {
280 if ( $iso{0} == '-' ) {
281 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
283 $text = intval( $iso );