c795618a774301396d4f78c3aa675a2ed1faa06e
3 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
10 * @todo preferences, OutputPage
16 var $mSource, $mTarget;
17 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
19 var $regexes, $pDays, $pMonths, $pYears;
20 var $rules, $xMonths, $preferences;
38 function DateFormatter() {
41 $this->monthNames
= $this->getMonthRegex();
42 for ( $i=1; $i<=12; $i++
) {
43 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
44 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
47 $this->regexTrail
= '(?![a-z])/iu';
49 # Partial regular expressions
50 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')]]';
51 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})]]';
52 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))]]';
53 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})]]';
54 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})]]';
56 # Real regular expressions
57 $this->regexes
[self
::DMY
] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
58 $this->regexes
[self
::YDM
] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
59 $this->regexes
[self
::MDY
] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
60 $this->regexes
[self
::YMD
] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
61 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
62 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
63 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
64 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
67 # See the comments in replace() for the meaning of the letters
68 $this->keys
[self
::DMY
] = 'jFY';
69 $this->keys
[self
::YDM
] = 'Y jF';
70 $this->keys
[self
::MDY
] = 'FjY';
71 $this->keys
[self
::YMD
] = 'Y Fj';
72 $this->keys
[self
::DM
] = 'jF';
73 $this->keys
[self
::MD
] = 'Fj';
74 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
75 $this->keys
[self
::ISO2
] = 'ymd';
78 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
79 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
80 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
81 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
82 $this->targets
[self
::DM
] = '[[F j|j F]]';
83 $this->targets
[self
::MD
] = '[[F j]]';
84 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
85 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
89 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
90 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
91 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
92 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
93 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
95 $this->preferences
= array(
96 'default' => self
::NONE
,
100 'ISO 8601' => self
::ISO1
,
107 function &getInstance() {
109 static $dateFormatter = false;
110 if ( !$dateFormatter ) {
111 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
112 if ( !$dateFormatter ) {
113 $dateFormatter = new DateFormatter
;
114 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
117 return $dateFormatter;
121 * @param string $preference User preference
122 * @param string $text Text to reformat
124 function reformat( $preference, $text ) {
125 if ( isset( $this->preferences
[$preference] ) ) {
126 $preference = $this->preferences
[$preference];
128 $preference = self
::NONE
;
130 for ( $i=1; $i<=self
::LAST
; $i++
) {
132 if ( isset ( $this->rules
[$preference][$i] ) ) {
134 $this->mTarget
= $this->rules
[$preference][$i];
135 } elseif ( isset ( $this->rules
[self
::ALL
][$i] ) ) {
137 $this->mTarget
= $this->rules
[self
::ALL
][$i];
138 } elseif ( $preference ) {
140 $this->mTarget
= $preference;
145 $text = preg_replace_callback( $this->regexes
[$i], array( &$this, 'replace' ), $text );
153 function replace( $matches ) {
154 # Extract information from $matches
156 $key = $this->keys
[$this->mSource
];
157 for ( $p=0; $p < strlen($key); $p++
) {
158 if ( $key{$p} != ' ' ) {
159 $bits[$key{$p}] = $matches[$p+
1];
163 $format = $this->targets
[$this->mTarget
];
169 for ( $p=0; $p < strlen( $format ); $p++
) {
172 case 'd': # ISO day of month
173 if ( !isset($bits['d']) ) {
174 $text .= sprintf( '%02d', $bits['j'] );
179 case 'm': # ISO month
180 if ( !isset($bits['m']) ) {
181 $m = $this->makeIsoMonth( $bits['F'] );
182 if ( !$m ||
$m == '00' ) {
192 if ( !isset( $bits['y'] ) ) {
193 $text .= $this->makeIsoYear( $bits['Y'] );
198 case 'j': # ordinary day of month
199 if ( !isset($bits['j']) ) {
200 $text .= intval( $bits['d'] );
205 case 'F': # long month
206 if ( !isset( $bits['F'] ) ) {
207 $m = intval($bits['m']);
208 if ( $m > 12 ||
$m < 1 ) {
212 $text .= $wgContLang->getMonthName( $m );
215 $text .= ucfirst( $bits['F'] );
218 case 'Y': # ordinary (optional BC) year
219 if ( !isset( $bits['Y'] ) ) {
220 $text .= $this->makeNormalYear( $bits['y'] );
238 function getMonthRegex() {
241 for( $i = 1; $i <= 12; $i++
) {
242 $names[] = $wgContLang->getMonthName( $i );
243 $names[] = $wgContLang->getMonthAbbreviation( $i );
245 return implode( '|', $names );
249 * Makes an ISO month, e.g. 02, from a month name
250 * @param $monthName String: month name
251 * @return string ISO month name
253 function makeIsoMonth( $monthName ) {
256 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
257 return sprintf( '%02d', $n );
262 * @param $year String: Year name
263 * @return string ISO year name
265 function makeIsoYear( $year ) {
266 # Assumes the year is in a nice format, as enforced by the regex
267 if ( substr( $year, -2 ) == 'BC' ) {
268 $num = intval(substr( $year, 0, -3 )) - 1;
269 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
270 $text = sprintf( '-%04d', $num );
273 $text = sprintf( '%04d', $year );
281 function makeNormalYear( $iso ) {
282 if ( $iso{0} == '-' ) {
283 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
285 $text = intval( $iso );