Fix blacklist.

- Fix: Blacklist did not always work properly
This commit is contained in:
Daniel Kraus
2017-08-30 22:39:17 +02:00
parent 28dcd244e8
commit 6294ee8679
2 changed files with 29 additions and 16 deletions

View File

@ -90,9 +90,15 @@ class Targets {
private function fetch() { private function fetch() {
( $this->config->preferShortTitles ) ? $sortOrder = 'ASC' : $sortOrder = 'DESC'; ( $this->config->preferShortTitles ) ? $sortOrder = 'ASC' : $sortOrder = 'DESC';
// Build a blacklist of pages that are not supposed to be link // Build a blacklist of pages that are not supposed to be link
// targets. This includes the current page. // targets. This includes the current page.
$blackList = str_replace( ' ', '_', '("' . implode( '","',$this->config->blackList ) . '")' ); if ( $this->config->blackList ) {
$blackList = 'page_title NOT IN ' .
str_replace( ' ', '_', '("' . implode( '","', str_replace( '"', '\"', $this->config->blackList ) ) . '")' );
} else {
$blackList = null;
}
// Build our weight list. Make sure current namespace is first element // Build our weight list. Make sure current namespace is first element
$nameSpaces = array_diff( $this->config->nameSpaces, [ $this->nameSpace ] ); $nameSpaces = array_diff( $this->config->nameSpaces, [ $this->nameSpace ] );
@ -117,10 +123,12 @@ class Targets {
$this->queryResult = $dbr->select( $this->queryResult = $dbr->select(
'page', 'page',
array( 'page_title', 'page_namespace' , "weight" => $weightSelect), array( 'page_title', 'page_namespace' , "weight" => $weightSelect),
array_filter(
array( array(
'page_namespace IN ' . $nameSpacesClause, 'page_namespace IN ' . $nameSpacesClause,
'CHAR_LENGTH(page_title) >= ' . $this->config->minimumTitleLength, 'CHAR_LENGTH(page_title) >= ' . $this->config->minimumTitleLength,
'page_title NOT IN ' . $blackList, $blackList,
)
), ),
__METHOD__, __METHOD__,
array( 'ORDER BY' => 'weight ASC, CHAR_LENGTH(page_title) ' . $sortOrder ) array( 'ORDER BY' => 'weight ASC, CHAR_LENGTH(page_title) ' . $sortOrder )
@ -129,10 +137,12 @@ class Targets {
$this->queryResult = $dbr->select( $this->queryResult = $dbr->select(
'page', 'page',
array( 'page_title', 'page_namespace' , "weight" => $weightSelect ), array( 'page_title', 'page_namespace' , "weight" => $weightSelect ),
array_filter(
array( array(
'page_namespace IN ' . $nameSpacesClause, 'page_namespace IN ' . $nameSpacesClause,
'LENGTH(page_title) >= ' . $this->config->minimumTitleLength, 'LENGTH(page_title) >= ' . $this->config->minimumTitleLength,
'page_title NOT IN ' . $blackList, $blackList,
)
), ),
__METHOD__, __METHOD__,
array( 'ORDER BY' => 'weight ASC, LENGTH(page_title) ' . $sortOrder ) array( 'ORDER BY' => 'weight ASC, LENGTH(page_title) ' . $sortOrder )

View File

@ -31,6 +31,7 @@
* (Ideally the test classes should be namespaced, but when you do that, they * (Ideally the test classes should be namespaced, but when you do that, they
* will no longer be automatically discovered.) * will no longer be automatically discovered.)
* *
* @group bovender
* @group Database * @group Database
*/ */
class LinkTitlesLinkerTest extends LinkTitles\TestCase { class LinkTitlesLinkerTest extends LinkTitles\TestCase {
@ -49,12 +50,13 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
/** /**
* @dataProvider provideLinkContentData * @dataProvider provideLinkContentTemplatesData
*/ */
public function testLinkContentTemplates( $skipTemplates, $input, $expectedOutput ) { public function testLinkContentTemplates( $skipTemplates, $input, $expectedOutput ) {
$config = new LinkTitles\Config(); $config = new LinkTitles\Config();
$config->firstOnly = false; $config->firstOnly = false;
$config->skipTemplates = $skipTemplates; $config->skipTemplates = $skipTemplates;
LinkTitles\Splitter::invalidate();
$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 ));
} }
@ -145,8 +147,8 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
[ [
false, // wgCapitalLinks false, // wgCapitalLinks
true, // smartMode true, // smartMode
'With smart mode on and $wgCapitalLinks = false, this page should link to Link Target', 'With smart mode on and $wgCapitalLinks = false, this page should link to Link target',
'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target|Link Target]]' 'With smart mode on and $wgCapitalLinks = false, this page should link to [[Link target]]'
], ],
[ [
false, // wgCapitalLinks false, // wgCapitalLinks
@ -194,7 +196,8 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
public function testLinkContentBlackList() { public function testLinkContentBlackList() {
$config = new LinkTitles\Config(); $config = new LinkTitles\Config();
$config->blackList = [ 'Foo', 'link target', 'Bar' ]; $config->blackList = [ 'Foo', 'Link target', 'Bar' ];
LinkTitles\Targets::invalidate();
$linker = new LinkTitles\Linker( $config ); $linker = new LinkTitles\Linker( $config );
$text = 'If the link target is blacklisted, it should not be linked'; $text = 'If the link target is blacklisted, it should not be linked';
$this->assertSame( $text, $linker->linkContent( $this->title, $text ) ); $this->assertSame( $text, $linker->linkContent( $this->title, $text ) );
@ -220,12 +223,12 @@ class LinkTitlesLinkerTest extends LinkTitles\TestCase {
[ [
[], // nameSpaces [], // nameSpaces
'With nameSpaces = [], page in custom namespace should not be linked', 'With nameSpaces = [], page in custom namespace should not be linked',
'With firstOnly = [], page in custom namespace should not be linked' 'With nameSpaces = [], page in custom namespace should not be linked'
], ],
[ [
[ 3000 ], // nameSpaces [ 3000 ], // nameSpaces
'With nameSpaces = [], page in custom namespace should be linked', 'With nameSpaces = 3000, page in custom namespace should be linked',
'With firstOnly = [], page [[in custom namespace]] should be linked' 'With nameSpaces = 3000, page [[custom_namespace:in custom namespace]] should be linked'
], ],
]; ];
} }