StringUtils: Deprecate Replacer classes
authorKevin Israel <pleasestand@live.com>
Tue, 10 Jul 2018 15:14:29 +0000 (11:14 -0400)
committerKunal Mehta <legoktm@member.fsf.org>
Thu, 12 Jul 2018 17:25:59 +0000 (10:25 -0700)
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
includes/libs/StringUtils.php
includes/libs/replacers/DoubleReplacer.php
includes/libs/replacers/HashtableReplacer.php
includes/libs/replacers/RegexlikeReplacer.php
includes/libs/replacers/Replacer.php
includes/parser/LinkHolderArray.php

index 878c841..ac68967 100644 (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
index d91ac85..51d1081 100644 (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 );
index fed023b..9d05e06 100644 (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;
index 11637d0..8247694 100644 (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;
        }
index 9874f52..bdc4dc0 100644 (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;
        }
 
index 655e771..5425eed 100644 (file)
 /**
  * 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' ];
        }
 
index 939fe73..7e150e9 100644 (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
+               );
        }
 
        /**