* Split Compatibility functions to own file
authorNiklas Laxström <nikerabbit@users.mediawiki.org>
Mon, 7 Jul 2008 19:54:43 +0000 (19:54 +0000)
committerNiklas Laxström <nikerabbit@users.mediawiki.org>
Mon, 7 Jul 2008 19:54:43 +0000 (19:54 +0000)
includes/CompatibilityFunctions.php [new file with mode: 0644]
includes/GlobalFunctions.php

diff --git a/includes/CompatibilityFunctions.php b/includes/CompatibilityFunctions.php
new file mode 100644 (file)
index 0000000..252a5fb
--- /dev/null
@@ -0,0 +1,76 @@
+<?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;
+       }
+}
index 4e60bd4..ff6fac0 100644 (file)
@@ -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;
        }
 }