Moved wfPickRandom to ArrayUtils.
authorAaron Schulz <aschulz@wikimedia.org>
Tue, 5 Mar 2013 23:05:21 +0000 (15:05 -0800)
committerAaron Schulz <aschulz@wikimedia.org>
Tue, 5 Mar 2013 23:07:09 +0000 (15:07 -0800)
Change-Id: I45e21e722245901ab2988be4892cdb393169c62c

includes/ArrayUtils.php
includes/GlobalFunctions.php
includes/db/LoadBalancer.php

index d1b72a0..39d43e8 100644 (file)
@@ -21,7 +21,7 @@ class ArrayUtils {
         *     various consistent hash implementations that existed before this
         *     function was introduced.
         */
-       static function consistentHashSort( &$array, $key, $separator = "\000" ) {
+       public static function consistentHashSort( &$array, $key, $separator = "\000" ) {
                $hashes = array();
                foreach ( $array as $elt ) {
                        $hashes[$elt] = md5( $elt . $separator . $key );
@@ -30,4 +30,38 @@ class ArrayUtils {
                        return strcmp( $hashes[$a], $hashes[$b] );
                } );
        }
+
+       /**
+        * Given an array of non-normalised probabilities, this function will select
+        * an element and return the appropriate key
+        *
+        * @param $weights array
+        *
+        * @return bool|int|string
+        */
+       public static function pickRandom( $weights ){
+               if ( !is_array( $weights ) || count( $weights ) == 0 ) {
+                       return false;
+               }
+
+               $sum = array_sum( $weights );
+               if ( $sum == 0 ) {
+                       # No loads on any of them
+                       # In previous versions, this triggered an unweighted random selection,
+                       # but this feature has been removed as of April 2006 to allow for strict
+                       # separation of query groups.
+                       return false;
+               }
+               $max = mt_getrandmax();
+               $rand = mt_rand( 0, $max ) / $max * $sum;
+
+               $sum = 0;
+               foreach ( $weights as $i => $w ) {
+                       $sum += $w;
+                       if ( $sum >= $rand ) {
+                               break;
+                       }
+               }
+               return $i;
+       }
 }
index 79a4920..cdd0ae9 100644 (file)
@@ -329,40 +329,6 @@ function wfRandomString( $length = 32 ) {
        return substr( $str, 0, $length );
 }
 
-/**
- * Given an array of non-normalised probabilities, this function will select
- * an element and return the appropriate key
- *
- * @param $weights array
- *
- * @return bool|int|string
- */
-function wfPickRandom( $weights ){
-       if ( !is_array( $weights ) || count( $weights ) == 0 ) {
-               return false;
-       }
-
-       $sum = array_sum( $weights );
-       if ( $sum == 0 ) {
-               # No loads on any of them
-               # In previous versions, this triggered an unweighted random selection,
-               # but this feature has been removed as of April 2006 to allow for strict
-               # separation of query groups.
-               return false;
-       }
-       $max = mt_getrandmax();
-       $rand = mt_rand( 0, $max ) / $max * $sum;
-
-       $sum = 0;
-       foreach ( $weights as $i => $w ) {
-               $sum += $w;
-               if ( $sum >= $rand ) {
-                       break;
-               }
-       }
-       return $i;
-}
-
 /**
  * We want some things to be included as literal characters in our title URLs
  * for prettiness, which urlencode encodes by default.  According to RFC 1738,
index 311691d..20d9ca9 100644 (file)
@@ -117,14 +117,14 @@ class LoadBalancer {
         * Given an array of non-normalised probabilities, this function will select
         * an element and return the appropriate key
         *
-        * @deprecated 1.21, use wfPickRandom()
+        * @deprecated 1.21, use ArrayUtils::pickRandom()
         *
         * @param $weights array
         *
         * @return bool|int|string
         */
        function pickRandom( $weights ) {
-               return wfPickRandom( $weights );
+               return ArrayUtils::pickRandom( $weights );
        }
 
        /**