Index: trunk/phase3/includes/GlobalFunctions.php |
— | — | @@ -104,6 +104,151 @@ |
105 | 105 | } |
106 | 106 | |
107 | 107 | /** |
| 108 | + * Array lookup |
| 109 | + * Returns an array where the values in the first array are replaced by the |
| 110 | + * values in the second array with the corresponding keys |
| 111 | + * |
| 112 | + * @param $a Array |
| 113 | + * @param $b Array |
| 114 | + * @return array |
| 115 | + */ |
| 116 | +function wfArrayLookup( $a, $b ) { |
| 117 | + return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) ); |
| 118 | +} |
| 119 | + |
| 120 | +/** |
| 121 | + * Appends to second array if $value differs from that in $default |
| 122 | + * |
| 123 | + * @param $key String|Int |
| 124 | + * @param $value Mixed |
| 125 | + * @param $default Mixed |
| 126 | + * @param $changed Array to alter |
| 127 | + */ |
| 128 | +function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) { |
| 129 | + if ( is_null( $changed ) ) { |
| 130 | + throw new MWException( 'GlobalFunctions::wfAppendToArrayIfNotDefault got null' ); |
| 131 | + } |
| 132 | + if ( $default[$key] !== $value ) { |
| 133 | + $changed[$key] = $value; |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +/** |
| 138 | + * Backwards array plus for people who haven't bothered to read the PHP manual |
| 139 | + * XXX: will not darn your socks for you. |
| 140 | + * |
| 141 | + * @param $array1 Array |
| 142 | + * @param [$array2, [...]] Arrays |
| 143 | + * @return Array |
| 144 | + */ |
| 145 | +function wfArrayMerge( $array1/* ... */ ) { |
| 146 | + $args = func_get_args(); |
| 147 | + $args = array_reverse( $args, true ); |
| 148 | + $out = array(); |
| 149 | + foreach ( $args as $arg ) { |
| 150 | + $out += $arg; |
| 151 | + } |
| 152 | + return $out; |
| 153 | +} |
| 154 | + |
| 155 | +/** |
| 156 | + * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal |
| 157 | + * e.g. |
| 158 | + * wfMergeErrorArrays( |
| 159 | + * array( array( 'x' ) ), |
| 160 | + * array( array( 'x', '2' ) ), |
| 161 | + * array( array( 'x' ) ), |
| 162 | + * array( array( 'y') ) |
| 163 | + * ); |
| 164 | + * returns: |
| 165 | + * array( |
| 166 | + * array( 'x', '2' ), |
| 167 | + * array( 'x' ), |
| 168 | + * array( 'y' ) |
| 169 | + * ) |
| 170 | + * @param varargs |
| 171 | + * @return Array |
| 172 | + */ |
| 173 | +function wfMergeErrorArrays( /*...*/ ) { |
| 174 | + $args = func_get_args(); |
| 175 | + $out = array(); |
| 176 | + foreach ( $args as $errors ) { |
| 177 | + foreach ( $errors as $params ) { |
| 178 | + # @todo FIXME: Sometimes get nested arrays for $params, |
| 179 | + # which leads to E_NOTICEs |
| 180 | + $spec = implode( "\t", $params ); |
| 181 | + $out[$spec] = $params; |
| 182 | + } |
| 183 | + } |
| 184 | + return array_values( $out ); |
| 185 | +} |
| 186 | + |
| 187 | +/** |
| 188 | + * Insert array into another array after the specified *KEY* |
| 189 | + * |
| 190 | + * @param $array Array: The array. |
| 191 | + * @param $insert Array: The array to insert. |
| 192 | + * @param $after Mixed: The key to insert after |
| 193 | + * @return Array |
| 194 | + */ |
| 195 | +function wfArrayInsertAfter( $array, $insert, $after ) { |
| 196 | + // Find the offset of the element to insert after. |
| 197 | + $keys = array_keys( $array ); |
| 198 | + $offsetByKey = array_flip( $keys ); |
| 199 | + |
| 200 | + $offset = $offsetByKey[$after]; |
| 201 | + |
| 202 | + // Insert at the specified offset |
| 203 | + $before = array_slice( $array, 0, $offset + 1, true ); |
| 204 | + $after = array_slice( $array, $offset + 1, count( $array ) - $offset, true ); |
| 205 | + |
| 206 | + $output = $before + $insert + $after; |
| 207 | + |
| 208 | + return $output; |
| 209 | +} |
| 210 | + |
| 211 | +/** |
| 212 | + * Recursively converts the parameter (an object) to an array with the same data |
| 213 | + * |
| 214 | + * @param $objOrArray Object|Array |
| 215 | + * @param $recursive Bool |
| 216 | + * @return Array |
| 217 | + */ |
| 218 | +function wfObjectToArray( $objOrArray, $recursive = true ) { |
| 219 | + $array = array(); |
| 220 | + if( is_object( $objOrArray ) ) { |
| 221 | + $objOrArray = get_object_vars( $objOrArray ); |
| 222 | + } |
| 223 | + foreach ( $objOrArray as $key => $value ) { |
| 224 | + if ( $recursive && ( is_object( $value ) || is_array( $value ) ) ) { |
| 225 | + $value = wfObjectToArray( $value ); |
| 226 | + } |
| 227 | + |
| 228 | + $array[$key] = $value; |
| 229 | + } |
| 230 | + |
| 231 | + return $array; |
| 232 | +} |
| 233 | + |
| 234 | +/** |
| 235 | + * Wrapper around array_map() which also taints variables |
| 236 | + * |
| 237 | + * @param $function Callback |
| 238 | + * @param $input Array |
| 239 | + * @return Array |
| 240 | + */ |
| 241 | +function wfArrayMap( $function, $input ) { |
| 242 | + $ret = array_map( $function, $input ); |
| 243 | + foreach ( $ret as $key => $value ) { |
| 244 | + $taint = istainted( $input[$key] ); |
| 245 | + if ( $taint ) { |
| 246 | + taint( $ret[$key], $taint ); |
| 247 | + } |
| 248 | + } |
| 249 | + return $ret; |
| 250 | +} |
| 251 | + |
| 252 | +/** |
108 | 253 | * Get a random decimal value between 0 and 1, in a way |
109 | 254 | * not likely to give duplicate values for any realistic |
110 | 255 | * number of articles. |
— | — | @@ -1789,19 +1934,6 @@ |
1790 | 1935 | } |
1791 | 1936 | |
1792 | 1937 | /** |
1793 | | - * Array lookup |
1794 | | - * Returns an array where the values in the first array are replaced by the |
1795 | | - * values in the second array with the corresponding keys |
1796 | | - * |
1797 | | - * @param $a Array |
1798 | | - * @param $b Array |
1799 | | - * @return array |
1800 | | - */ |
1801 | | -function wfArrayLookup( $a, $b ) { |
1802 | | - return array_flip( array_intersect( array_flip( $a ), array_keys( $b ) ) ); |
1803 | | -} |
1804 | | - |
1805 | | -/** |
1806 | 1938 | * Reference-counted warning suppression |
1807 | 1939 | * |
1808 | 1940 | * @param $end Bool |
— | — | @@ -2206,23 +2338,6 @@ |
2207 | 2339 | } |
2208 | 2340 | |
2209 | 2341 | /** |
2210 | | - * Appends to second array if $value differs from that in $default |
2211 | | - * |
2212 | | - * @param $key String|Int |
2213 | | - * @param $value Mixed |
2214 | | - * @param $default Mixed |
2215 | | - * @param $changed Array to alter |
2216 | | - */ |
2217 | | -function wfAppendToArrayIfNotDefault( $key, $value, $default, &$changed ) { |
2218 | | - if ( is_null( $changed ) ) { |
2219 | | - throw new MWException( 'GlobalFunctions::wfAppendToArrayIfNotDefault got null' ); |
2220 | | - } |
2221 | | - if ( $default[$key] !== $value ) { |
2222 | | - $changed[$key] = $value; |
2223 | | - } |
2224 | | -} |
2225 | | - |
2226 | | -/** |
2227 | 2342 | * Since wfMsg() and co suck, they don't return false if the message key they |
2228 | 2343 | * looked up didn't exist but a XHTML string, this function checks for the |
2229 | 2344 | * nonexistance of messages by checking the MessageCache::get() result directly. |
— | — | @@ -2573,56 +2688,6 @@ |
2574 | 2689 | } |
2575 | 2690 | |
2576 | 2691 | /** |
2577 | | - * Backwards array plus for people who haven't bothered to read the PHP manual |
2578 | | - * XXX: will not darn your socks for you. |
2579 | | - * |
2580 | | - * @param $array1 Array |
2581 | | - * @param [$array2, [...]] Arrays |
2582 | | - * @return Array |
2583 | | - */ |
2584 | | -function wfArrayMerge( $array1/* ... */ ) { |
2585 | | - $args = func_get_args(); |
2586 | | - $args = array_reverse( $args, true ); |
2587 | | - $out = array(); |
2588 | | - foreach ( $args as $arg ) { |
2589 | | - $out += $arg; |
2590 | | - } |
2591 | | - return $out; |
2592 | | -} |
2593 | | - |
2594 | | -/** |
2595 | | - * Merge arrays in the style of getUserPermissionsErrors, with duplicate removal |
2596 | | - * e.g. |
2597 | | - * wfMergeErrorArrays( |
2598 | | - * array( array( 'x' ) ), |
2599 | | - * array( array( 'x', '2' ) ), |
2600 | | - * array( array( 'x' ) ), |
2601 | | - * array( array( 'y') ) |
2602 | | - * ); |
2603 | | - * returns: |
2604 | | - * array( |
2605 | | - * array( 'x', '2' ), |
2606 | | - * array( 'x' ), |
2607 | | - * array( 'y' ) |
2608 | | - * ) |
2609 | | - * @param varargs |
2610 | | - * @return Array |
2611 | | - */ |
2612 | | -function wfMergeErrorArrays( /*...*/ ) { |
2613 | | - $args = func_get_args(); |
2614 | | - $out = array(); |
2615 | | - foreach ( $args as $errors ) { |
2616 | | - foreach ( $errors as $params ) { |
2617 | | - # @todo FIXME: Sometimes get nested arrays for $params, |
2618 | | - # which leads to E_NOTICEs |
2619 | | - $spec = implode( "\t", $params ); |
2620 | | - $out[$spec] = $params; |
2621 | | - } |
2622 | | - } |
2623 | | - return array_values( $out ); |
2624 | | -} |
2625 | | - |
2626 | | -/** |
2627 | 2692 | * parse_url() work-alike, but non-broken. Differences: |
2628 | 2693 | * |
2629 | 2694 | * 1) Does not raise warnings on bad URLs (just returns false) |
— | — | @@ -3333,53 +3398,6 @@ |
3334 | 3399 | } |
3335 | 3400 | |
3336 | 3401 | /** |
3337 | | - * Insert array into another array after the specified *KEY* |
3338 | | - * |
3339 | | - * @param $array Array: The array. |
3340 | | - * @param $insert Array: The array to insert. |
3341 | | - * @param $after Mixed: The key to insert after |
3342 | | - * @return Array |
3343 | | - */ |
3344 | | -function wfArrayInsertAfter( $array, $insert, $after ) { |
3345 | | - // Find the offset of the element to insert after. |
3346 | | - $keys = array_keys( $array ); |
3347 | | - $offsetByKey = array_flip( $keys ); |
3348 | | - |
3349 | | - $offset = $offsetByKey[$after]; |
3350 | | - |
3351 | | - // Insert at the specified offset |
3352 | | - $before = array_slice( $array, 0, $offset + 1, true ); |
3353 | | - $after = array_slice( $array, $offset + 1, count( $array ) - $offset, true ); |
3354 | | - |
3355 | | - $output = $before + $insert + $after; |
3356 | | - |
3357 | | - return $output; |
3358 | | -} |
3359 | | - |
3360 | | -/** |
3361 | | - * Recursively converts the parameter (an object) to an array with the same data |
3362 | | - * |
3363 | | - * @param $objOrArray Object|Array |
3364 | | - * @param $recursive Bool |
3365 | | - * @return Array |
3366 | | - */ |
3367 | | -function wfObjectToArray( $objOrArray, $recursive = true ) { |
3368 | | - $array = array(); |
3369 | | - if( is_object( $objOrArray ) ) { |
3370 | | - $objOrArray = get_object_vars( $objOrArray ); |
3371 | | - } |
3372 | | - foreach ( $objOrArray as $key => $value ) { |
3373 | | - if ( $recursive && ( is_object( $value ) || is_array( $value ) ) ) { |
3374 | | - $value = wfObjectToArray( $value ); |
3375 | | - } |
3376 | | - |
3377 | | - $array[$key] = $value; |
3378 | | - } |
3379 | | - |
3380 | | - return $array; |
3381 | | -} |
3382 | | - |
3383 | | -/** |
3384 | 3402 | * Set PHP's memory limit to the larger of php.ini or $wgMemoryLimit; |
3385 | 3403 | * |
3386 | 3404 | * @return Integer value memory was set to. |
— | — | @@ -3471,25 +3489,6 @@ |
3472 | 3490 | } |
3473 | 3491 | |
3474 | 3492 | /** |
3475 | | - * Wrapper around array_map() which also taints variables |
3476 | | - * |
3477 | | - * @param $function Callback |
3478 | | - * @param $input Array |
3479 | | - * @return Array |
3480 | | - */ |
3481 | | -function wfArrayMap( $function, $input ) { |
3482 | | - $ret = array_map( $function, $input ); |
3483 | | - foreach ( $ret as $key => $value ) { |
3484 | | - $taint = istainted( $input[$key] ); |
3485 | | - if ( $taint ) { |
3486 | | - taint( $ret[$key], $taint ); |
3487 | | - } |
3488 | | - } |
3489 | | - return $ret; |
3490 | | -} |
3491 | | - |
3492 | | - |
3493 | | -/** |
3494 | 3493 | * Get a cache object. |
3495 | 3494 | * |
3496 | 3495 | * @param integer $inputType Cache type, one the the CACHE_* constants. |