Reedy has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/51322


Change subject: Move LoadBalancer::pickRandom() to wfPickRandom()
......................................................................

Move LoadBalancer::pickRandom() to wfPickRandom()

Code is copypaste reused in other extensions

Change-Id: I4c677ddc01ee264f3b72bb17135972adc96144ae
---
M includes/GlobalFunctions.php
M includes/db/LoadBalancer.php
2 files changed, 37 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/22/51322/1

diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php
index a2b882f..4d60d9c 100644
--- a/includes/GlobalFunctions.php
+++ b/includes/GlobalFunctions.php
@@ -330,6 +330,40 @@
 }
 
 /**
+ * Given an array of non-normalised probabilities, this function will select
+ * an element and return the appropriate key
+ *
+ * @param $weights array
+ *
+ * @return int
+ */
+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,
  * all of the following should be safe:
diff --git a/includes/db/LoadBalancer.php b/includes/db/LoadBalancer.php
index 13fa466..d249c27 100644
--- a/includes/db/LoadBalancer.php
+++ b/includes/db/LoadBalancer.php
@@ -117,34 +117,14 @@
         * 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 );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/51322
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I4c677ddc01ee264f3b72bb17135972adc96144ae
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Reedy <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to