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