MediaWiki  1.29.1
WikiTextStructure.php
Go to the documentation of this file.
1 <?php
2 
3 use HtmlFormatter\HtmlFormatter;
4 
12  private $openingText;
16  private $allText;
20  private $auxText = [];
24  private $parserOutput;
25 
30  // "it looks like you don't have javascript enabled..." – do not need to index
31  'audio', 'video',
32  // The [1] for references
33  'sup.reference',
34  // The ↑ next to references in the references section
35  '.mw-cite-backlink',
36  // Headings are already indexed in their own field.
37  'h1', 'h2', 'h3', 'h4', 'h5', 'h6',
38  // Collapsed fields are hidden by default so we don't want them showing up.
39  '.autocollapse',
40  // Content explicitly decided to be not searchable by editors such
41  // as custom navigation templates.
42  '.navigation-not-searchable'
43  ];
44 
49  // Thumbnail captions aren't really part of the text proper
50  '.thumbcaption',
51  // Neither are tables
52  'table',
53  // Common style for "See also:".
54  '.rellink',
55  // Common style for calling out helpful links at the top of the article.
56  '.dablink',
57  // New class users can use to mark stuff as auxiliary to searches.
58  '.searchaux',
59  ];
60 
65  public function __construct( ParserOutput $parserOutput ) {
66  $this->parserOutput = $parserOutput;
67  }
68 
82  public function headings() {
83  $headings = [];
84  $ignoredHeadings = $this->getIgnoredHeadings();
85  foreach ( $this->parserOutput->getSections() as $heading ) {
86  $heading = $heading[ 'line' ];
87 
88  // Some wikis wrap the brackets in a span:
89  // https://en.wikipedia.org/wiki/MediaWiki:Cite_reference_link
90  $heading = preg_replace( '/<\/?span>/', '', $heading );
91  // Normalize [] so the following regexp would work.
92  $heading = preg_replace( [ '/&#91;/', '/&#93;/' ], [ '[', ']' ], $heading );
93  $heading = preg_replace( '/<sup>\s*\[\s*\d+\s*\]\s*<\/sup>/is', '', $heading );
94 
95  // Strip tags from the heading or else we'll display them (escaped) in search results
96  $heading = trim( Sanitizer::stripAllTags( $heading ) );
97 
98  // Note that we don't take the level of the heading into account - all headings are equal.
99  // Except the ones we ignore.
100  if ( !in_array( $heading, $ignoredHeadings ) ) {
101  $headings[] = $heading;
102  }
103  }
104  return $headings;
105  }
106 
114  public static function parseSettingsInMessage( $message ) {
115  $lines = explode( "\n", $message );
116  $lines = preg_replace( '/#.*$/', '', $lines ); // Remove comments
117  $lines = array_map( 'trim', $lines ); // Remove extra spaces
118  $lines = array_filter( $lines ); // Remove empty lines
119  return $lines;
120  }
121 
126  private function getIgnoredHeadings() {
127  static $ignoredHeadings = null;
128  if ( $ignoredHeadings === null ) {
129  $ignoredHeadings = [];
130  $source = wfMessage( 'search-ignored-headings' )->inContentLanguage();
131  if ( $source->isBlank() ) {
132  // Try old version too, just in case
133  $source = wfMessage( 'cirrussearch-ignored-headings' )->inContentLanguage();
134  }
135  if ( !$source->isDisabled() ) {
137  $ignoredHeadings = $lines; // Now we just have headings!
138  }
139  }
140  return $ignoredHeadings;
141  }
142 
146  private function extractWikitextParts() {
147  if ( !is_null( $this->allText ) ) {
148  return;
149  }
150  $this->parserOutput->setEditSectionTokens( false );
151  $this->parserOutput->setTOCEnabled( false );
152  $text = $this->parserOutput->getText();
153  if ( strlen( $text ) == 0 ) {
154  $this->allText = "";
155  // empty text - nothing to seek here
156  return;
157  }
158  $opening = null;
159 
160  $this->openingText = $this->extractHeadingBeforeFirstHeading( $text );
161 
162  // Add extra spacing around break tags so text crammed together like<br>this
163  // doesn't make one word.
164  $text = str_replace( '<br', "\n<br", $text );
165 
166  $formatter = new HtmlFormatter( $text );
167 
168  // Strip elements from the page that we never want in the search text.
169  $formatter->remove( $this->excludedElementSelectors );
170  $formatter->filterContent();
171 
172  // Strip elements from the page that are auxiliary text. These will still be
173  // searched but matches will be ranked lower and non-auxiliary matches will be
174  // preferred in highlighting.
175  $formatter->remove( $this->auxiliaryElementSelectors );
176  $auxiliaryElements = $formatter->filterContent();
177  $this->allText = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
178  foreach ( $auxiliaryElements as $auxiliaryElement ) {
179  $this->auxText[] =
180  trim( Sanitizer::stripAllTags( $formatter->getText( $auxiliaryElement ) ) );
181  }
182  }
183 
189  private function extractHeadingBeforeFirstHeading( $text ) {
190  $matches = [];
191  if ( !preg_match( '/<h[123456]>/', $text, $matches, PREG_OFFSET_CAPTURE ) ) {
192  // There isn't a first heading so we interpret this as the article
193  // being entirely without heading.
194  return null;
195  }
196  $text = substr( $text, 0, $matches[ 0 ][ 1 ] );
197  if ( !$text ) {
198  // There isn't any text before the first heading so we declare there isn't
199  // a first heading.
200  return null;
201  }
202 
203  $formatter = new HtmlFormatter( $text );
204  $formatter->remove( $this->excludedElementSelectors );
205  $formatter->remove( $this->auxiliaryElementSelectors );
206  $formatter->filterContent();
207  $text = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
208 
209  if ( !$text ) {
210  // There isn't any text after filtering before the first heading so we declare
211  // that there isn't a first heading.
212  return null;
213  }
214 
215  return $text;
216  }
217 
222  public function getOpeningText() {
223  $this->extractWikitextParts();
224  return $this->openingText;
225  }
226 
231  public function getMainText() {
232  $this->extractWikitextParts();
233  return $this->allText;
234  }
235 
240  public function getAuxiliaryText() {
241  $this->extractWikitextParts();
242  return $this->auxText;
243  }
244 
249  public function getDefaultSort() {
250  return $this->parserOutput->getProperty( 'defaultsort' );
251  }
252 }
WikiTextStructure\parseSettingsInMessage
static parseSettingsInMessage( $message)
Parse a message content into an array.
Definition: WikiTextStructure.php:114
ParserOutput
Definition: ParserOutput.php:24
WikiTextStructure\getAuxiliaryText
getAuxiliaryText()
Get auxiliary text.
Definition: WikiTextStructure.php:240
WikiTextStructure\getMainText
getMainText()
Get main text.
Definition: WikiTextStructure.php:231
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
WikiTextStructure\headings
headings()
Get headings on the page.
Definition: WikiTextStructure.php:82
php
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
$matches
$matches
Definition: NoLocalSettings.php:24
WikiTextStructure\$allText
string $allText
Definition: WikiTextStructure.php:16
WikiTextStructure\extractHeadingBeforeFirstHeading
extractHeadingBeforeFirstHeading( $text)
Get text before first heading.
Definition: WikiTextStructure.php:189
WikiTextStructure\extractWikitextParts
extractWikitextParts()
Extract parts of the text - opening, main and auxiliary.
Definition: WikiTextStructure.php:146
$lines
$lines
Definition: router.php:67
WikiTextStructure\$auxText
string[] $auxText
Definition: WikiTextStructure.php:20
WikiTextStructure\$openingText
string $openingText
Definition: WikiTextStructure.php:12
WikiTextStructure\getDefaultSort
getDefaultSort()
Get the defaultsort property.
Definition: WikiTextStructure.php:249
WikiTextStructure\$excludedElementSelectors
string[] $excludedElementSelectors
selectors to elements that are excluded entirely from search
Definition: WikiTextStructure.php:29
WikiTextStructure\getIgnoredHeadings
getIgnoredHeadings()
Get list of heading to ignore.
Definition: WikiTextStructure.php:126
WikiTextStructure\__construct
__construct(ParserOutput $parserOutput)
WikiTextStructure constructor.
Definition: WikiTextStructure.php:65
as
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
$source
$source
Definition: mwdoc-filter.php:45
WikiTextStructure
Class allowing to explore structure of parsed wikitext.
Definition: WikiTextStructure.php:8
wfMessage
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 unset offset - wrap String Wrap the message in html(usually something like "&lt
WikiTextStructure\$auxiliaryElementSelectors
string[] $auxiliaryElementSelectors
selectors to elements that are considered auxiliary to article text for search
Definition: WikiTextStructure.php:48
WikiTextStructure\getOpeningText
getOpeningText()
Get opening text.
Definition: WikiTextStructure.php:222
WikiTextStructure\$parserOutput
ParserOutput $parserOutput
Definition: WikiTextStructure.php:24