Fix for bug 9413 and the related Malayalam issue reported on wikitech-l.
[lhc/web/wiklou.git] / languages / Language.php
index 19520fa..690d592 100644 (file)
@@ -62,6 +62,11 @@ class Language {
        var $minSearchLength;
        var $mExtendedSpecialPageAliases;
 
+       /**
+        * ReplacementArray object caches
+        */
+       var $transformData = array();
+
        static public $dataCache;
        static public $mLangObjCache = array();
 
@@ -1509,9 +1514,15 @@ class Language {
        }
 
        function ucfirst( $str ) {
-               if ( empty($str) ) return $str;
-               if ( ord($str[0]) < 128 ) return ucfirst($str);
-               else return self::uc($str,true); // fall back to more complex logic in case of multibyte strings
+               $o = ord( $str );
+               if ( $o < 96 ) {
+                       return $str;
+               } elseif ( $o < 128 ) {
+                       return ucfirst($str);
+               } else {
+                       // fall back to more complex logic in case of multibyte strings
+                       return self::uc($str,true); 
+               }
        }
 
        function uc( $str, $first = false ) {
@@ -1541,13 +1552,17 @@ class Language {
        }
        
        function lcfirst( $str ) {
-               if ( empty($str) ) return $str;
-               if ( is_string( $str ) && ord($str[0]) < 128 ) {
-                       // editing string in place = cool
-                       $str[0]=strtolower($str[0]);
+               $o = ord( $str );
+               if ( !$o ) {
+                       return strval( $str );
+               } elseif ( $o >= 128 ) {
+                       return self::lc( $str, true );
+               } elseif ( $o > 96 ) {
+                       return $str;
+               } else {
+                       $str[0] = strtolower( $str[0] );
                        return $str;
                }
-               else return self::lc( $str, true );
        }
 
        function lc( $str, $first = false ) {
@@ -1855,6 +1870,36 @@ class Language {
                }
        }
 
+       /**
+        * Convert a UTF-8 string to normal form C. In Malayalam and Arabic, this
+        * also cleans up certain backwards-compatible sequences, converting them 
+        * to the modern Unicode equivalent.
+        *
+        * This is language-specific for performance reasons only.
+        */
+       function normalize( $s ) {
+               return UtfNormal::cleanUp( $s );
+       }
+
+       /**
+        * Transform a string using serialized data stored in the given file (which
+        * must be in the serialized subdirectory of $IP). The file contains pairs
+        * mapping source characters to destination characters. 
+        *
+        * The data is cached in process memory. This will go faster if you have the 
+        * FastStringSearch extension.
+        */
+       function transformUsingPairFile( $file, $string ) {
+               if ( !isset( $this->transformData[$file] ) ) {
+                       $data = wfGetPrecompiledData( $file );
+                       if ( $data === false ) {
+                               throw new MWException( __METHOD__.": The transformation file $file is missing" );
+                       }
+                       $this->transformData[$file] = new ReplacementArray( $data );
+               }
+               return $this->transformData[$file]->replace( $string );
+       }
+
        /**
         * For right-to-left language support
         *
@@ -1935,7 +1980,9 @@ class Language {
        function getMagic( $mw ) {
                if ( !$this->mMagicHookDone ) {
                        $this->mMagicHookDone = true;
+                       wfProfileIn( 'LanguageGetMagic' );
                        wfRunHooks( 'LanguageGetMagic', array( &$this->mMagicExtensions, $this->getCode() ) );
+                       wfProfileOut( 'LanguageGetMagic' );
                }
                if ( isset( $this->mMagicExtensions[$mw->mId] ) ) {
                        $rawEntry = $this->mMagicExtensions[$mw->mId];
@@ -2357,8 +2404,8 @@ class Language {
        }
 
 
-       function getPreferredVariant( $fromUser = true ) {
-               return $this->mConverter->getPreferredVariant( $fromUser );
+       function getPreferredVariant( $fromUser = true, $fromHeader = false ) {
+               return $this->mConverter->getPreferredVariant( $fromUser, $fromHeader );
        }
 
        /**