MediaWiki  REL1_20
Fallback.php
Go to the documentation of this file.
00001 <?php
00026 class Fallback {
00027 
00034         public static function iconv( $from, $to, $string ) {
00035                 if ( substr( $to, -8 ) == '//IGNORE' ) {
00036                         $to = substr( $to, 0, strlen( $to ) - 8 );
00037                 }
00038                 if( strcasecmp( $from, $to ) == 0 ) {
00039                         return $string;
00040                 }
00041                 if( strcasecmp( $from, 'utf-8' ) == 0 ) {
00042                         return utf8_decode( $string );
00043                 }
00044                 if( strcasecmp( $to, 'utf-8' ) == 0 ) {
00045                         return utf8_encode( $string );
00046                 }
00047                 return $string;
00048         }
00049 
00066         public static function mb_substr( $str, $start, $count = 'end' ) {
00067                 if( $start != 0 ) {
00068                         $split = self::mb_substr_split_unicode( $str, intval( $start ) );
00069                         $str = substr( $str, $split );
00070                 }
00071 
00072                 if( $count !== 'end' ) {
00073                         $split = self::mb_substr_split_unicode( $str, intval( $count ) );
00074                         $str = substr( $str, 0, $split );
00075                 }
00076 
00077                 return $str;
00078         }
00079 
00085         public static function mb_substr_split_unicode( $str, $splitPos ) {
00086                 if( $splitPos == 0 ) {
00087                         return 0;
00088                 }
00089 
00090                 $byteLen = strlen( $str );
00091 
00092                 if( $splitPos > 0 ) {
00093                         if( $splitPos > 256 ) {
00094                                 // Optimize large string offsets by skipping ahead N bytes.
00095                                 // This will cut out most of our slow time on Latin-based text,
00096                                 // and 1/2 to 1/3 on East European and Asian scripts.
00097                                 $bytePos = $splitPos;
00098                                 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00099                                         ++$bytePos;
00100                                 }
00101                                 $charPos = mb_strlen( substr( $str, 0, $bytePos ) );
00102                         } else {
00103                                 $charPos = 0;
00104                                 $bytePos = 0;
00105                         }
00106 
00107                         while( $charPos++ < $splitPos ) {
00108                                 ++$bytePos;
00109                                 // Move past any tail bytes
00110                                 while ( $bytePos < $byteLen && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00111                                         ++$bytePos;
00112                                 }
00113                         }
00114                 } else {
00115                         $splitPosX = $splitPos + 1;
00116                         $charPos = 0; // relative to end of string; we don't care about the actual char position here
00117                         $bytePos = $byteLen;
00118                         while( $bytePos > 0 && $charPos-- >= $splitPosX ) {
00119                                 --$bytePos;
00120                                 // Move past any tail bytes
00121                                 while ( $bytePos > 0 && $str[$bytePos] >= "\x80" && $str[$bytePos] < "\xc0" ) {
00122                                         --$bytePos;
00123                                 }
00124                         }
00125                 }
00126 
00127                 return $bytePos;
00128         }
00129 
00136         public static function mb_strlen( $str, $enc = '' ) {
00137                 $counts = count_chars( $str );
00138                 $total = 0;
00139 
00140                 // Count ASCII bytes
00141                 for( $i = 0; $i < 0x80; $i++ ) {
00142                         $total += $counts[$i];
00143                 }
00144 
00145                 // Count multibyte sequence heads
00146                 for( $i = 0xc0; $i < 0xff; $i++ ) {
00147                         $total += $counts[$i];
00148                 }
00149                 return $total;
00150         }
00151 
00152 
00161         public static function mb_strpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
00162                 $needle = preg_quote( $needle, '/' );
00163 
00164                 $ar = array();
00165                 preg_match( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
00166 
00167                 if( isset( $ar[0][1] ) ) {
00168                         return $ar[0][1];
00169                 } else {
00170                         return false;
00171                 }
00172         }
00173 
00182         public static function mb_strrpos( $haystack, $needle, $offset = 0, $encoding = '' ) {
00183                 $needle = preg_quote( $needle, '/' );
00184 
00185                 $ar = array();
00186                 preg_match_all( '/' . $needle . '/u', $haystack, $ar, PREG_OFFSET_CAPTURE, $offset );
00187 
00188                 if( isset( $ar[0] ) && count( $ar[0] ) > 0 &&
00189                         isset( $ar[0][count( $ar[0] ) - 1][1] ) ) {
00190                         return $ar[0][count( $ar[0] ) - 1][1];
00191                 } else {
00192                         return false;
00193                 }
00194         }
00195 
00202         public static function stream_resolve_include_path( $filename ) {
00203                 $pathArray = explode( PATH_SEPARATOR, get_include_path() );
00204                 foreach ( $pathArray as $path ) {
00205                         $fullFilename = $path . DIRECTORY_SEPARATOR . $filename;
00206                         if ( file_exists( $fullFilename ) ) {
00207                                 return $fullFilename;
00208                         }
00209                 }
00210                 return false;
00211         }
00212 
00213 }