diff --git a/includes/ApiQueryExtracts.php b/includes/ApiQueryExtracts.php index b5a5314..e251378 100644 --- a/includes/ApiQueryExtracts.php +++ b/includes/ApiQueryExtracts.php @@ -10,7 +10,6 @@ use Config; use FauxRequest; use MediaWiki\Logger\LoggerFactory; use MediaWiki\MediaWikiServices; -use MWTidy; use ParserOptions; use Title; use User; @@ -304,10 +303,10 @@ class ApiQueryExtracts extends ApiQueryBase { * @return string */ private function truncate( $text ) { - if ( !$this->params['plaintext'] && MWTidy::isEnabled() ) { - $truncator = new TextTruncator( MWTidy::singleton() ); + if ( !$this->params['plaintext'] ) { + $truncator = new TextTruncator( true ); } else { - $truncator = new TextTruncator(); + $truncator = new TextTruncator( false ); } if ( $this->params['chars'] ) { diff --git a/includes/TextTruncator.php b/includes/TextTruncator.php index 770ee25..4540cd2 100644 --- a/includes/TextTruncator.php +++ b/includes/TextTruncator.php @@ -2,27 +2,26 @@ namespace TextExtracts; -use MediaWiki\Tidy\TidyDriverBase; +use MWTidy; /** * This class needs to understand HTML as well as plain text. It tries to not break HTML tags, but - * might break pairs of tags, leaving unclosed tags behind. A Tidy instance must be provided to fix + * might break pairs of tags, leaving unclosed tags behind. We can tidy the output to fix * this. * * @license GPL-2.0-or-later */ class TextTruncator { + /** + * @var bool Whether to tidy the output + */ + private $useTidy; /** - * @var TidyDriverBase|null + * @param bool $useTidy */ - private $tidyDriver; - - /** - * @param TidyDriverBase|null $tidy - */ - public function __construct( TidyDriverBase $tidy = null ) { - $this->tidyDriver = $tidy; + public function __construct( bool $useTidy ) { + $this->useTidy = $useTidy; } /** @@ -99,9 +98,8 @@ class TextTruncator { * @return string */ private function tidy( $text ) { - if ( $this->tidyDriver ) { - // Fix possibly unclosed HTML tags. - $text = $this->tidyDriver->tidy( $text ); + if ( $this->useTidy ) { + $text = MWTidy::tidy( $text ); } return trim( $text ); diff --git a/tests/phpunit/TextTruncatorTest.php b/tests/phpunit/TextTruncatorTest.php index eeff881..1b13f23 100644 --- a/tests/phpunit/TextTruncatorTest.php +++ b/tests/phpunit/TextTruncatorTest.php @@ -2,7 +2,6 @@ namespace TextExtracts\Test; -use MediaWiki\Tidy\TidyDriverBase; use TextExtracts\TextTruncator; /** @@ -19,7 +18,7 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase { * @param string $expected */ public function testGetFirstSentences( $text, $sentences, $expected ) { - $truncator = new TextTruncator(); + $truncator = new TextTruncator( false ); $this->assertSame( $expected, $truncator->getFirstSentences( $text, $sentences ) ); } @@ -136,7 +135,7 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase { * @param string $expected */ public function testGetFirstChars( $text, $chars, $expected ) { - $truncator = new TextTruncator(); + $truncator = new TextTruncator( false ); $this->assertSame( $expected, $truncator->getFirstChars( $text, $chars ) ); } @@ -169,16 +168,11 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase { } public function testTidyIntegration() { - $tidy = $this->createMock( TidyDriverBase::class ); - $tidy->method( 'tidy' ) - ->willReturnCallback( function ( $text ) { - return "$text"; - } ); - $truncator = new TextTruncator( $tidy ); + $truncator = new TextTruncator( true ); - $text = 'Aa. Bb.'; - $this->assertSame( 'Aa.', $truncator->getFirstSentences( $text, 1 ) ); - $this->assertSame( 'Aa', $truncator->getFirstChars( $text, 1 ) ); + $text = 'Aa. Bb.'; + $this->assertSame( '

Aa.

', $truncator->getFirstSentences( $text, 1 ) ); + $this->assertSame( '

Aa

', $truncator->getFirstChars( $text, 4 ) ); } }