Apparently we can commit code that doesn't compile but I am not allowed to have commi...
[mediawiki.git] / includes / Fallback.php
blobb517cd169e4ed05298f8f255e838ec6c2e9277c4
1 <?php
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
21 /**
22 * Fallback functions for PHP installed without mbstring support
24 class Fallback {
26 /**
27 * @param $from
28 * @param $to
29 * @param $string
30 * @return string
32 public static function iconv( $from, $to, $string ) {
33 if ( substr( $to, -8 ) == '//IGNORE' ) {
34 $to = substr( $to, 0, strlen( $to ) - 8 );
36 if( strcasecmp( $from, $to ) == 0 ) {
37 return $string;
39 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
40 return utf8_decode( $string );
42 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
43 return utf8_encode( $string );
45 return $string;
48 /**
49 * Fallback implementation for mb_substr, hardcoded to UTF-8.
50 * Attempts to be at least _moderately_ efficient; best optimized
51 * for relatively small offset and count values -- about 5x slower
52 * than native mb_string in my testing.
54 * Larger offsets are still fairly efficient for Latin text, but
55 * can be up to 100x slower than native if the text is heavily
56 * multibyte and we have to slog through a few hundred kb.
58 * @param $str
59 * @param $start
60 * @param $count string
62 * @return string
64 public static function mb_substr( $str, $start, $count = 'end' ) {
65 if( $start != 0 ) {
66 $split = self::mb_substr_split_unicode( $str, intval( $start ) );
67 $str = substr( $str, $split );
70 if( $count !== 'end' ) {
71 $split = self::mb_substr_split_unicode( $str, intval( $count ) );
72 $str = substr( $str, 0, $split );
75 return $str;
78 /**
79 * @param $str
80 * @param $splitPos
81 * @return int
83 public static function mb_substr_split_unicode( $str, $splitPos ) {
84 if( $splitPos == 0 ) {
85 return 0;
88 $byteLen = strlen( $str );
90 if( $splitPos > 0 ) {
91 if( $splitPos > 256 ) {
92 // Optimize large string offsets by skipping ahead N bytes.
93 // This will cut out most of our slow time on Latin-based text,
94 // and 1/2 to 1/3 on East European and Asian scripts.
95 $bytePos = $splitPos;
96 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
97 ++$bytePos;
99 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
100 } else {
101 $charPos = 0;
102 $bytePos = 0;
105 while( $charPos++ < $splitPos ) {
106 ++$bytePos;
107 // Move past any tail bytes
108 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
109 ++$bytePos;
112 } else {
113 $splitPosX = $splitPos + 1;
114 $charPos = 0; // relative to end of string; we don't care about the actual char position here
115 $bytePos = $byteLen;
116 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
117 --$bytePos;
118 // Move past any tail bytes
119 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
120 --$bytePos;
125 return $bytePos;
129 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
130 * @param string $str
131 * @param string $enc optional encoding; ignored
132 * @return int
134 public static function mb_strlen( $str, $enc = '' ) {
135 $counts = count_chars( $str );
136 $total = 0;
138 // Count ASCII bytes
139 for( $i = 0; $i < 0x80; $i++ ) {
140 $total += $counts[$i];
143 // Count multibyte sequence heads
144 for( $i = 0xc0; $i < 0xff; $i++ ) {
145 $total += $counts[$i];
147 return $total;
152 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
153 * @param $haystack String
154 * @param $needle String
155 * @param $offset String: optional start position
156 * @param $encoding String: optional encoding; ignored
157 * @return int
159 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
160 $needle = preg_quote( $needle, '/' );
162 $ar = array();
163 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
165 if( isset( $ar[0][1] ) ) {
166 return $ar[0][1];
167 } else {
168 return false;
173 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
174 * @param $haystack String
175 * @param $needle String
176 * @param $offset String: optional start position
177 * @param $encoding String: optional encoding; ignored
178 * @return int
180 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
181 $needle = preg_quote( $needle, '/' );
183 $ar = array();
184 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
186 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
187 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
188 return $ar[0][count( $ar[0] ) - 1][1];
189 } else {
190 return false;
195 * Fallback implementation of stream_resolve_include_path()
196 * Native stream_resolve_include_path is available for PHP 5 >= 5.3.2
197 * @param $filename String
198 * @return String
200 public static function stream_resolve_include_path( $filename ) {
201 $pathArray = explode( PATH_SEPARATOR, get_include_path() );
202 foreach ( $pathArray as $path ) {
203 $fullFilename = $path . DIRECTORY_SEPARATOR . $filename;
204 if ( file_exists( $fullFilename ) ) {
205 return $fullFilename;
208 return false;