| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\TextExtracts; |
| 4 |
|
| 5 |
use DOMElement; |
| 6 |
use HtmlFormatter\HtmlFormatter; |
| 7 |
use Wikimedia\Parsoid\Utils\DOMCompat; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
class ExtractFormatter extends HtmlFormatter { |
| 15 |
public const SECTION_MARKER_START = "\1\2"; |
| 16 |
public const SECTION_MARKER_END = "\2\1"; |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
private $plainText; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( $text, $plainText ) { |
| 28 |
parent::__construct( HtmlFormatter::wrapHTML( $text ) ); |
| 29 |
$this->plainText = $plainText; |
| 30 |
|
| 31 |
$this->setRemoveMedia( true ); |
| 32 |
|
| 33 |
if ( $plainText ) { |
| 34 |
$this->flattenAllTags(); |
| 35 |
} else { |
| 36 |
$this->flatten( [ 'a' ] ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
public function getText( $element = null ): string { |
| 49 |
$this->filterContent(); |
| 50 |
$text = parent::getText(); |
| 51 |
if ( $this->plainText ) { |
| 52 |
$text = html_entity_decode( $text ); |
| 53 |
|
| 54 |
$text = str_replace( "\u{00A0}", ' ', $text ); |
| 55 |
|
| 56 |
$text = str_replace( "\r", "\n", $text ); |
| 57 |
|
| 58 |
$text = preg_replace( "/\n{3,}/", "\n\n", $text ); |
| 59 |
} |
| 60 |
return trim( $text ); |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public function onHtmlReady( string $html ): string { |
| 68 |
if ( $this->plainText ) { |
| 69 |
$html = preg_replace( '/\s*(<h([1-6])\b)/i', |
| 70 |
"\n\n" . self::SECTION_MARKER_START . '$2' . self::SECTION_MARKER_END . '$1', |
| 71 |
$html |
| 72 |
); |
| 73 |
} |
| 74 |
return $html; |
| 75 |
} |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
public function filterContent(): array { |
| 84 |
$doc = $this->getDoc(); |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
$headings = DOMCompat::querySelectorAll( $doc->documentElement, 'h1, h2, h3, h4, h5, h6' ); |
| 90 |
foreach ( $headings as $heading ) { |
| 91 |
|
| 92 |
if ( DOMCompat::getClassList( $heading->parentNode )->contains( 'mw-heading' ) ) { |
| 93 |
$heading->parentNode->parentNode->insertBefore( $heading, $heading->parentNode ); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
$removed = parent::filterContent(); |
| 98 |
|
| 99 |
$spans = $doc->getElementsByTagName( 'span' ); |
| 100 |
|
| 101 |
|
| 102 |
foreach ( $spans as $span ) { |
| 103 |
$span->removeAttribute( 'class' ); |
| 104 |
$span->removeAttribute( 'style' ); |
| 105 |
} |
| 106 |
|
| 107 |
return $removed; |
| 108 |
} |
| 109 |
} |