MediaWiki REL1_33
ArrayUtils.php
Go to the documentation of this file.
1<?php
49 public static function consistentHashSort( &$array, $key, $separator = "\000" ) {
50 $hashes = [];
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,
113 $comparisonCallback, $target
114 ) {
115 if ( $valueCount === 0 ) {
116 return false;
117 }
118
119 $min = 0;
120 $max = $valueCount;
121 do {
122 $mid = $min + ( ( $max - $min ) >> 1 );
123 $item = $valueCallback( $mid );
124 $comparison = $comparisonCallback( $target, $item );
125 if ( $comparison > 0 ) {
126 $min = $mid;
127 } elseif ( $comparison == 0 ) {
128 $min = $mid;
129 break;
130 } else {
131 $max = $mid;
132 }
133 } while ( $min < $max - 1 );
134
135 if ( $min == 0 ) {
136 $item = $valueCallback( $min );
137 $comparison = $comparisonCallback( $target, $item );
138 if ( $comparison < 0 ) {
139 // Before the first item
140 return false;
141 }
142 }
143 return $min;
144 }
145
158 public static function arrayDiffAssocRecursive( $array1 ) {
159 $arrays = func_get_args();
160 array_shift( $arrays );
161 $ret = [];
162
163 foreach ( $array1 as $key => $value ) {
164 if ( is_array( $value ) ) {
165 $args = [ $value ];
166 foreach ( $arrays as $array ) {
167 if ( isset( $array[$key] ) ) {
168 $args[] = $array[$key];
169 }
170 }
171 $valueret = self::arrayDiffAssocRecursive( ...$args );
172 if ( count( $valueret ) ) {
173 $ret[$key] = $valueret;
174 }
175 } else {
176 foreach ( $arrays as $array ) {
177 if ( isset( $array[$key] ) && $array[$key] === $value ) {
178 continue 2;
179 }
180 }
181 $ret[$key] = $value;
182 }
183 }
184
185 return $ret;
186 }
187}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
if( $line===false) $args
Definition cdb.php:64
A collection of static methods to play with arrays.
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...
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...
static pickRandom( $weights)
Given an array of non-normalised probabilities, this function will select an element and return the a...
static arrayDiffAssocRecursive( $array1)
Do array_diff_assoc() on multi-dimensional arrays.
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
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:2003
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37