diff --git a/NEWS b/NEWS index f271161..05aa26c 100644 --- a/NEWS +++ b/NEWS @@ -1,3 +1,12 @@ +Version 5.0.3 (2017-09-06) +------------------------------------------------------------------------ + +- Fix: tag was broken. +- Fix: tag did not work in parse-on-render mode. + +* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * + + Version 5.0.2 (2017-09-05) ------------------------------------------------------------------------ diff --git a/extension.json b/extension.json index 0634b08..7370ccc 100644 --- a/extension.json +++ b/extension.json @@ -9,7 +9,7 @@ ], "type": "parserhook", "url": "https://www.mediawiki.org/wiki/Extension:LinkTitles", - "version": "5.0.2", + "version": "5.0.3", "license-name": "GPL-2.0+", "descriptionmsg": "linktitles-desc", "requires": { diff --git a/gh-pages b/gh-pages index 81d2968..ca9037c 160000 --- a/gh-pages +++ b/gh-pages @@ -1 +1 @@ -Subproject commit 81d296866161c2ad5a530bcc75f6bb2d78c29b8d +Subproject commit ca9037ca4edbf7cb4ba6a07201da5b22c86ceb6e diff --git a/includes/Extension.php b/includes/Extension.php index d46cad8..dbe3d7e 100644 --- a/includes/Extension.php +++ b/includes/Extension.php @@ -126,7 +126,10 @@ public static function onParserFirstCallInit( \Parser $parser ) { * 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; } /* @@ -137,8 +140,10 @@ public static function doNoautolinksTag( $input, array $args, \Parser $parser, \ public static function doAutolinksTag( $input, array $args, \Parser $parser, \PPFrame $frame ) { $config = new Config(); $linker = new Linker( $config ); - $source = Source::createFromParser( $parser, $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 @@ public function __construct( Config &$config ) { * @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 @@ private function smartModeCallback( array $matches ) { 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 da98678..b3dd7ec 100644 --- a/tests/phpunit/ExtensionTest.php +++ b/tests/phpunit/ExtensionTest.php @@ -26,23 +26,87 @@ */ class ExtensionTest extends LinkTitles\TestCase { - public function testParseOnEdit() { + /** + * @dataProvider provideParseOnEditData + */ + public function testParseOnEdit( $parseOnEdit, $input, $expectedOutput) { $this->setMwGlobals( [ - 'wgLinkTitlesParseOnEdit' => true, - 'wgLinkTitlesParseOnRender' => false + 'wgLinkTitlesParseOnEdit' => $parseOnEdit, + 'wgLinkTitlesParseOnRender' => !$parseOnEdit ] ); - $pageId = $this->insertPage( 'test page', 'This page should link to the link target but not to test page' )['id']; + $pageId = $this->insertPage( 'test page', $input )['id']; $page = WikiPage::newFromId( $pageId ); - $this->assertSame( 'This page should link to the [[link target]] but not to test page', self::getPageText( $page ) ); + $this->assertSame( $expectedOutput, self::getPageText( $page ) ); } - public function testDoNotParseOnEdit() { + public function provideParseOnEditData() { + return [ + [ + true, // parseOnEdit + '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' + ], + [ + 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' + ], + ]; + } + + + /** + * @dataProvider provideParseOnRenderData + */ + public function testParseOnRender( $parseOnRender, $input, $expectedOutput) { $this->setMwGlobals( [ - 'wgLinkTitlesParseOnEdit' => false, - 'wgLinkTitlesParseOnRender' => false + 'wgLinkTitlesParseOnEdit' => false, // do not modify the page as we create it + 'wgLinkTitlesParseOnRender' => $parseOnRender ] ); - $pageId = $this->insertPage( 'test page', 'This page should not link to the link target' )['id']; - $page = WikiPage::newFromId( $pageId ); - $this->assertSame( 'This page should not link to the link target', self::getPageText( $page ) ); + $title = $this->insertPage( 'test page', $input )['title']; + $page = new WikiPage( $title ); + $output = $page->getParserOutput( new ParserOptions(), null, true ); + $lines = explode( "\n", $output->getText() ); + $this->assertRegexp( $expectedOutput, $lines[0] ); + } + + public function provideParseOnRenderData() { + return [ + [ + true, // parseOnRender + '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, // 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 @@ public static function provideSplitData() { // "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" ] + ], ]; } }