MediaWiki  1.28.0
WikiTextStructure.php
Go to the documentation of this file.
1 <?php
2 
4 
12  private $openingText;
16  private $allText;
20  private $auxText = [];
24  private $parserOutput;
25 
30  'audio', 'video', // "it looks like you don't have javascript enabled..."
31  // do not need to index
32  'sup.reference', // The [1] for references
33  '.mw-cite-backlink', // The ↑ next to references in the references section
34  'h1', 'h2', 'h3', // Headings are already indexed in their own field.
35  'h5', 'h6', 'h4',
36  '.autocollapse', // Collapsed fields are hidden by default so we don't want them
37  // showing up.
38  ];
39 
44  '.thumbcaption', // Thumbnail captions aren't really part of the text proper
45  'table', // Neither are tables
46  '.rellink', // Common style for "See also:".
47  '.dablink', // Common style for calling out helpful links at the top
48  // of the article.
49  '.searchaux', // New class users can use to mark stuff as auxiliary to searches.
50  ];
51 
56  public function __construct( ParserOutput $parserOutput ) {
57  $this->parserOutput = $parserOutput;
58  }
59 
73  public function headings() {
74  $headings = [];
75  $ignoredHeadings = $this->getIgnoredHeadings();
76  foreach ( $this->parserOutput->getSections() as $heading ) {
77  $heading = $heading[ 'line' ];
78 
79  // Some wikis wrap the brackets in a span:
80  // https://en.wikipedia.org/wiki/MediaWiki:Cite_reference_link
81  $heading = preg_replace( '/<\/?span>/', '', $heading );
82  // Normalize [] so the following regexp would work.
83  $heading = preg_replace( [ '/&#91;/', '/&#93;/' ], [ '[', ']' ], $heading );
84  $heading = preg_replace( '/<sup>\s*\[\s*\d+\s*\]\s*<\/sup>/is', '', $heading );
85 
86  // Strip tags from the heading or else we'll display them (escaped) in search results
87  $heading = trim( Sanitizer::stripAllTags( $heading ) );
88 
89  // Note that we don't take the level of the heading into account - all headings are equal.
90  // Except the ones we ignore.
91  if ( !in_array( $heading, $ignoredHeadings ) ) {
92  $headings[] = $heading;
93  }
94  }
95  return $headings;
96  }
97 
105  public static function parseSettingsInMessage( $message ) {
106  $lines = explode( "\n", $message );
107  $lines = preg_replace( '/#.*$/', '', $lines ); // Remove comments
108  $lines = array_map( 'trim', $lines ); // Remove extra spaces
109  $lines = array_filter( $lines ); // Remove empty lines
110  return $lines;
111  }
112 
117  private function getIgnoredHeadings() {
118  static $ignoredHeadings = null;
119  if ( $ignoredHeadings === null ) {
120  $ignoredHeadings = [];
121  $source = wfMessage( 'search-ignored-headings' )->inContentLanguage();
122  if ( $source->isBlank() ) {
123  // Try old version too, just in case
124  $source = wfMessage( 'cirrussearch-ignored-headings' )->inContentLanguage();
125  }
126  if ( !$source->isDisabled() ) {
127  $lines = self::parseSettingsInMessage( $source->plain() );
128  $ignoredHeadings = $lines; // Now we just have headings!
129  }
130  }
131  return $ignoredHeadings;
132  }
133 
137  private function extractWikitextParts() {
138  if ( !is_null( $this->allText ) ) {
139  return;
140  }
141  $this->parserOutput->setEditSectionTokens( false );
142  $this->parserOutput->setTOCEnabled( false );
143  $text = $this->parserOutput->getText();
144  if ( strlen( $text ) == 0 ) {
145  $this->allText = "";
146  // empty text - nothing to seek here
147  return;
148  }
149  $opening = null;
150 
151  $this->openingText = $this->extractHeadingBeforeFirstHeading( $text );
152 
153  // Add extra spacing around break tags so text crammed together like<br>this
154  // doesn't make one word.
155  $text = str_replace( '<br', "\n<br", $text );
156 
157  $formatter = new HtmlFormatter( $text );
158 
159  // Strip elements from the page that we never want in the search text.
160  $formatter->remove( $this->excludedElementSelectors );
161  $formatter->filterContent();
162 
163  // Strip elements from the page that are auxiliary text. These will still be
164  // searched but matches will be ranked lower and non-auxiliary matches will be
165  // preferred in highlighting.
166  $formatter->remove( $this->auxiliaryElementSelectors );
167  $auxiliaryElements = $formatter->filterContent();
168  $this->allText = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
169  foreach ( $auxiliaryElements as $auxiliaryElement ) {
170  $this->auxText[] =
171  trim( Sanitizer::stripAllTags( $formatter->getText( $auxiliaryElement ) ) );
172  }
173  }
174 
180  private function extractHeadingBeforeFirstHeading( $text ) {
181  $matches = [];
182  if ( !preg_match( '/<h[123456]>/', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
183  // There isn't a first heading so we interpret this as the article
184  // being entirely without heading.
185  return null;
186  }
187  $text = substr( $text, 0, $matches[ 0 ][ 1 ] );
188  if ( !$text ) {
189  // There isn't any text before the first heading so we declare there isn't
190  // a first heading.
191  return null;
192  }
193 
194  $formatter = new HtmlFormatter( $text );
195  $formatter->remove( $this->excludedElementSelectors );
196  $formatter->remove( $this->auxiliaryElementSelectors );
197  $formatter->filterContent();
198  $text = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
199 
200  if ( !$text ) {
201  // There isn't any text after filtering before the first heading so we declare
202  // that there isn't a first heading.
203  return null;
204  }
205 
206  return $text;
207  }
208 
213  public function getOpeningText() {
214  $this->extractWikitextParts();
215  return $this->openingText;
216  }
217 
222  public function getMainText() {
223  $this->extractWikitextParts();
224  return $this->allText;
225  }
226 
231  public function getAuxiliaryText() {
232  $this->extractWikitextParts();
233  return $this->auxText;
234  }
235 
240  public function getDefaultSort() {
241  return $this->parserOutput->getProperty( 'defaultsort' );
242  }
243 }
ParserOutput $parserOutput
string[] $auxiliaryElementSelectors
selectors to elements that are considered auxiliary to article text for search
getAuxiliaryText()
Get auxiliary text.
getMainText()
Get main text.
getOpeningText()
Get opening text.
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
headings()
Get headings on the page.
Class allowing to explore structure of parsed wikitext.
$source
static stripAllTags($text)
Take a fragment of (potentially invalid) HTML and return a version with any tags removed, encoded as plain text.
Definition: Sanitizer.php:1823
extractHeadingBeforeFirstHeading($text)
Get text before first heading.
static parseSettingsInMessage($message)
Parse a message content into an array.
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
string[] $excludedElementSelectors
selectors to elements that are excluded entirely from search
extractWikitextParts()
Extract parts of the text - opening, main and auxiliary.
getIgnoredHeadings()
Get list of heading to ignore.
__construct(ParserOutput $parserOutput)
WikiTextStructure constructor.
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
$lines
Definition: router.php:67
getDefaultSort()
Get the defaultsort property.
$matches