Merge "Drop index oi_name_archive_name on table oldimage"
[lhc/web/wiklou.git] / includes / parser / DateFormatter.php
index 40da368..605a873 100644 (file)
  * @ingroup Parser
  */
 class DateFormatter {
-       public $mSource, $mTarget;
-       public $monthNames = '', $rxDM, $rxMD, $rxDMY, $rxYDM, $rxMDY, $rxYMD;
+       private $mSource, $mTarget;
+       private $monthNames = '';
 
-       public $regexes, $pDays, $pMonths, $pYears;
-       public $rules, $xMonths, $preferences;
+       private $regexes;
+       private $rules, $xMonths, $preferences;
 
-       protected $lang, $mLinked;
+       private $lang, $mLinked;
+
+       /** @var string[] */
+       private $keys;
+
+       /** @var string[] */
+       private $targets;
 
        const ALL = -1;
        const NONE = 0;
@@ -101,7 +107,7 @@ class DateFormatter {
                $this->targets[self::ISO2] = '[[y-m-d]]';
 
                # Rules
-               #            pref    source       target
+               #            pref       source      target
                $this->rules[self::DMY][self::MD] = self::DM;
                $this->rules[self::ALL][self::MD] = self::MD;
                $this->rules[self::MDY][self::DM] = self::MD;
@@ -121,7 +127,7 @@ class DateFormatter {
         * Get a DateFormatter object
         *
         * @param Language|string|null $lang In which language to format the date
-        *              Defaults to the site content language
+        *     Defaults to the site content language
         * @return DateFormatter
         */
        public static function getInstance( $lang = null ) {
@@ -191,17 +197,19 @@ class DateFormatter {
 
                        // Another horrible hack
                        $this->mLinked = $linked;
-                       $text = preg_replace_callback( $regex, [ &$this, 'replace' ], $text );
+                       $text = preg_replace_callback( $regex, [ $this, 'replace' ], $text );
                        unset( $this->mLinked );
                }
                return $text;
        }
 
        /**
+        * Regexp replacement callback
+        *
         * @param array $matches
         * @return string
         */
-       public function replace( $matches ) {
+       private function replace( $matches ) {
                # Extract information from $matches
                $linked = true;
                if ( isset( $this->mLinked ) ) {
@@ -217,15 +225,17 @@ class DateFormatter {
                        }
                }
 
-               return $this->formatDate( $bits, $linked );
+               return $this->formatDate( $bits, $matches[0], $linked );
        }
 
        /**
         * @param array $bits
+        * @param string $orig Original input string, to be returned
+        *  on formatting failure.
         * @param bool $link
         * @return string
         */
-       public function formatDate( $bits, $link = true ) {
+       private function formatDate( $bits, $orig, $link = true ) {
                $format = $this->targets[$this->mTarget];
 
                if ( !$link ) {
@@ -300,8 +310,9 @@ class DateFormatter {
                        }
                }
                if ( $fail ) {
-                       /** @todo FIXME: $matches doesn't exist here, what's expected? */
-                       $text = $matches[0];
+                       // This occurs when parsing a date with day or month outside the bounds
+                       // of possibilities.
+                       $text = $orig;
                }
 
                $isoBits = [];
@@ -323,7 +334,7 @@ class DateFormatter {
         * Return a regex that can be used to find month names in string
         * @return string regex to find the months with
         */
-       public function getMonthRegex() {
+       private function getMonthRegex() {
                $names = [];
                for ( $i = 1; $i <= 12; $i++ ) {
                        $names[] = $this->lang->getMonthName( $i );
@@ -337,7 +348,7 @@ class DateFormatter {
         * @param string $monthName Month name
         * @return string ISO month name
         */
-       public function makeIsoMonth( $monthName ) {
+       private function makeIsoMonth( $monthName ) {
                $n = $this->xMonths[$this->lang->lc( $monthName )];
                return sprintf( '%02d', $n );
        }
@@ -347,7 +358,7 @@ class DateFormatter {
         * @param string $year Year name
         * @return string ISO year name
         */
-       public function makeIsoYear( $year ) {
+       private function makeIsoYear( $year ) {
                # Assumes the year is in a nice format, as enforced by the regex
                if ( substr( $year, -2 ) == 'BC' ) {
                        $num = intval( substr( $year, 0, -3 ) ) - 1;
@@ -366,7 +377,7 @@ class DateFormatter {
         * @return int|string int representing year number in case of AD dates, or string containing
         *   year number and 'BC' at the end otherwise.
         */
-       public function makeNormalYear( $iso ) {
+       private function makeNormalYear( $iso ) {
                if ( $iso[0] == '-' ) {
                        $text = ( intval( substr( $iso, 1 ) ) + 1 ) . ' BC';
                } else {