Merge "Prepare for REL1_33 cut, labelling master as 1.34-alpha"
[lhc/web/wiklou.git] / includes / parser / DateFormatter.php
index 2aefc03..c9bbc43 100644 (file)
@@ -21,6 +21,8 @@
  * @ingroup Parser
  */
 
+use MediaWiki\MediaWikiServices;
+
 /**
  * Date formatter, recognises dates in plain text and formats them according to user preferences.
  * @todo preferences, OutputPage
@@ -131,9 +133,9 @@ class DateFormatter {
         * @return DateFormatter
         */
        public static function getInstance( Language $lang = null ) {
-               global $wgContLang, $wgMainCacheType;
+               global $wgMainCacheType;
 
-               $lang = $lang ?: $wgContLang;
+               $lang = $lang ?? MediaWikiServices::getInstance()->getContentLanguage();
                $cache = ObjectCache::getLocalServerInstance( $wgMainCacheType );
 
                static $dateFormatter = false;
@@ -211,10 +213,7 @@ class DateFormatter {
         */
        private function replace( $matches ) {
                # Extract information from $matches
-               $linked = true;
-               if ( isset( $this->mLinked ) ) {
-                       $linked = $this->mLinked;
-               }
+               $linked = $this->mLinked ?? true;
 
                $bits = [];
                $key = $this->keys[$this->mSource];
@@ -259,7 +258,7 @@ class DateFormatter {
 
                if ( !isset( $bits['m'] ) ) {
                        $m = $this->makeIsoMonth( $bits['F'] );
-                       if ( !$m || $m == '00' ) {
+                       if ( $m === false ) {
                                $fail = true;
                        } else {
                                $bits['m'] = $m;
@@ -312,7 +311,7 @@ class DateFormatter {
                if ( $fail ) {
                        // This occurs when parsing a date with day or month outside the bounds
                        // of possibilities.
-                       $text = $orig;
+                       return $orig;
                }
 
                $isoBits = [];
@@ -337,8 +336,8 @@ class DateFormatter {
        private function getMonthRegex() {
                $names = [];
                for ( $i = 1; $i <= 12; $i++ ) {
-                       $names[] = $this->lang->getMonthName( $i );
-                       $names[] = $this->lang->getMonthAbbreviation( $i );
+                       $names[] = preg_quote( $this->lang->getMonthName( $i ), '/' );
+                       $names[] = preg_quote( $this->lang->getMonthAbbreviation( $i ), '/' );
                }
                return implode( '|', $names );
        }
@@ -346,11 +345,14 @@ class DateFormatter {
        /**
         * Makes an ISO month, e.g. 02, from a month name
         * @param string $monthName Month name
-        * @return string ISO month name
+        * @return string|false ISO month name, or false if the input was invalid
         */
        private function makeIsoMonth( $monthName ) {
-               $n = $this->xMonths[$this->lang->lc( $monthName )];
-               return sprintf( '%02d', $n );
+               $isoMonth = $this->xMonths[$this->lang->lc( $monthName )] ?? false;
+               if ( $isoMonth === false ) {
+                       return false;
+               }
+               return sprintf( '%02d', $isoMonth );
        }
 
        /**