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
of using showFatalError directly: OutputPage::showFileDeleteError()
OutputPage::showFileNotFoundError(), OutputPage::showFileRenameError()
OutputPage::showFileCopyError() and OutputPage::showUnexpectedValueError().
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
=== Other changes in 1.32 ===
* (T198811) The following tables have had their UNIQUE indexes turned into proper
* @return string The string with the matches replaced
*/
static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
* @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
+ );
$text = str_replace( $placeholder, '', $text );
// Replace instances of the separator inside HTML-like tags with the placeholder
$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 );
// Explode, then put the replaced separators back in
$items = explode( $separator, $cleaned );
$text = str_replace( $placeholder, '', $text );
// Replace instances of the separator inside HTML-like tags with the placeholder
$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 );
// Explode, then put the replaced separators back in
$cleaned = str_replace( $search, $replace, $cleaned );
/**
* Class to perform secondary replacement within each replacement string
/**
* Class to perform secondary replacement within each replacement string
+ *
+ * @deprecated since 1.32, use a Closure instead
*/
class DoubleReplacer extends Replacer {
/**
*/
class DoubleReplacer extends Replacer {
/**
* @param int $index
*/
public function __construct( $from, $to, $index = 0 ) {
* @param int $index
*/
public function __construct( $from, $to, $index = 0 ) {
+ wfDeprecated( __METHOD__, '1.32' );
$this->from = $from;
$this->to = $to;
$this->index = $index;
$this->from = $from;
$this->to = $to;
$this->index = $index;
/**
* Class to perform replacement based on a simple hashtable lookup
/**
* 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;
*/
class HashtableReplacer extends Replacer {
private $table, $index;
* @param int $index
*/
public function __construct( $table, $index = 0 ) {
* @param int $index
*/
public function __construct( $table, $index = 0 ) {
+ wfDeprecated( __METHOD__, '1.32' );
$this->table = $table;
$this->index = $index;
}
$this->table = $table;
$this->index = $index;
}
/**
* Class to replace regex matches with a string similar to that used in preg_replace()
/**
* 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;
*/
class RegexlikeReplacer extends Replacer {
private $r;
* @param string $r
*/
public function __construct( $r ) {
* @param string $r
*/
public function __construct( $r ) {
+ wfDeprecated( __METHOD__, '1.32' );
/**
* Base class for "replacers", objects used in preg_replace_callback() and
* StringUtils::delimiterReplaceCallback()
/**
* 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() {
*/
abstract class Replacer {
/**
* @return array
*/
public function cb() {
+ wfDeprecated( __METHOD__, '1.32' );
return [ $this, 'replace' ];
}
return [ $this, 'replace' ];
}
$replacePairs[$searchkey] = $link;
}
}
$replacePairs[$searchkey] = $link;
}
}
- $replacer = new HashtableReplacer( $replacePairs, 1 );
# Do the thing
$text = preg_replace_callback(
'/(<!--LINK\'" .*?-->)/',
# Do the thing
$text = preg_replace_callback(
'/(<!--LINK\'" .*?-->)/',
+ function ( array $matches ) use ( $replacePairs ) {
+ return $replacePairs[$matches[1]];
+ },
);
$output->addInterwikiLink( $link['title'] );
}
);
$output->addInterwikiLink( $link['title'] );
}
- $replacer = new HashtableReplacer( $replacePairs, 1 );
$text = preg_replace_callback(
'/<!--IWLINK\'" (.*?)-->/',
$text = preg_replace_callback(
'/<!--IWLINK\'" (.*?)-->/',
- $replacer->cb(),
- $text );
+ function ( array $matches ) use ( $replacePairs ) {
+ return $replacePairs[$matches[1]];
+ },
+ $text
+ );