7 if( defined( "MEDIAWIKI" ) ) {
9 # This file and LanguageLatin1.php may be included from within functions, so
10 # we need to have global statements
12 global $wgInputEncoding, $wgOutputEncoding, $wikiUpperChars, $wikiLowerChars;
13 global $wgDBname, $wgMemc;
15 $wgInputEncoding = "UTF-8";
16 $wgOutputEncoding = "UTF-8";
18 if( function_exists( 'mb_strtoupper' ) ) {
19 mb_internal_encoding('UTF-8');
21 # Hack our own case conversion routines
23 # Loading serialized arrays is faster than parsing code :P
24 $wikiUpperChars = $wgMemc->get( $key1 = "$wgDBname:utf8:upper" );
25 $wikiLowerChars = $wgMemc->get( $key2 = "$wgDBname:utf8:lower" );
27 if(empty( $wikiUpperChars) ||
empty($wikiLowerChars )) {
28 require_once( "includes/Utf8Case.php" );
29 $wgMemc->set( $key1, $wikiUpperChars );
30 $wgMemc->set( $key2, $wikiLowerChars );
35 * Base stuff useful to all UTF-8 based language files
38 class LanguageUtf8
extends Language
{
40 # These functions use mbstring library, if it is loaded
41 # or compiled and character mapping arrays otherwise.
42 # In case of language-specific character mismatch
43 # it should be dealt with in Language classes.
45 function ucfirst( $str ) {
46 return LanguageUtf8
::uc( $str, true );
49 function uc( $str, $first = false ) {
50 if ( function_exists( 'mb_strtoupper' ) )
52 if ( LanguageUtf8
::isMultibyte( $str ) )
53 return mb_strtoupper( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
55 return ucfirst( $str );
57 return LanguageUtf8
::isMultibyte( $str ) ?
mb_strtoupper( $str ) : strtoupper( $str );
59 if ( LanguageUtf8
::isMultibyte( $str ) ) {
60 global $wikiUpperChars;
61 $x = $first ?
'^' : '';
63 "/$x([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
64 "strtr( \"\$1\" , \$wikiUpperChars )",
68 return $first ?
ucfirst( $str ) : strtoupper( $str );
71 function lcfirst( $str ) {
72 return LanguageUtf8
::lc( $str, true );
75 function lc( $str, $first = false ) {
76 if ( function_exists( 'mb_strtolower' ) )
78 if ( LanguageUtf8
::isMultibyte( $str ) )
79 return mb_strtolower( mb_substr( $str, 0, 1 ) ) . mb_substr( $str, 1 );
81 return strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 );
83 return LanguageUtf8
::isMultibyte( $str ) ?
mb_strtolower( $str ) : strtolower( $str );
85 if ( LanguageUtf8
::isMultibyte( $str ) ) {
86 global $wikiLowerChars;
87 $x = $first ?
'^' : '';
89 "/$x([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
90 "strtr( \"\$1\" , \$wikiLowerChars )",
94 return $first ?
strtolower( substr( $str, 0, 1 ) ) . substr( $str, 1 ) : strtolower( $str );
97 function isMultibyte( $str ) {
98 return (bool)preg_match( '/^[\x80-\xff]/', $str );
101 function stripForSearch( $string ) {
102 # MySQL fulltext index doesn't grok utf-8, so we
103 # need to fold cases and convert to hex
105 # In Language:: it just returns lowercase, maybe
106 # all strtolower on stripped output or argument
107 # should be removed and all stripForSearch
108 # methods adjusted to that.
110 wfProfileIn( "LanguageUtf8::stripForSearch" );
111 if( function_exists( 'mb_strtolower' ) ) {
113 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
114 "'U8' . bin2hex( \"$1\" )",
115 mb_strtolower( $string ) );
117 global $wikiLowerChars;
119 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
120 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
123 wfProfileOut( "LanguageUtf8::stripForSearch" );
127 function fallback8bitEncoding() {
128 # Windows codepage 1252 is a superset of iso 8859-1
129 # override this to use difference source encoding to
130 # translate incoming 8-bit URLs.
131 return "windows-1252";
134 function checkTitleEncoding( $s ) {
135 global $wgInputEncoding;
137 if( is_array( $s ) ) {
138 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
140 # Check for non-UTF-8 URLs
141 $ishigh = preg_match( '/[\x80-\xff]/', $s);
142 if(!$ishigh) return $s;
144 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
145 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
146 if( $isutf8 ) return $s;
148 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
151 function firstChar( $s ) {
152 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
153 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
155 return isset( $matches[1] ) ?
$matches[1] : "";
158 # Crop a string from the beginning or end to a certain number of bytes.
159 # (Bytes are used because our storage has limited byte lengths for some
160 # columns in the database.) Multibyte charsets will need to make sure that
161 # only whole characters are included!
163 # $length does not include the optional ellipsis.
164 # If $length is negative, snip from the beginning
165 function truncate( $string, $length, $ellipsis = "" ) {
169 if ( strlen( $string ) <= abs( $length ) ) {
173 $string = substr( $string, 0, $length );
174 $char = ord( $string[strlen( $string ) - 1] );
176 # We got the first byte only of a multibyte char; remove it.
177 $string = substr( $string, 0, -1 );
178 } elseif( $char >= 0x80 &&
179 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
180 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
181 # We chopped in the middle of a character; remove it
184 return $string . $ellipsis;
186 $string = substr( $string, $length );
187 $char = ord( $string[0] );
188 if( $char >= 0x80 && $char < 0xc0 ) {
189 # We chopped in the middle of a character; remove the whole thing
190 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
192 return $ellipsis . $string;