From 7ae68dcfb9f7a4e8359c1eeba1bed4e81ff1143e Mon Sep 17 00:00:00 2001 From: Timo Tijhof Date: Tue, 24 Jun 2014 01:47:01 +0200 Subject: [PATCH] SpecialPage: DRY array filter for prefixSearchSubpages() Everytime this gets duplicated it isn't immediately obvious what all the nested array_slice/preg_quote stuff does. Having it once place removes the need to figure it out again or look at it as new code (and ensures they're in sync and saves maintenance). Change-Id: Iefe340729a55c9bb52a4931310966d0f33041205 --- includes/specialpage/SpecialPage.php | 15 +++++++++++++++ includes/specials/SpecialEditWatchlist.php | 15 ++++++++++----- includes/specials/SpecialLog.php | 3 +-- includes/specials/SpecialWatchlist.php | 12 +++++++++--- 4 files changed, 35 insertions(+), 10 deletions(-) diff --git a/includes/specialpage/SpecialPage.php b/includes/specialpage/SpecialPage.php index c062e27250..4cfde33e13 100644 --- a/includes/specialpage/SpecialPage.php +++ b/includes/specialpage/SpecialPage.php @@ -335,6 +335,21 @@ class SpecialPage { return array(); } + /** + * Helper function for implementations of prefixSearchSubpages() that + * filter the values in memory (as oppposed to making a query). + * + * @since 1.24 + * @param string $search + * @param int $limit + * @param array $subpages + * @return string[] + */ + protected static function prefixSearchArray( $search, $limit, Array $subpages ) { + $escaped = preg_quote( $search ); + return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit ); + } + /** * Sets headers - this should be called from the execute() method of all derived classes! */ diff --git a/includes/specials/SpecialEditWatchlist.php b/includes/specials/SpecialEditWatchlist.php index 02d8d709ba..369f11f73a 100644 --- a/includes/specials/SpecialEditWatchlist.php +++ b/includes/specials/SpecialEditWatchlist.php @@ -125,11 +125,16 @@ class SpecialEditWatchlist extends UnlistedSpecialPage { * @return string[] Matching subpages */ public function prefixSearchSubpages( $search, $limit = 10 ) { - // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added - // here and there - no 'edit' here, because that the default for this page - $subpages = array( 'clear', 'raw' ); - $escaped = preg_quote( $search ); - return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit ); + return self::prefixSearchArray( + $search, + $limit, + // SpecialWatchlist uses SpecialEditWatchlist::getMode, so new types should be added + // here and there - no 'edit' here, because that the default for this page + array( + 'clear', + 'raw', + ) + ); } /** diff --git a/includes/specials/SpecialLog.php b/includes/specials/SpecialLog.php index aaa55a3a21..ced5d25453 100644 --- a/includes/specials/SpecialLog.php +++ b/includes/specials/SpecialLog.php @@ -127,8 +127,7 @@ class SpecialLog extends SpecialPage { $subpages = $wgLogTypes; $subpages[] = 'all'; sort( $subpages ); - $escaped = preg_quote( $search ); - return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit ); + return self::prefixSearchArray( $search, $limit, $subpages ); } private function parseParams( FormOptions $opts, $par ) { diff --git a/includes/specials/SpecialWatchlist.php b/includes/specials/SpecialWatchlist.php index 2aec8dfec2..aa56920f8e 100644 --- a/includes/specials/SpecialWatchlist.php +++ b/includes/specials/SpecialWatchlist.php @@ -88,9 +88,15 @@ class SpecialWatchlist extends ChangesListSpecialPage { */ public function prefixSearchSubpages( $search, $limit = 10 ) { // See also SpecialEditWatchlist::prefixSearchSubpages - $subpages = array( 'clear', 'edit', 'raw' ); - $escaped = preg_quote( $search ); - return array_slice( preg_grep( "/^$escaped/i", $subpages ), 0, $limit ); + return self::prefixSearchArray( + $search, + $limit, + array( + 'clear', + 'edit', + 'raw', + ) + ); } /** -- 2.20.1