Translation updates from translatewiki.net
[lhc/web/wiklou.git] / languages / LanguageConverter.php
index 23c7cb0..2b9cc8e 100644 (file)
@@ -72,7 +72,7 @@ class LanguageConverter {
                $this->mMainLanguageCode = $maincode;
                $this->mVariants = array_diff( $variants, $wgDisabledVariants );
                $this->mVariantFallbacks = $variantfallbacks;
-               $this->mVariantNames = Language::getLanguageNames();
+               $this->mVariantNames = Language::fetchLanguageNames();
                $this->mCacheKey = wfMemcKey( 'conversiontables', $maincode );
                $defaultflags = array(
                        // 'S' show converted text
@@ -117,7 +117,7 @@ class LanguageConverter {
         * in this case. Right now this is only used by zh.
         *
         * @param $variant String: the language code of the variant
-        * @return String: The code of the fallback language or the
+        * @return String|array: The code of the fallback language or the
         *                               main code if there is no fallback
         */
        public function getVariantFallbacks( $variant ) {
@@ -158,7 +158,7 @@ class LanguageConverter {
                // not memoized (i.e. there return value is not cached) since
                // new information might appear during processing after this
                // is first called.
-               if ( $req = $this->validateVariant( $req ) ) {
+               if ( $this->validateVariant( $req ) ) {
                        return $req;
                }
                return $this->mMainLanguageCode;
@@ -189,7 +189,7 @@ class LanguageConverter {
         * @param $variant String: the variant to validate
         * @return Mixed: returns the variant if it is valid, null otherwise
         */
-       protected function validateVariant( $variant = null ) {
+       public function validateVariant( $variant = null ) {
                if ( $variant !== null && in_array( $variant, $this->mVariants ) ) {
                        return $variant;
                }
@@ -322,6 +322,11 @@ class LanguageConverter {
                        }
                }
 
+               if( $this->guessVariant( $text, $toVariant ) ) {
+                       wfProfileOut( __METHOD__ );
+                       return $text;
+               }
+
                /* we convert everything except:
                   1. HTML markups (anything between < and >)
                   2. HTML entities
@@ -368,11 +373,11 @@ class LanguageConverter {
                        $sourceBlob .= substr( $text, $startPos, $elementPos - $startPos ) . "\000";
 
                        // Advance to the next position
-                       $startPos = $elementPos + strlen( $element );           
+                       $startPos = $elementPos + strlen( $element );
 
                        // Translate any alt or title attributes inside the matched element
-                       if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element, 
-                               $elementMatches ) ) 
+                       if ( $element !== '' && preg_match( '/^(<[^>\s]*)\s([^>]*)(.*)$/', $element,
+                               $elementMatches ) )
                        {
                                $attrs = Sanitizer::decodeTagAttributes( $elementMatches[2] );
                                $changed = false;
@@ -385,7 +390,7 @@ class LanguageConverter {
                                        if ( !strpos( $attr, '://' ) ) {
                                                $attr = $this->translate( $attr, $toVariant );
                                        }
-                                       
+
                                        // Remove HTML tags to avoid disrupting the layout
                                        $attr = preg_replace( '/<[^>]+>/', '', $attr );
                                        if ( $attr !== $attrs[$attrName] ) {
@@ -394,7 +399,7 @@ class LanguageConverter {
                                        }
                                }
                                if ( $changed ) {
-                                       $element = $elementMatches[1] . Html::expandAttributes( $attrs ) . 
+                                       $element = $elementMatches[1] . Html::expandAttributes( $attrs ) .
                                                $elementMatches[3];
                                }
                        }
@@ -571,7 +576,7 @@ class LanguageConverter {
         */
        public function convertTo( $text, $variant ) {
                global $wgDisableLangConversion;
-               if ( $wgDisableLangConversion ) {
+               if ( $wgDisableLangConversion || $this->guessVariant( $text, $variant ) ) {
                        return $text;
                }
                return $this->recursiveConvertTopLevel( $text, $variant );
@@ -621,6 +626,7 @@ class LanguageConverter {
         * @param $startPos int
         * @param $depth Integer: depth of recursion
         *
+        * @throws MWException
         * @return String: converted text
         */
        protected function recursiveConvertRule( $text, $variant, &$startPos, $depth = 0 ) {
@@ -772,11 +778,26 @@ class LanguageConverter {
                return '!' . $variant;
        }
 
+       /**
+        * Guess if a text is written in a variant. This should be implemented in subclasses.
+        *
+        * @param string        $text the text to be checked
+        * @param string        $variant language code of the variant to be checked for
+        * @return bool true if $text appears to be written in $variant, false if not
+        *
+        * @author Nikola Smolenski <smolensk@eunet.rs>
+        * @since 1.19
+        */
+       public function guessVariant($text, $variant) {
+               return false;
+       }
+
        /**
         * Load default conversion tables.
         * This method must be implemented in derived class.
         *
         * @private
+        * @throws MWException
         */
        function loadDefaultTables() {
                $name = get_class( $this );
@@ -870,26 +891,26 @@ class LanguageConverter {
                        return array();
                }
 
-               if ( strpos( $code, '/' ) === false ) {
-                       $txt = MessageCache::singleton()->get( 'Conversiontable', true, $code );
-                       if ( $txt === false ) {
-                               # @todo FIXME: This method doesn't seem to be expecting
-                               # this possible outcome...
-                               $txt = '&lt;Conversiontable&gt;';
-                       }
+               $parsed[$key] = true;
+
+               if ( $subpage === '' ) {
+                       $txt = MessageCache::singleton()->get( 'conversiontable', true, $code );
                } else {
-                       $title = Title::makeTitleSafe(
-                               NS_MEDIAWIKI,
-                               "Conversiontable/$code"
-                       );
+                       $txt = false;
+                       $title = Title::makeTitleSafe( NS_MEDIAWIKI, $key );
                        if ( $title && $title->exists() ) {
-                               $article = new Article( $title );
-                               $txt = $article->getContents();
-                       } else {
-                               $txt = '';
+                               $revision = Revision::newFromTitle( $title );
+                               if ( $revision ) {
+                                       $txt = $revision->getRawText();
+                               }
                        }
                }
 
+               # Nothing to parse if there's no text
+               if ( $txt === false || $txt === null || $txt === '' ) {
+                       return array();
+               }
+
                // get all subpage links of the form
                // [[MediaWiki:Conversiontable/zh-xx/...|...]]
                $linkhead = $this->mLangObj->getNsText( NS_MEDIAWIKI ) .
@@ -938,7 +959,6 @@ class LanguageConverter {
                                $ret[trim( $m[0] )] = trim( $tt[0] );
                        }
                }
-               $parsed[$key] = true;
 
                // recursively parse the subpages
                if ( $recursive ) {
@@ -997,8 +1017,8 @@ class LanguageConverter {
         * @param $summary String: edit summary of the edit
         * @param $isMinor Boolean: was the edit marked as minor?
         * @param $isWatch Boolean: did the user watch this page or not?
-        * @param $section Unused
-        * @param $flags Bitfield
+        * @param $section
+        * @param $flags int Bitfield
         * @param $revision Object: new Revision object or null
         * @return Boolean: true
         */
@@ -1364,19 +1384,21 @@ class ConverterRule {
                        if ( isset( $this->mVariantFlags[$variant] ) ) {
                                // then convert <text to convert> to current language
                                $this->mRules = $this->mConverter->autoConvert( $this->mRules,
-                                                                                                                               $variant );
+                                       $variant );
                        } else { // if current variant no in flags,
                                   // then we check its fallback variants.
                                $variantFallbacks =
                                        $this->mConverter->getVariantFallbacks( $variant );
-                               foreach ( $variantFallbacks as $variantFallback ) {
-                                       // if current variant's fallback exist in flags
-                                       if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
-                                               // then convert <text to convert> to fallback language
-                                               $this->mRules =
-                                                       $this->mConverter->autoConvert( $this->mRules,
-                                                                                                                       $variantFallback );
-                                               break;
+                               if( is_array( $variantFallbacks ) ) {
+                                       foreach ( $variantFallbacks as $variantFallback ) {
+                                               // if current variant's fallback exist in flags
+                                               if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
+                                                       // then convert <text to convert> to fallback language
+                                                       $this->mRules =
+                                                               $this->mConverter->autoConvert( $this->mRules,
+                                                                       $variantFallback );
+                                                       break;
+                                               }
                                        }
                                }
                        }