68 private const ALL = -1;
74 private const MDY = 1;
77 private const DMY = 2;
80 private const YMD = 3;
83 private const ISO = 4;
86 private const YDM = 5;
101 $monthRegexParts = [];
102 for ( $i = 1; $i <= 12; $i++ ) {
103 $monthName =
$lang->getMonthName( $i );
104 $monthAbbrev =
$lang->getMonthAbbreviation( $i );
105 $this->monthNames[$i] = $monthName;
106 $monthRegexParts[] = preg_quote( $monthName,
'/' );
107 $monthRegexParts[] = preg_quote( $monthAbbrev,
'/' );
108 $this->xMonths[mb_strtolower( $monthName )] = $i;
109 $this->xMonths[mb_strtolower( $monthAbbrev )] = $i;
114 $dm =
"(?<day>\d{1,2})[ _](?<monthName>{$monthNames})";
115 $md =
"(?<monthName>{$monthNames})[ _](?<day>\d{1,2})";
116 $y =
'(?<year>\d{1,4}([ _]BC|))';
117 $iso =
'(?<isoYear>-?\d{4})-(?<isoMonth>\d{2})-(?<isoDay>\d{2})';
120 self::DMY =>
"/^{$dm}(?: *, *| +){$y}$/iu",
121 self::YDM =>
"/^{$y}(?: *, *| +){$dm}$/iu",
122 self::MDY =>
"/^{$md}(?: *, *| +){$y}$/iu",
123 self::YMD =>
"/^{$y}(?: *, *| +){$md}$/iu",
124 self::DM =>
"/^{$dm}$/iu",
125 self::MD =>
"/^{$md}$/iu",
126 self::ISO =>
"/^{$iso}$/iu",
130 $this->targetFormats = [
131 self::DMY =>
'j F Y',
132 self::YDM =>
'Y, j F',
133 self::MDY =>
'F j, Y',
134 self::YMD =>
'Y F j',
137 self::ISO =>
'y-m-d',
148 $this->preferenceIDs = [
167 $lang =
$lang ?? MediaWikiServices::getInstance()->getContentLanguage();
168 return MediaWikiServices::getInstance()->getDateFormatterFactory()->get(
$lang );
180 public function reformat( $preference, $text, $options = [] ) {
181 if ( isset( $this->preferenceIDs[$preference] ) ) {
182 $preference = $this->preferenceIDs[$preference];
187 if ( isset( $this->rules[$preference][
$source] ) ) {
189 $target = $this->rules[$preference][
$source];
190 } elseif ( isset( $this->rules[self::ALL][
$source] ) ) {
193 } elseif ( $preference ) {
195 $target = $preference;
200 $regex = $this->regexes[
$source];
202 $text = preg_replace_callback( $regex,
203 function ( $match ) use ( $target ) {
204 $format = $this->targetFormats[$target];
209 if ( !isset( $match[
'isoYear'] ) && isset( $match[
'year'] ) ) {
210 $match[
'isoYear'] = $this->
makeIsoYear( $match[
'year'] );
212 if ( !isset( $match[
'year'] ) && isset( $match[
'isoYear'] ) ) {
216 if ( !isset( $match[
'isoMonth'] ) ) {
218 if ( $m ===
false ) {
222 $match[
'isoMonth'] = $m;
226 if ( !isset( $match[
'isoDay'] ) ) {
227 $match[
'isoDay'] = sprintf(
'%02d', $match[
'day'] );
230 $formatLength = strlen( $format );
231 for ( $p = 0; $p < $formatLength; $p++ ) {
235 $text .= $match[
'isoDay'];
238 $text .= $match[
'isoMonth'];
241 $text .= $match[
'isoYear'];
244 if ( !isset( $match[
'day'] ) ) {
245 $text .= intval( $match[
'isoDay'] );
247 $text .= $match[
'day'];
251 $m = intval( $match[
'isoMonth'] );
252 if ( $m > 12 || $m < 1 ) {
256 $text .= $this->monthNames[$m];
260 $text .= $match[
'year'];
268 if ( isset( $match[
'isoYear'] ) ) {
269 $isoBits[] = $match[
'isoYear'];
271 $isoBits[] = $match[
'isoMonth'];
272 $isoBits[] = $match[
'isoDay'];
273 $isoDate = implode(
'-', $isoBits );
276 return Html::rawElement(
'span',
277 [
'class' =>
'mw-formatted-date',
'title' => $isoDate ], $text );
290 $isoMonth = $this->xMonths[mb_strtolower( $monthName )] ??
false;
291 if ( $isoMonth ===
false ) {
294 return sprintf(
'%02d', $isoMonth );
304 if ( substr( $year, -2 ) ==
'BC' ) {
305 $num = intval( substr( $year, 0, -3 ) ) - 1;
307 $text = sprintf(
'-%04d', $num );
309 $text = sprintf(
'%04d', $year );
321 if ( $iso[0] ==
'-' ) {
322 $text = ( intval( substr( $iso, 1 ) ) + 1 ) .
' BC';
324 $text = intval( $iso );
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
if(!isset( $args[0])) $lang