Rename Delimiters to Splitter; add tests.

This commit is contained in:
Daniel Kraus
2017-08-27 22:32:32 +02:00
parent 3f2141e6c7
commit 1e27f47750
5 changed files with 43 additions and 33 deletions

View File

@ -37,7 +37,7 @@
"LinkTitles\\Linker": "includes/Linker.php", "LinkTitles\\Linker": "includes/Linker.php",
"LinkTitles\\Target": "includes/Target.php", "LinkTitles\\Target": "includes/Target.php",
"LinkTitles\\Targets": "includes/Targets.php", "LinkTitles\\Targets": "includes/Targets.php",
"LinkTitles\\Delimiters": "includes/Delimiters.php", "LinkTitles\\Splitter": "includes/Splitter.php",
"LinkTitles\\Config": "includes/Config.php", "LinkTitles\\Config": "includes/Config.php",
"LinkTitles\\Special": "includes/Special.php", "LinkTitles\\Special": "includes/Special.php",
"LinkTitles\\TestCase": "tests/phpunit/TestCase.php" "LinkTitles\\TestCase": "tests/phpunit/TestCase.php"

View File

@ -72,7 +72,7 @@ class Linker {
$limitReached = false; $limitReached = false;
$newText = $text; $newText = $text;
$delimiters = Delimiters::default( $this->config ); $splitter = Splitter::default( $this->config );
$targets = Targets::default( $title, $this->config ); $targets = Targets::default( $title, $this->config );
// Iterate through the target page titles // Iterate through the target page titles
@ -89,7 +89,7 @@ class Linker {
// 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
$arr = $delimiters->split( $newText ); $arr = $splitter->split( $newText );
$count = 0; $count = 0;
// Cache the target title text for the regex callbacks // Cache the target title text for the regex callbacks
@ -113,7 +113,7 @@ class Linker {
if ( $this->config->smartMode && !$limitReached ) { if ( $this->config->smartMode && !$limitReached ) {
if ( $count > 0 ) { if ( $count > 0 ) {
// Split the text again because it was changed in the first pass. // Split the text again because it was changed in the first pass.
$arr = $delimiters->split( $newText ); $arr = $splitter->split( $newText );
} }
for ( $i = 0; $i < count( $arr ); $i+=2 ) { for ( $i = 0; $i < count( $arr ); $i+=2 ) {

View File

@ -1,6 +1,6 @@
<?php <?php
/** /**
* The Delimiters class caches a regular expression that delimits text to be parsed. * The Splitter class caches a regular expression that delimits text to be parsed.
* *
* Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender') * Copyright 2012-2017 Daniel Kraus <bovender@bovender.de> ('bovender')
* *
@ -26,7 +26,7 @@ namespace LinkTitles;
/** /**
* Caches a regular expression that delimits text to be parsed. * Caches a regular expression that delimits text to be parsed.
*/ */
class Delimiters { class Splitter {
/** /**
* The splitting expression that separates text to be parsed from text that * The splitting expression that separates text to be parsed from text that
* must not be parsed. * must not be parsed.
@ -35,7 +35,7 @@ class Delimiters {
public $splitter; public $splitter;
/** /**
* The LinkTitles configuration for this Delimiters instance. * The LinkTitles configuration for this Splitter instance.
* @var Config $config * @var Config $config
*/ */
public $config; public $config;
@ -43,12 +43,12 @@ class Delimiters {
private static $instance; private static $instance;
/** /**
* Gets the Delimiters singleton; may build one with the given config or the * Gets the Splitter singleton; may build one with the given config or the
* default config if none is given. * default config if none is given.
* *
* If the instance was already created, it does not matter what Config this * If the instance was already created, it does not matter what Config this
* method is called with. To re-create an instance with a different Config, * method is called with. To re-create an instance with a different Config,
* call Delimiters::invalidate() first. * call Splitter::invalidate() first.
* *
* @param Config|null $config LinkTitles configuration. * @param Config|null $config LinkTitles configuration.
*/ */
@ -57,7 +57,7 @@ class Delimiters {
if ( $config === null ) { if ( $config === null ) {
$config = new Config(); $config = new Config();
} }
self::$instance = new Delimiters( $config ); self::$instance = new Splitter( $config );
} }
return self::$instance; return self::$instance;
} }
@ -73,7 +73,7 @@ class Delimiters {
protected function __construct( Config $config) { protected function __construct( Config $config) {
$this->config = $config; $this->config = $config;
$this->buildDelimiters(); $this->buildSplitter();
} }
/** /**
@ -92,7 +92,7 @@ class Delimiters {
* text that should be parsed from text that should not be * text that should be parsed from text that should not be
* parsed (e.g. inside existing links etc.) * parsed (e.g. inside existing links etc.)
*/ */
private function buildDelimiters() { private function buildSplitter() {
if ( $this->config->skipTemplates ) if ( $this->config->skipTemplates )
{ {
// Use recursive regex to balance curly braces; // Use recursive regex to balance curly braces;

View File

@ -1,21 +0,0 @@
<?php
/**
* @group bovender
*/
class DelimitersTest extends MediaWikiTestCase {
/**
* @dataProvider provideSplitData
*/
public function testSplit( $input, $output ) {
}
public static function provideSplitData() {
return [
[
'this may be linked [[this may not be linked]]',
[ 'this may be linked', '[[this may not be linked]]']
]
];
}
}

View File

@ -0,0 +1,31 @@
<?php
/**
* @group bovender
*/
class SplitterTest extends MediaWikiTestCase {
/**
* @dataProvider provideSplitData
*/
public function testSplit( $input, $expectedOutput ) {
$splitter = LinkTitles\Splitter::default();
$this->assertSame( $expectedOutput, $splitter->split( $input ) );
}
// TODO: Add more examples.
public static function provideSplitData() {
return [
[
'this may be linked [[this may not be linked]]',
[ 'this may be linked ', '[[this may not be linked]]', '' ]
],
[
'this may be linked <gallery>this may not be linked</gallery>',
[ 'this may be linked ', '<gallery>this may not be linked</gallery>', '' ]
],
[
'this may be linked {{mytemplate|param={{transcluded}}}}',
[ 'this may be linked ', '{{mytemplate|param={{transcluded}}}}', '' ]
],
];
}
}