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
This commit is contained in:
Kevin Israel 2018-07-10 11:14:29 -04:00 committed by Kunal Mehta
parent a72fa4c99d
commit 93fc424fc1
7 changed files with 43 additions and 14 deletions

View file

@ -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

View file

@ -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 );

View file

@ -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;

View file

@ -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;
}

View file

@ -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;
}

View file

@ -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' ];
}

View file

@ -406,12 +406,13 @@ class LinkHolderArray {
$replacePairs[$searchkey] = $link;
}
}
$replacer = new HashtableReplacer( $replacePairs, 1 );
# Do the thing
$text = preg_replace_callback(
'/(<!--LINK\'" .*?-->)/',
$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(
'/<!--IWLINK\'" (.*?)-->/',
$replacer->cb(),
$text );
function ( array $matches ) use ( $replacePairs ) {
return $replacePairs[$matches[1]];
},
$text
);
}
/**