Move LoadBalancer::pickRandom() to wfPickRandom()
authorReedy <reedy@wikimedia.org>
Thu, 28 Feb 2013 01:22:49 +0000 (01:22 +0000)
committerASchulz <aschulz@wikimedia.org>
Thu, 28 Feb 2013 20:52:35 +0000 (12:52 -0800)
Code is copypaste reused in other extensions

Change-Id: I4c677ddc01ee264f3b72bb17135972adc96144ae

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

index a2b882f..121d6ca 100644 (file)
@@ -329,6 +329,40 @@ 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 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 13fa466..d249c27 100644 (file)
@@ -117,34 +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()
+        *
         * @param $weights array
         *
         * @return int
         */
        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;
+               return wfPickRandom( $weights );
        }
 
        /**