* added redundand join condition for index usage as per comment on CR r88008
[lhc/web/wiklou.git] / includes / Fallback.php
1 <?php
2
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.
8 *
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.
13 *
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
18 *
19 */
20
21 /**
22 * Fallback functions for PHP installed without mbstring support
23 */
24 class Fallback {
25
26 public static function iconv( $from, $to, $string ) {
27 if ( substr( $to, -8 ) == '//IGNORE' ) {
28 $to = substr( $to, 0, strlen( $to ) - 8 );
29 }
30 if( strcasecmp( $from, $to ) == 0 ) {
31 return $string;
32 }
33 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
34 return utf8_decode( $string );
35 }
36 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
37 return utf8_encode( $string );
38 }
39 return $string;
40 }
41
42 /**
43 * Fallback implementation for mb_substr, hardcoded to UTF-8.
44 * Attempts to be at least _moderately_ efficient; best optimized
45 * for relatively small offset and count values -- about 5x slower
46 * than native mb_string in my testing.
47 *
48 * Larger offsets are still fairly efficient for Latin text, but
49 * can be up to 100x slower than native if the text is heavily
50 * multibyte and we have to slog through a few hundred kb.
51 *
52 * @param $str
53 * @param $start
54 * @param $count string
55 *
56 * @return string
57 */
58 public static function mb_substr( $str, $start, $count = 'end' ) {
59 if( $start != 0 ) {
60 $split = self::mb_substr_split_unicode( $str, intval( $start ) );
61 $str = substr( $str, $split );
62 }
63
64 if( $count !== 'end' ) {
65 $split = self::mb_substr_split_unicode( $str, intval( $count ) );
66 $str = substr( $str, 0, $split );
67 }
68
69 return $str;
70 }
71
72 public static function mb_substr_split_unicode( $str, $splitPos ) {
73 if( $splitPos == 0 ) {
74 return 0;
75 }
76
77 $byteLen = strlen( $str );
78
79 if( $splitPos > 0 ) {
80 if( $splitPos > 256 ) {
81 // Optimize large string offsets by skipping ahead N bytes.
82 // This will cut out most of our slow time on Latin-based text,
83 // and 1/2 to 1/3 on East European and Asian scripts.
84 $bytePos = $splitPos;
85 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
86 ++$bytePos;
87 }
88 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
89 } else {
90 $charPos = 0;
91 $bytePos = 0;
92 }
93
94 while( $charPos++ < $splitPos ) {
95 ++$bytePos;
96 // Move past any tail bytes
97 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
98 ++$bytePos;
99 }
100 }
101 } else {
102 $splitPosX = $splitPos + 1;
103 $charPos = 0; // relative to end of string; we don't care about the actual char position here
104 $bytePos = $byteLen;
105 while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
106 --$bytePos;
107 // Move past any tail bytes
108 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
109 --$bytePos;
110 }
111 }
112 }
113
114 return $bytePos;
115 }
116
117 /**
118 * Fallback implementation of mb_strlen, hardcoded to UTF-8.
119 * @param string $str
120 * @param string $enc optional encoding; ignored
121 * @return int
122 */
123 public static function mb_strlen( $str, $enc = '' ) {
124 $counts = count_chars( $str );
125 $total = 0;
126
127 // Count ASCII bytes
128 for( $i = 0; $i < 0x80; $i++ ) {
129 $total += $counts[$i];
130 }
131
132 // Count multibyte sequence heads
133 for( $i = 0xc0; $i < 0xff; $i++ ) {
134 $total += $counts[$i];
135 }
136 return $total;
137 }
138
139
140 /**
141 * Fallback implementation of mb_strpos, hardcoded to UTF-8.
142 * @param $haystack String
143 * @param $needle String
144 * @param $offset String: optional start position
145 * @param $encoding String: optional encoding; ignored
146 * @return int
147 */
148 public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
149 $needle = preg_quote( $needle, '/' );
150
151 $ar = array();
152 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
153
154 if( isset( $ar[0][1] ) ) {
155 return $ar[0][1];
156 } else {
157 return false;
158 }
159 }
160
161 /**
162 * Fallback implementation of mb_strrpos, hardcoded to UTF-8.
163 * @param $haystack String
164 * @param $needle String
165 * @param $offset String: optional start position
166 * @param $encoding String: optional encoding; ignored
167 * @return int
168 */
169 public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
170 $needle = preg_quote( $needle, '/' );
171
172 $ar = array();
173 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
174
175 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
176 isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
177 return $ar[0][count( $ar[0] ) - 1][1];
178 } else {
179 return false;
180 }
181 }
182
183 /**
184 * Fallback implementation of stream_resolve_include_path()
185 * Native stream_resolve_include_path is available for PHP 5 >= 5.3.2
186 * @param $filename String
187 * @return String
188 */
189 public static function stream_resolve_include_path( $filename ) {
190 $pathArray = explode( PATH_SEPARATOR, get_include_path() );
191 foreach ( $pathArray as $path ) {
192 $fullFilename = $path . DIRECTORY_SEPARATOR . $filename;
193 if ( file_exists( $fullFilename ) ) {
194 return $fullFilename;
195 }
196 }
197 return false;
198 }
199
200 }