MediaWiki master
WikiTextStructure.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Content;
4
5use MediaWiki\Parser\ParserOutput;
6use MediaWiki\Parser\Sanitizer;
7use Wikimedia\Parsoid\Utils\DOMCompat;
8use Wikimedia\Parsoid\Utils\DOMUtils;
9
14
15 private ?string $openingText = null;
16 private ?string $allText = null;
18 private array $auxText = [];
19
23 private const EXCLUDED_ELEMENT_SELECTORS = [
24 // "it looks like you don't have javascript enabled..." – do not need to index
25 'audio', 'video',
26 // CSS stylesheets aren't content
27 'style',
28 // The [1] for references from Cite
29 'sup.reference',
30 // The ↑ next to references in the references section from Cite
31 '.mw-cite-backlink',
32 // Headings are already indexed in their own field.
33 'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
34 // Collapsed fields are hidden by default, so we don't want them showing up.
35 '.autocollapse',
36 // Content explicitly decided to be not searchable by editors such
37 // as custom navigation templates.
38 '.navigation-not-searchable',
39 // User-facing interface code prompting the user to act from WikibaseMediaInfo
40 '.wbmi-entityview-emptyCaption',
41 ];
42
46 private const AUXILIARY_ELEMENT_SELECTORS = [
47 // Thumbnail captions aren't really part of the text proper
48 '.thumbcaption',
49 'figcaption',
50 // Neither are tables
51 'table',
52 // Common style for "See also:".
53 '.rellink',
54 // Common style for calling out helpful links at the top of the article.
55 '.dablink',
56 // New class users can use to mark stuff as auxiliary to searches.
57 '.searchaux',
58 ];
59
65 public function __construct(
66 private readonly ParserOutput $parserOutput,
67 private readonly array $additionalExcludedSelectors = [],
68 ) {
69 }
70
88 public function headings() {
89 $headings = [];
90 $tocData = $this->parserOutput->getTOCData();
91 if ( $tocData === null ) {
92 return $headings;
93 }
94 $ignoredHeadings = $this->getIgnoredHeadings();
95 foreach ( $tocData->getSections() as $heading ) {
96 $heading = $heading->line;
97
98 // Some wikis wrap the brackets in a span:
99 // https://en.wikipedia.org/wiki/MediaWiki:Cite_reference_link
100 $heading = preg_replace( '/<\/?span>/', '', $heading );
101 // Normalize [] so the following regexp would work.
102 $heading = preg_replace( [ '/&#91;/', '/&#93;/' ], [ '[', ']' ], $heading );
103 $heading = preg_replace( '/<sup>\s*\‍[\s*\d+\s*\‍]\s*<\/sup>/i', '', $heading );
104
105 // Strip tags from the heading or else we'll display them (escaped) in search results
106 $heading = trim( Sanitizer::stripAllTags( $heading ) );
107
108 // Note that we don't take the level of the heading into account - all headings are equal.
109 // Except the ones we ignore.
110 if ( !in_array( $heading, $ignoredHeadings ) ) {
111 $headings[] = $heading;
112 }
113 }
114
115 return $headings;
116 }
117
126 public static function parseSettingsInMessage( $message ) {
127 $lines = explode( "\n", $message );
128 // Remove comments
129 $lines = preg_replace( '/#.*$/', '', $lines );
130 // Remove extra spaces
131 $lines = array_map( trim( ... ), $lines );
132
133 // Remove empty lines
134 return array_filter( $lines );
135 }
136
142 private function getIgnoredHeadings() {
143 static $ignoredHeadings = null;
144 if ( $ignoredHeadings === null ) {
145 $ignoredHeadings = [];
146 $source = wfMessage( 'search-ignored-headings' )->inContentLanguage();
147 if ( !$source->isDisabled() ) {
148 $lines = self::parseSettingsInMessage( $source->plain() );
149 // Now we just have headings!
150 $ignoredHeadings = $lines;
151 }
152 }
153
154 return $ignoredHeadings;
155 }
156
160 private function extractWikitextParts() {
161 if ( $this->allText !== null ) {
162 return;
163 }
164 $text = $this->parserOutput->getContentHolderText();
165 if ( $text === '' ) {
166 $this->allText = "";
167
168 // empty text - nothing to seek here
169 return;
170 }
171
172 $this->openingText = $this->extractTextBeforeFirstHeading( $text );
173
174 $doc = DOMUtils::parseHTML( $text );
175
176 // Strip elements from the page that we never want in the search text.
177 foreach ( array_merge( self::EXCLUDED_ELEMENT_SELECTORS, $this->additionalExcludedSelectors ) as $selector ) {
178 foreach ( DOMCompat::querySelectorAll( $doc, $selector ) as $element ) {
179 $element->parentNode->removeChild( $element );
180 }
181 }
182
183 // Strip elements from the page that are auxiliary text. These will still be
184 // searched, but matches will be ranked lower and non-auxiliary matches will be
185 // preferred in highlighting.
186 foreach ( self::AUXILIARY_ELEMENT_SELECTORS as $selector ) {
187 foreach ( DOMCompat::querySelectorAll( $doc, $selector ) as $element ) {
188 $this->auxText[] = trim( Sanitizer::stripAllTags( DOMCompat::getInnerHTML( $element ) ) );
189 $element->parentNode->removeChild( $element );
190 }
191 }
192
193 $this->allText = trim( Sanitizer::stripAllTags( DOMCompat::getInnerHTML( DOMCompat::getBody( $doc ) ) ) );
194 }
195
203 private function extractTextBeforeFirstHeading( $text ) {
204 $matches = [];
205 if ( !preg_match( '/<h[123456]\b/', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
206 // There isn't a first heading, so we interpret this as the article
207 // being entirely without heading.
208 return null;
209 }
210 $text = substr( $text, 0, $matches[ 0 ][ 1 ] );
211 if ( !$text ) {
212 // There isn't any text before the first heading, so we declare there isn't
213 // a first heading.
214 return null;
215 }
216
217 $doc = DOMUtils::parseHTML( $text );
218 $excludedSelectors = array_merge( self::EXCLUDED_ELEMENT_SELECTORS,
219 self::AUXILIARY_ELEMENT_SELECTORS, $this->additionalExcludedSelectors );
220 foreach ( $excludedSelectors as $selector ) {
221 foreach ( DOMCompat::querySelectorAll( $doc, $selector ) as $element ) {
222 $element->parentNode->removeChild( $element );
223 }
224 }
225
226 $text = trim( Sanitizer::stripAllTags( DOMCompat::getInnerHTML( DOMCompat::getBody( $doc ) ) ) );
227
228 if ( !$text ) {
229 // There isn't any text after filtering before the first heading, so we declare
230 // that there isn't a first heading.
231 return null;
232 }
233
234 return $text;
235 }
236
240 public function getOpeningText() {
241 $this->extractWikitextParts();
242
243 return $this->openingText;
244 }
245
249 public function getMainText() {
250 $this->extractWikitextParts();
251
252 return $this->allText;
253 }
254
258 public function getAuxiliaryText() {
259 $this->extractWikitextParts();
260
261 return $this->auxText;
262 }
263
269 public function getDefaultSort() {
270 $sort = $this->parserOutput->getPageProperty( 'defaultsort' );
271 if ( $sort === false ) {
272 return null;
273 }
274
275 return $sort;
276 }
277}
278
280class_alias( WikiTextStructure::class, 'WikiTextStructure' );
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Class allowing to explore the structure of parsed wikitext.
headings()
Gets headings from the page.
static parseSettingsInMessage( $message)
Parse a message content into an array.
getDefaultSort()
Get the "defaultsort" property.
__construct(private readonly ParserOutput $parserOutput, private readonly array $additionalExcludedSelectors=[],)
ParserOutput is a rendering of a Content object or a message.
$source