Move wfEscapeWikiText() to Parser::escapeWikitext()
authorKunal Mehta <legoktm@member.fsf.org>
Wed, 14 Sep 2016 05:34:24 +0000 (22:34 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Wed, 14 Sep 2016 05:34:24 +0000 (22:34 -0700)
wfEscapeWikiText() used $wgEnableMagicLinks, but that could result in an
inconsistency when something modifies the magic link related
ParserOptions.

In general, most uses of wfEscapeWikiText() are in parser functions or
when message parsing, so the Parser is a logical place for it.

A future patch will make it easy to use Parser::escapeWikitext() in
message parameters.

Change-Id: I0fd4d5c135541971b1384a20328f1302b03d715f

includes/GlobalFunctions.php
includes/parser/CoreParserFunctions.php
includes/parser/Parser.php

index 0e59653..ee5ebd0 100644 (file)
@@ -1656,6 +1656,8 @@ function wfClientAcceptsGzip( $force = false ) {
 }
 
 /**
+ * @deprecated since 1.28, use Parser::escapeWikitext() directly
+ *
  * Escapes the given text so that it may be output using addWikiText()
  * without any linking, formatting, etc. making its way through. This
  * is achieved by substituting certain characters with HTML entities.
@@ -1665,47 +1667,8 @@ function wfClientAcceptsGzip( $force = false ) {
  * @return string
  */
 function wfEscapeWikiText( $text ) {
-       global $wgEnableMagicLinks;
-       static $repl = null, $repl2 = null;
-       if ( $repl === null ) {
-               $repl = [
-                       '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
-                       '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
-                       '{' => '&#123;', '|' => '&#124;', '}' => '&#125;', ';' => '&#59;',
-                       "\n#" => "\n&#35;", "\r#" => "\r&#35;",
-                       "\n*" => "\n&#42;", "\r*" => "\r&#42;",
-                       "\n:" => "\n&#58;", "\r:" => "\r&#58;",
-                       "\n " => "\n&#32;", "\r " => "\r&#32;",
-                       "\n\n" => "\n&#10;", "\r\n" => "&#13;\n",
-                       "\n\r" => "\n&#13;", "\r\r" => "\r&#13;",
-                       "\n\t" => "\n&#9;", "\r\t" => "\r&#9;", // "\n\t\n" is treated like "\n\n"
-                       "\n----" => "\n&#45;---", "\r----" => "\r&#45;---",
-                       '__' => '_&#95;', '://' => '&#58;//',
-               ];
-
-               $magicLinks = array_keys( array_filter( $wgEnableMagicLinks ) );
-               // We have to catch everything "\s" matches in PCRE
-               foreach ( $magicLinks as $magic ) {
-                       $repl["$magic "] = "$magic&#32;";
-                       $repl["$magic\t"] = "$magic&#9;";
-                       $repl["$magic\r"] = "$magic&#13;";
-                       $repl["$magic\n"] = "$magic&#10;";
-                       $repl["$magic\f"] = "$magic&#12;";
-               }
-
-               // And handle protocols that don't use "://"
-               global $wgUrlProtocols;
-               $repl2 = [];
-               foreach ( $wgUrlProtocols as $prot ) {
-                       if ( substr( $prot, -1 ) === ':' ) {
-                               $repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
-                       }
-               }
-               $repl2 = $repl2 ? '/\b(' . implode( '|', $repl2 ) . '):/i' : '/^(?!)/';
-       }
-       $text = substr( strtr( "\n$text", $repl ), 1 );
-       $text = preg_replace( $repl2, '$1&#58;', $text );
-       return $text;
+       global $wgParser;
+       return $wgParser->escapeWikitext( $text );
 }
 
 /**
index 01cce02..b3dc17c 100644 (file)
@@ -594,98 +594,98 @@ class CoreParserFunctions {
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getText() );
+               return $parser->escapeWikitext( $t->getText() );
        }
-       public static function pagenamee( $parser, $title = null ) {
+       public static function pagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getPartialURL() );
+               return $parser->escapeWikitext( $t->getPartialURL() );
        }
-       public static function fullpagename( $parser, $title = null ) {
+       public static function fullpagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getPrefixedText() );
+               return $parser->escapeWikitext( $t->getPrefixedText() );
        }
-       public static function fullpagenamee( $parser, $title = null ) {
+       public static function fullpagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getPrefixedURL() );
+               return $parser->escapeWikitext( $t->getPrefixedURL() );
        }
-       public static function subpagename( $parser, $title = null ) {
+       public static function subpagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getSubpageText() );
+               return $parser->escapeWikitext( $t->getSubpageText() );
        }
-       public static function subpagenamee( $parser, $title = null ) {
+       public static function subpagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getSubpageUrlForm() );
+               return $parser->escapeWikitext( $t->getSubpageUrlForm() );
        }
-       public static function rootpagename( $parser, $title = null ) {
+       public static function rootpagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getRootText() );
+               return $parser->escapeWikitext( $t->getRootText() );
        }
-       public static function rootpagenamee( $parser, $title = null ) {
+       public static function rootpagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) );
+               return $parser->escapeWikitext( wfUrlencode( str_replace( ' ', '_', $t->getRootText() ) ) );
        }
-       public static function basepagename( $parser, $title = null ) {
+       public static function basepagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getBaseText() );
+               return $parser->escapeWikitext( $t->getBaseText() );
        }
-       public static function basepagenamee( $parser, $title = null ) {
+       public static function basepagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) );
+               return $parser->escapeWikitext( wfUrlencode( str_replace( ' ', '_', $t->getBaseText() ) ) );
        }
-       public static function talkpagename( $parser, $title = null ) {
+       public static function talkpagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getTalkPage()->getPrefixedText() );
+               return $parser->escapeWikitext( $t->getTalkPage()->getPrefixedText() );
        }
-       public static function talkpagenamee( $parser, $title = null ) {
+       public static function talkpagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) || !$t->canTalk() ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getTalkPage()->getPrefixedURL() );
+               return $parser->escapeWikitext( $t->getTalkPage()->getPrefixedURL() );
        }
-       public static function subjectpagename( $parser, $title = null ) {
+       public static function subjectpagename( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedText() );
+               return $parser->escapeWikitext( $t->getSubjectPage()->getPrefixedText() );
        }
-       public static function subjectpagenamee( $parser, $title = null ) {
+       public static function subjectpagenamee( Parser $parser, $title = null ) {
                $t = Title::newFromText( $title );
                if ( is_null( $t ) ) {
                        return '';
                }
-               return wfEscapeWikiText( $t->getSubjectPage()->getPrefixedURL() );
+               return $parser->escapeWikitext( $t->getSubjectPage()->getPrefixedURL() );
        }
 
        /**
index 7c18798..0fb021c 100644 (file)
@@ -6028,4 +6028,62 @@ class Parser {
                OutputPage::setupOOUI();
                $this->mOutput->setEnableOOUI( true );
        }
+
+       /**
+        * Escapes the given text so that it may be output using addWikiText()
+        * without any linking, formatting, etc. making its way through. This
+        * is achieved by substituting certain characters with HTML entities.
+        * As required by the callers, "<nowiki>" is not used.
+        *
+        * @since 1.28
+        *
+        * @param string $text Text to be escaped
+        * @return string
+        */
+       public function escapeWikitext( $text ) {
+               static $repl = null, $repl2 = null;
+               if ( $repl === null ) {
+                       $repl = [
+                               '"' => '&#34;', '&' => '&#38;', "'" => '&#39;', '<' => '&#60;',
+                               '=' => '&#61;', '>' => '&#62;', '[' => '&#91;', ']' => '&#93;',
+                               '{' => '&#123;', '|' => '&#124;', '}' => '&#125;', ';' => '&#59;',
+                               "\n#" => "\n&#35;", "\r#" => "\r&#35;",
+                               "\n*" => "\n&#42;", "\r*" => "\r&#42;",
+                               "\n:" => "\n&#58;", "\r:" => "\r&#58;",
+                               "\n " => "\n&#32;", "\r " => "\r&#32;",
+                               "\n\n" => "\n&#10;", "\r\n" => "&#13;\n",
+                               "\n\r" => "\n&#13;", "\r\r" => "\r&#13;",
+                               "\n\t" => "\n&#9;", "\r\t" => "\r&#9;", // "\n\t\n" is treated like "\n\n"
+                               "\n----" => "\n&#45;---", "\r----" => "\r&#45;---",
+                               '__' => '_&#95;', '://' => '&#58;//',
+                       ];
+
+                       $magicLinks = array_keys( array_filter( [
+                               'ISBN' => $this->mOptions->getMagicISBNLinks(),
+                               'PMID' => $this->mOptions->getMagicPMIDLinks(),
+                               'RFC' => $this->mOptions->getMagicRFCLinks(),
+                       ] ) );
+                       // We have to catch everything "\s" matches in PCRE
+                       foreach ( $magicLinks as $magic ) {
+                               $repl["$magic "] = "$magic&#32;";
+                               $repl["$magic\t"] = "$magic&#9;";
+                               $repl["$magic\r"] = "$magic&#13;";
+                               $repl["$magic\n"] = "$magic&#10;";
+                               $repl["$magic\f"] = "$magic&#12;";
+                       }
+
+                       // And handle protocols that don't use "://"
+                       global $wgUrlProtocols;
+                       $repl2 = [];
+                       foreach ( $wgUrlProtocols as $prot ) {
+                               if ( substr( $prot, -1 ) === ':' ) {
+                                       $repl2[] = preg_quote( substr( $prot, 0, -1 ), '/' );
+                               }
+                       }
+                       $repl2 = $repl2 ? '/\b(' . implode( '|', $repl2 ) . '):/i' : '/^(?!)/';
+               }
+               $text = substr( strtr( "\n$text", $repl ), 1 );
+               $text = preg_replace( $repl2, '$1&#58;', $text );
+               return $text;
+       }
 }