Merge "(Bug 40367) MediaWiki:Contributions should support gender"
[lhc/web/wiklou.git] / languages / Language.php
index 40d1f36..7475d2f 100644 (file)
@@ -48,12 +48,13 @@ class FakeConverter {
        /**
         * @var Language
         */
-       var $mLang;
+       public $mLang;
        function __construct( $langobj ) { $this->mLang = $langobj; }
        function autoConvertToAllVariants( $text ) { return array( $this->mLang->getCode() => $text ); }
        function convert( $t ) { return $t; }
        function convertTo( $text, $variant ) { return $text; }
        function convertTitle( $t ) { return $t->getPrefixedText(); }
+       function convertNamespace( $ns ) { return $this->mLang->getFormattedNsText( $ns ); }
        function getVariants() { return array( $this->mLang->getCode() ); }
        function getPreferredVariant() { return $this->mLang->getCode(); }
        function getDefaultVariant() { return $this->mLang->getCode(); }
@@ -77,21 +78,21 @@ class Language {
        /**
         * @var LanguageConverter
         */
-       var $mConverter;
+       public $mConverter;
 
-       var $mVariants, $mCode, $mLoaded = false;
-       var $mMagicExtensions = array(), $mMagicHookDone = false;
+       public $mVariants, $mCode, $mLoaded = false;
+       public $mMagicExtensions = array(), $mMagicHookDone = false;
        private $mHtmlCode = null;
 
-       var $dateFormatStrings = array();
-       var $mExtendedSpecialPageAliases;
+       public $dateFormatStrings = array();
+       public $mExtendedSpecialPageAliases;
 
        protected $namespaceNames, $mNamespaceIds, $namespaceAliases;
 
        /**
         * ReplacementArray object caches
         */
-       var $transformData = array();
+       public $transformData = array();
 
        /**
         * @var LocalisationCache
@@ -356,7 +357,7 @@ class Language {
         * @deprecated in 1.19
         */
        function getFallbackLanguageCode() {
-               wfDeprecated( __METHOD__ );
+               wfDeprecated( __METHOD__, '1.19' );
                return self::getFallbackFor( $this->mCode );
        }
 
@@ -419,6 +420,16 @@ class Language {
         */
        public function setNamespaces( array $namespaces ) {
                $this->namespaceNames = $namespaces;
+               $this->mNamespaceIds = null;
+       }
+
+       /**
+        * Resets all of the namespace caches. Mainly used for testing
+        */
+       public function resetNamespaces( ) {
+               $this->namespaceNames = null;
+               $this->mNamespaceIds = null;
+               $this->namespaceAliases = null;
        }
 
        /**
@@ -3409,6 +3420,18 @@ class Language {
                if ( !count( $forms ) ) {
                        return '';
                }
+
+               // Handle explicit 0= and 1= forms
+               foreach ( $forms as $index => $form ) {
+                       if ( isset( $form[1] ) && $form[1] === '=' ) {
+                               if ( $form[0] === (string) $count ) {
+                                       return substr( $form, 2 );
+                               }
+                               unset( $forms[$index] );
+                       }
+               }
+               $forms = array_values( $forms );
+
                $pluralForm = $this->getPluralForm( $count );
                $pluralForm = min( $pluralForm, count( $forms ) - 1 );
                return $forms[$pluralForm];
@@ -3459,8 +3482,22 @@ class Language {
                                }
                        }
                }
-               // If all else fails, return the original string.
-               return $str;
+
+               // If all else fails, return a standard duration or timestamp description.
+               $time = strtotime( $str, 0 );
+               if ( $time === false ) { // Unknown format. Return it as-is in case.
+                       return $str;
+               } elseif ( $time !== strtotime( $str, 1 ) ) { // It's a relative timestamp.
+                       // $time is relative to 0 so it's a duration length.
+                       return $this->formatDuration( $time );
+               } else { // It's an absolute timestamp.
+                       if ( $time === 0 ) {
+                               // wfTimestamp() handles 0 as current time instead of epoch.
+                               return $this->timeanddate( '19700101000000' );
+                       } else {
+                               return $this->timeanddate( $time );
+                       }
+               }
        }
 
        /**
@@ -3524,6 +3561,16 @@ class Language {
                return $this->mConverter->convertTitle( $title );
        }
 
+       /**
+        * Convert a namespace index to a string in the preferred variant
+        *
+        * @param $ns int
+        * @return string
+        */
+       public function convertNamespace( $ns ) {
+               return $this->mConverter->convertNamespace( $ns );
+       }
+
        /**
         * Check if this is a language with variants
         *
@@ -3657,15 +3704,24 @@ class Language {
        }
 
        /**
-        * Enclose a string with the "no conversion" tag. This is used by
-        * various functions in the Parser
+        * Prepare external link text for conversion. When the text is
+        * a URL, it shouldn't be converted, and it'll be wrapped in
+        * the "raw" tag (-{R| }-) to prevent conversion.
         *
-        * @param $text String: text to be tagged for no conversion
-        * @param $noParse bool
+        * This function is called "markNoConversion" for historical
+        * reasons.
+        *
+        * @param $text String: text to be used for external link
+        * @param $noParse bool: wrap it without confirming it's a real URL first
         * @return string the tagged text
         */
        public function markNoConversion( $text, $noParse = false ) {
-               return $this->mConverter->markNoConversion( $text, $noParse );
+               // Excluding protocal-relative URLs may avoid many false positives.
+               if ( $noParse || preg_match( '/^(?:' . wfUrlProtocolsWithoutProtRel() . ')/', $text ) ) {
+                       return $this->mConverter->markNoConversion( $text );
+               } else {
+                       return $text;
+               }
        }
 
        /**