From 51c0a9fe2ec9605cc3b58fa8079e2229351d7743 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Mon, 7 Jul 2008 19:54:43 +0000 Subject: [PATCH] * Split Compatibility functions to own file --- includes/CompatibilityFunctions.php | 76 ++++++++++++++++++++++++++++ includes/GlobalFunctions.php | 78 +++-------------------------- 2 files changed, 82 insertions(+), 72 deletions(-) create mode 100644 includes/CompatibilityFunctions.php diff --git a/includes/CompatibilityFunctions.php b/includes/CompatibilityFunctions.php new file mode 100644 index 0000000000..252a5fb973 --- /dev/null +++ b/includes/CompatibilityFunctions.php @@ -0,0 +1,76 @@ += 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 4e60bd493b..ff6fac0f3b 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -12,78 +12,12 @@ require_once dirname(__FILE__) . '/LogPage.php'; require_once dirname(__FILE__) . '/normal/UtfNormalUtil.php'; require_once dirname(__FILE__) . '/XmlFunctions.php'; -/** - * 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; +$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; } } -- 2.20.1