MediaWiki
REL1_20
|
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 $bits = array(); 00205 $key = $this->keys[$this->mSource]; 00206 for ( $p=0; $p < strlen($key); $p++ ) { 00207 if ( $key[$p] != ' ' ) { 00208 $bits[$key[$p]] = $matches[$p+1]; 00209 } 00210 } 00211 00212 return $this->formatDate( $bits, $linked ); 00213 } 00214 00220 function formatDate( $bits, $link = true ) { 00221 $format = $this->targets[$this->mTarget]; 00222 00223 if (!$link) { 00224 // strip piped links 00225 $format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format ); 00226 // strip remaining links 00227 $format = str_replace( array( '[[', ']]' ), '', $format ); 00228 } 00229 00230 # Construct new date 00231 $text = ''; 00232 $fail = false; 00233 00234 // Pre-generate y/Y stuff because we need the year for the <span> title. 00235 if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) 00236 $bits['y'] = $this->makeIsoYear( $bits['Y'] ); 00237 if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) 00238 $bits['Y'] = $this->makeNormalYear( $bits['y'] ); 00239 00240 if ( !isset( $bits['m'] ) ) { 00241 $m = $this->makeIsoMonth( $bits['F'] ); 00242 if ( !$m || $m == '00' ) { 00243 $fail = true; 00244 } else { 00245 $bits['m'] = $m; 00246 } 00247 } 00248 00249 if ( !isset($bits['d']) ) { 00250 $bits['d'] = sprintf( '%02d', $bits['j'] ); 00251 } 00252 00253 for ( $p=0; $p < strlen( $format ); $p++ ) { 00254 $char = $format[$p]; 00255 switch ( $char ) { 00256 case 'd': # ISO day of month 00257 $text .= $bits['d']; 00258 break; 00259 case 'm': # ISO month 00260 $text .= $bits['m']; 00261 break; 00262 case 'y': # ISO year 00263 $text .= $bits['y']; 00264 break; 00265 case 'j': # ordinary day of month 00266 if ( !isset($bits['j']) ) { 00267 $text .= intval( $bits['d'] ); 00268 } else { 00269 $text .= $bits['j']; 00270 } 00271 break; 00272 case 'F': # long month 00273 if ( !isset( $bits['F'] ) ) { 00274 $m = intval($bits['m']); 00275 if ( $m > 12 || $m < 1 ) { 00276 $fail = true; 00277 } else { 00278 $text .= $this->lang->getMonthName( $m ); 00279 } 00280 } else { 00281 $text .= ucfirst( $bits['F'] ); 00282 } 00283 break; 00284 case 'Y': # ordinary (optional BC) year 00285 $text .= $bits['Y']; 00286 break; 00287 default: 00288 $text .= $char; 00289 } 00290 } 00291 if ( $fail ) { 00292 $text = $matches[0]; 00293 } 00294 00295 $isoBits = array(); 00296 if ( isset($bits['y']) ) 00297 $isoBits[] = $bits['y']; 00298 $isoBits[] = $bits['m']; 00299 $isoBits[] = $bits['d']; 00300 $isoDate = implode( '-', $isoBits ); 00301 00302 // Output is not strictly HTML (it's wikitext), but <span> is whitelisted. 00303 $text = Html::rawElement( 'span', 00304 array( 'class' => 'mw-formatted-date', 'title' => $isoDate ), $text ); 00305 00306 return $text; 00307 } 00308 00313 function getMonthRegex() { 00314 $names = array(); 00315 for( $i = 1; $i <= 12; $i++ ) { 00316 $names[] = $this->lang->getMonthName( $i ); 00317 $names[] = $this->lang->getMonthAbbreviation( $i ); 00318 } 00319 return implode( '|', $names ); 00320 } 00321 00327 function makeIsoMonth( $monthName ) { 00328 $n = $this->xMonths[$this->lang->lc( $monthName )]; 00329 return sprintf( '%02d', $n ); 00330 } 00331 00337 function makeIsoYear( $year ) { 00338 # Assumes the year is in a nice format, as enforced by the regex 00339 if ( substr( $year, -2 ) == 'BC' ) { 00340 $num = intval(substr( $year, 0, -3 )) - 1; 00341 # PHP bug note: sprintf( "%04d", -1 ) fails poorly 00342 $text = sprintf( '-%04d', $num ); 00343 00344 } else { 00345 $text = sprintf( '%04d', $year ); 00346 } 00347 return $text; 00348 } 00349 00354 function makeNormalYear( $iso ) { 00355 if ( $iso[0] == '-' ) { 00356 $text = (intval( substr( $iso, 1 ) ) + 1) . ' BC'; 00357 } else { 00358 $text = intval( $iso ); 00359 } 00360 return $text; 00361 } 00362 }