mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/TextExtracts
synced 2024-11-15 03:35:20 +00:00
Merge "Tidy is no longer configurable in MW 1.35"
This commit is contained in:
commit
47f07178ee
|
@ -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'] ) {
|
||||
|
|
|
@ -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 );
|
||||
|
|
|
@ -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 "<tidy>$text</tidy>";
|
||||
} );
|
||||
$truncator = new TextTruncator( $tidy );
|
||||
$truncator = new TextTruncator( true );
|
||||
|
||||
$text = 'Aa. Bb.';
|
||||
$this->assertSame( '<tidy>Aa.</tidy>', $truncator->getFirstSentences( $text, 1 ) );
|
||||
$this->assertSame( '<tidy>Aa</tidy>', $truncator->getFirstChars( $text, 1 ) );
|
||||
$text = '<b>Aa. Bb.</b>';
|
||||
$this->assertSame( '<p><b>Aa.</b></p>', $truncator->getFirstSentences( $text, 1 ) );
|
||||
$this->assertSame( '<p><b>Aa</b></p>', $truncator->getFirstChars( $text, 4 ) );
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue