From 9b0c621d7f982c66c02ab4ddedc25b51040aaeb9 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Wed, 18 Jul 2018 01:42:52 -0700 Subject: [PATCH] Deprecate wfArrayFilter() and wfArrayFilterByKey() Now that all our supported PHP versions have array_filter() with a third parameter, these functions aren't needed anymore. Depends-On: I3b097a1a048baabcaca15dc214a3a1bb06e746cc Depends-On: I0187e27ac47cbab099249572201d1a649226a734 Change-Id: I7cabd0252691a083cb749cf9d3a7a23f1d076c39 --- RELEASE-NOTES-1.32 | 2 ++ includes/GlobalFunctions.php | 16 ++++------------ includes/actions/HistoryAction.php | 5 ++++- includes/changes/EnhancedChangesList.php | 9 ++++++--- includes/logging/LogEventsList.php | 5 ++++- includes/shell/Command.php | 8 ++++---- includes/specials/SpecialNewpages.php | 5 ++++- includes/specials/pagers/ContribsPager.php | 5 ++++- .../specials/pagers/DeletedContribsPager.php | 5 ++++- .../GlobalFunctions/wfArrayFilterTest.php | 4 +++- 10 files changed, 39 insertions(+), 25 deletions(-) diff --git a/RELEASE-NOTES-1.32 b/RELEASE-NOTES-1.32 index 79f5bf9d9a..54352138a3 100644 --- a/RELEASE-NOTES-1.32 +++ b/RELEASE-NOTES-1.32 @@ -277,6 +277,8 @@ because of Phabricator reports. to running searchText/searchTitle. * (T199657) Messages for $wgFilterLogTypes labels should be no longer be in the 'log-show-hide-[type]' format. Instead use 'logeventslist-[type]-log'. +* Global functions wfArrayFilter() and wfArrayFilterByKey() are deprecated. + use array_filter() directly. === Other changes in 1.32 === * (T198811) The following tables have had their UNIQUE indexes turned into diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 3ea020fd3c..afad5c468b 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -141,7 +141,7 @@ function wfArrayDiff2_cmp( $a, $b ) { } /** - * Like array_filter with ARRAY_FILTER_USE_BOTH, but works pre-5.6. + * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_BOTH directly * * @param array $arr * @param callable $callback Will be called with the array value and key (in that order) and @@ -149,17 +149,11 @@ function wfArrayDiff2_cmp( $a, $b ) { * @return array */ function wfArrayFilter( array $arr, callable $callback ) { - if ( defined( 'ARRAY_FILTER_USE_BOTH' ) ) { - return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH ); - } - $filteredKeys = array_filter( array_keys( $arr ), function ( $key ) use ( $arr, $callback ) { - return call_user_func( $callback, $arr[$key], $key ); - } ); - return array_intersect_key( $arr, array_fill_keys( $filteredKeys, true ) ); + return array_filter( $arr, $callback, ARRAY_FILTER_USE_BOTH ); } /** - * Like array_filter with ARRAY_FILTER_USE_KEY, but works pre-5.6. + * @deprecated since 1.32, use array_filter() with ARRAY_FILTER_USE_KEY directly * * @param array $arr * @param callable $callback Will be called with the array key and should return a bool which @@ -167,9 +161,7 @@ function wfArrayFilter( array $arr, callable $callback ) { * @return array */ function wfArrayFilterByKey( array $arr, callable $callback ) { - return wfArrayFilter( $arr, function ( $val, $key ) use ( $callback ) { - return call_user_func( $callback, $key ); - } ); + return array_filter( $arr, $callback, ARRAY_FILTER_USE_KEY ); } /** diff --git a/includes/actions/HistoryAction.php b/includes/actions/HistoryAction.php index bd64a41479..20637fcdf7 100644 --- a/includes/actions/HistoryAction.php +++ b/includes/actions/HistoryAction.php @@ -788,7 +788,10 @@ class HistoryPager extends ReverseChronologicalPager { $attribs = [ 'data-mw-revid' => $rev->getId() ]; Hooks::run( 'PageHistoryLineEnding', [ $this, &$row, &$s, &$classes, &$attribs ] ); - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); if ( $classes ) { $attribs['class'] = implode( ' ', $classes ); diff --git a/includes/changes/EnhancedChangesList.php b/includes/changes/EnhancedChangesList.php index cdfbf56576..28b30d8ba4 100644 --- a/includes/changes/EnhancedChangesList.php +++ b/includes/changes/EnhancedChangesList.php @@ -472,7 +472,10 @@ class EnhancedChangesList extends ChangesList { // skip entry if hook aborted it return []; } - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); $lineParams['recentChangesFlagsRaw'] = []; if ( isset( $data['recentChangesFlags'] ) ) { @@ -704,9 +707,9 @@ class EnhancedChangesList extends ChangesList { } $attribs = $data['attribs']; unset( $data['attribs'] ); - $attribs = wfArrayFilterByKey( $attribs, function ( $key ) { + $attribs = array_filter( $attribs, function ( $key ) { return $key === 'class' || Sanitizer::isReservedDataAttribute( $key ); - } ); + }, ARRAY_FILTER_USE_KEY ); $prefix = ''; if ( is_callable( $this->changeLinePrefixer ) ) { diff --git a/includes/logging/LogEventsList.php b/includes/logging/LogEventsList.php index 4b9c91c4c4..73aaa4feaf 100644 --- a/includes/logging/LogEventsList.php +++ b/includes/logging/LogEventsList.php @@ -423,7 +423,10 @@ class LogEventsList extends ContextSource { // Let extensions add data Hooks::run( 'LogEventsListLineEnding', [ $this, &$ret, $entry, &$classes, &$attribs ] ); - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); $attribs['class'] = implode( ' ', $classes ); return Html::rawElement( 'li', $attribs, $ret ) . "\n"; diff --git a/includes/shell/Command.php b/includes/shell/Command.php index 8ae517eed4..1154e05886 100644 --- a/includes/shell/Command.php +++ b/includes/shell/Command.php @@ -437,12 +437,12 @@ class Command { @trigger_error( '' ); restore_error_handler(); - $readPipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) { + $readPipes = array_filter( $pipes, function ( $fd ) use ( $desc ) { return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'r'; - } ); - $writePipes = wfArrayFilterByKey( $pipes, function ( $fd ) use ( $desc ) { + }, ARRAY_FILTER_USE_KEY ); + $writePipes = array_filter( $pipes, function ( $fd ) use ( $desc ) { return $desc[$fd][0] === 'pipe' && $desc[$fd][1] === 'w'; - } ); + }, ARRAY_FILTER_USE_KEY ); // stream_select parameter names are from the POV of us being able to do the operation; // proc_open desriptor types are from the POV of the process doing it. // So $writePipes is passed as the $read parameter and $readPipes as $write. diff --git a/includes/specials/SpecialNewpages.php b/includes/specials/SpecialNewpages.php index a93b5222c0..da2b688144 100644 --- a/includes/specials/SpecialNewpages.php +++ b/includes/specials/SpecialNewpages.php @@ -398,7 +398,10 @@ class SpecialNewpages extends IncludableSpecialPage { // Let extensions add data Hooks::run( 'NewPagesLineEnding', [ $this, &$ret, $result, &$classes, &$attribs ] ); - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); if ( count( $classes ) ) { $attribs['class'] = implode( ' ', $classes ); diff --git a/includes/specials/pagers/ContribsPager.php b/includes/specials/pagers/ContribsPager.php index 205364f81b..59fa948ac6 100644 --- a/includes/specials/pagers/ContribsPager.php +++ b/includes/specials/pagers/ContribsPager.php @@ -593,7 +593,10 @@ class ContribsPager extends RangeChronologicalPager { // Let extensions add data Hooks::run( 'ContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] ); - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); // TODO: Handle exceptions in the catch block above. Do any extensions rely on // receiving empty rows? diff --git a/includes/specials/pagers/DeletedContribsPager.php b/includes/specials/pagers/DeletedContribsPager.php index f261b72d8d..ee7eb3e4d8 100644 --- a/includes/specials/pagers/DeletedContribsPager.php +++ b/includes/specials/pagers/DeletedContribsPager.php @@ -221,7 +221,10 @@ class DeletedContribsPager extends IndexPager { // Let extensions add data Hooks::run( 'DeletedContributionsLineEnding', [ $this, &$ret, $row, &$classes, &$attribs ] ); - $attribs = wfArrayFilterByKey( $attribs, [ Sanitizer::class, 'isReservedDataAttribute' ] ); + $attribs = array_filter( $attribs, + [ Sanitizer::class, 'isReservedDataAttribute' ], + ARRAY_FILTER_USE_KEY + ); if ( $classes === [] && $attribs === [] && $ret === '' ) { wfDebug( "Dropping Special:DeletedContribution row that could not be formatted\n" ); diff --git a/tests/phpunit/includes/GlobalFunctions/wfArrayFilterTest.php b/tests/phpunit/includes/GlobalFunctions/wfArrayFilterTest.php index 1011a37c3e..bc930be45d 100644 --- a/tests/phpunit/includes/GlobalFunctions/wfArrayFilterTest.php +++ b/tests/phpunit/includes/GlobalFunctions/wfArrayFilterTest.php @@ -5,8 +5,9 @@ * @covers ::wfArrayFilter * @covers ::wfArrayFilterByKey */ -class WfArrayFilterTest extends \PHPUnit\Framework\TestCase { +class WfArrayFilterTest extends MediaWikiTestCase { public function testWfArrayFilter() { + $this->hideDeprecated( 'wfArrayFilter' ); $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; $filtered = wfArrayFilter( $arr, function ( $val, $key ) { return $key !== 'b'; @@ -27,6 +28,7 @@ class WfArrayFilterTest extends \PHPUnit\Framework\TestCase { } public function testWfArrayFilterByKey() { + $this->hideDeprecated( 'wfArrayFilterByKey' ); $arr = [ 'a' => 1, 'b' => 2, 'c' => 3 ]; $filtered = wfArrayFilterByKey( $arr, function ( $key ) { return $key !== 'b'; -- 2.20.1