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
This commit is contained in:
C. Scott Ananian 2020-05-02 01:25:10 -04:00
parent b2e383658d
commit c1397847c0
3 changed files with 20 additions and 29 deletions

View file

@ -10,7 +10,6 @@ use Config;
use FauxRequest; use FauxRequest;
use MediaWiki\Logger\LoggerFactory; use MediaWiki\Logger\LoggerFactory;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MWTidy;
use ParserOptions; use ParserOptions;
use Title; use Title;
use User; use User;
@ -304,10 +303,10 @@ class ApiQueryExtracts extends ApiQueryBase {
* @return string * @return string
*/ */
private function truncate( $text ) { private function truncate( $text ) {
if ( !$this->params['plaintext'] && MWTidy::isEnabled() ) { if ( !$this->params['plaintext'] ) {
$truncator = new TextTruncator( MWTidy::singleton() ); $truncator = new TextTruncator( true );
} else { } else {
$truncator = new TextTruncator(); $truncator = new TextTruncator( false );
} }
if ( $this->params['chars'] ) { if ( $this->params['chars'] ) {

View file

@ -2,27 +2,26 @@
namespace TextExtracts; 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 * 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. * this.
* *
* @license GPL-2.0-or-later * @license GPL-2.0-or-later
*/ */
class TextTruncator { class TextTruncator {
/**
* @var bool Whether to tidy the output
*/
private $useTidy;
/** /**
* @var TidyDriverBase|null * @param bool $useTidy
*/ */
private $tidyDriver; public function __construct( bool $useTidy ) {
$this->useTidy = $useTidy;
/**
* @param TidyDriverBase|null $tidy
*/
public function __construct( TidyDriverBase $tidy = null ) {
$this->tidyDriver = $tidy;
} }
/** /**
@ -99,9 +98,8 @@ class TextTruncator {
* @return string * @return string
*/ */
private function tidy( $text ) { private function tidy( $text ) {
if ( $this->tidyDriver ) { if ( $this->useTidy ) {
// Fix possibly unclosed HTML tags. $text = MWTidy::tidy( $text );
$text = $this->tidyDriver->tidy( $text );
} }
return trim( $text ); return trim( $text );

View file

@ -2,7 +2,6 @@
namespace TextExtracts\Test; namespace TextExtracts\Test;
use MediaWiki\Tidy\TidyDriverBase;
use TextExtracts\TextTruncator; use TextExtracts\TextTruncator;
/** /**
@ -19,7 +18,7 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase {
* @param string $expected * @param string $expected
*/ */
public function testGetFirstSentences( $text, $sentences, $expected ) { public function testGetFirstSentences( $text, $sentences, $expected ) {
$truncator = new TextTruncator(); $truncator = new TextTruncator( false );
$this->assertSame( $expected, $truncator->getFirstSentences( $text, $sentences ) ); $this->assertSame( $expected, $truncator->getFirstSentences( $text, $sentences ) );
} }
@ -136,7 +135,7 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase {
* @param string $expected * @param string $expected
*/ */
public function testGetFirstChars( $text, $chars, $expected ) { public function testGetFirstChars( $text, $chars, $expected ) {
$truncator = new TextTruncator(); $truncator = new TextTruncator( false );
$this->assertSame( $expected, $truncator->getFirstChars( $text, $chars ) ); $this->assertSame( $expected, $truncator->getFirstChars( $text, $chars ) );
} }
@ -169,16 +168,11 @@ class TextTruncatorTest extends \PHPUnit\Framework\TestCase {
} }
public function testTidyIntegration() { public function testTidyIntegration() {
$tidy = $this->createMock( TidyDriverBase::class ); $truncator = new TextTruncator( true );
$tidy->method( 'tidy' )
->willReturnCallback( function ( $text ) {
return "<tidy>$text</tidy>";
} );
$truncator = new TextTruncator( $tidy );
$text = 'Aa. Bb.'; $text = '<b>Aa. Bb.</b>';
$this->assertSame( '<tidy>Aa.</tidy>', $truncator->getFirstSentences( $text, 1 ) ); $this->assertSame( '<p><b>Aa.</b></p>', $truncator->getFirstSentences( $text, 1 ) );
$this->assertSame( '<tidy>Aa</tidy>', $truncator->getFirstChars( $text, 1 ) ); $this->assertSame( '<p><b>Aa</b></p>', $truncator->getFirstChars( $text, 4 ) );
} }
} }