MediaWiki  1.23.0
ArrayUtils.php
Go to the documentation of this file.
1 <?php
28 class ArrayUtils {
49  public static function consistentHashSort( &$array, $key, $separator = "\000" ) {
50  $hashes = array();
51  foreach ( $array as $elt ) {
52  $hashes[$elt] = md5( $elt . $separator . $key );
53  }
54  uasort( $array, function ( $a, $b ) use ( $hashes ) {
55  return strcmp( $hashes[$a], $hashes[$b] );
56  } );
57  }
58 
66  public static function pickRandom( $weights ) {
67  if ( !is_array( $weights ) || count( $weights ) == 0 ) {
68  return false;
69  }
70 
71  $sum = array_sum( $weights );
72  if ( $sum == 0 ) {
73  # No loads on any of them
74  # In previous versions, this triggered an unweighted random selection,
75  # but this feature has been removed as of April 2006 to allow for strict
76  # separation of query groups.
77  return false;
78  }
79  $max = mt_getrandmax();
80  $rand = mt_rand( 0, $max ) / $max * $sum;
81 
82  $sum = 0;
83  foreach ( $weights as $i => $w ) {
84  $sum += $w;
85  # Do not return keys if they have 0 weight.
86  # Note that the "all 0 weight" case is handed above
87  if ( $w > 0 && $sum >= $rand ) {
88  break;
89  }
90  }
91 
92  return $i;
93  }
94 
112  public static function findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target ) {
113  if ( $valueCount === 0 ) {
114  return false;
115  }
116 
117  $min = 0;
118  $max = $valueCount;
119  do {
120  $mid = $min + ( ( $max - $min ) >> 1 );
121  $item = call_user_func( $valueCallback, $mid );
122  $comparison = call_user_func( $comparisonCallback, $target, $item );
123  if ( $comparison > 0 ) {
124  $min = $mid;
125  } elseif ( $comparison == 0 ) {
126  $min = $mid;
127  break;
128  } else {
129  $max = $mid;
130  }
131  } while ( $min < $max - 1 );
132 
133  if ( $min == 0 ) {
134  $item = call_user_func( $valueCallback, $min );
135  $comparison = call_user_func( $comparisonCallback, $target, $item );
136  if ( $comparison < 0 ) {
137  // Before the first item
138  return false;
139  }
140  }
141  return $min;
142  }
143 
157  public static function arrayDiffAssocRecursive( $array1 ) {
158  $arrays = func_get_args();
159  array_shift( $arrays );
160  $ret = array();
161 
162  foreach ( $array1 as $key => $value ) {
163  if ( is_array( $value ) ) {
164  $args = array( $value );
165  foreach ( $arrays as $array ) {
166  if ( isset( $array[$key] ) ) {
167  $args[] = $array[$key];
168  }
169  }
170  $valueret = call_user_func_array( __METHOD__, $args );
171  if ( count( $valueret ) ) {
172  $ret[$key] = $valueret;
173  }
174  } else {
175  foreach ( $arrays as $array ) {
176  if ( isset( $array[$key] ) && $array[$key] === $value ) {
177  continue 2;
178  }
179  }
180  $ret[$key] = $value;
181  }
182  }
183 
184  return $ret;
185  }
186 }
ArrayUtils\consistentHashSort
static consistentHashSort(&$array, $key, $separator="\000")
Sort the given array in a pseudo-random order which depends only on the given key and each element va...
Definition: ArrayUtils.php:49
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
$ret
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses & $ret
Definition: hooks.txt:1530
ArrayUtils\arrayDiffAssocRecursive
static arrayDiffAssocRecursive( $array1)
Do array_diff_assoc() on multi-dimensional arrays.
Definition: ArrayUtils.php:157
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
$value
$value
Definition: styleTest.css.php:45
$args
if( $line===false) $args
Definition: cdb.php:62
ArrayUtils\pickRandom
static pickRandom( $weights)
Given an array of non-normalised probabilities, this function will select an element and return the a...
Definition: ArrayUtils.php:66
ArrayUtils
A collection of static methods to play with arrays.
Definition: ArrayUtils.php:28
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
$hashes
$hashes
Definition: testCompression.php:62
ArrayUtils\findLowerBound
static findLowerBound( $valueCallback, $valueCount, $comparisonCallback, $target)
Do a binary search, and return the index of the largest item that sorts less than or equal to the tar...
Definition: ArrayUtils.php:112