129 $monthRegexParts = [];
130 for ( $i = 1; $i <= 12; $i++ ) {
131 $monthName =
$lang->getMonthName( $i );
132 $monthAbbrev =
$lang->getMonthAbbreviation( $i );
133 $this->monthNames[$i] = $monthName;
134 $monthRegexParts[] = preg_quote( $monthName,
'/' );
135 $monthRegexParts[] = preg_quote( $monthAbbrev,
'/' );
136 $this->xMonths[mb_strtolower( $monthName )] = $i;
137 $this->xMonths[mb_strtolower( $monthAbbrev )] = $i;
141 $monthNames = implode(
'|', $monthRegexParts );
142 $dm =
"(?<day>\d{1,2})[ _](?<monthName>{$monthNames})";
143 $md =
"(?<monthName>{$monthNames})[ _](?<day>\d{1,2})";
144 $y =
'(?<year>\d{1,4}([ _]BC|))';
145 $iso =
'(?<isoYear>-?\d{4})-(?<isoMonth>\d{2})-(?<isoDay>\d{2})';
148 self::DMY =>
"/^{$dm}(?: *, *| +){$y}$/iu",
149 self::YDM =>
"/^{$y}(?: *, *| +){$dm}$/iu",
150 self::MDY =>
"/^{$md}(?: *, *| +){$y}$/iu",
151 self::YMD =>
"/^{$y}(?: *, *| +){$md}$/iu",
152 self::DM =>
"/^{$dm}$/iu",
153 self::MD =>
"/^{$md}$/iu",
154 self::ISO =>
"/^{$iso}$/iu",
181 public function reformat( $preference, $text, $options = [] ) {
182 if ( isset( self::PREFERENCE_IDS[$preference] ) ) {
183 $userFormatId = self::PREFERENCE_IDS[$preference];
185 $userFormatId = self::NONE;
188 if ( isset( self::RULES[$userFormatId][
$source] ) ) {
191 $target = self::RULES[$userFormatId][
$source];
192 } elseif ( isset( self::RULES[self::ALL][
$source] ) ) {
195 $target = self::RULES[self::ALL][
$source];
196 } elseif ( $userFormatId ) {
198 $target = $userFormatId;
204 $format = self::TARGET_FORMATS[$target];
205 $regex = $this->regexes[
$source];
207 $text = preg_replace_callback( $regex,
208 function ( $match ) use ( $format ) {
212 if ( !isset( $match[
'isoYear'] ) && isset( $match[
'year'] ) ) {
213 $match[
'isoYear'] = $this->makeIsoYear( $match[
'year'] );
215 if ( !isset( $match[
'year'] ) && isset( $match[
'isoYear'] ) ) {
216 $match[
'year'] = $this->makeNormalYear( $match[
'isoYear'] );
219 if ( !isset( $match[
'isoMonth'] ) ) {
220 $m = $this->makeIsoMonth( $match[
'monthName'] );
221 if ( $m ===
false ) {
225 $match[
'isoMonth'] = $m;
229 if ( !isset( $match[
'isoDay'] ) ) {
230 $match[
'isoDay'] = sprintf(
'%02d', $match[
'day'] );
233 $formatLength = strlen( $format );
234 for ( $p = 0; $p < $formatLength; $p++ ) {
238 $text .= $match[
'isoDay'];
241 $text .= $match[
'isoMonth'];
245 $text .= $match[
'isoYear'];
248 if ( !isset( $match[
'day'] ) ) {
249 $text .= intval( $match[
'isoDay'] );
251 $text .= $match[
'day'];
255 $m = intval( $match[
'isoMonth'] );
256 if ( $m > 12 || $m < 1 ) {
260 $text .= $this->monthNames[$m];
265 $text .= $match[
'year'];
273 if ( isset( $match[
'isoYear'] ) ) {
274 $isoBits[] = $match[
'isoYear'];
276 $isoBits[] = $match[
'isoMonth'];
277 $isoBits[] = $match[
'isoDay'];
278 $isoDate = implode(
'-', $isoBits );
281 return Html::rawElement(
'span',
282 [
'class' =>
'mw-formatted-date',
'title' => $isoDate ], $text );