From 04c1be307bb21d7574c9075ea5e928e15c843cd2 Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 6 Sep 2017 22:31:59 +0200 Subject: [PATCH] Fix tag in parse-on-render mode. - Fix: tag did not work in parse-on-render mode. --- includes/Extension.php | 7 +++++- includes/Linker.php | 26 ++++++++++++++++++++- tests/phpunit/ExtensionTest.php | 40 ++++++++++++++++++++++++++------- tests/phpunit/SplitterTest.php | 6 +++++ 4 files changed, 69 insertions(+), 10 deletions(-) diff --git a/includes/Extension.php b/includes/Extension.php index 82ef389..dbe3d7e 100644 --- a/includes/Extension.php +++ b/includes/Extension.php @@ -126,7 +126,10 @@ class Extension { * See https://www.mediawiki.org/wiki/Manual:Tag_extensions#Example */ public static function doNoautolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) { - return $parser->recursiveTagParse( $input, $frame ); + Linker::lock(); + $result = $parser->recursiveTagParse( $input, $frame ); + Linker::unlock(); + return $result; } /* @@ -138,7 +141,9 @@ class Extension { $config = new Config(); $linker = new Linker( $config ); $source = Source::createFromParserAndText( $parser, $input, $config ); + Linker::unlock(); $result = $linker->linkContent( $source ); + Linker::lock(); if ( $result ) { return $parser->recursiveTagParse( $result, $frame ); } else { diff --git a/includes/Linker.php b/includes/Linker.php index d875339..04871dc 100644 --- a/includes/Linker.php +++ b/includes/Linker.php @@ -46,6 +46,8 @@ class Linker { */ private $linkValue; + private static $locked = 0; + /** * Constructs a new instance of the Linker class. * @@ -68,7 +70,7 @@ class Linker { * @return String|null Source page text with links to target pages, or null if no links were added */ public function linkContent( Source $source ) { - if ( !$source->canBeLinked() ) { + if ( self::$locked > 0 || !$source->canBeLinked() ) { return; } @@ -197,6 +199,28 @@ class Linker { return '[[' . $matches[ 0 ] . ']]'; } } + + /** + * Increases an internal static lock counter by 1. + * + * If the Linker class is locked (counter > 0), linkContent() will be a no-op. + * Locking is necessary to enable nested and tags in + * parseOnRender mode. + */ + public static function lock() { + self::$locked += 1; + } + + /** + * Decreases an internal static lock counter by 1. + * + * If the Linker class is locked (counter > 0), linkContent() will be a no-op. + * Locking is necessary to enable nested and tags in + * parseOnRender mode. + */ + public static function unlock() { + self::$locked -= 1; + } } // vim: ts=2:sw=2:noet:comments^=\:/// diff --git a/tests/phpunit/ExtensionTest.php b/tests/phpunit/ExtensionTest.php index 9f9be0a..b3dd7ec 100644 --- a/tests/phpunit/ExtensionTest.php +++ b/tests/phpunit/ExtensionTest.php @@ -47,10 +47,15 @@ class ExtensionTest extends LinkTitles\TestCase { 'This page should link to the [[link target]] but not to test page' ], [ - false, + false, // parseOnEdit 'This page should *not* link to the link target', 'This page should *not* link to the link target' - ] + ], + [ + true, // parseOnEdit + 'With __NOAUTOLINKS__, this page should not link to the link target', + 'With __NOAUTOLINKS__, this page should not link to the link target' + ], ]; } @@ -65,8 +70,7 @@ class ExtensionTest extends LinkTitles\TestCase { ] ); $title = $this->insertPage( 'test page', $input )['title']; $page = new WikiPage( $title ); - $content = $page->getContent(); - $output = $content->getParserOutput( $title, null, null, false ); + $output = $page->getParserOutput( new ParserOptions(), null, true ); $lines = explode( "\n", $output->getText() ); $this->assertRegexp( $expectedOutput, $lines[0] ); } @@ -75,14 +79,34 @@ class ExtensionTest extends LinkTitles\TestCase { return [ [ true, // parseOnRender - 'This page should link to the link target but not to test page', - '_This page should link to the ]+>link target but not to test page_' + 'This page should link to the link target but not to the test page', + '_This page should link to the ]+>link target but not to the test page_' ], [ - false, + false, // parseOnRender 'This page should not link to the link target', '_This page should not link to the link target_' - ] + ], + [ + true, // parseOnRender + '__NOAUTOLINKS__With noautolinks magic word, this page should not link to the link target', + '_With noautolinks magic word, this page should not link to the link target_' + ], + [ + true, // parseOnRender + '__NOAUTOLINKS__With noautolinks magic word, link target in autolinks tag should be linked', + '_With noautolinks magic word, ]+>link target in autolinks tag should be linked_' + ], + [ + true, // parseOnRender + 'In a noautolinks tag, link target should NOT be linked', + '_In a noautolinks tag, link target should NOT be linked_' + ], + [ + true, // parseOnRender + 'In a noautolinks tag, link target in autolinks tag should be linked', + '_In a noautolinks tag, ]+>link target in autolinks tag should be linked_' + ], ]; } } diff --git a/tests/phpunit/SplitterTest.php b/tests/phpunit/SplitterTest.php index 489ff46..21716f6 100644 --- a/tests/phpunit/SplitterTest.php +++ b/tests/phpunit/SplitterTest.php @@ -105,6 +105,12 @@ class SplitterTest extends MediaWikiTestCase { // "With parseHeadings = false,\n==an improperly formatted heading may be linked=\n", // [ "With parseHeadings = false,\n==an improperly formatted heading may be linked=\n" ] // ], + [ + true, // skipTemplates + true, // parseHeadings + "Text in noautolinks tagshould be excluded", + [ "Text ", "in noautolinks tag", "should be excluded" ] + ], ]; } }