9 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
10 * @todo preferences, OutputPage
15 var $mSource, $mTarget;
16 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
18 var $regexes, $pDays, $pMonths, $pYears;
19 var $rules, $xMonths, $preferences;
37 function __construct() {
40 $this->monthNames
= $this->getMonthRegex();
41 for ( $i=1; $i<=12; $i++
) {
42 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
43 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
46 $this->regexTrail
= '(?![a-z])/iu';
48 # Partial regular expressions
49 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')\]\]';
50 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})\]\]';
51 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))\]\]';
52 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
53 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
55 # Real regular expressions
56 $this->regexes
[self
::DMY
] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
57 $this->regexes
[self
::YDM
] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
58 $this->regexes
[self
::MDY
] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
59 $this->regexes
[self
::YMD
] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
60 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
61 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
62 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
63 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
66 # See the comments in replace() for the meaning of the letters
67 $this->keys
[self
::DMY
] = 'jFY';
68 $this->keys
[self
::YDM
] = 'Y jF';
69 $this->keys
[self
::MDY
] = 'FjY';
70 $this->keys
[self
::YMD
] = 'Y Fj';
71 $this->keys
[self
::DM
] = 'jF';
72 $this->keys
[self
::MD
] = 'Fj';
73 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
74 $this->keys
[self
::ISO2
] = 'ymd';
77 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
78 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
79 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
80 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
81 $this->targets
[self
::DM
] = '[[F j|j F]]';
82 $this->targets
[self
::MD
] = '[[F j]]';
83 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
84 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
88 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
89 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
90 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
91 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
92 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
94 $this->preferences
= array(
95 'default' => self
::NONE
,
99 'ISO 8601' => self
::ISO1
,
104 * Get a DateFormatter object
106 * @return DateFormatter object
108 public static function &getInstance() {
110 static $dateFormatter = false;
111 if ( !$dateFormatter ) {
112 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
113 if ( !$dateFormatter ) {
114 $dateFormatter = new DateFormatter
;
115 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
118 return $dateFormatter;
122 * @param $preference String: User preference
123 * @param $text String: Text to reformat
124 * @param $options Array: can contain 'linked' and/or 'match-whole'
125 * @return mixed|String
127 function reformat( $preference, $text, $options = array('linked') ) {
129 $linked = in_array( 'linked', $options );
130 $match_whole = in_array( 'match-whole', $options );
132 if ( isset( $this->preferences
[$preference] ) ) {
133 $preference = $this->preferences
[$preference];
135 $preference = self
::NONE
;
137 for ( $i=1; $i<=self
::LAST
; $i++
) {
139 if ( isset ( $this->rules
[$preference][$i] ) ) {
141 $this->mTarget
= $this->rules
[$preference][$i];
142 } elseif ( isset ( $this->rules
[self
::ALL
][$i] ) ) {
144 $this->mTarget
= $this->rules
[self
::ALL
][$i];
145 } elseif ( $preference ) {
147 $this->mTarget
= $preference;
152 $regex = $this->regexes
[$i];
156 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
160 // Let's hope this works
161 $regex = preg_replace( '!^/!', '/^', $regex );
162 $regex = str_replace( $this->regexTrail
,
163 '$'.$this->regexTrail
, $regex );
166 // Another horrible hack
167 $this->mLinked
= $linked;
168 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
169 unset($this->mLinked
);
178 function replace( $matches ) {
179 # Extract information from $matches
181 if ( isset( $this->mLinked
) )
182 $linked = $this->mLinked
;
185 $key = $this->keys
[$this->mSource
];
186 for ( $p=0; $p < strlen($key); $p++
) {
187 if ( $key[$p] != ' ' ) {
188 $bits[$key[$p]] = $matches[$p+
1];
192 return $this->formatDate( $bits, $linked );
195 function formatDate( $bits, $link = true ) {
196 $format = $this->targets
[$this->mTarget
];
200 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
201 // strip remaining links
202 $format = str_replace( array( '[[', ']]' ), '', $format );
209 // Pre-generate y/Y stuff because we need the year for the <span> title.
210 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
211 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
212 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
213 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
215 if ( !isset( $bits['m'] ) ) {
216 $m = $this->makeIsoMonth( $bits['F'] );
217 if ( !$m ||
$m == '00' ) {
224 if ( !isset($bits['d']) ) {
225 $bits['d'] = sprintf( '%02d', $bits['j'] );
228 for ( $p=0; $p < strlen( $format ); $p++
) {
231 case 'd': # ISO day of month
234 case 'm': # ISO month
240 case 'j': # ordinary day of month
241 if ( !isset($bits['j']) ) {
242 $text .= intval( $bits['d'] );
247 case 'F': # long month
248 if ( !isset( $bits['F'] ) ) {
249 $m = intval($bits['m']);
250 if ( $m > 12 ||
$m < 1 ) {
254 $text .= $wgContLang->getMonthName( $m );
257 $text .= ucfirst( $bits['F'] );
260 case 'Y': # ordinary (optional BC) year
272 if ( isset($bits['y']) )
273 $isoBits[] = $bits['y'];
274 $isoBits[] = $bits['m'];
275 $isoBits[] = $bits['d'];
276 $isoDate = implode( '-', $isoBits );
278 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
279 $text = Html
::rawElement( 'span',
280 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
289 function getMonthRegex() {
292 for( $i = 1; $i <= 12; $i++
) {
293 $names[] = $wgContLang->getMonthName( $i );
294 $names[] = $wgContLang->getMonthAbbreviation( $i );
296 return implode( '|', $names );
300 * Makes an ISO month, e.g. 02, from a month name
301 * @param $monthName String: month name
302 * @return string ISO month name
304 function makeIsoMonth( $monthName ) {
307 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
308 return sprintf( '%02d', $n );
313 * @param $year String: Year name
314 * @return string ISO year name
316 function makeIsoYear( $year ) {
317 # Assumes the year is in a nice format, as enforced by the regex
318 if ( substr( $year, -2 ) == 'BC' ) {
319 $num = intval(substr( $year, 0, -3 )) - 1;
320 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
321 $text = sprintf( '-%04d', $num );
324 $text = sprintf( '%04d', $year );
333 function makeNormalYear( $iso ) {
334 if ( $iso[0] == '-' ) {
335 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
337 $text = intval( $iso );