5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 namespace MediaWiki\Parser
;
26 use MediaWiki\Html\Html
;
27 use MediaWiki\Language\Language
;
28 use MediaWiki\MediaWikiServices
;
31 * Date formatter. Recognises dates and formats them according to a specified preference.
33 * This class was originally introduced to detect and transform dates in free text. It is now
34 * only used by the {{#dateformat}} parser function. This is a very rudimentary date formatter;
35 * Language::sprintfDate() has many more features and is the correct choice for most new code.
36 * The main advantage of this date formatter is that it is able to format incomplete dates with an
42 /** @var string[] Date format regexes indexed the class constants */
46 * @var int[][] Array of special rules. The first key is the preference ID
47 * (one of the class constants), the second key is the detected source
48 * format, and the value is the ID of the target format that will be used
51 private const RULES
= [
57 self
::ISO
=> self
::ISO
,
68 * @var array<string,int> Month numbers by lowercase name
70 private $xMonths = [];
73 * @var array<int,string> Month names by number
75 private $monthNames = [];
78 * @var int[] A map of descriptive preference text to internal format ID
80 private const PREFERENCE_IDS
= [
81 'default' => self
::NONE
,
85 'ISO 8601' => self
::ISO
,
88 /** @var string[] Format strings similar to those used by date(), indexed by ID */
89 private const TARGET_FORMATS
= [
90 self
::MDY
=> 'F j, Y',
94 self
::YDM
=> 'Y, j F',
99 /** Used as a preference ID for rules that apply regardless of preference */
100 private const ALL
= -1;
102 /** No preference: the date may be left in the same format as the input */
103 private const NONE
= 0;
105 /** e.g. January 15, 2001 */
106 private const MDY
= 1;
108 /** e.g. 15 January 2001 */
109 private const DMY
= 2;
111 /** e.g. 2001 January 15 */
112 private const YMD
= 3;
114 /** e.g. 2001-01-15 */
115 private const ISO
= 4;
117 /** e.g. 2001, 15 January */
118 private const YDM
= 5;
120 /** e.g. 15 January */
121 private const DM
= 6;
123 /** e.g. January 15 */
124 private const MD
= 7;
127 * @param Language $lang In which language to format the date
129 public function __construct( Language
$lang ) {
130 $monthRegexParts = [];
131 for ( $i = 1; $i <= 12; $i++
) {
132 $monthName = $lang->getMonthName( $i );
133 $monthAbbrev = $lang->getMonthAbbreviation( $i );
134 $this->monthNames
[$i] = $monthName;
135 $monthRegexParts[] = preg_quote( $monthName, '/' );
136 $monthRegexParts[] = preg_quote( $monthAbbrev, '/' );
137 $this->xMonths
[mb_strtolower( $monthName )] = $i;
138 $this->xMonths
[mb_strtolower( $monthAbbrev )] = $i;
141 // Partial regular expressions
142 $monthNames = implode( '|', $monthRegexParts );
143 $dm = "(?<day>\d{1,2})[ _](?<monthName>{$monthNames})";
144 $md = "(?<monthName>{$monthNames})[ _](?<day>\d{1,2})";
145 $y = '(?<year>\d{1,4}([ _]BC|))';
146 $iso = '(?<isoYear>-?\d{4})-(?<isoMonth>\d{2})-(?<isoDay>\d{2})';
149 self
::DMY
=> "/^{$dm}(?: *, *| +){$y}$/iu",
150 self
::YDM
=> "/^{$y}(?: *, *| +){$dm}$/iu",
151 self
::MDY
=> "/^{$md}(?: *, *| +){$y}$/iu",
152 self
::YMD
=> "/^{$y}(?: *, *| +){$md}$/iu",
153 self
::DM
=> "/^{$dm}$/iu",
154 self
::MD
=> "/^{$md}$/iu",
155 self
::ISO
=> "/^{$iso}$/iu",
160 * Get a DateFormatter object
162 * @deprecated since 1.33 use MediaWikiServices::getDateFormatterFactory()
164 * @param Language|null $lang In which language to format the date
165 * Defaults to the site content language
166 * @return DateFormatter
168 public static function getInstance( ?Language
$lang = null ) {
169 $lang ??
= MediaWikiServices
::getInstance()->getContentLanguage();
170 return MediaWikiServices
::getInstance()->getDateFormatterFactory()->get( $lang );
174 * @param string $preference User preference, must be one of "default",
175 * "dmy", "mdy", "ymd" or "ISO 8601".
176 * @param string $text Text to reformat
177 * @param array $options Ignored. Since 1.33, 'match-whole' is implied, and
178 * 'linked' has been removed.
182 public function reformat( $preference, $text, $options = [] ) {
183 $userFormatId = self
::PREFERENCE_IDS
[$preference] ?? self
::NONE
;
184 foreach ( self
::TARGET_FORMATS
as $source => $_ ) {
185 if ( isset( self
::RULES
[$userFormatId][$source] ) ) {
187 $target = self
::RULES
[$userFormatId][$source];
188 } elseif ( isset( self
::RULES
[self
::ALL
][$source] ) ) {
190 $target = self
::RULES
[self
::ALL
][$source];
191 } elseif ( $userFormatId ) {
193 $target = $userFormatId;
198 $format = self
::TARGET_FORMATS
[$target];
199 $regex = $this->regexes
[$source];
201 $text = preg_replace_callback( $regex,
202 function ( $match ) use ( $format ) {
205 // Pre-generate y/Y stuff because we need the year for the <span> title.
206 if ( !isset( $match['isoYear'] ) && isset( $match['year'] ) ) {
207 $match['isoYear'] = $this->makeIsoYear( $match['year'] );
209 if ( !isset( $match['year'] ) && isset( $match['isoYear'] ) ) {
210 $match['year'] = $this->makeNormalYear( $match['isoYear'] );
213 if ( !isset( $match['isoMonth'] ) ) {
214 $m = $this->makeIsoMonth( $match['monthName'] );
219 $match['isoMonth'] = $m;
222 if ( !isset( $match['isoDay'] ) ) {
223 $match['isoDay'] = sprintf( '%02d', $match['day'] );
226 $formatLength = strlen( $format );
227 for ( $p = 0; $p < $formatLength; $p++
) {
230 case 'd': // ISO day of month
231 $text .= $match['isoDay'];
233 case 'm': // ISO month
234 $text .= $match['isoMonth'];
236 case 'y': // ISO year
237 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
238 $text .= $match['isoYear'];
240 case 'j': // ordinary day of month
241 if ( !isset( $match['day'] ) ) {
242 $text .= intval( $match['isoDay'] );
244 $text .= $match['day'];
247 case 'F': // long month
248 $m = intval( $match['isoMonth'] );
249 if ( $m > 12 ||
$m < 1 ) {
253 $text .= $this->monthNames
[$m];
255 case 'Y': // ordinary (optional BC) year
256 // @phan-suppress-next-line PhanTypePossiblyInvalidDimOffset False positive
257 $text .= $match['year'];
265 if ( isset( $match['isoYear'] ) ) {
266 $isoBits[] = $match['isoYear'];
268 $isoBits[] = $match['isoMonth'];
269 $isoBits[] = $match['isoDay'];
270 $isoDate = implode( '-', $isoBits );
272 // Output is not strictly HTML (it's wikitext), but <span> is allowed.
273 return Html
::rawElement( 'span',
274 [ 'class' => 'mw-formatted-date', 'title' => $isoDate ], $text );
282 * @param string $monthName
283 * @return string|null 2-digit month number, e.g. "02", or null if the input was invalid
285 private function makeIsoMonth( $monthName ) {
286 $number = $this->xMonths
[mb_strtolower( $monthName )] ??
null;
287 return $number !== null ?
sprintf( '%02d', $number ) : null;
291 * Make an ISO year from a year name, for instance: '-1199' from '1200 BC'
292 * @param string $year Year name
293 * @return string ISO year name
295 private function makeIsoYear( $year ) {
296 // Assumes the year is in a nice format, as enforced by the regex
297 if ( substr( $year, -2 ) == 'BC' ) {
298 $num = intval( substr( $year, 0, -3 ) ) - 1;
299 // PHP bug note: sprintf( "%04d", -1 ) fails poorly
300 $text = sprintf( '-%04d', $num );
302 $text = sprintf( '%04d', $year );
308 * Make a year from an ISO year, for instance: '400 BC' from '-0399'.
309 * @param string $iso ISO year
310 * @return int|string int representing year number in case of AD dates, or string containing
311 * year number and 'BC' at the end otherwise.
313 private function makeNormalYear( $iso ) {
315 $text = ( intval( substr( $iso, 1 ) ) +
1 ) . ' BC';
317 $text = intval( $iso );
323 /** @deprecated class alias since 1.43 */
324 class_alias( DateFormatter
::class, 'DateFormatter' );