fix some issues with phpdoc
[lhc/web/wiklou.git] / languages / LanguageUtf8.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Language
5 */
6
7 if( defined( "MEDIAWIKI" ) ) {
8
9 # This file and LanguageLatin1.php may be included from within functions, so
10 # we need to have global statements
11
12 global $wgInputEncoding, $wgOutputEncoding, $wikiUpperChars, $wikiLowerChars;
13 global $wgDBname, $wgMemc;
14
15 $wgInputEncoding = "UTF-8";
16 $wgOutputEncoding = "UTF-8";
17
18 if( function_exists( 'mb_strtoupper' ) ) {
19 mb_internal_encoding('UTF-8');
20 } else {
21 # Hack our own case conversion routines
22
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" );
26
27 if(empty( $wikiUpperChars) || empty($wikiLowerChars )) {
28 require_once( "includes/Utf8Case.php" );
29 $wgMemc->set( $key1, $wikiUpperChars );
30 $wgMemc->set( $key2, $wikiLowerChars );
31 }
32 }
33
34 /**
35 * Base stuff useful to all UTF-8 based language files
36 * @package MediaWiki
37 */
38 class LanguageUtf8 extends Language {
39
40 # These two 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.
44
45 function ucfirst( $string ) {
46 /**
47 * On pages with many links we can get called a lot.
48 * The multibyte uppercase functions are relatively
49 * slow, so check first if we can use a faster ASCII
50 * version instead; it saves a few milliseconds.
51 */
52 if( preg_match( '/^[\x80-\xff]/', $string ) ) {
53 if (function_exists('mb_strtoupper')) {
54 return mb_strtoupper(mb_substr($string,0,1)).mb_substr($string,1);
55 } else {
56 global $wikiUpperChars;
57 return preg_replace (
58 "/^([a-z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
59 "strtr ( \"\$1\" , \$wikiUpperChars )",
60 $string );
61 }
62 }
63 return ucfirst( $string );
64 }
65
66 function lcfirst( $string ) {
67 if (function_exists('mb_strtolower')) {
68 return mb_strtolower(mb_substr($string,0,1)).mb_substr($string,1);
69 } else {
70 global $wikiLowerChars;
71 return preg_replace (
72 "/^([A-Z]|[\\xc0-\\xff][\\x80-\\xbf]*)/e",
73 "strtr ( \"\$1\" , \$wikiLowerChars )",
74 $string );
75 }
76 }
77
78 function stripForSearch( $string ) {
79 # MySQL fulltext index doesn't grok utf-8, so we
80 # need to fold cases and convert to hex
81
82 # In Language:: it just returns lowercase, maybe
83 # all strtolower on stripped output or argument
84 # should be removed and all stripForSearch
85 # methods adjusted to that.
86
87 wfProfileIn( "LanguageUtf8::stripForSearch" );
88 if( function_exists( 'mb_strtolower' ) ) {
89 $out = preg_replace(
90 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
91 "'U8' . bin2hex( \"$1\" )",
92 mb_strtolower( $string ) );
93 } else {
94 global $wikiLowerChars;
95 $out = preg_replace(
96 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
97 "'U8' . bin2hex( strtr( \"\$1\", \$wikiLowerChars ) )",
98 $string );
99 }
100 wfProfileOut( "LanguageUtf8::stripForSearch" );
101 return $out;
102 }
103
104 function fallback8bitEncoding() {
105 # Windows codepage 1252 is a superset of iso 8859-1
106 # override this to use difference source encoding to
107 # translate incoming 8-bit URLs.
108 return "windows-1252";
109 }
110
111 function checkTitleEncoding( $s ) {
112 global $wgInputEncoding;
113
114 if( is_array( $s ) ) {
115 wfDebugDieBacktrace( 'Given array to checkTitleEncoding.' );
116 }
117 # Check for non-UTF-8 URLs
118 $ishigh = preg_match( '/[\x80-\xff]/', $s);
119 if(!$ishigh) return $s;
120
121 $isutf8 = preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
122 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})+$/', $s );
123 if( $isutf8 ) return $s;
124
125 return $this->iconv( $this->fallback8bitEncoding(), "utf-8", $s );
126 }
127
128 function firstChar( $s ) {
129 preg_match( '/^([\x00-\x7f]|[\xc0-\xdf][\x80-\xbf]|' .
130 '[\xe0-\xef][\x80-\xbf]{2}|[\xf0-\xf7][\x80-\xbf]{3})/', $s, $matches);
131
132 return isset( $matches[1] ) ? $matches[1] : "";
133 }
134
135 # Crop a string from the beginning or end to a certain number of bytes.
136 # (Bytes are used because our storage has limited byte lengths for some
137 # columns in the database.) Multibyte charsets will need to make sure that
138 # only whole characters are included!
139 #
140 # $length does not include the optional ellipsis.
141 # If $length is negative, snip from the beginning
142 function truncate( $string, $length, $ellipsis = "" ) {
143 if( $length == 0 ) {
144 return $ellipsis;
145 }
146 if ( strlen( $string ) <= abs( $length ) ) {
147 return $string;
148 }
149 if( $length > 0 ) {
150 $string = substr( $string, 0, $length );
151 $char = ord( $string[strlen( $string ) - 1] );
152 if ($char >= 0xc0) {
153 # We got the first byte only of a multibyte char; remove it.
154 $string = substr( $string, 0, -1 );
155 } elseif( $char >= 0x80 &&
156 preg_match( '/^(.*)(?:[\xe0-\xef][\x80-\xbf]|' .
157 '[\xf0-\xf7][\x80-\xbf]{1,2})$/', $string, $m ) ) {
158 # We chopped in the middle of a character; remove it
159 $string = $m[1];
160 }
161 return $string . $ellipsis;
162 } else {
163 $string = substr( $string, $length );
164 $char = ord( $string[0] );
165 if( $char >= 0x80 && $char < 0xc0 ) {
166 # We chopped in the middle of a character; remove the whole thing
167 $string = preg_replace( '/^[\x80-\xbf]+/', '', $string );
168 }
169 return $ellipsis . $string;
170 }
171 }
172 }
173
174 } # ifdef MEDIAWIKI
175
176 ?>