From 78debba3aabc17b482b1881b7009546a1a48755a Mon Sep 17 00:00:00 2001 From: Kunal Mehta Date: Fri, 9 Sep 2016 00:28:49 -0700 Subject: [PATCH] Parser: Allow disabling magic link functionality 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 | 12 ++++++ includes/GlobalFunctions.php | 4 +- includes/parser/Parser.php | 10 ++++- includes/parser/ParserOptions.php | 64 ++++++++++++++++++++++++++++++- tests/parser/ParserTestRunner.php | 4 ++ tests/parser/parserTests.txt | 18 ++++++++- 6 files changed, 108 insertions(+), 4 deletions(-) diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index ff7430b06d..3ab8829a59 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -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 } /************************************************************************//** diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index f93c64eedb..0e596530c3 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -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 ) { '__' => '__', '://' => '://', ]; + $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 "; $repl["$magic\t"] = "$magic "; $repl["$magic\r"] = "$magic "; diff --git a/includes/parser/Parser.php b/includes/parser/Parser.php index d83ea34efd..7c18798f2e 100644 --- a/includes/parser/Parser.php +++ b/includes/parser/Parser.php @@ -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 diff --git a/includes/parser/ParserOptions.php b/includes/parser/ParserOptions.php index 891c4dde6c..fd826a204c 100644 --- a/includes/parser/ParserOptions.php +++ b/includes/parser/ParserOptions.php @@ -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' ); diff --git a/tests/parser/ParserTestRunner.php b/tests/parser/ParserTestRunner.php index ba7f8f89eb..6659ff8d1c 100644 --- a/tests/parser/ParserTestRunner.php +++ b/tests/parser/ParserTestRunner.php @@ -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 ) { diff --git a/tests/parser/parserTests.txt b/tests/parser/parserTests.txt index e1a54fbbb1..2c8b163a5f 100644 --- a/tests/parser/parserTests.txt +++ b/tests/parser/parserTests.txt @@ -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]

Xfoo

!! 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 +

ISBN 0-306-40615-2 +PMID 1234 +RFC 4321 +

+!! end + ### ### Templates #### -- 2.20.1