* Remove unnecessary {{ns:#}} vars. The generic names are already replaced by the...
[lhc/web/wiklou.git] / languages / LanguageUtf8.php
1 <?php
2 #$Id$
3 if( defined( "MEDIAWIKI" ) ) {
4
5 # This file and LanguageLatin1.php may be included from within functions, so
6 # we need to have global statements
7
8 global $wgInputEncoding, $wgOutputEncoding, $wikiUpperChars, $wikiLowerChars;
9 global $wgDBname, $wgMemc;
10
11 $wgInputEncoding = "UTF-8";
12 $wgOutputEncoding = "UTF-8";
13
14 if (function_exists('mb_internal_encoding')) {
15 mb_internal_encoding('UTF-8');
16 } else {
17 # Hack our own case conversion routines
18
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" );
22
23 if(empty( $wikiUpperChars) || empty($wikiLowerChars )) {
24 require_once( "includes/Utf8Case.php" );
25 $wgMemc->set( $key1, $wikiUpperChars );
26 $wgMemc->set( $key2, $wikiLowerChars );
27 }
28 }
29
30 # Base stuff useful to all UTF-8 based language files
31 class LanguageUtf8 extends Language {
32
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.
37
38 function ucfirst( $string ) {
39 if (function_exists('mb_strtoupper')) {
40 return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1);
41 } else {
42 global $wikiUpperChars;
43 return preg_replace (
44 "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
45 "strtr ( \"\$1\" , \$wikiUpperChars )",
46 $string );
47 }
48 }
49
50 function lcfirst( $string ) {
51 if (function_exists('mb_strtolower')) {
52 return mb_strtolower(mb_substr($string,0,1)).mb_substr($string,1);
53 } else {
54 global $wikiLowerChars;
55 return preg_replace (
56 "/^([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
57 "strtr ( \"\$1\" , \$wikiLowerChars )",
58 $string );
59 }
60 }
61
62 function stripForSearch( $string ) {
63 # MySQL fulltext index doesn't grok utf-8, so we
64 # need to fold cases and convert to hex
65
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.
70
71 wfProfileIn( "LanguageUtf8::stripForSearch" );
72 if( function_exists( 'mb_strtolower' ) ) {
73 $out = preg_replace(
74 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
75 "'U8' . bin2hex( \"$1\" )",
76 mb_strtolower( $string ) );
77 } else {
78 global $wikiLowerChars;
79 $out = preg_replace(
80 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
81 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
82 $string );
83 }
84 wfProfileOut( "LanguageUtf8::stripForSearch" );
85 return $out;
86 }
87
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";
93 }
94
95 function checkTitleEncoding( $s ) {
96 global $wgInputEncoding;
97
98 # Check for non-UTF-8 URLs
99 $ishigh = preg_match( '/[\x80-\xff]/', $s);
100 if(!$ishigh) return $s;
101
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;
105
106 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
107 }
108
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);
112
113 return isset( $matches[1] ) ? $matches[1] : "";
114 }
115
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!
120 #
121 # $length does not include the optional ellipsis.
122 # If $length is negative, snip from the beginning
123 function truncate( $string, $length, $ellipsis = "" ) {
124 if( $length == 0 ) {
125 return $ellipsis;
126 }
127 if ( strlen( $string ) <= abs( $length ) ) {
128 return $string;
129 }
130 if( $length > 0 ) {
131 $string = substr( $string, 0, $length );
132 $char = ord( $string[strlen( $string ) - 1] );
133 if ($char >= 0xc0) {
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
140 $string = $m[1];
141 }
142 return $string . $ellipsis;
143 } else {
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 );
149 }
150 return $ellipsis . $string;
151 }
152 }
153 }
154
155 } # ifdef MEDIAWIKI
156
157 ?>