From 93fc424fc151865eb16ec3d29fd3224724ee2f21 Mon Sep 17 00:00:00 2001 From: Kevin Israel Date: Tue, 10 Jul 2018 11:14:29 -0400 Subject: [PATCH] StringUtils: Deprecate Replacer classes The Replacer classes were added in 1.9, when MediaWiki supported PHP 5.0 and 5.1. They were designed to be used with preg_replace_callback() and StringUtils::delimiterReplaceCallback(). Now that Closures exist in PHP 5.3 and newer, there is no need to define a class for this purpose. All existing Replacer subclasses are simple enough that their few uses can easily be replaced with Closures, without making the code harder to understand. In fact, the code probably becomes easier to understand, as what each match is replaced with becomes more obvious -- no need to refer to a separate class. MediaWiki code search finds no uses in extensions. Thus, these classes are hard deprecated immediately. Change-Id: I441c21689909fb06a1ea07a305259eeb82cb2345 --- RELEASE-NOTES-1.32 | 3 +- includes/libs/StringUtils.php | 29 ++++++++++++++----- includes/libs/replacers/DoubleReplacer.php | 3 ++ includes/libs/replacers/HashtableReplacer.php | 3 ++ includes/libs/replacers/RegexlikeReplacer.php | 3 ++ includes/libs/replacers/Replacer.php | 3 ++ includes/parser/LinkHolderArray.php | 13 +++++---- 7 files changed, 43 insertions(+), 14 deletions(-) diff --git a/RELEASE-NOTES-1.32 b/RELEASE-NOTES-1.32 index 878c84166e..ac68967c6b 100644 --- a/RELEASE-NOTES-1.32 +++ b/RELEASE-NOTES-1.32 @@ -240,7 +240,8 @@ because of Phabricator reports. of using showFatalError directly: OutputPage::showFileDeleteError() OutputPage::showFileNotFoundError(), OutputPage::showFileRenameError() OutputPage::showFileCopyError() and OutputPage::showUnexpectedValueError(). - +* The Replacer, DoubleReplacer, HashtableReplacer, and RegexlikeReplacer + classes are now deprecated. Use a Closure instead. === Other changes in 1.32 === * (T198811) The following tables have had their UNIQUE indexes turned into proper diff --git a/includes/libs/StringUtils.php b/includes/libs/StringUtils.php index d91ac85adb..51d108168a 100644 --- a/includes/libs/StringUtils.php +++ b/includes/libs/StringUtils.php @@ -243,10 +243,13 @@ class StringUtils { * @return string The string with the matches replaced */ static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) { - $replacer = new RegexlikeReplacer( $replace ); - - return self::delimiterReplaceCallback( $startDelim, $endDelim, - $replacer->cb(), $subject, $flags ); + return self::delimiterReplaceCallback( + $startDelim, $endDelim, + function ( array $matches ) use ( $replace ) { + return strtr( $replace, [ '$0' => $matches[0], '$1' => $matches[1] ] ); + }, + $subject, $flags + ); } /** @@ -263,8 +266,13 @@ class StringUtils { $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder - $replacer = new DoubleReplacer( $separator, $placeholder ); - $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); + $cleaned = self::delimiterReplaceCallback( + '<', '>', + function ( array $matches ) use ( $separator, $placeholder ) { + return str_replace( $separator, $placeholder, $matches[0] ); + }, + $text + ); // Explode, then put the replaced separators back in $items = explode( $separator, $cleaned ); @@ -290,8 +298,13 @@ class StringUtils { $text = str_replace( $placeholder, '', $text ); // Replace instances of the separator inside HTML-like tags with the placeholder - $replacer = new DoubleReplacer( $search, $placeholder ); - $cleaned = self::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text ); + $cleaned = self::delimiterReplaceCallback( + '<', '>', + function ( array $matches ) use ( $search, $placeholder ) { + return str_replace( $search, $placeholder, $matches[0] ); + }, + $text + ); // Explode, then put the replaced separators back in $cleaned = str_replace( $search, $replace, $cleaned ); diff --git a/includes/libs/replacers/DoubleReplacer.php b/includes/libs/replacers/DoubleReplacer.php index fed023b1ba..9d05e062a9 100644 --- a/includes/libs/replacers/DoubleReplacer.php +++ b/includes/libs/replacers/DoubleReplacer.php @@ -20,6 +20,8 @@ /** * Class to perform secondary replacement within each replacement string + * + * @deprecated since 1.32, use a Closure instead */ class DoubleReplacer extends Replacer { /** @@ -28,6 +30,7 @@ class DoubleReplacer extends Replacer { * @param int $index */ public function __construct( $from, $to, $index = 0 ) { + wfDeprecated( __METHOD__, '1.32' ); $this->from = $from; $this->to = $to; $this->index = $index; diff --git a/includes/libs/replacers/HashtableReplacer.php b/includes/libs/replacers/HashtableReplacer.php index 11637d0d43..8247694a0d 100644 --- a/includes/libs/replacers/HashtableReplacer.php +++ b/includes/libs/replacers/HashtableReplacer.php @@ -20,6 +20,8 @@ /** * Class to perform replacement based on a simple hashtable lookup + * + * @deprecated since 1.32, use a Closure instead */ class HashtableReplacer extends Replacer { private $table, $index; @@ -29,6 +31,7 @@ class HashtableReplacer extends Replacer { * @param int $index */ public function __construct( $table, $index = 0 ) { + wfDeprecated( __METHOD__, '1.32' ); $this->table = $table; $this->index = $index; } diff --git a/includes/libs/replacers/RegexlikeReplacer.php b/includes/libs/replacers/RegexlikeReplacer.php index 9874f524c2..bdc4dc0044 100644 --- a/includes/libs/replacers/RegexlikeReplacer.php +++ b/includes/libs/replacers/RegexlikeReplacer.php @@ -20,6 +20,8 @@ /** * Class to replace regex matches with a string similar to that used in preg_replace() + * + * @deprecated since 1.32, use a Closure instead */ class RegexlikeReplacer extends Replacer { private $r; @@ -28,6 +30,7 @@ class RegexlikeReplacer extends Replacer { * @param string $r */ public function __construct( $r ) { + wfDeprecated( __METHOD__, '1.32' ); $this->r = $r; } diff --git a/includes/libs/replacers/Replacer.php b/includes/libs/replacers/Replacer.php index 655e771087..5425eedd74 100644 --- a/includes/libs/replacers/Replacer.php +++ b/includes/libs/replacers/Replacer.php @@ -21,12 +21,15 @@ /** * Base class for "replacers", objects used in preg_replace_callback() and * StringUtils::delimiterReplaceCallback() + * + * @deprecated since 1.32, use a Closure instead */ abstract class Replacer { /** * @return array */ public function cb() { + wfDeprecated( __METHOD__, '1.32' ); return [ $this, 'replace' ]; } diff --git a/includes/parser/LinkHolderArray.php b/includes/parser/LinkHolderArray.php index 939fe73ccd..7e150e9cab 100644 --- a/includes/parser/LinkHolderArray.php +++ b/includes/parser/LinkHolderArray.php @@ -406,12 +406,13 @@ class LinkHolderArray { $replacePairs[$searchkey] = $link; } } - $replacer = new HashtableReplacer( $replacePairs, 1 ); # Do the thing $text = preg_replace_callback( '/()/', - $replacer->cb(), + function ( array $matches ) use ( $replacePairs ) { + return $replacePairs[$matches[1]]; + }, $text ); } @@ -436,12 +437,14 @@ class LinkHolderArray { ); $output->addInterwikiLink( $link['title'] ); } - $replacer = new HashtableReplacer( $replacePairs, 1 ); $text = preg_replace_callback( '//', - $replacer->cb(), - $text ); + function ( array $matches ) use ( $replacePairs ) { + return $replacePairs[$matches[1]]; + }, + $text + ); } /** -- 2.20.1