MediaWiki master
WikiTextStructure.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\Content;
4
5use HtmlFormatter\HtmlFormatter;
8
13
14 private ?string $openingText = null;
15 private ?string $allText = null;
17 private array $auxText = [];
18 private ParserOutput $parserOutput;
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
63 public function __construct( ParserOutput $parserOutput ) {
64 $this->parserOutput = $parserOutput;
65 }
66
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 // Some wikis wrap the brackets in a span:
95 // https://en.wikipedia.org/wiki/MediaWiki:Cite_reference_link
96 $heading = preg_replace( '/<\/?span>/', '', $heading );
97 // Normalize [] so the following regexp would work.
98 $heading = preg_replace( [ '/&#91;/', '/&#93;/' ], [ '[', ']' ], $heading );
99 $heading = preg_replace( '/<sup>\s*\[\s*\d+\s*\]\s*<\/sup>/i', '', $heading );
100
101 // Strip tags from the heading or else we'll display them (escaped) in search results
102 $heading = trim( Sanitizer::stripAllTags( $heading ) );
103
104 // Note that we don't take the level of the heading into account - all headings are equal.
105 // Except the ones we ignore.
106 if ( !in_array( $heading, $ignoredHeadings ) ) {
107 $headings[] = $heading;
108 }
109 }
110
111 return $headings;
112 }
113
122 public static function parseSettingsInMessage( $message ) {
123 $lines = explode( "\n", $message );
124 // Remove comments
125 $lines = preg_replace( '/#.*$/', '', $lines );
126 // Remove extra spaces
127 $lines = array_map( 'trim', $lines );
128
129 // Remove empty lines
130 return array_filter( $lines );
131 }
132
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() ) {
145 // Now we just have headings!
146 $ignoredHeadings = $lines;
147 }
148 }
149
150 return $ignoredHeadings;
151 }
152
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 // empty text - nothing to seek here
165 return;
166 }
167
168 $this->openingText = $this->extractTextBeforeFirstHeading( $text );
169
170 $formatter = new HtmlFormatter( $text );
171
172 // Strip elements from the page that we never want in the search text.
173 $formatter->remove( self::EXCLUDED_ELEMENT_SELECTORS );
174 $formatter->filterContent();
175
176 // Strip elements from the page that are auxiliary text. These will still be
177 // searched, but matches will be ranked lower and non-auxiliary matches will be
178 // preferred in highlighting.
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
195 private function extractTextBeforeFirstHeading( $text ) {
196 $matches = [];
197 if ( !preg_match( '/<h[123456]\b/', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
198 // There isn't a first heading, so we interpret this as the article
199 // being entirely without heading.
200 return null;
201 }
202 $text = substr( $text, 0, $matches[ 0 ][ 1 ] );
203 if ( !$text ) {
204 // There isn't any text before the first heading, so we declare there isn't
205 // a first heading.
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 // There isn't any text after filtering before the first heading, so we declare
217 // that there isn't a first heading.
218 return null;
219 }
220
221 return $text;
222 }
223
227 public function getOpeningText() {
228 $this->extractWikitextParts();
229
230 return $this->openingText;
231 }
232
236 public function getMainText() {
237 $this->extractWikitextParts();
238
239 return $this->allText;
240 }
241
245 public function getAuxiliaryText() {
246 $this->extractWikitextParts();
247
248 return $this->auxText;
249 }
250
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
267class_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.
__construct(ParserOutput $parserOutput)
headings()
Gets headings from the page.
static parseSettingsInMessage( $message)
Parse a message content into an array.
getDefaultSort()
Get the "defaultsort" property.
ParserOutput is a rendering of a Content object or a message.
HTML sanitizer for MediaWiki.
Definition Sanitizer.php:46
$source
if(!file_exists( $CREDITS)) $lines