mirror of
https://github.com/diocloid/LinkTitles.git
synced 2025-07-13 01:39:30 +02:00
Add linker tests, fix firstOnly behavior.
- Fix: The firstOnly option finally also works if a page contains a link to a given other page that was not currently added by the extension, i.e. that existed prior to an edit or that was manually added. Closes #12.
This commit is contained in:
@ -86,6 +86,14 @@ class Linker {
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Dealing with existing links if the firstOnly option is set:
|
||||||
|
// A link to the current page should only be recognized if it appears in
|
||||||
|
// clear text, i.e. we do not count piped links as existing links.
|
||||||
|
// (Similarly, by design, redirections should not be counted as existing links.)
|
||||||
|
if ( $limit == 1 && preg_match( '/[[' . $target->getCaseSensitiveLinkValueRegex() . ']]/' , $text ) ) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
// Split the page content by non-linkable sections.
|
// Split the page content by non-linkable sections.
|
||||||
// Credits to inhan @ StackOverflow for suggesting preg_split.
|
// Credits to inhan @ StackOverflow for suggesting preg_split.
|
||||||
// See http://stackoverflow.com/questions/10672286
|
// See http://stackoverflow.com/questions/10672286
|
||||||
@ -161,7 +169,7 @@ class Linker {
|
|||||||
// we need to ignore the first letter of the page titles, as
|
// we need to ignore the first letter of the page titles, as
|
||||||
// it does not matter for linking.
|
// it does not matter for linking.
|
||||||
if ( strcmp( substr( $this->targetTitleText, 1 ), substr( $matches[ 0 ], 1) ) == 0 ) {
|
if ( strcmp( substr( $this->targetTitleText, 1 ), substr( $matches[ 0 ], 1) ) == 0 ) {
|
||||||
// Case-sensitive match: no need to bulid piped link.
|
// Case-sensitive match: no need to build piped link.
|
||||||
return '[[' . $matches[ 0 ] . ']]';
|
return '[[' . $matches[ 0 ] . ']]';
|
||||||
} else {
|
} else {
|
||||||
// Case-insensitive match: build piped link.
|
// Case-insensitive match: build piped link.
|
||||||
@ -171,7 +179,7 @@ class Linker {
|
|||||||
// If $wgCapitalLinks is false, we can use the simple variant
|
// If $wgCapitalLinks is false, we can use the simple variant
|
||||||
// of the callback function.
|
// of the callback function.
|
||||||
if ( strcmp( $this->targetTitleText, $matches[ 0 ] ) == 0 ) {
|
if ( strcmp( $this->targetTitleText, $matches[ 0 ] ) == 0 ) {
|
||||||
// Case-sensitive match: no need to bulid piped link.
|
// Case-sensitive match: no need to build piped link.
|
||||||
return '[[' . $matches[ 0 ] . ']]';
|
return '[[' . $matches[ 0 ] . ']]';
|
||||||
} else {
|
} else {
|
||||||
// Case-insensitive match: build piped link.
|
// Case-insensitive match: build piped link.
|
||||||
|
@ -60,6 +60,8 @@ class Target {
|
|||||||
*/
|
*/
|
||||||
private $config;
|
private $config;
|
||||||
|
|
||||||
|
private $caseSensitiveLinkValueRegex;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Constructs a new Target object
|
* Constructs a new Target object
|
||||||
*
|
*
|
||||||
@ -103,20 +105,7 @@ class Target {
|
|||||||
* @return String regular expression for this title.
|
* @return String regular expression for this title.
|
||||||
*/
|
*/
|
||||||
public function getCaseSensitiveRegex() {
|
public function getCaseSensitiveRegex() {
|
||||||
$regexSafeTitle = $this->getRegexSafeTitle();
|
return $this->buildRegex( $this->getCaseSensitiveLinkValueRegex() );
|
||||||
|
|
||||||
// Depending on the $config->capitalLinks setting,
|
|
||||||
// the title has to be searched for either in a strictly case-sensitive
|
|
||||||
// way, or in a 'fuzzy' way where the first letter of the title may
|
|
||||||
// be either case.
|
|
||||||
//
|
|
||||||
if ( $this->config->capitalLinks && ( $regexSafeTitle[0] != '\\' )) {
|
|
||||||
$searchTerm = '((?i)' . $regexSafeTitle[0] . '(?-i)' . substr($regexSafeTitle, 1) . ')';
|
|
||||||
} else {
|
|
||||||
$searchTerm = '(' . $regexSafeTitle . ')';
|
|
||||||
}
|
|
||||||
|
|
||||||
return $this->buildRegex( $searchTerm );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -138,6 +127,27 @@ class Target {
|
|||||||
return '/(?<![\:\.\@\/\?\&])' . $this->wordStart . $searchTerm . $this->wordEnd . '/S';
|
return '/(?<![\:\.\@\/\?\&])' . $this->wordStart . $searchTerm . $this->wordEnd . '/S';
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Gets the (cached) regex for the link value.
|
||||||
|
*
|
||||||
|
* Depending on the $config->capitalLinks setting, the title has to be
|
||||||
|
* searched for either in a strictly case-sensitive way, or in a 'fuzzy' way
|
||||||
|
* where the first letter of the title may be either case.
|
||||||
|
*
|
||||||
|
* @return String regular expression pattern for the link value.
|
||||||
|
*/
|
||||||
|
public function getCaseSensitiveLinkValueRegex() {
|
||||||
|
if ( $this->caseSensitiveLinkValueRegex === null ) {
|
||||||
|
$regexSafeTitle = $this->getRegexSafeTitle();
|
||||||
|
if ( $this->config->capitalLinks && ( $regexSafeTitle[0] != '\\' )) {
|
||||||
|
$this->caseSensitiveLinkValueRegex = '((?i)' . $regexSafeTitle[0] . '(?-i)' . substr($regexSafeTitle, 1) . ')';
|
||||||
|
} else {
|
||||||
|
$this->caseSensitiveLinkValueRegex = '(' . $regexSafeTitle . ')';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return $this->caseSensitiveLinkValueRegex;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the \Content of the target page.
|
* Returns the \Content of the target page.
|
||||||
*
|
*
|
||||||
|
@ -1,5 +1,13 @@
|
|||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
|
* Unit tests for the LinkTitles\Linker class.
|
||||||
|
*
|
||||||
|
* The test class is prefixed with 'LinkTitles' to avoid a naming collision
|
||||||
|
* with a class that exists in the MediaWiki core.
|
||||||
|
*
|
||||||
|
* Ideally the test classes should be namespaced, but when you do that, they
|
||||||
|
* will no longer be automatically discovered.
|
||||||
|
*
|
||||||
* @group bovender
|
* @group bovender
|
||||||
* @group Database
|
* @group Database
|
||||||
*/
|
*/
|
||||||
@ -17,12 +25,13 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
|
|||||||
public function testLinkContentSmartMode( $capitalLinks, $smartMode, $input, $expectedOutput) {
|
public function testLinkContentSmartMode( $capitalLinks, $smartMode, $input, $expectedOutput) {
|
||||||
$this->setMwGlobals( 'wgCapitalLinks', $capitalLinks );
|
$this->setMwGlobals( 'wgCapitalLinks', $capitalLinks );
|
||||||
$config = new LinkTitles\Config();
|
$config = new LinkTitles\Config();
|
||||||
|
$config->firstOnly = false;
|
||||||
$config->smartMode = $smartMode;
|
$config->smartMode = $smartMode;
|
||||||
$linker = new LinkTitles\Linker( $config );
|
$linker = new LinkTitles\Linker( $config );
|
||||||
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
}
|
}
|
||||||
|
|
||||||
public static function provideLinkContentSmartModeData() {
|
public function provideLinkContentSmartModeData() {
|
||||||
return [
|
return [
|
||||||
[
|
[
|
||||||
true, // wgCapitalLinks
|
true, // wgCapitalLinks
|
||||||
@ -86,4 +95,39 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
|
|||||||
],
|
],
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @dataProvider provideLinkContentFirstOnlyData
|
||||||
|
*/
|
||||||
|
public function testLinkContentFirstOnly( $firstOnly, $input, $expectedOutput ) {
|
||||||
|
$config = new LinkTitles\Config();
|
||||||
|
$config->firstOnly = $firstOnly;
|
||||||
|
$linker = new LinkTitles\Linker( $config );
|
||||||
|
$this->assertSame( $expectedOutput, $linker->linkContent( $this->title, $input ));
|
||||||
|
}
|
||||||
|
|
||||||
|
public function provideLinkContentFirstOnlyData() {
|
||||||
|
return [
|
||||||
|
[
|
||||||
|
false, // firstOnly
|
||||||
|
'With firstOnly = false, link target is a link target multiple times',
|
||||||
|
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
false, // firstOnly
|
||||||
|
'With firstOnly = false, [[link target]] is a link target multiple times',
|
||||||
|
'With firstOnly = false, [[link target]] is a [[link target]] multiple times'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // firstOnly
|
||||||
|
'With firstOnly = true, link target is a link target only once',
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once'
|
||||||
|
],
|
||||||
|
[
|
||||||
|
true, // firstOnly
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once',
|
||||||
|
'With firstOnly = true, [[link target]] is a link target only once'
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user