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