MediaWiki
REL1_24
|
00001 <?php 00029 class DateFormatter { 00030 public $mSource, $mTarget; 00031 public $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD; 00032 00033 public $regexes, $pDays, $pMonths, $pYears; 00034 public $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 public 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 00149 public function reformat( $preference, $text, $options = array( 'linked' ) ) { 00150 $linked = in_array( 'linked', $options ); 00151 $match_whole = in_array( 'match-whole', $options ); 00152 00153 if ( isset( $this->preferences[$preference] ) ) { 00154 $preference = $this->preferences[$preference]; 00155 } else { 00156 $preference = self::NONE; 00157 } 00158 for ( $i = 1; $i <= self::LAST; $i++ ) { 00159 $this->mSource = $i; 00160 if ( isset( $this->rules[$preference][$i] ) ) { 00161 # Specific rules 00162 $this->mTarget = $this->rules[$preference][$i]; 00163 } elseif ( isset( $this->rules[self::ALL][$i] ) ) { 00164 # General rules 00165 $this->mTarget = $this->rules[self::ALL][$i]; 00166 } elseif ( $preference ) { 00167 # User preference 00168 $this->mTarget = $preference; 00169 } else { 00170 # Default 00171 $this->mTarget = $i; 00172 } 00173 $regex = $this->regexes[$i]; 00174 00175 // Horrible hack 00176 if ( !$linked ) { 00177 $regex = str_replace( array( '\[\[', '\]\]' ), '', $regex ); 00178 } 00179 00180 if ( $match_whole ) { 00181 // Let's hope this works 00182 $regex = preg_replace( '!^/!', '/^', $regex ); 00183 $regex = str_replace( $this->regexTrail, 00184 '$' . $this->regexTrail, $regex ); 00185 } 00186 00187 // Another horrible hack 00188 $this->mLinked = $linked; 00189 $text = preg_replace_callback( $regex, array( &$this, 'replace' ), $text ); 00190 unset( $this->mLinked ); 00191 } 00192 return $text; 00193 } 00194 00199 public function replace( $matches ) { 00200 # Extract information from $matches 00201 $linked = true; 00202 if ( isset( $this->mLinked ) ) { 00203 $linked = $this->mLinked; 00204 } 00205 00206 $bits = array(); 00207 $key = $this->keys[$this->mSource]; 00208 $keyLength = strlen( $key ); 00209 for ( $p = 0; $p < $keyLength; $p++ ) { 00210 if ( $key[$p] != ' ' ) { 00211 $bits[$key[$p]] = $matches[$p + 1]; 00212 } 00213 } 00214 00215 return $this->formatDate( $bits, $linked ); 00216 } 00217 00223 public function formatDate( $bits, $link = true ) { 00224 $format = $this->targets[$this->mTarget]; 00225 00226 if ( !$link ) { 00227 // strip piped links 00228 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format ); 00229 // strip remaining links 00230 $format = str_replace( array( '[[', ']]' ), '', $format ); 00231 } 00232 00233 # Construct new date 00234 $text = ''; 00235 $fail = false; 00236 00237 // Pre-generate y/Y stuff because we need the year for the <span> title. 00238 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) { 00239 $bits['y'] = $this->makeIsoYear( $bits['Y'] ); 00240 } 00241 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) { 00242 $bits['Y'] = $this->makeNormalYear( $bits['y'] ); 00243 } 00244 00245 if ( !isset( $bits['m'] ) ) { 00246 $m = $this->makeIsoMonth( $bits['F'] ); 00247 if ( !$m || $m == '00' ) { 00248 $fail = true; 00249 } else { 00250 $bits['m'] = $m; 00251 } 00252 } 00253 00254 if ( !isset( $bits['d'] ) ) { 00255 $bits['d'] = sprintf( '%02d', $bits['j'] ); 00256 } 00257 00258 $formatLength = strlen( $format ); 00259 for ( $p = 0; $p < $formatLength; $p++ ) { 00260 $char = $format[$p]; 00261 switch ( $char ) { 00262 case 'd': # ISO day of month 00263 $text .= $bits['d']; 00264 break; 00265 case 'm': # ISO month 00266 $text .= $bits['m']; 00267 break; 00268 case 'y': # ISO year 00269 $text .= $bits['y']; 00270 break; 00271 case 'j': # ordinary day of month 00272 if ( !isset( $bits['j'] ) ) { 00273 $text .= intval( $bits['d'] ); 00274 } else { 00275 $text .= $bits['j']; 00276 } 00277 break; 00278 case 'F': # long month 00279 if ( !isset( $bits['F'] ) ) { 00280 $m = intval( $bits['m'] ); 00281 if ( $m > 12 || $m < 1 ) { 00282 $fail = true; 00283 } else { 00284 $text .= $this->lang->getMonthName( $m ); 00285 } 00286 } else { 00287 $text .= ucfirst( $bits['F'] ); 00288 } 00289 break; 00290 case 'Y': # ordinary (optional BC) year 00291 $text .= $bits['Y']; 00292 break; 00293 default: 00294 $text .= $char; 00295 } 00296 } 00297 if ( $fail ) { 00299 $text = $matches[0]; 00300 } 00301 00302 $isoBits = array(); 00303 if ( isset( $bits['y'] ) ) { 00304 $isoBits[] = $bits['y']; 00305 } 00306 $isoBits[] = $bits['m']; 00307 $isoBits[] = $bits['d']; 00308 $isoDate = implode( '-', $isoBits ); 00309 00310 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted. 00311 $text = Html::rawElement( 'span', 00312 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text ); 00313 00314 return $text; 00315 } 00316 00321 public function getMonthRegex() { 00322 $names = array(); 00323 for ( $i = 1; $i <= 12; $i++ ) { 00324 $names[] = $this->lang->getMonthName( $i ); 00325 $names[] = $this->lang->getMonthAbbreviation( $i ); 00326 } 00327 return implode( '|', $names ); 00328 } 00329 00335 public function makeIsoMonth( $monthName ) { 00336 $n = $this->xMonths[$this->lang->lc( $monthName )]; 00337 return sprintf( '%02d', $n ); 00338 } 00339 00345 public function makeIsoYear( $year ) { 00346 # Assumes the year is in a nice format, as enforced by the regex 00347 if ( substr( $year, -2 ) == 'BC' ) { 00348 $num = intval( substr( $year, 0, -3 ) ) - 1; 00349 # PHP bug note: sprintf( "%04d", -1 ) fails poorly 00350 $text = sprintf( '-%04d', $num ); 00351 00352 } else { 00353 $text = sprintf( '%04d', $year ); 00354 } 00355 return $text; 00356 } 00357 00363 public function makeNormalYear( $iso ) { 00364 if ( $iso[0] == '-' ) { 00365 $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC'; 00366 } else { 00367 $text = intval( $iso ); 00368 } 00369 return $text; 00370 } 00371 }