Merge "Remove DB2 support"
[lhc/web/wiklou.git] / includes / GlobalFunctions.php
index f677033..79a4920 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 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,
@@ -1386,9 +1420,13 @@ function wfUILang() {
 }
 
 /**
- * This is the new function for getting translated interface messages.
- * See the Message class for documentation how to use them.
- * The intention is that this function replaces all old wfMsg* functions.
+ * This is the function for getting translated interface messages.
+ *
+ * @see Message class for documentation how to use them.
+ * @see https://www.mediawiki.org/wiki/Manual:Messages_API
+ *
+ * This function replaces all old wfMsg* functions.
+ *
  * @param $key \string Message key.
  * Varargs: normal message parameters.
  * @return Message