MediaWiki  REL1_22
DateFormatter.php
Go to the documentation of this file.
00001 <?php
00029 class DateFormatter {
00030     var $mSource, $mTarget;
00031     var $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
00032 
00033     var $regexes, $pDays, $pMonths, $pYears;
00034     var $rules, $xMonths, $preferences;
00035 
00036     protected $lang;
00037 
00038     const ALL = -1;
00039     const NONE = 0;
00040     const MDY = 1;
00041     const DMY = 2;
00042     const YMD = 3;
00043     const ISO1 = 4;
00044     const LASTPREF = 4;
00045     const ISO2 = 5;
00046     const YDM = 6;
00047     const DM = 7;
00048     const MD = 8;
00049     const LAST = 8;
00050 
00054     function __construct( Language $lang ) {
00055         $this->lang = $lang;
00056 
00057         $this->monthNames = $this->getMonthRegex();
00058         for ( $i = 1; $i <= 12; $i++ ) {
00059             $this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
00060             $this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
00061         }
00062 
00063         $this->regexTrail = '(?![a-z])/iu';
00064 
00065         # Partial regular expressions
00066         $this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
00067         $this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
00068         $this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
00069         $this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
00070         $this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
00071 
00072         # Real regular expressions
00073         $this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
00074         $this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
00075         $this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
00076         $this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
00077         $this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
00078         $this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
00079         $this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
00080         $this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
00081 
00082         # Extraction keys
00083         # See the comments in replace() for the meaning of the letters
00084         $this->keys[self::DMY] = 'jFY';
00085         $this->keys[self::YDM] = 'Y jF';
00086         $this->keys[self::MDY] = 'FjY';
00087         $this->keys[self::YMD] = 'Y Fj';
00088         $this->keys[self::DM] = 'jF';
00089         $this->keys[self::MD] = 'Fj';
00090         $this->keys[self::ISO1] = 'ymd'; # y means ISO year
00091         $this->keys[self::ISO2] = 'ymd';
00092 
00093         # Target date formats
00094         $this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
00095         $this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
00096         $this->targets[self::MDY] = '[[F j]], [[Y]]';
00097         $this->targets[self::YMD] = '[[Y]] [[F j]]';
00098         $this->targets[self::DM] = '[[F j|j F]]';
00099         $this->targets[self::MD] = '[[F j]]';
00100         $this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
00101         $this->targets[self::ISO2] = '[[y-m-d]]';
00102 
00103         # Rules
00104         #            pref    source       target
00105         $this->rules[self::DMY][self::MD] = self::DM;
00106         $this->rules[self::ALL][self::MD] = self::MD;
00107         $this->rules[self::MDY][self::DM] = self::MD;
00108         $this->rules[self::ALL][self::DM] = self::DM;
00109         $this->rules[self::NONE][self::ISO2] = self::ISO1;
00110 
00111         $this->preferences = array(
00112             'default' => self::NONE,
00113             'dmy' => self::DMY,
00114             'mdy' => self::MDY,
00115             'ymd' => self::YMD,
00116             'ISO 8601' => self::ISO1,
00117         );
00118     }
00119 
00127     public static function &getInstance( $lang = null ) {
00128         global $wgMemc, $wgContLang;
00129         static $dateFormatter = false;
00130         $lang = $lang ? wfGetLangObj( $lang ) : $wgContLang;
00131         $key = wfMemcKey( 'dateformatter', $lang->getCode() );
00132         if ( !$dateFormatter ) {
00133             $dateFormatter = $wgMemc->get( $key );
00134             if ( !$dateFormatter ) {
00135                 $dateFormatter = new DateFormatter( $lang );
00136                 $wgMemc->set( $key, $dateFormatter, 3600 );
00137             }
00138         }
00139         return $dateFormatter;
00140     }
00141 
00148     function reformat( $preference, $text, $options = array( 'linked' ) ) {
00149         $linked = in_array( 'linked', $options );
00150         $match_whole = in_array( 'match-whole', $options );
00151 
00152         if ( isset( $this->preferences[$preference] ) ) {
00153             $preference = $this->preferences[$preference];
00154         } else {
00155             $preference = self::NONE;
00156         }
00157         for ( $i = 1; $i <= self::LAST; $i++ ) {
00158             $this->mSource = $i;
00159             if ( isset( $this->rules[$preference][$i] ) ) {
00160                 # Specific rules
00161                 $this->mTarget = $this->rules[$preference][$i];
00162             } elseif ( isset( $this->rules[self::ALL][$i] ) ) {
00163                 # General rules
00164                 $this->mTarget = $this->rules[self::ALL][$i];
00165             } elseif ( $preference ) {
00166                 # User preference
00167                 $this->mTarget = $preference;
00168             } else {
00169                 # Default
00170                 $this->mTarget = $i;
00171             }
00172             $regex = $this->regexes[$i];
00173 
00174             // Horrible hack
00175             if ( !$linked ) {
00176                 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex );
00177             }
00178 
00179             if ( $match_whole ) {
00180                 // Let's hope this works
00181                 $regex = preg_replace( '!^/!', '/^', $regex );
00182                 $regex = str_replace( $this->regexTrail,
00183                     '$' . $this->regexTrail, $regex );
00184             }
00185 
00186             // Another horrible hack
00187             $this->mLinked = $linked;
00188             $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text );
00189             unset( $this->mLinked );
00190         }
00191         return $text;
00192     }
00193 
00198     function replace( $matches ) {
00199         # Extract information from $matches
00200         $linked = true;
00201         if ( isset( $this->mLinked ) ) {
00202             $linked = $this->mLinked;
00203         }
00204 
00205         $bits = array();
00206         $key = $this->keys[$this->mSource];
00207         for ( $p = 0; $p < strlen( $key ); $p++ ) {
00208             if ( $key[$p] != ' ' ) {
00209                 $bits[$key[$p]] = $matches[$p + 1];
00210             }
00211         }
00212 
00213         return $this->formatDate( $bits, $linked );
00214     }
00215 
00221     function formatDate( $bits, $link = true ) {
00222         $format = $this->targets[$this->mTarget];
00223 
00224         if ( !$link ) {
00225             // strip piped links
00226             $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
00227             // strip remaining links
00228             $format = str_replace( array( '[[', ']]' ), '', $format );
00229         }
00230 
00231         # Construct new date
00232         $text = '';
00233         $fail = false;
00234 
00235         // Pre-generate y/Y stuff because we need the year for the <span> title.
00236         if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
00237             $bits['y'] = $this->makeIsoYear( $bits['Y'] );
00238         }
00239         if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
00240             $bits['Y'] = $this->makeNormalYear( $bits['y'] );
00241         }
00242 
00243         if ( !isset( $bits['m'] ) ) {
00244             $m = $this->makeIsoMonth( $bits['F'] );
00245             if ( !$m || $m == '00' ) {
00246                 $fail = true;
00247             } else {
00248                 $bits['m'] = $m;
00249             }
00250         }
00251 
00252         if ( !isset( $bits['d'] ) ) {
00253             $bits['d'] = sprintf( '%02d', $bits['j'] );
00254         }
00255 
00256         for ( $p = 0; $p < strlen( $format ); $p++ ) {
00257             $char = $format[$p];
00258             switch ( $char ) {
00259                 case 'd': # ISO day of month
00260                     $text .= $bits['d'];
00261                     break;
00262                 case 'm': # ISO month
00263                     $text .= $bits['m'];
00264                     break;
00265                 case 'y': # ISO year
00266                     $text .= $bits['y'];
00267                     break;
00268                 case 'j': # ordinary day of month
00269                     if ( !isset( $bits['j'] ) ) {
00270                         $text .= intval( $bits['d'] );
00271                     } else {
00272                         $text .= $bits['j'];
00273                     }
00274                     break;
00275                 case 'F': # long month
00276                     if ( !isset( $bits['F'] ) ) {
00277                         $m = intval( $bits['m'] );
00278                         if ( $m > 12 || $m < 1 ) {
00279                             $fail = true;
00280                         } else {
00281                             $text .= $this->lang->getMonthName( $m );
00282                         }
00283                     } else {
00284                         $text .= ucfirst( $bits['F'] );
00285                     }
00286                     break;
00287                 case 'Y': # ordinary (optional BC) year
00288                     $text .= $bits['Y'];
00289                     break;
00290                 default:
00291                     $text .= $char;
00292             }
00293         }
00294         if ( $fail ) {
00295             $text = $matches[0];
00296         }
00297 
00298         $isoBits = array();
00299         if ( isset( $bits['y'] ) ) {
00300             $isoBits[] = $bits['y'];
00301         }
00302         $isoBits[] = $bits['m'];
00303         $isoBits[] = $bits['d'];
00304         $isoDate = implode( '-', $isoBits );
00305 
00306         // Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
00307         $text = Html::rawElement( 'span',
00308                     array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text );
00309 
00310         return $text;
00311     }
00312 
00317     function getMonthRegex() {
00318         $names = array();
00319         for ( $i = 1; $i <= 12; $i++ ) {
00320             $names[] = $this->lang->getMonthName( $i );
00321             $names[] = $this->lang->getMonthAbbreviation( $i );
00322         }
00323         return implode( '|', $names );
00324     }
00325 
00331     function makeIsoMonth( $monthName ) {
00332         $n = $this->xMonths[$this->lang->lc( $monthName )];
00333         return sprintf( '%02d', $n );
00334     }
00335 
00341     function makeIsoYear( $year ) {
00342         # Assumes the year is in a nice format, as enforced by the regex
00343         if ( substr( $year, -2 ) == 'BC' ) {
00344             $num = intval( substr( $year, 0, -3 ) ) - 1;
00345             # PHP bug note: sprintf( "%04d", -1 ) fails poorly
00346             $text = sprintf( '-%04d', $num );
00347 
00348         } else {
00349             $text = sprintf( '%04d', $year );
00350         }
00351         return $text;
00352     }
00353 
00358     function makeNormalYear( $iso ) {
00359         if ( $iso[0] == '-' ) {
00360             $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
00361         } else {
00362             $text = intval( $iso );
00363         }
00364         return $text;
00365     }
00366 }