From 7a69e87a8d4efb2d1f19d7bed57a48fac6b6644a Mon Sep 17 00:00:00 2001 From: Daniel Kraus Date: Wed, 27 Jun 2018 21:31:16 +0200 Subject: [PATCH] Fix handling of target titles with numbers. - Fix: Improve handling of page titles that start or end with numbers. --- includes/Target.php | 4 ++-- tests/phpunit/LinkerTest.php | 38 ++++++++++++++++++++++++++++++++++++ tests/phpunit/TargetTest.php | 4 ++-- 3 files changed, 42 insertions(+), 4 deletions(-) diff --git a/includes/Target.php b/includes/Target.php index b152f34..64d7d12 100644 --- a/includes/Target.php +++ b/includes/Target.php @@ -81,8 +81,8 @@ class Target { // to detect whole words containing non-ASCII characters as well. // Note that this requires a PCRE library that was compiled with // --enable-unicode-properties - ( $config->wordStartOnly ) ? $this->wordStart = '(?wordStart = ''; - ( $config->wordEndOnly ) ? $this->wordEnd = '(?!\pL)' : $this->wordEnd = ''; + ( $config->wordStartOnly ) ? $this->wordStart = '(?wordStart = ''; + ( $config->wordEndOnly ) ? $this->wordEnd = '(?!\pL|\pN)' : $this->wordEnd = ''; } /** diff --git a/tests/phpunit/LinkerTest.php b/tests/phpunit/LinkerTest.php index e43f029..6314185 100644 --- a/tests/phpunit/LinkerTest.php +++ b/tests/phpunit/LinkerTest.php @@ -47,6 +47,44 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase { parent::addDBDataOnce(); // call parent after adding page to have targets invalidated } + /** + * @dataProvider provideTestTitleWithNumberData + */ + public function testTitleWithNumber( $input, $expectedOutput ) { + $config = new LinkTitles\Config(); + $config->wordStartOnly = true; + $config->wordEndOnly = true; + $this->insertPage( 'numbered-1', 'This page serves as a link target with a numbered title' ); + $this->insertPage( 'numbered-101', 'This page serves as a link target with a numbered title' ); + parent::addDBDataOnce(); // call parent after adding page to have targets invalidated + $source = LinkTitles\Source::createFromTitleAndText( $this->title, $input, $config ); + $linker = new LinkTitles\Linker( $config ); + $result = $linker->linkContent( $source ); + if ( !$result ) { $result = $input; } + $this->assertSame( $expectedOutput, $result ); + } + + public function provideTestTitleWithNumberData() { + return [ + [ + "Page text with numbered-1 in it.", + "Page text with [[numbered-1]] in it.", + ], + [ + "Page text with numbered-101 in it.", + "Page text with [[numbered-101]] in it.", + ], + [ + "Page text with numbered-1010 in it.", + "Page text with numbered-1010 in it.", + ], + [ + "Page text with link target1 in it.", + "Page text with link target1 in it.", + ], + ]; + } + /** * @dataProvider provideLinkContentTemplatesData */ diff --git a/tests/phpunit/TargetTest.php b/tests/phpunit/TargetTest.php index f1d0c79..8b3a282 100644 --- a/tests/phpunit/TargetTest.php +++ b/tests/phpunit/TargetTest.php @@ -37,7 +37,7 @@ class TargetTest extends MediaWikiTestCase { public static function provideStartOnly() { return [ - [ true, '(?