Refactor array_intersect_key from ApiResult.php to GlobalFunctions.php
authorSam Reed <reedy@users.mediawiki.org>
Sat, 23 Jan 2010 22:31:37 +0000 (22:31 +0000)
committerSam Reed <reedy@users.mediawiki.org>
Sat, 23 Jan 2010 22:31:37 +0000 (22:31 +0000)
includes/GlobalFunctions.php
includes/api/ApiResult.php

index b8916c2..219df38 100644 (file)
@@ -190,6 +190,37 @@ if ( !function_exists( 'array_diff_key' ) ) {
        }
 }
 
+if ( !function_exists( 'array_intersect_key' ) ) {
+       /**
+       * Exists in 5.1.0+
+       * Define our own array_intersect_key function
+       */
+       function array_intersect_key( $isec, $keys ) {
+               $argc = func_num_args();
+
+               if ( $argc > 2 ) {
+                       for ( $i = 1; $isec && $i < $argc; $i++ ) {
+                               $arr = func_get_arg( $i );
+
+                               foreach ( array_keys( $isec ) as $key ) {
+                                       if ( !isset( $arr[$key] ) )
+                                               unset( $isec[$key] );
+                               }
+                       }
+
+                       return $isec;
+               } else {
+                       $res = array();
+                       foreach ( array_keys( $isec ) as $key ) {
+                               if ( isset( $keys[$key] ) )
+                                       $res[$key] = $isec[$key];
+                       }
+
+                       return $res;
+               }
+       }
+}
+
 // Support for Wietse Venema's taint feature
 if ( !function_exists( 'istainted' ) ) {
        function istainted( $var ) {
index 58cfd7d..e2355db 100644 (file)
@@ -315,31 +315,3 @@ class ApiResult extends ApiBase {
                return __CLASS__ . ': $Id$';
        }
 }
-
-/* For compatibility with PHP versions < 5.1.0, define our own array_intersect_key function. */
-if ( !function_exists( 'array_intersect_key' ) ) {
-       function array_intersect_key( $isec, $keys ) {
-               $argc = func_num_args();
-
-               if ( $argc > 2 ) {
-                       for ( $i = 1; $isec && $i < $argc; $i++ ) {
-                               $arr = func_get_arg( $i );
-
-                               foreach ( array_keys( $isec ) as $key ) {
-                                       if ( !isset( $arr[$key] ) )
-                                               unset( $isec[$key] );
-                               }
-                       }
-
-                       return $isec;
-               } else {
-                       $res = array();
-                       foreach ( array_keys( $isec ) as $key ) {
-                               if ( isset( $keys[$key] ) )
-                                       $res[$key] = $isec[$key];
-                       }
-
-                       return $res;
-               }
-       }
-}