| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Content; |
| 4 |
|
| 5 |
use HtmlFormatter\HtmlFormatter; |
| 6 |
use MediaWiki\Parser\ParserOutput; |
| 7 |
use MediaWiki\Parser\Sanitizer; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
class WikiTextStructure { |
| 13 |
|
| 14 |
private ?string $openingText = null; |
| 15 |
private ?string $allText = null; |
| 16 |
|
| 17 |
private array $auxText = []; |
| 18 |
private ParserOutput $parserOutput; |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
private const EXCLUDED_ELEMENT_SELECTORS = [ |
| 24 |
ex |
| 25 |
'audio', 'video', |
| 26 |
|
| 27 |
'style', |
| 28 |
|
| 29 |
'sup.reference', |
| 30 |
te |
| 31 |
'.mw-cite-backlink', |
| 32 |
|
| 33 |
'h1', 'h2', 'h3', 'h4', 'h5', 'h6', |
| 34 |
|
| 35 |
'.autocollapse', |
| 36 |
|
| 37 |
|
| 38 |
'.navigation-not-searchable', |
| 39 |
|
| 40 |
'.wbmi-entityview-emptyCaption', |
| 41 |
]; |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private const AUXILIARY_ELEMENT_SELECTORS = [ |
| 47 |
|
| 48 |
'.thumbcaption', |
| 49 |
'figcaption', |
| 50 |
|
| 51 |
'table', |
| 52 |
|
| 53 |
'.rellink', |
| 54 |
|
| 55 |
'.dablink', |
| 56 |
|
| 57 |
'.searchaux', |
| 58 |
]; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( ParserOutput $parserOutput ) { |
| 64 |
$this->parserOutput = $parserOutput; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
== |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function headings() { |
| 85 |
$headings = []; |
| 86 |
$tocData = $this->parserOutput->getTOCData(); |
| 87 |
if ( $tocData === null ) { |
| 88 |
return $headings; |
| 89 |
} |
| 90 |
$ignoredHeadings = $this->getIgnoredHeadings(); |
| 91 |
foreach ( $tocData->getSections() as $heading ) { |
| 92 |
$heading = $heading->line; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
$heading = preg_replace( '/<\/?span>/', '', $heading ); |
| 97 |
|
| 98 |
$heading = preg_replace( [ '/[/', '/]/' ], [ '[', ']' ], $heading ); |
| 99 |
$heading = preg_replace( '/<sup>\s*\[\s*\d+\s*\]\s*<\/sup>/i', '', $heading ); |
| 100 |
|
| 101 |
|
| 102 |
$heading = trim( Sanitizer::stripAllTags( $heading ) ); |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
if ( !in_array( $heading, $ignoredHeadings ) ) { |
| 107 |
$headings[] = $heading; |
| 108 |
} |
| 109 |
} |
| 110 |
|
| 111 |
return $headings; |
| 112 |
} |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static function parseSettingsInMessage( $message ) { |
| 123 |
$lines = explode( "\n", $message ); |
| 124 |
|
| 125 |
$lines = preg_replace( '/#.*$/', '', $lines ); |
| 126 |
|
| 127 |
$lines = array_map( 'trim', $lines ); |
| 128 |
|
| 129 |
|
| 130 |
return array_filter( $lines ); |
| 131 |
} |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
private function getIgnoredHeadings() { |
| 139 |
static $ignoredHeadings = null; |
| 140 |
if ( $ignoredHeadings === null ) { |
| 141 |
$ignoredHeadings = []; |
| 142 |
$source = wfMessage( 'search-ignored-headings' )->inContentLanguage(); |
| 143 |
if ( !$source->isDisabled() ) { |
| 144 |
$lines = self::parseSettingsInMessage( $source->plain() ); |
| 145 |
|
| 146 |
$ignoredHeadings = $lines; |
| 147 |
} |
| 148 |
} |
| 149 |
|
| 150 |
return $ignoredHeadings; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
private function extractWikitextParts() { |
| 157 |
if ( $this->allText !== null ) { |
| 158 |
return; |
| 159 |
} |
| 160 |
$text = $this->parserOutput->getRawText(); |
| 161 |
if ( $text === '' ) { |
| 162 |
$this->allText = ""; |
| 163 |
|
| 164 |
|
| 165 |
return; |
| 166 |
} |
| 167 |
|
| 168 |
$this->openingText = $this->extractTextBeforeFirstHeading( $text ); |
| 169 |
|
| 170 |
$formatter = new HtmlFormatter( $text ); |
| 171 |
|
| 172 |
|
| 173 |
$formatter->remove( self::EXCLUDED_ELEMENT_SELECTORS ); |
| 174 |
$formatter->filterContent(); |
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
$formatter->remove( self::AUXILIARY_ELEMENT_SELECTORS ); |
| 180 |
$auxiliaryElements = $formatter->filterContent(); |
| 181 |
$this->allText = trim( Sanitizer::stripAllTags( $formatter->getText() ) ); |
| 182 |
foreach ( $auxiliaryElements as $auxiliaryElement ) { |
| 183 |
$this->auxText[] = |
| 184 |
trim( Sanitizer::stripAllTags( $formatter->getText( $auxiliaryElement ) ) ); |
| 185 |
} |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
private function extractTextBeforeFirstHeading( $text ) { |
| 196 |
$matches = []; |
| 197 |
if ( !preg_match( '/<h[123456]\b/', $text, $matches, PREG_OFFSET_CAPTURE ) ) { |
| 198 |
|
| 199 |
|
| 200 |
return null; |
| 201 |
} |
| 202 |
$text = substr( $text, 0, $matches[ 0 ][ 1 ] ); |
| 203 |
if ( !$text ) { |
| 204 |
|
| 205 |
|
| 206 |
return null; |
| 207 |
} |
| 208 |
|
| 209 |
$formatter = new HtmlFormatter( $text ); |
| 210 |
$formatter->remove( self::EXCLUDED_ELEMENT_SELECTORS ); |
| 211 |
$formatter->remove( self::AUXILIARY_ELEMENT_SELECTORS ); |
| 212 |
$formatter->filterContent(); |
| 213 |
$text = trim( Sanitizer::stripAllTags( $formatter->getText() ) ); |
| 214 |
|
| 215 |
if ( !$text ) { |
| 216 |
|
| 217 |
|
| 218 |
return null; |
| 219 |
} |
| 220 |
|
| 221 |
return $text; |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
public function getOpeningText() { |
| 228 |
$this->extractWikitextParts(); |
| 229 |
|
| 230 |
return $this->openingText; |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
public function getMainText() { |
| 237 |
$this->extractWikitextParts(); |
| 238 |
|
| 239 |
return $this->allText; |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
public function getAuxiliaryText() { |
| 246 |
$this->extractWikitextParts(); |
| 247 |
|
| 248 |
return $this->auxText; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
public function getDefaultSort() { |
| 257 |
$sort = $this->parserOutput->getPageProperty( 'defaultsort' ); |
| 258 |
if ( $sort === false ) { |
| 259 |
return null; |
| 260 |
} |
| 261 |
|
| 262 |
return $sort; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
class_alias( WikiTextStructure::class, 'WikiTextStructure' ); |