Parser: Allow disabling magic link functionality
authorKunal Mehta <legoktm@member.fsf.org>
Fri, 9 Sep 2016 07:28:49 +0000 (00:28 -0700)
committerKunal Mehta <legoktm@member.fsf.org>
Tue, 13 Sep 2016 05:00:05 +0000 (22:00 -0700)
The magic link functionality is "old backwards-compatibility baggage"
that we probably want to get rid of eventually. The first step to doing
so would be making it configurable and allowing it to be turned off on
wikis that don't use it.

This adds each of the 3 magic link types as individual parser options,
which can be controlled by the $wgEnableMagicLinks setting.

Additionally, wfEscapeWikiText() was updated to only escape enabled
magic link types.

Bug: T47942
Change-Id: If63965f31d17da4b864510146e0018da1cae188c

includes/DefaultSettings.php
includes/GlobalFunctions.php
includes/parser/Parser.php
includes/parser/ParserOptions.php
tests/parser/ParserTestRunner.php
tests/parser/parserTests.txt

index ff7430b..3ab8829 100644 (file)
@@ -4353,6 +4353,18 @@ $wgEnableScaryTranscluding = false;
  */
 $wgTranscludeCacheExpiry = 3600;
 
+/**
+ * Enable the magic links feature of automatically turning ISBN xxx,
+ * PMID xxx, RFC xxx into links
+ *
+ * @since 1.28
+ */
+$wgEnableMagicLinks = [
+       'ISBN' => true,
+       'PMID' => true,
+       'RFC' => true
+];
+
 /** @} */ # end of parser settings }
 
 /************************************************************************//**
index f93c64e..0e59653 100644 (file)
@@ -1665,6 +1665,7 @@ function wfClientAcceptsGzip( $force = false ) {
  * @return string
  */
 function wfEscapeWikiText( $text ) {
+       global $wgEnableMagicLinks;
        static $repl = null, $repl2 = null;
        if ( $repl === null ) {
                $repl = [
@@ -1682,8 +1683,9 @@ function wfEscapeWikiText( $text ) {
                        '__' => '_&#95;', '://' => '&#58;//',
                ];
 
+               $magicLinks = array_keys( array_filter( $wgEnableMagicLinks ) );
                // We have to catch everything "\s" matches in PCRE
-               foreach ( [ 'ISBN', 'RFC', 'PMID' ] as $magic ) {
+               foreach ( $magicLinks as $magic ) {
                        $repl["$magic "] = "$magic&#32;";
                        $repl["$magic\t"] = "$magic&#9;";
                        $repl["$magic\r"] = "$magic&#13;";
index d83ea34..7c18798 100644 (file)
@@ -1439,11 +1439,17 @@ class Parser {
                } elseif ( isset( $m[5] ) && $m[5] !== '' ) {
                        # RFC or PMID
                        if ( substr( $m[0], 0, 3 ) === 'RFC' ) {
+                               if ( !$this->mOptions->getMagicRFCLinks() ) {
+                                       return $m[0];
+                               }
                                $keyword = 'RFC';
                                $urlmsg = 'rfcurl';
                                $cssClass = 'mw-magiclink-rfc';
                                $id = $m[5];
                        } elseif ( substr( $m[0], 0, 4 ) === 'PMID' ) {
+                               if ( !$this->mOptions->getMagicPMIDLinks() ) {
+                                       return $m[0];
+                               }
                                $keyword = 'PMID';
                                $urlmsg = 'pubmedurl';
                                $cssClass = 'mw-magiclink-pmid';
@@ -1454,7 +1460,9 @@ class Parser {
                        }
                        $url = wfMessage( $urlmsg, $id )->inContentLanguage()->text();
                        return Linker::makeExternalLink( $url, "{$keyword} {$id}", true, $cssClass, [], $this->mTitle );
-               } elseif ( isset( $m[6] ) && $m[6] !== '' ) {
+               } elseif ( isset( $m[6] ) && $m[6] !== ''
+                       && $this->mOptions->getMagicISBNLinks()
+               ) {
                        # ISBN
                        $isbn = $m[6];
                        $space = self::SPACE_NOT_NL; #  non-newline space
index 891c4dd..fd826a2 100644 (file)
@@ -215,6 +215,21 @@ class ParserOptions {
         */
        private $mExtraKey = '';
 
+       /**
+        * Are magic ISBN links enabled?
+        */
+       private $mMagicISBNLinks = true;
+
+       /**
+        * Are magic PMID links enabled?
+        */
+       private $mMagicPMIDLinks = true;
+
+       /**
+        * Are magic RFC links enabled?
+        */
+       private $mMagicRFCLinks = true;
+
        /**
         * Function to be called when an option is accessed.
         */
@@ -419,6 +434,28 @@ class ParserOptions {
                return $this->getUserLangObj()->getCode();
        }
 
+       /**
+        * @since 1.28
+        * @return bool
+        */
+       public function getMagicISBNLinks() {
+               return $this->mMagicISBNLinks;
+       }
+
+       /**
+        * @since 1.28
+        * @return bool
+        */
+       public function getMagicPMIDLinks() {
+               return $this->mMagicPMIDLinks;
+       }
+       /**
+        * @since 1.28
+        * @return bool
+        */
+       public function getMagicRFCLinks() {
+               return $this->mMagicRFCLinks;
+       }
        public function setInterwikiMagic( $x ) {
                return wfSetVar( $this->mInterwikiMagic, $x );
        }
@@ -558,6 +595,27 @@ class ParserOptions {
                return wfSetVar( $this->mIsPrintable, $x );
        }
 
+       /**
+        * @since 1.28
+        */
+       public function setMagicISBNLinks( $x ) {
+               return wfSetVar( $this->mMagicISBNLinks, $x );
+       }
+
+       /**
+        * @since 1.28
+        */
+       public function setMagicPMIDLinks( $x ) {
+               return wfSetVar( $this->mMagicPMIDLinks, $x );
+       }
+
+       /**
+        * @since 1.28
+        */
+       public function setMagicRFCLinks( $x ) {
+               return wfSetVar( $this->mMagicRFCLinks, $x );
+       }
+
        /**
         * Set the redirect target.
         *
@@ -667,7 +725,8 @@ class ParserOptions {
                        $wgAllowExternalImagesFrom, $wgEnableImageWhitelist, $wgAllowSpecialInclusion,
                        $wgMaxArticleSize, $wgMaxPPNodeCount, $wgMaxTemplateDepth, $wgMaxPPExpandDepth,
                        $wgCleanSignatures, $wgExternalLinkTarget, $wgExpensiveParserFunctionLimit,
-                       $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion;
+                       $wgMaxGeneratedPPNodeCount, $wgDisableLangConversion, $wgDisableTitleConversion,
+                       $wgEnableMagicLinks;
 
                // *UPDATE* ParserOptions::matches() if any of this changes as needed
                $this->mInterwikiMagic = $wgInterwikiMagic;
@@ -685,6 +744,9 @@ class ParserOptions {
                $this->mExternalLinkTarget = $wgExternalLinkTarget;
                $this->mDisableContentConversion = $wgDisableLangConversion;
                $this->mDisableTitleConversion = $wgDisableLangConversion || $wgDisableTitleConversion;
+               $this->mMagicISBNLinks = $wgEnableMagicLinks['ISBN'];
+               $this->mMagicPMIDLinks = $wgEnableMagicLinks['PMID'];
+               $this->mMagicRFCLinks = $wgEnableMagicLinks['RFC'];
 
                $this->mUser = $user;
                $this->mNumberHeadings = $user->getOption( 'numberheadings' );
index ba7f8f8..6659ff8 100644 (file)
@@ -993,6 +993,10 @@ class ParserTestRunner {
                        'wgThumbLimits' => [ self::getOptionValue( 'thumbsize', $opts, 180 ) ],
                        'wgDefaultLanguageVariant' => $variant,
                        'wgLinkHolderBatchSize' => $linkHolderBatchSize,
+                       // Set as a JSON object like:
+                       // wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
+                       'wgEnableMagicLinks' => self::getOptionValue( 'wgEnableMagicLinks', $opts, [] )
+                               + [ 'ISBN' => true, 'PMID' => true, 'RFC' => true ],
                ];
 
                if ( $config ) {
index e1a54fb..2c8b163 100644 (file)
@@ -35,7 +35,8 @@
 #
 # You can also set the following parser properties via test options:
 #  wgEnableUploads, wgAllowExternalImages, wgMaxTocLevel,
-#  wgLinkHolderBatchSize, wgRawHtml, wgInterwikiMagic
+#  wgLinkHolderBatchSize, wgRawHtml, wgInterwikiMagic,
+#  wgEnableMagicLinks
 #
 # For testing purposes, temporary articles can created:
 # !!article / NAMESPACE:TITLE / !!text / ARTICLE TEXT / !!endarticle
@@ -10541,6 +10542,21 @@ X[//tools.ietf.org/html/rfc1234 foo]
 <p>X<a rel="mw:ExtLink" href="//tools.ietf.org/html/rfc1234">foo</a></p>
 !! end
 
+!! test
+Magic links: All disabled (T47942)
+!! options
+wgEnableMagicLinks={"ISBN":false, "PMID":false, "RFC":false}
+!! wikitext
+ISBN 0-306-40615-2
+PMID 1234
+RFC 4321
+!! html/php
+<p>ISBN 0-306-40615-2
+PMID 1234
+RFC 4321
+</p>
+!! end
+
 ###
 ### Templates
 ####