3 if( defined( "MEDIAWIKI" ) ) {
5 # This file and LanguageLatin1.php may be included from within functions, so
6 # we need to have global statements
8 global $wgInputEncoding, $wgOutputEncoding, $wikiUpperChars, $wikiLowerChars;
9 global $wgDBname, $wgMemc;
11 $wgInputEncoding = "UTF-8";
12 $wgOutputEncoding = "UTF-8";
14 if (function_exists('mb_internal_encoding')) {
15 mb_internal_encoding('UTF-8');
17 # Hack our own case conversion routines
19 # Loading serialized arrays is faster than parsing code :P
20 $wikiUpperChars = $wgMemc->get( $key1 = "$wgDBname:utf8:upper" );
21 $wikiLowerChars = $wgMemc->get( $key2 = "$wgDBname:utf8:lower" );
23 if(empty( $wikiUpperChars) ||
empty($wikiLowerChars )) {
24 require_once( "includes/Utf8Case.php" );
25 $wgMemc->set( $key1, $wikiUpperChars );
26 $wgMemc->set( $key2, $wikiLowerChars );
30 # Base stuff useful to all UTF-8 based language files
31 class LanguageUtf8
extends Language
{
33 # These two functions use mbstring library, if it is loaded
34 # or compiled and character mapping arrays otherwise.
35 # In case of language-specific character mismatch
36 # it should be dealt with in Language classes.
38 function ucfirst( $string ) {
39 if (function_exists('mb_strtoupper')) {
40 return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1);
42 global $wikiUpperChars;
44 "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
45 "strtr ( \"\$1\" , \$wikiUpperChars )",
50 function lcfirst( $string ) {
51 if (function_exists('mb_strtolower')) {
52 return mb_strtolower(mb_substr($string,0,1)).mb_substr($string,1);
54 global $wikiLowerChars;
56 "/^([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
57 "strtr ( \"\$1\" , \$wikiLowerChars )",
62 function stripForSearch( $string ) {
63 # MySQL fulltext index doesn't grok utf-8, so we
64 # need to fold cases and convert to hex
66 # In Language:: it just returns lowercase, maybe
67 # all strtolower on stripped output or argument
68 # should be removed and all stripForSearch
69 # methods adjusted to that.
71 wfProfileIn( "LanguageUtf8::stripForSearch" );
72 if( function_exists( 'mb_strtolower' ) ) {
74 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
75 "'U8' . bin2hex( \"$1\" )",
76 mb_strtolower( $string ) );
78 global $wikiLowerChars;
80 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
81 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
84 wfProfileOut( "LanguageUtf8::stripForSearch" );
88 function fallback8bitEncoding() {
89 # Windows codepage 1252 is a superset of iso 8859-1
90 # override this to use difference source encoding to
91 # translate incoming 8-bit URLs.
92 return "windows-1252";
95 function checkTitleEncoding( $s ) {
96 global $wgInputEncoding;
98 # Check for non-UTF-8 URLs
99 $ishigh = preg_match( '/[\x80-\xff]/', $s);
100 if(!$ishigh) return $s;
102 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
103 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
104 if( $isutf8 ) return $s;
106 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
109 function firstChar( $s ) {
110 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
111 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
113 return isset( $matches[1] ) ?
$matches[1] : "";
116 # Crop a string from the beginning or end to a certain number of bytes.
117 # (Bytes are used because our storage has limited byte lengths for some
118 # columns in the database.) Multibyte charsets will need to make sure that
119 # only whole characters are included!
121 # $length does not include the optional ellipsis.
122 # If $length is negative, snip from the beginning
123 function truncate( $string, $length, $ellipsis = "" ) {
127 if ( strlen( $string ) <= abs( $length ) ) {
131 $string = substr( $string, 0, $length );
132 $char = ord( $string[strlen( $string ) - 1] );
134 # We got the first byte only of a multibyte char; remove it.
135 $string = substr( $string, 0, -1 );
136 } elseif( $char >= 0x80 &&
137 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
138 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
139 # We chopped in the middle of a character; remove it
142 return $string . $ellipsis;
144 $string = substr( $string, $length );
145 $char = ord( $string[0] );
146 if( $char >= 0x80 && $char < 0xc0 ) {
147 # We chopped in the middle of a character; remove the whole thing
148 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
150 return $ellipsis . $string;