00001 <?php
00002
00008 class DateFormatter
00009 {
00010 var $mSource, $mTarget;
00011 var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
00012
00013 var $regexes, $pDays, $pMonths, $pYears;
00014 var $rules, $xMonths, $preferences;
00015
00016 const ALL = -1;
00017 const NONE = 0;
00018 const MDY = 1;
00019 const DMY = 2;
00020 const YMD = 3;
00021 const ISO1 = 4;
00022 const LASTPREF = 4;
00023 const ISO2 = 5;
00024 const YDM = 6;
00025 const DM = 7;
00026 const MD = 8;
00027 const LAST = 8;
00028
00032 function DateFormatter() {
00033 global $wgContLang;
00034
00035 $this->monthNames = $this->getMonthRegex();
00036 for ( $i=1; $i<=12; $i++ ) {
00037 $this->xMonths[$wgContLang->lc( $wgContLang->getMonthName( $i ) )] = $i;
00038 $this->xMonths[$wgContLang->lc( $wgContLang->getMonthAbbreviation( $i ) )] = $i;
00039 }
00040
00041 $this->regexTrail = '(?![a-z])/iu';
00042
00043 # Partial regular expressions
00044 $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
00045 $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
00046 $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
00047 $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
00048 $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
00049
00050 # Real regular expressions
00051 $this->regexes[self::DMY] = "/{$this->prxDM} *,? *{$this->prxY}{$this->regexTrail}";
00052 $this->regexes[self::YDM] = "/{$this->prxY} *,? *{$this->prxDM}{$this->regexTrail}";
00053 $this->regexes[self::MDY] = "/{$this->prxMD} *,? *{$this->prxY}{$this->regexTrail}";
00054 $this->regexes[self::YMD] = "/{$this->prxY} *,? *{$this->prxMD}{$this->regexTrail}";
00055 $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
00056 $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
00057 $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
00058 $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
00059
00060 # Extraction keys
00061 # See the comments in replace() for the meaning of the letters
00062 $this->keys[self::DMY] = 'jFY';
00063 $this->keys[self::YDM] = 'Y jF';
00064 $this->keys[self::MDY] = 'FjY';
00065 $this->keys[self::YMD] = 'Y Fj';
00066 $this->keys[self::DM] = 'jF';
00067 $this->keys[self::MD] = 'Fj';
00068 $this->keys[self::ISO1] = 'ymd'; # y means ISO year
00069 $this->keys[self::ISO2] = 'ymd';
00070
00071 # Target date formats
00072 $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
00073 $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
00074 $this->targets[self::MDY] = '[[F j]], [[Y]]';
00075 $this->targets[self::YMD] = '[[Y]] [[F j]]';
00076 $this->targets[self::DM] = '[[F j|j F]]';
00077 $this->targets[self::MD] = '[[F j]]';
00078 $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
00079 $this->targets[self::ISO2] = '[[y-m-d]]';
00080
00081 # Rules
00082 # pref source target
00083 $this->rules[self::DMY][self::MD] = self::DM;
00084 $this->rules[self::ALL][self::MD] = self::MD;
00085 $this->rules[self::MDY][self::DM] = self::MD;
00086 $this->rules[self::ALL][self::DM] = self::DM;
00087 $this->rules[self::NONE][self::ISO2] = self::ISO1;
00088
00089 $this->preferences = array(
00090 'default' => self::NONE,
00091 'dmy' => self::DMY,
00092 'mdy' => self::MDY,
00093 'ymd' => self::YMD,
00094 'ISO 8601' => self::ISO1,
00095 );
00096 }
00097
00103 public static function &getInstance() {
00104 global $wgMemc;
00105 static $dateFormatter = false;
00106 if ( !$dateFormatter ) {
00107 $dateFormatter = $wgMemc->get( wfMemcKey( 'dateformatter' ) );
00108 if ( !$dateFormatter ) {
00109 $dateFormatter = new DateFormatter;
00110 $wgMemc->set( wfMemcKey( 'dateformatter' ), $dateFormatter, 3600 );
00111 }
00112 }
00113 return $dateFormatter;
00114 }
00115
00120 function reformat( $preference, $text, $options = array('linked') ) {
00121
00122 $linked = in_array( 'linked', $options );
00123 $match_whole = in_array( 'match-whole', $options );
00124
00125 if ( isset( $this->preferences[$preference] ) ) {
00126 $preference = $this->preferences[$preference];
00127 } else {
00128 $preference = self::NONE;
00129 }
00130 for ( $i=1; $i<=self::LAST; $i++ ) {
00131 $this->mSource = $i;
00132 if ( isset ( $this->rules[$preference][$i] ) ) {
00133 # Specific rules
00134 $this->mTarget = $this->rules[$preference][$i];
00135 } elseif ( isset ( $this->rules[self::ALL][$i] ) ) {
00136 # General rules
00137 $this->mTarget = $this->rules[self::ALL][$i];
00138 } elseif ( $preference ) {
00139 # User preference
00140 $this->mTarget = $preference;
00141 } else {
00142 # Default
00143 $this->mTarget = $i;
00144 }
00145 $regex = $this->regexes[$i];
00146
00147
00148 if (!$linked) {
00149 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
00150 }
00151
00152 if ($match_whole) {
00153
00154 $regex = preg_replace( '!^/!', '/^', $regex );
00155 $regex = str_replace( $this->regexTrail,
00156 '$'.$this->regexTrail, $regex );
00157 }
00158
00159
00160 $this->mLinked = $linked;
00161 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
00162 unset($this->mLinked);
00163 }
00164 return $text;
00165 }
00166
00170 function replace( $matches ) {
00171 # Extract information from $matches
00172 $linked = true;
00173 if ( isset( $this->mLinked ) )
00174 $linked = $this->mLinked;
00175
00176 $bits = array();
00177 $key = $this->keys[$this->mSource];
00178 for ( $p=0; $p < strlen($key); $p++ ) {
00179 if ( $key{$p} != ' ' ) {
00180 $bits[$key{$p}] = $matches[$p+1];
00181 }
00182 }
00183
00184 return $this->formatDate( $bits, $linked );
00185 }
00186
00187 function formatDate( $bits, $link = true ) {
00188 $format = $this->targets[$this->mTarget];
00189
00190 if (!$link) {
00191
00192 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
00193
00194 $format = str_replace( array( '[[', ']]' ), '', $format );
00195 }
00196
00197 # Construct new date
00198 $text = '';
00199 $fail = false;
00200
00201
00202 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) )
00203 $bits['y'] = $this->makeIsoYear( $bits['Y'] );
00204 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) )
00205 $bits['Y'] = $this->makeNormalYear( $bits['y'] );
00206
00207 if ( !isset( $bits['m'] ) ) {
00208 $m = $this->makeIsoMonth( $bits['F'] );
00209 if ( !$m || $m == '00' ) {
00210 $fail = true;
00211 } else {
00212 $bits['m'] = $m;
00213 }
00214 }
00215
00216 if ( !isset($bits['d']) ) {
00217 $bits['d'] = sprintf( '%02d', $bits['j'] );
00218 }
00219
00220 for ( $p=0; $p < strlen( $format ); $p++ ) {
00221 $char = $format{$p};
00222 switch ( $char ) {
00223 case 'd': # ISO day of month
00224 $text .= $bits['d'];
00225 break;
00226 case 'm': # ISO month
00227 $text .= $bits['m'];
00228 break;
00229 case 'y': # ISO year
00230 $text .= $bits['y'];
00231 break;
00232 case 'j': # ordinary day of month
00233 if ( !isset($bits['j']) ) {
00234 $text .= intval( $bits['d'] );
00235 } else {
00236 $text .= $bits['j'];
00237 }
00238 break;
00239 case 'F': # long month
00240 if ( !isset( $bits['F'] ) ) {
00241 $m = intval($bits['m']);
00242 if ( $m > 12 || $m < 1 ) {
00243 $fail = true;
00244 } else {
00245 global $wgContLang;
00246 $text .= $wgContLang->getMonthName( $m );
00247 }
00248 } else {
00249 $text .= ucfirst( $bits['F'] );
00250 }
00251 break;
00252 case 'Y': # ordinary (optional BC) year
00253 $text .= $bits['Y'];
00254 break;
00255 default:
00256 $text .= $char;
00257 }
00258 }
00259 if ( $fail ) {
00260 $text = $matches[0];
00261 }
00262
00263 $isoBits = array();
00264 if ( isset($bits['y']) )
00265 $isoBits[] = $bits['y'];
00266 $isoBits[] = $bits['m'];
00267 $isoBits[] = $bits['d'];
00268 $isoDate = implode( '-', $isoBits );;
00269
00270
00271 $text = Xml::tags( 'span',
00272 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
00273
00274 return $text;
00275 }
00276
00280 function getMonthRegex() {
00281 global $wgContLang;
00282 $names = array();
00283 for( $i = 1; $i <= 12; $i++ ) {
00284 $names[] = $wgContLang->getMonthName( $i );
00285 $names[] = $wgContLang->getMonthAbbreviation( $i );
00286 }
00287 return implode( '|', $names );
00288 }
00289
00295 function makeIsoMonth( $monthName ) {
00296 global $wgContLang;
00297
00298 $n = $this->xMonths[$wgContLang->lc( $monthName )];
00299 return sprintf( '%02d', $n );
00300 }
00301
00307 function makeIsoYear( $year ) {
00308 # Assumes the year is in a nice format, as enforced by the regex
00309 if ( substr( $year, -2 ) == 'BC' ) {
00310 $num = intval(substr( $year, 0, -3 )) - 1;
00311 # PHP bug note: sprintf( "%04d", -1 ) fails poorly
00312 $text = sprintf( '-%04d', $num );
00313
00314 } else {
00315 $text = sprintf( '%04d', $year );
00316 }
00317 return $text;
00318 }
00319
00323 function makeNormalYear( $iso ) {
00324 if ( $iso{0} == '-' ) {
00325 $text = (intval( substr( $iso, 1 ) ) + 1) . ' BC';
00326 } else {
00327 $text = intval( $iso );
00328 }
00329 return $text;
00330 }
00331 }