LangRU update to lang 1.659
[lhc/web/wiklou.git] / languages / LanguageUtf8.php
index 8b86bd0..67433b8 100644 (file)
@@ -1,19 +1,29 @@
 <?php
-#$Id$
+/**
+  * @package MediaWiki
+  * @subpackage Language
+  */
+
 if( defined( "MEDIAWIKI" ) ) {
 
+# This file and LanguageLatin1.php may be included from within functions, so
+# we need to have global statements
+
+global $wgInputEncoding, $wgOutputEncoding, $wikiUpperChars, $wikiLowerChars;
+global $wgDBname, $wgMemc;
+
 $wgInputEncoding    = "UTF-8";
 $wgOutputEncoding      = "UTF-8";
 
-if (function_exists('mb_internal_encoding')) {
+if( function_exists( 'mb_strtoupper' ) ) {
        mb_internal_encoding('UTF-8');
 } else {
        # Hack our own case conversion routines
-       
+
        # Loading serialized arrays is faster than parsing code :P
        $wikiUpperChars = $wgMemc->get( $key1 = "$wgDBname:utf8:upper" );
        $wikiLowerChars = $wgMemc->get( $key2 = "$wgDBname:utf8:lower" );
-       
+
        if(empty( $wikiUpperChars) || empty($wikiLowerChars )) {
                require_once( "includes/Utf8Case.php" );
                $wgMemc->set( $key1, $wikiUpperChars );
@@ -21,26 +31,38 @@ if (function_exists('mb_internal_encoding')) {
        }
 }
 
-# Base stuff useful to all UTF-8 based language files
+/**
+ * Base stuff useful to all UTF-8 based language files
+ * @package MediaWiki
+ */
 class LanguageUtf8 extends Language {
 
        # These two functions use mbstring library, if it is loaded
-       # or compiled and character mapping arrays otherwise. 
+       # or compiled and character mapping arrays otherwise.
        # In case of language-specific character mismatch
        # it should be dealt with in Language classes.
 
        function ucfirst( $string ) {
-               if (function_exists('mb_strtoupper')) {
-                       return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1);
-               } else {
-                   global $wikiUpperChars;
-                   return preg_replace (
-                   "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
-                   "strtr ( \"\$1\" , \$wikiUpperChars )",
-                   $string );
+               /**
+                * On pages with many links we can get called a lot.
+                * The multibyte uppercase functions are relatively
+                * slow, so check first if we can use a faster ASCII
+                * version instead; it saves a few milliseconds.
+                */
+               if( preg_match( '/^[\x80-\xff]/', $string ) ) {
+                       if (function_exists('mb_strtoupper')) {
+                               return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1);
+                       } else {
+                               global $wikiUpperChars;
+                               return preg_replace (
+                                       "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
+                                       "strtr ( \"\$1\" , \$wikiUpperChars )",
+                                       $string );
+                       }
                }
+               return ucfirst( $string );
        }
-       
+
        function lcfirst( $string ) {
                if (function_exists('mb_strtolower')) {
                        return mb_strtolower(mb_substr($string,0,1)).mb_substr($string,1);
@@ -61,18 +83,22 @@ class LanguageUtf8 extends Language {
                # all strtolower on stripped output or argument
                # should be removed and all stripForSearch
                # methods adjusted to that.
+
+               wfProfileIn( "LanguageUtf8::stripForSearch" );
                if( function_exists( 'mb_strtolower' ) ) {
-                       return preg_replace(
+                       $out = preg_replace(
                                "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
                                "'U8' . bin2hex( \"$1\" )",
                                mb_strtolower( $string ) );
                } else {
                        global $wikiLowerChars;
-                       return preg_replace(
+                       $out = preg_replace(
                                "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
                                "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
                                $string );
                }
+               wfProfileOut( "LanguageUtf8::stripForSearch" );
+               return $out;
        }
 
        function fallback8bitEncoding() {
@@ -85,10 +111,13 @@ class LanguageUtf8 extends Language {
        function checkTitleEncoding( $s ) {
                global $wgInputEncoding;
 
+               if( is_array( $s ) ) {
+                       wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
+               }
                # Check for non-UTF-8 URLs
                $ishigh = preg_match( '/[\x80-\xff]/', $s);
                if(!$ishigh) return $s;
-               
+
                $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
                 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
                if( $isutf8 ) return $s;
@@ -99,7 +128,7 @@ class LanguageUtf8 extends Language {
        function firstChar( $s ) {
                preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
                '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
-               
+
                return isset( $matches[1] ) ? $matches[1] : "";
        }