merged master
[lhc/web/wiklou.git] / languages / LanguageConverter.php
index 3997d7f..1edc990 100644 (file)
  * @maintainers fdcn <fdcn64@gmail.com>, shinjiman <shinjiman@gmail.com>, PhiLiP <philip.npc@gmail.com>
  */
 class LanguageConverter {
+
+       /**
+        * languages supporting variants
+        * @since 1.20
+        * @var array
+        */
+       static public $languagesWithVariants = array(
+               'gan',
+               'iu',
+               'kk',
+               'ku',
+               'shi',
+               'sr',
+               'tg',
+               'zh',
+       );
+
        var $mMainLanguageCode;
        var $mVariants, $mVariantFallbacks, $mVariantNames;
        var $mTablesLoaded = false;
@@ -576,7 +593,7 @@ class LanguageConverter {
         */
        public function convertTo( $text, $variant ) {
                global $wgDisableLangConversion;
-               if ( $wgDisableLangConversion || $this->guessVariant( $text, $variant ) ) {
+               if ( $wgDisableLangConversion ) {
                        return $text;
                }
                return $this->recursiveConvertTopLevel( $text, $variant );
@@ -595,18 +612,22 @@ class LanguageConverter {
                $startPos = 0;
                $out = '';
                $length = strlen( $text );
+               $shouldConvert = !$this->guessVariant( $text, $variant );
+
                while ( $startPos < $length ) {
                        $pos = strpos( $text, '-{', $startPos );
 
                        if ( $pos === false ) {
                                // No more markup, append final segment
-                               $out .= $this->autoConvert( substr( $text, $startPos ), $variant );
+                               $fragment = substr( $text, $startPos );
+                               $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
                                return $out;
                        }
 
                        // Markup found
                        // Append initial segment
-                       $out .= $this->autoConvert( substr( $text, $startPos, $pos - $startPos ), $variant );
+                       $fragment = substr( $text, $startPos, $pos - $startPos );
+                       $out .= $shouldConvert? $this->autoConvert( $fragment, $variant ): $fragment;
 
                        // Advance position
                        $startPos = $pos;
@@ -810,6 +831,8 @@ class LanguageConverter {
         * @param $fromCache Boolean: load from memcached? Defaults to true.
         */
        function loadTables( $fromCache = true ) {
+               global $wgLangConvMemc;
+
                if ( $this->mTablesLoaded ) {
                        return;
                }
@@ -819,7 +842,7 @@ class LanguageConverter {
                $this->mTables = false;
                if ( $fromCache ) {
                        wfProfileIn( __METHOD__ . '-cache' );
-                       $this->mTables = $this->cacheFetch( $this->mCacheKey );
+                       $this->mTables = $wgLangConvMemc->get( $this->mCacheKey );
                        wfProfileOut( __METHOD__ . '-cache' );
                }
                if ( !$this->mTables
@@ -837,48 +860,12 @@ class LanguageConverter {
                        $this->postLoadTables();
                        $this->mTables[self::CACHE_VERSION_KEY] = true;
 
-                       $this->cacheStore( $this->mCacheKey, $this->mTables, 43200 );
+                       $wgLangConvMemc->set( $this->mCacheKey, $this->mTables, 43200 );
                        wfProfileOut( __METHOD__ . '-recache' );
                }
                wfProfileOut( __METHOD__ );
        }
 
-       /**
-        * Read an object from the cache
-        * @param $key string
-        * @return mixed
-        */
-       protected function cacheFetch( $key ) {
-               global $wgLanguageConverterCacheType, $wgMemc;
-
-               if ( $wgLanguageConverterCacheType === 'apc' ) {
-                       return apc_fetch( $key );
-               } elseif ( $wgLanguageConverterCacheType === 'main' ) {
-                       return $wgMemc->get( $key );
-               }
-
-               return false; // disabled
-       }
-
-       /**
-        * Store an object into the cache
-        * @param $key string
-        * @param $val mixed
-        * @param $ttl integer Seconds to live
-        * @return bool Success
-        */
-       protected function cacheStore( $key, $val, $ttl ) {
-               global $wgLanguageConverterCacheType, $wgMemc;
-
-               if ( $wgLanguageConverterCacheType === 'apc' ) {
-                       return apc_store( $key, $val, $ttl );
-               } elseif ( $wgLanguageConverterCacheType === 'main' ) {
-                       return $wgMemc->set( $key, $val, $ttl );
-               }
-
-               return true; // disabled
-       }
-
        /**
         * Hook for post processing after conversion tables are loaded.
         */
@@ -937,7 +924,11 @@ class LanguageConverter {
                        if ( $title && $title->exists() ) {
                                $revision = Revision::newFromTitle( $title );
                                if ( $revision ) {
-                                       $txt = $revision->getRawText();
+                                       if ( $revision->getContentModel() == CONTENT_MODEL_WIKITEXT ) {
+                                               $txt = $revision->getContent( Revision::RAW )->getNativeData();
+                                       }
+
+                                       //@todo: in the future, use a specialized content model, perhaps based on json!
                                }
                        }
                }