From: Brion Vibber Date: Mon, 7 Jul 2008 20:15:16 +0000 (+0000) Subject: Revert r37281 "Split Compatibility functions to own file" X-Git-Tag: 1.31.0-rc.0~46647 X-Git-Url: http://git.cyclocoop.org/?a=commitdiff_plain;h=c0edae192bd3fdbac439717c032e2c0a133bf320;p=lhc%2Fweb%2Fwiklou.git Revert r37281 "Split Compatibility functions to own file" This would make maintenance harder by duplicating information between GlobalFunctions.php and CompatibilityFunctions.php. If you forget to add a function to the list, the compat functions might not get loaded and you'll run into surprise errors. --- diff --git a/includes/CompatibilityFunctions.php b/includes/CompatibilityFunctions.php deleted file mode 100644 index 252a5fb973..0000000000 --- a/includes/CompatibilityFunctions.php +++ /dev/null @@ -1,76 +0,0 @@ -= 3 ) { - $end = func_get_arg( 2 ); - return join( '', array_slice( $ar[0], $start, $end ) ); - } else { - return join( '', array_slice( $ar[0], $start ) ); - } - } -} - -if ( !function_exists( 'mb_strlen' ) ) { - /** - * Fallback implementation of mb_strlen, hardcoded to UTF-8. - * @param string $str - * @param string $enc optional encoding; ignored - * @return int - */ - function mb_strlen( $str, $enc="" ) { - $counts = count_chars( $str ); - $total = 0; - - // Count ASCII bytes - for( $i = 0; $i < 0x80; $i++ ) { - $total += $counts[$i]; - } - - // Count multibyte sequence heads - for( $i = 0xc0; $i < 0xff; $i++ ) { - $total += $counts[$i]; - } - return $total; - } -} - -if ( !function_exists( 'array_diff_key' ) ) { - /** - * Exists in PHP 5.1.0+ - * Not quite compatible, two-argument version only - * Null values will cause problems due to this use of isset() - */ - function array_diff_key( $left, $right ) { - $result = $left; - foreach ( $left as $key => $unused ) { - if ( isset( $right[$key] ) ) { - unset( $result[$key] ); - } - } - return $result; - } -} diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index ff6fac0f3b..4e60bd493b 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -12,12 +12,78 @@ require_once dirname(__FILE__) . '/LogPage.php'; require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php'; require_once dirname(__FILE__) . '/XmlFunctions.php'; -$compatibilityFunctions = array( 'iconv', 'mb_substr', 'mb_strlen', 'array_diff_key' ); -foreach ( $compatibilityFunctions as $function ) { - if ( !function_exists($function) ) { - wfDebug( "Compatibility functions in use\n" ); - require_once dirname(__FILE__) . '/CompatibilityFunctions.php'; - break; +/** + * Compatibility functions + * + * We more or less support PHP 5.0.x and up. + * Re-implementations of newer functions or functions in non-standard + * PHP extensions may be included here. + */ +if( !function_exists('iconv') ) { + # iconv support is not in the default configuration and so may not be present. + # Assume will only ever use utf-8 and iso-8859-1. + # This will *not* work in all circumstances. + function iconv( $from, $to, $string ) { + if(strcasecmp( $from, $to ) == 0) return $string; + if(strcasecmp( $from, 'utf-8' ) == 0) return utf8_decode( $string ); + if(strcasecmp( $to, 'utf-8' ) == 0) return utf8_encode( $string ); + return $string; + } +} + +# UTF-8 substr function based on a PHP manual comment +if ( !function_exists( 'mb_substr' ) ) { + function mb_substr( $str, $start ) { + $ar = array(); + preg_match_all( '/./us', $str, $ar ); + + if( func_num_args() >= 3 ) { + $end = func_get_arg( 2 ); + return join( '', array_slice( $ar[0], $start, $end ) ); + } else { + return join( '', array_slice( $ar[0], $start ) ); + } + } +} + +if ( !function_exists( 'mb_strlen' ) ) { + /** + * Fallback implementation of mb_strlen, hardcoded to UTF-8. + * @param string $str + * @param string $enc optional encoding; ignored + * @return int + */ + function mb_strlen( $str, $enc="" ) { + $counts = count_chars( $str ); + $total = 0; + + // Count ASCII bytes + for( $i = 0; $i < 0x80; $i++ ) { + $total += $counts[$i]; + } + + // Count multibyte sequence heads + for( $i = 0xc0; $i < 0xff; $i++ ) { + $total += $counts[$i]; + } + return $total; + } +} + +if ( !function_exists( 'array_diff_key' ) ) { + /** + * Exists in PHP 5.1.0+ + * Not quite compatible, two-argument version only + * Null values will cause problems due to this use of isset() + */ + function array_diff_key( $left, $right ) { + $result = $left; + foreach ( $left as $key => $unused ) { + if ( isset( $right[$key] ) ) { + unset( $result[$key] ); + } + } + return $result; } }