5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
25 * Date formatter, recognises dates in plain text and formats them accoding to user preferences.
26 * @todo preferences, OutputPage
31 var $mSource, $mTarget;
32 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
34 var $regexes, $pDays, $pMonths, $pYears;
35 var $rules, $xMonths, $preferences;
53 function __construct() {
56 $this->monthNames
= $this->getMonthRegex();
57 for ( $i=1; $i<=12; $i++
) {
58 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
59 $this->xMonths
[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
62 $this->regexTrail
= '(?![a-z])/iu';
64 # Partial regular expressions
65 $this->prxDM
= '\[\[(\d{1,2})[ _](' . $this->monthNames
. ')\]\]';
66 $this->prxMD
= '\[\[(' . $this->monthNames
. ')[ _](\d{1,2})\]\]';
67 $this->prxY
= '\[\[(\d{1,4}([ _]BC|))\]\]';
68 $this->prxISO1
= '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
69 $this->prxISO2
= '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
71 # Real regular expressions
72 $this->regexes
[self
::DMY
] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
73 $this->regexes
[self
::YDM
] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
74 $this->regexes
[self
::MDY
] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
75 $this->regexes
[self
::YMD
] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
76 $this->regexes
[self
::DM
] = "/{$this->prxDM}{$this->regexTrail}";
77 $this->regexes
[self
::MD
] = "/{$this->prxMD}{$this->regexTrail}";
78 $this->regexes
[self
::ISO1
] = "/{$this->prxISO1}{$this->regexTrail}";
79 $this->regexes
[self
::ISO2
] = "/{$this->prxISO2}{$this->regexTrail}";
82 # See the comments in replace() for the meaning of the letters
83 $this->keys
[self
::DMY
] = 'jFY';
84 $this->keys
[self
::YDM
] = 'Y jF';
85 $this->keys
[self
::MDY
] = 'FjY';
86 $this->keys
[self
::YMD
] = 'Y Fj';
87 $this->keys
[self
::DM
] = 'jF';
88 $this->keys
[self
::MD
] = 'Fj';
89 $this->keys
[self
::ISO1
] = 'ymd'; # y means ISO year
90 $this->keys
[self
::ISO2
] = 'ymd';
93 $this->targets
[self
::DMY
] = '[[F j|j F]] [[Y]]';
94 $this->targets
[self
::YDM
] = '[[Y]], [[F j|j F]]';
95 $this->targets
[self
::MDY
] = '[[F j]], [[Y]]';
96 $this->targets
[self
::YMD
] = '[[Y]] [[F j]]';
97 $this->targets
[self
::DM
] = '[[F j|j F]]';
98 $this->targets
[self
::MD
] = '[[F j]]';
99 $this->targets
[self
::ISO1
] = '[[Y|y]]-[[F j|m-d]]';
100 $this->targets
[self
::ISO2
] = '[[y-m-d]]';
104 $this->rules
[self
::DMY
][self
::MD
] = self
::DM
;
105 $this->rules
[self
::ALL
][self
::MD
] = self
::MD
;
106 $this->rules
[self
::MDY
][self
::DM
] = self
::MD
;
107 $this->rules
[self
::ALL
][self
::DM
] = self
::DM
;
108 $this->rules
[self
::NONE
][self
::ISO2
] = self
::ISO1
;
110 $this->preferences
= array(
111 'default' => self
::NONE
,
115 'ISO 8601' => self
::ISO1
,
120 * Get a DateFormatter object
122 * @return DateFormatter object
124 public static function &getInstance() {
126 static $dateFormatter = false;
127 if ( !$dateFormatter ) {
128 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
129 if ( !$dateFormatter ) {
130 $dateFormatter = new DateFormatter
;
131 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
134 return $dateFormatter;
138 * @param $preference String: User preference
139 * @param $text String: Text to reformat
140 * @param $options Array: can contain 'linked' and/or 'match-whole'
141 * @return mixed|String
143 function reformat( $preference, $text, $options = array('linked') ) {
145 $linked = in_array( 'linked', $options );
146 $match_whole = in_array( 'match-whole', $options );
148 if ( isset( $this->preferences
[$preference] ) ) {
149 $preference = $this->preferences
[$preference];
151 $preference = self
::NONE
;
153 for ( $i=1; $i<=self
::LAST
; $i++
) {
155 if ( isset ( $this->rules
[$preference][$i] ) ) {
157 $this->mTarget
= $this->rules
[$preference][$i];
158 } elseif ( isset ( $this->rules
[self
::ALL
][$i] ) ) {
160 $this->mTarget
= $this->rules
[self
::ALL
][$i];
161 } elseif ( $preference ) {
163 $this->mTarget
= $preference;
168 $regex = $this->regexes
[$i];
172 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
176 // Let's hope this works
177 $regex = preg_replace( '!^/!', '/^', $regex );
178 $regex = str_replace( $this->regexTrail
,
179 '$'.$this->regexTrail
, $regex );
182 // Another horrible hack
183 $this->mLinked
= $linked;
184 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
185 unset($this->mLinked
);
194 function replace( $matches ) {
195 # Extract information from $matches
197 if ( isset( $this->mLinked
) )
198 $linked = $this->mLinked
;
201 $key = $this->keys
[$this->mSource
];
202 for ( $p=0; $p < strlen($key); $p++
) {
203 if ( $key[$p] != ' ' ) {
204 $bits[$key[$p]] = $matches[$p+
1];
208 return $this->formatDate( $bits, $linked );
211 function formatDate( $bits, $link = true ) {
212 $format = $this->targets
[$this->mTarget
];
216 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
217 // strip remaining links
218 $format = str_replace( array( '[[', ']]' ), '', $format );
225 // Pre-generate y/Y stuff because we need the year for the <span> title.
226 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
227 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
228 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
229 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
231 if ( !isset( $bits['m'] ) ) {
232 $m = $this->makeIsoMonth( $bits['F'] );
233 if ( !$m ||
$m == '00' ) {
240 if ( !isset($bits['d']) ) {
241 $bits['d'] = sprintf( '%02d', $bits['j'] );
244 for ( $p=0; $p < strlen( $format ); $p++
) {
247 case 'd': # ISO day of month
250 case 'm': # ISO month
256 case 'j': # ordinary day of month
257 if ( !isset($bits['j']) ) {
258 $text .= intval( $bits['d'] );
263 case 'F': # long month
264 if ( !isset( $bits['F'] ) ) {
265 $m = intval($bits['m']);
266 if ( $m > 12 ||
$m < 1 ) {
270 $text .= $wgContLang->getMonthName( $m );
273 $text .= ucfirst( $bits['F'] );
276 case 'Y': # ordinary (optional BC) year
288 if ( isset($bits['y']) )
289 $isoBits[] = $bits['y'];
290 $isoBits[] = $bits['m'];
291 $isoBits[] = $bits['d'];
292 $isoDate = implode( '-', $isoBits );
294 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
295 $text = Html
::rawElement( 'span',
296 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
305 function getMonthRegex() {
308 for( $i = 1; $i <= 12; $i++
) {
309 $names[] = $wgContLang->getMonthName( $i );
310 $names[] = $wgContLang->getMonthAbbreviation( $i );
312 return implode( '|', $names );
316 * Makes an ISO month, e.g. 02, from a month name
317 * @param $monthName String: month name
318 * @return string ISO month name
320 function makeIsoMonth( $monthName ) {
323 $n = $this->xMonths
[$wgContLang->lc( $monthName )];
324 return sprintf( '%02d', $n );
329 * @param $year String: Year name
330 * @return string ISO year name
332 function makeIsoYear( $year ) {
333 # Assumes the year is in a nice format, as enforced by the regex
334 if ( substr( $year, -2 ) == 'BC' ) {
335 $num = intval(substr( $year, 0, -3 )) - 1;
336 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
337 $text = sprintf( '-%04d', $num );
340 $text = sprintf( '%04d', $year );
349 function makeNormalYear( $iso ) {
350 if ( $iso[0] == '-' ) {
351 $text = (intval( substr( $iso, 1 ) ) +
1) . ' BC';
353 $text = intval( $iso );