2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2010-08-22 14:31:05 +00:00
|
|
|
/**
|
|
|
|
|
* Date formatter
|
|
|
|
|
*
|
2012-04-30 09:22:16 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2010-08-22 14:31:05 +00:00
|
|
|
* @file
|
2012-04-30 09:22:16 +00:00
|
|
|
* @ingroup Parser
|
2010-08-22 14:31:05 +00:00
|
|
|
*/
|
2007-03-28 14:16:43 +00:00
|
|
|
|
|
|
|
|
/**
|
2013-03-13 07:42:41 +00:00
|
|
|
* Date formatter, recognises dates in plain text and formats them according to user preferences.
|
2004-09-02 23:28:24 +00:00
|
|
|
* @todo preferences, OutputPage
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Parser
|
2004-09-02 23:28:24 +00:00
|
|
|
*/
|
2012-06-01 09:52:57 +00:00
|
|
|
class DateFormatter {
|
2017-01-18 19:01:26 +00:00
|
|
|
private $mSource, $mTarget;
|
|
|
|
|
private $monthNames = '';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2017-01-18 19:01:26 +00:00
|
|
|
private $regexes;
|
|
|
|
|
private $rules, $xMonths, $preferences;
|
2008-04-14 07:45:50 +00:00
|
|
|
|
2017-01-18 19:01:26 +00:00
|
|
|
private $lang, $mLinked;
|
|
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
|
private $keys;
|
|
|
|
|
|
|
|
|
|
/** @var string[] */
|
|
|
|
|
private $targets;
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2006-07-14 17:02:49 +00:00
|
|
|
const ALL = -1;
|
|
|
|
|
const NONE = 0;
|
|
|
|
|
const MDY = 1;
|
|
|
|
|
const DMY = 2;
|
|
|
|
|
const YMD = 3;
|
|
|
|
|
const ISO1 = 4;
|
|
|
|
|
const LASTPREF = 4;
|
|
|
|
|
const ISO2 = 5;
|
|
|
|
|
const YDM = 6;
|
|
|
|
|
const DM = 7;
|
|
|
|
|
const MD = 8;
|
|
|
|
|
const LAST = 8;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param Language $lang In which language to format the date
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
2014-08-11 20:24:54 +00:00
|
|
|
public function __construct( Language $lang ) {
|
2012-06-01 09:52:57 +00:00
|
|
|
$this->lang = $lang;
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
$this->monthNames = $this->getMonthRegex();
|
2013-03-07 16:50:43 +00:00
|
|
|
for ( $i = 1; $i <= 12; $i++ ) {
|
2012-06-01 09:52:57 +00:00
|
|
|
$this->xMonths[$this->lang->lc( $this->lang->getMonthName( $i ) )] = $i;
|
|
|
|
|
$this->xMonths[$this->lang->lc( $this->lang->getMonthAbbreviation( $i ) )] = $i;
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2005-10-29 12:08:48 +00:00
|
|
|
$this->regexTrail = '(?![a-z])/iu';
|
|
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Partial regular expressions
|
2009-03-10 01:07:47 +00:00
|
|
|
$this->prxDM = '\[\[(\d{1,2})[ _](' . $this->monthNames . ')\]\]';
|
|
|
|
|
$this->prxMD = '\[\[(' . $this->monthNames . ')[ _](\d{1,2})\]\]';
|
|
|
|
|
$this->prxY = '\[\[(\d{1,4}([ _]BC|))\]\]';
|
|
|
|
|
$this->prxISO1 = '\[\[(-?\d{4})]]-\[\[(\d{2})-(\d{2})\]\]';
|
|
|
|
|
$this->prxISO2 = '\[\[(-?\d{4})-(\d{2})-(\d{2})\]\]';
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Real regular expressions
|
2009-06-02 12:39:14 +00:00
|
|
|
$this->regexes[self::DMY] = "/{$this->prxDM}(?: *, *| +){$this->prxY}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::YDM] = "/{$this->prxY}(?: *, *| +){$this->prxDM}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::MDY] = "/{$this->prxMD}(?: *, *| +){$this->prxY}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::YMD] = "/{$this->prxY}(?: *, *| +){$this->prxMD}{$this->regexTrail}";
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->regexes[self::DM] = "/{$this->prxDM}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::MD] = "/{$this->prxMD}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::ISO1] = "/{$this->prxISO1}{$this->regexTrail}";
|
|
|
|
|
$this->regexes[self::ISO2] = "/{$this->prxISO2}{$this->regexTrail}";
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Extraction keys
|
|
|
|
|
# See the comments in replace() for the meaning of the letters
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->keys[self::DMY] = 'jFY';
|
|
|
|
|
$this->keys[self::YDM] = 'Y jF';
|
|
|
|
|
$this->keys[self::MDY] = 'FjY';
|
|
|
|
|
$this->keys[self::YMD] = 'Y Fj';
|
|
|
|
|
$this->keys[self::DM] = 'jF';
|
|
|
|
|
$this->keys[self::MD] = 'Fj';
|
|
|
|
|
$this->keys[self::ISO1] = 'ymd'; # y means ISO year
|
|
|
|
|
$this->keys[self::ISO2] = 'ymd';
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
# Target date formats
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->targets[self::DMY] = '[[F j|j F]] [[Y]]';
|
|
|
|
|
$this->targets[self::YDM] = '[[Y]], [[F j|j F]]';
|
|
|
|
|
$this->targets[self::MDY] = '[[F j]], [[Y]]';
|
|
|
|
|
$this->targets[self::YMD] = '[[Y]] [[F j]]';
|
|
|
|
|
$this->targets[self::DM] = '[[F j|j F]]';
|
|
|
|
|
$this->targets[self::MD] = '[[F j]]';
|
|
|
|
|
$this->targets[self::ISO1] = '[[Y|y]]-[[F j|m-d]]';
|
|
|
|
|
$this->targets[self::ISO2] = '[[y-m-d]]';
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
# Rules
|
2017-02-25 21:53:36 +00:00
|
|
|
# pref source target
|
2013-02-03 19:42:08 +00:00
|
|
|
$this->rules[self::DMY][self::MD] = self::DM;
|
|
|
|
|
$this->rules[self::ALL][self::MD] = self::MD;
|
|
|
|
|
$this->rules[self::MDY][self::DM] = self::MD;
|
|
|
|
|
$this->rules[self::ALL][self::DM] = self::DM;
|
|
|
|
|
$this->rules[self::NONE][self::ISO2] = self::ISO1;
|
2006-07-26 07:15:39 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$this->preferences = [
|
2006-07-26 07:15:39 +00:00
|
|
|
'default' => self::NONE,
|
|
|
|
|
'dmy' => self::DMY,
|
|
|
|
|
'mdy' => self::MDY,
|
|
|
|
|
'ymd' => self::YMD,
|
|
|
|
|
'ISO 8601' => self::ISO1,
|
2016-02-17 09:09:32 +00:00
|
|
|
];
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2005-04-20 15:42:08 +00:00
|
|
|
/**
|
2009-01-23 20:13:39 +00:00
|
|
|
* Get a DateFormatter object
|
|
|
|
|
*
|
2017-09-13 12:57:44 +00:00
|
|
|
* @param Language|null $lang In which language to format the date
|
2017-02-25 21:53:36 +00:00
|
|
|
* Defaults to the site content language
|
2014-07-24 17:43:25 +00:00
|
|
|
* @return DateFormatter
|
2005-04-20 15:42:08 +00:00
|
|
|
*/
|
2017-09-13 13:47:01 +00:00
|
|
|
public static function getInstance( Language $lang = null ) {
|
2015-10-26 07:59:10 +00:00
|
|
|
global $wgContLang, $wgMainCacheType;
|
|
|
|
|
|
2017-09-13 13:47:01 +00:00
|
|
|
$lang = $lang ?: $wgContLang;
|
2015-10-31 18:42:48 +00:00
|
|
|
$cache = ObjectCache::getLocalServerInstance( $wgMainCacheType );
|
2015-10-26 07:59:10 +00:00
|
|
|
|
|
|
|
|
static $dateFormatter = false;
|
2005-04-20 15:42:08 +00:00
|
|
|
if ( !$dateFormatter ) {
|
2015-10-26 07:59:10 +00:00
|
|
|
$dateFormatter = $cache->getWithSetCallback(
|
|
|
|
|
$cache->makeKey( 'dateformatter', $lang->getCode() ),
|
2015-10-19 17:52:19 +00:00
|
|
|
$cache::TTL_HOUR,
|
2015-10-26 07:59:10 +00:00
|
|
|
function () use ( $lang ) {
|
|
|
|
|
return new DateFormatter( $lang );
|
|
|
|
|
}
|
|
|
|
|
);
|
2005-04-20 15:42:08 +00:00
|
|
|
}
|
2015-10-26 07:59:10 +00:00
|
|
|
|
2005-04-20 15:42:08 +00:00
|
|
|
return $dateFormatter;
|
2006-01-07 13:31:29 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $preference User preference
|
|
|
|
|
* @param string $text Text to reformat
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param array $options Array can contain 'linked' and/or 'match-whole'
|
2014-04-09 10:33:55 +00:00
|
|
|
*
|
|
|
|
|
* @return string
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
2016-02-17 09:09:32 +00:00
|
|
|
public function reformat( $preference, $text, $options = [ 'linked' ] ) {
|
2009-03-10 05:19:05 +00:00
|
|
|
$linked = in_array( 'linked', $options );
|
|
|
|
|
$match_whole = in_array( 'match-whole', $options );
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2006-07-26 07:15:39 +00:00
|
|
|
if ( isset( $this->preferences[$preference] ) ) {
|
|
|
|
|
$preference = $this->preferences[$preference];
|
|
|
|
|
} else {
|
|
|
|
|
$preference = self::NONE;
|
|
|
|
|
}
|
2013-03-07 16:50:43 +00:00
|
|
|
for ( $i = 1; $i <= self::LAST; $i++ ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$this->mSource = $i;
|
2013-04-26 12:18:06 +00:00
|
|
|
if ( isset( $this->rules[$preference][$i] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Specific rules
|
|
|
|
|
$this->mTarget = $this->rules[$preference][$i];
|
2013-04-26 12:18:06 +00:00
|
|
|
} elseif ( isset( $this->rules[self::ALL][$i] ) ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# General rules
|
2006-07-14 17:02:49 +00:00
|
|
|
$this->mTarget = $this->rules[self::ALL][$i];
|
2003-11-20 13:12:41 +00:00
|
|
|
} elseif ( $preference ) {
|
|
|
|
|
# User preference
|
|
|
|
|
$this->mTarget = $preference;
|
|
|
|
|
} else {
|
|
|
|
|
# Default
|
|
|
|
|
$this->mTarget = $i;
|
|
|
|
|
}
|
2009-03-10 01:07:47 +00:00
|
|
|
$regex = $this->regexes[$i];
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2009-03-10 01:07:47 +00:00
|
|
|
// Horrible hack
|
2013-02-03 19:42:08 +00:00
|
|
|
if ( !$linked ) {
|
2016-02-17 09:09:32 +00:00
|
|
|
$regex = str_replace( [ '\[\[', '\]\]' ], '', $regex );
|
2009-03-10 01:07:47 +00:00
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2013-02-03 19:42:08 +00:00
|
|
|
if ( $match_whole ) {
|
2009-03-10 05:19:05 +00:00
|
|
|
// Let's hope this works
|
|
|
|
|
$regex = preg_replace( '!^/!', '/^', $regex );
|
|
|
|
|
$regex = str_replace( $this->regexTrail,
|
2013-02-03 19:42:08 +00:00
|
|
|
'$' . $this->regexTrail, $regex );
|
2009-03-10 05:19:05 +00:00
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2009-03-10 01:07:47 +00:00
|
|
|
// Another horrible hack
|
|
|
|
|
$this->mLinked = $linked;
|
2017-02-01 04:01:54 +00:00
|
|
|
$text = preg_replace_callback( $regex, [ $this, 'replace' ], $text );
|
2013-02-03 19:42:08 +00:00
|
|
|
unset( $this->mLinked );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2017-01-18 19:01:26 +00:00
|
|
|
* Regexp replacement callback
|
|
|
|
|
*
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param array $matches
|
2012-02-09 21:35:05 +00:00
|
|
|
* @return string
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
2017-01-18 19:01:26 +00:00
|
|
|
private function replace( $matches ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Extract information from $matches
|
2009-03-10 01:07:47 +00:00
|
|
|
$linked = true;
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( isset( $this->mLinked ) ) {
|
2009-03-10 01:07:47 +00:00
|
|
|
$linked = $this->mLinked;
|
2013-04-20 15:38:24 +00:00
|
|
|
}
|
2012-10-10 18:13:40 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$bits = [];
|
2003-11-20 13:12:41 +00:00
|
|
|
$key = $this->keys[$this->mSource];
|
2014-05-10 23:03:45 +00:00
|
|
|
$keyLength = strlen( $key );
|
|
|
|
|
for ( $p = 0; $p < $keyLength; $p++ ) {
|
2011-04-17 07:59:58 +00:00
|
|
|
if ( $key[$p] != ' ' ) {
|
2013-04-13 11:36:24 +00:00
|
|
|
$bits[$key[$p]] = $matches[$p + 1];
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2017-01-18 00:48:59 +00:00
|
|
|
return $this->formatDate( $bits, $matches[0], $linked );
|
2009-03-09 23:51:58 +00:00
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
|
|
|
|
/**
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param array $bits
|
2017-01-18 00:48:59 +00:00
|
|
|
* @param string $orig Original input string, to be returned
|
|
|
|
|
* on formatting failure.
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param bool $link
|
2012-06-01 09:52:57 +00:00
|
|
|
* @return string
|
|
|
|
|
*/
|
2017-01-18 00:48:59 +00:00
|
|
|
private function formatDate( $bits, $orig, $link = true ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
$format = $this->targets[$this->mTarget];
|
2012-10-10 18:13:40 +00:00
|
|
|
|
2013-02-03 19:42:08 +00:00
|
|
|
if ( !$link ) {
|
2009-03-10 01:07:47 +00:00
|
|
|
// strip piped links
|
|
|
|
|
$format = preg_replace( '/\[\[[^|]+\|([^\]]+)\]\]/', '$1', $format );
|
|
|
|
|
// strip remaining links
|
2016-02-17 09:09:32 +00:00
|
|
|
$format = str_replace( [ '[[', ']]' ], '', $format );
|
2009-03-10 01:07:47 +00:00
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
# Construct new date
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = '';
|
2003-11-20 13:12:41 +00:00
|
|
|
$fail = false;
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2009-03-09 23:51:58 +00:00
|
|
|
// Pre-generate y/Y stuff because we need the year for the <span> title.
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( !isset( $bits['y'] ) && isset( $bits['Y'] ) ) {
|
2009-03-09 23:51:58 +00:00
|
|
|
$bits['y'] = $this->makeIsoYear( $bits['Y'] );
|
2013-04-20 15:38:24 +00:00
|
|
|
}
|
|
|
|
|
if ( !isset( $bits['Y'] ) && isset( $bits['y'] ) ) {
|
2009-03-09 23:51:58 +00:00
|
|
|
$bits['Y'] = $this->makeNormalYear( $bits['y'] );
|
2013-04-20 15:38:24 +00:00
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2009-03-10 01:07:47 +00:00
|
|
|
if ( !isset( $bits['m'] ) ) {
|
|
|
|
|
$m = $this->makeIsoMonth( $bits['F'] );
|
|
|
|
|
if ( !$m || $m == '00' ) {
|
|
|
|
|
$fail = true;
|
|
|
|
|
} else {
|
|
|
|
|
$bits['m'] = $m;
|
|
|
|
|
}
|
|
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2013-02-03 19:42:08 +00:00
|
|
|
if ( !isset( $bits['d'] ) ) {
|
2009-03-10 01:07:47 +00:00
|
|
|
$bits['d'] = sprintf( '%02d', $bits['j'] );
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2014-05-10 23:03:45 +00:00
|
|
|
$formatLength = strlen( $format );
|
|
|
|
|
for ( $p = 0; $p < $formatLength; $p++ ) {
|
2011-04-17 07:59:58 +00:00
|
|
|
$char = $format[$p];
|
2003-11-20 13:12:41 +00:00
|
|
|
switch ( $char ) {
|
|
|
|
|
case 'd': # ISO day of month
|
2009-03-10 01:07:47 +00:00
|
|
|
$text .= $bits['d'];
|
2003-11-20 13:12:41 +00:00
|
|
|
break;
|
|
|
|
|
case 'm': # ISO month
|
2009-03-10 01:07:47 +00:00
|
|
|
$text .= $bits['m'];
|
2003-11-20 13:12:41 +00:00
|
|
|
break;
|
|
|
|
|
case 'y': # ISO year
|
2009-03-09 23:51:58 +00:00
|
|
|
$text .= $bits['y'];
|
2003-11-20 13:12:41 +00:00
|
|
|
break;
|
|
|
|
|
case 'j': # ordinary day of month
|
2013-02-03 19:42:08 +00:00
|
|
|
if ( !isset( $bits['j'] ) ) {
|
2005-08-16 23:36:16 +00:00
|
|
|
$text .= intval( $bits['d'] );
|
2003-11-20 13:12:41 +00:00
|
|
|
} else {
|
|
|
|
|
$text .= $bits['j'];
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'F': # long month
|
2004-12-29 03:27:16 +00:00
|
|
|
if ( !isset( $bits['F'] ) ) {
|
2013-02-03 19:42:08 +00:00
|
|
|
$m = intval( $bits['m'] );
|
2003-11-20 13:12:41 +00:00
|
|
|
if ( $m > 12 || $m < 1 ) {
|
|
|
|
|
$fail = true;
|
|
|
|
|
} else {
|
2012-06-01 09:52:57 +00:00
|
|
|
$text .= $this->lang->getMonthName( $m );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$text .= ucfirst( $bits['F'] );
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
case 'Y': # ordinary (optional BC) year
|
2009-03-09 23:51:58 +00:00
|
|
|
$text .= $bits['Y'];
|
2003-11-20 13:12:41 +00:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
$text .= $char;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if ( $fail ) {
|
2017-01-18 00:48:59 +00:00
|
|
|
// This occurs when parsing a date with day or month outside the bounds
|
|
|
|
|
// of possibilities.
|
|
|
|
|
$text = $orig;
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2016-02-17 09:09:32 +00:00
|
|
|
$isoBits = [];
|
2013-04-20 15:38:24 +00:00
|
|
|
if ( isset( $bits['y'] ) ) {
|
2009-03-10 01:07:47 +00:00
|
|
|
$isoBits[] = $bits['y'];
|
2013-04-20 15:38:24 +00:00
|
|
|
}
|
2009-03-10 01:07:47 +00:00
|
|
|
$isoBits[] = $bits['m'];
|
|
|
|
|
$isoBits[] = $bits['d'];
|
2010-09-11 21:55:21 +00:00
|
|
|
$isoDate = implode( '-', $isoBits );
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2009-03-09 23:51:58 +00:00
|
|
|
// Output is not strictly HTML (it's wikitext), but <span> is whitelisted.
|
2009-08-21 20:39:35 +00:00
|
|
|
$text = Html::rawElement( 'span',
|
2016-02-17 09:09:32 +00:00
|
|
|
[ 'class' => 'mw-formatted-date', 'title' => $isoDate ], $text );
|
2012-06-01 09:52:57 +00:00
|
|
|
|
2003-11-20 13:12:41 +00:00
|
|
|
return $text;
|
|
|
|
|
}
|
2006-01-07 13:31:29 +00:00
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2015-01-02 21:46:37 +00:00
|
|
|
* Return a regex that can be used to find month names in string
|
|
|
|
|
* @return string regex to find the months with
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
2017-01-18 19:01:26 +00:00
|
|
|
private function getMonthRegex() {
|
2016-02-17 09:09:32 +00:00
|
|
|
$names = [];
|
2013-04-20 15:38:24 +00:00
|
|
|
for ( $i = 1; $i <= 12; $i++ ) {
|
2012-06-01 09:52:57 +00:00
|
|
|
$names[] = $this->lang->getMonthName( $i );
|
|
|
|
|
$names[] = $this->lang->getMonthAbbreviation( $i );
|
2004-12-29 03:27:16 +00:00
|
|
|
}
|
|
|
|
|
return implode( '|', $names );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
|
|
|
|
* Makes an ISO month, e.g. 02, from a month name
|
2014-04-21 23:38:39 +00:00
|
|
|
* @param string $monthName Month name
|
2004-09-03 16:51:45 +00:00
|
|
|
* @return string ISO month name
|
|
|
|
|
*/
|
2017-01-18 19:01:26 +00:00
|
|
|
private function makeIsoMonth( $monthName ) {
|
2012-06-01 09:52:57 +00:00
|
|
|
$n = $this->xMonths[$this->lang->lc( $monthName )];
|
2004-06-08 23:56:09 +00:00
|
|
|
return sprintf( '%02d', $n );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2015-01-02 21:46:37 +00:00
|
|
|
* Make an ISO year from a year name, for instance: '-1199' from '1200 BC'
|
2013-03-11 17:15:01 +00:00
|
|
|
* @param string $year Year name
|
2004-09-03 16:51:45 +00:00
|
|
|
* @return string ISO year name
|
|
|
|
|
*/
|
2017-01-18 19:01:26 +00:00
|
|
|
private function makeIsoYear( $year ) {
|
2003-11-20 13:12:41 +00:00
|
|
|
# Assumes the year is in a nice format, as enforced by the regex
|
|
|
|
|
if ( substr( $year, -2 ) == 'BC' ) {
|
2013-03-24 10:01:51 +00:00
|
|
|
$num = intval( substr( $year, 0, -3 ) ) - 1;
|
2003-11-20 13:12:41 +00:00
|
|
|
# PHP bug note: sprintf( "%04d", -1 ) fails poorly
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = sprintf( '-%04d', $num );
|
2003-11-20 13:12:41 +00:00
|
|
|
|
|
|
|
|
} else {
|
2004-06-08 23:56:09 +00:00
|
|
|
$text = sprintf( '%04d', $year );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
|
2004-09-03 16:51:45 +00:00
|
|
|
/**
|
2015-01-02 21:46:37 +00:00
|
|
|
* Make a year one from an ISO year, for instance: '400 BC' from '-0399'.
|
|
|
|
|
* @param string $iso ISO year
|
|
|
|
|
* @return int|string int representing year number in case of AD dates, or string containing
|
|
|
|
|
* year number and 'BC' at the end otherwise.
|
2004-09-03 16:51:45 +00:00
|
|
|
*/
|
2017-01-18 19:01:26 +00:00
|
|
|
private function makeNormalYear( $iso ) {
|
2011-04-17 07:59:58 +00:00
|
|
|
if ( $iso[0] == '-' ) {
|
2013-04-26 12:18:06 +00:00
|
|
|
$text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
|
2003-11-20 13:12:41 +00:00
|
|
|
} else {
|
2005-08-16 23:36:16 +00:00
|
|
|
$text = intval( $iso );
|
2003-11-20 13:12:41 +00:00
|
|
|
}
|
|
|
|
|
return $text;
|
|
|
|
|
}
|
|
|
|
|
}
|