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