From c1397847c0e902b11d25c0c4683c7fc37fa9d37b Mon Sep 17 00:00:00 2001 From: "C. Scott Ananian" Date: Sat, 2 May 2020 01:25:10 -0400 Subject: [PATCH] Tidy is no longer configurable in MW 1.35 Remove use of deprecated MWTidy::isEnabled() and internal MWTidy::singleton() methods. See I3584181070da7ed4888beaaf04e083114aca1eab for context. Bug: T198214 Change-Id: I511068cc7b2398773a837f66e08def206cbb5626 --- includes/ApiQueryExtracts.php | 7 +++---- includes/TextTruncator.php | 24 +++++++++++------------- tests/phpunit/TextTruncatorTest.php | 18 ++++++------------ 3 files changed, 20 insertions(+), 29 deletions(-) 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 ) ); } }