parserOutput = $parserOutput;
}
/**
* Gets headings from the page.
*
* @return string[]
* First strip out things that look like references. We can't use HTML filtering because
* the references come back as tags without a class. To keep from breaking stuff like
* ==Applicability of the strict mass–energy equivalence formula, ''E'' = ''mc''2==
* we don't remove the whole tag.
*
* We also don't want to strip the tag and remove everything that looks like [2] because,
* I don't know, maybe there is a band named Word [2] Foo r something. Whatever.
*
* So we only strip things that look like tags wrapping a reference. And since the data
* looks like:
* Reference in heading [1][2]
* we can not really use HtmlFormatter as we have no suitable selector.
*/
public function headings() {
$headings = [];
$tocData = $this->parserOutput->getTOCData();
if ( $tocData === null ) {
return $headings;
}
$ignoredHeadings = $this->getIgnoredHeadings();
foreach ( $tocData->getSections() as $heading ) {
$heading = $heading->line;
// Some wikis wrap the brackets in a span:
// https://en.wikipedia.org/wiki/MediaWiki:Cite_reference_link
$heading = preg_replace( '/<\/?span>/', '', $heading );
// Normalize [] so the following regexp would work.
$heading = preg_replace( [ '/[/', '/]/' ], [ '[', ']' ], $heading );
$heading = preg_replace( '/\s*\[\s*\d+\s*\]\s*<\/sup>/i', '', $heading );
// Strip tags from the heading or else we'll display them (escaped) in search results
$heading = trim( Sanitizer::stripAllTags( $heading ) );
// Note that we don't take the level of the heading into account - all headings are equal.
// Except the ones we ignore.
if ( !in_array( $heading, $ignoredHeadings ) ) {
$headings[] = $heading;
}
}
return $headings;
}
/**
* Parse a message content into an array. This function is generally used to
* parse settings stored as i18n messages (see search-ignored-headings).
*
* @param string $message
*
* @return string[]
*/
public static function parseSettingsInMessage( $message ) {
$lines = explode( "\n", $message );
// Remove comments
$lines = preg_replace( '/#.*$/', '', $lines );
// Remove extra spaces
$lines = array_map( 'trim', $lines );
// Remove empty lines
return array_filter( $lines );
}
/**
* Gets a list of heading to ignore.
*
* @return string[]
*/
private function getIgnoredHeadings() {
static $ignoredHeadings = null;
if ( $ignoredHeadings === null ) {
$ignoredHeadings = [];
$source = wfMessage( 'search-ignored-headings' )->inContentLanguage();
if ( !$source->isDisabled() ) {
$lines = self::parseSettingsInMessage( $source->plain() );
// Now we just have headings!
$ignoredHeadings = $lines;
}
}
return $ignoredHeadings;
}
/**
* Extract parts of the text - opening, main and auxiliary.
*/
private function extractWikitextParts() {
if ( $this->allText !== null ) {
return;
}
$text = $this->parserOutput->getRawText();
if ( $text === '' ) {
$this->allText = "";
// empty text - nothing to seek here
return;
}
$this->openingText = $this->extractTextBeforeFirstHeading( $text );
$formatter = new HtmlFormatter( $text );
// Strip elements from the page that we never want in the search text.
$formatter->remove( self::EXCLUDED_ELEMENT_SELECTORS );
$formatter->filterContent();
// Strip elements from the page that are auxiliary text. These will still be
// searched, but matches will be ranked lower and non-auxiliary matches will be
// preferred in highlighting.
$formatter->remove( self::AUXILIARY_ELEMENT_SELECTORS );
$auxiliaryElements = $formatter->filterContent();
$this->allText = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
foreach ( $auxiliaryElements as $auxiliaryElement ) {
$this->auxText[] =
trim( Sanitizer::stripAllTags( $formatter->getText( $auxiliaryElement ) ) );
}
}
/**
* Get text before first heading.
*
* @param string $text
*
* @return string|null
*/
private function extractTextBeforeFirstHeading( $text ) {
$matches = [];
if ( !preg_match( '/remove( self::EXCLUDED_ELEMENT_SELECTORS );
$formatter->remove( self::AUXILIARY_ELEMENT_SELECTORS );
$formatter->filterContent();
$text = trim( Sanitizer::stripAllTags( $formatter->getText() ) );
if ( !$text ) {
// There isn't any text after filtering before the first heading, so we declare
// that there isn't a first heading.
return null;
}
return $text;
}
/**
* @return string|null
*/
public function getOpeningText() {
$this->extractWikitextParts();
return $this->openingText;
}
/**
* @return string
*/
public function getMainText() {
$this->extractWikitextParts();
return $this->allText;
}
/**
* @return string[]
*/
public function getAuxiliaryText() {
$this->extractWikitextParts();
return $this->auxText;
}
/**
* Get the "defaultsort" property
*
* @return string|null
*/
public function getDefaultSort() {
$sort = $this->parserOutput->getPageProperty( 'defaultsort' );
if ( $sort === false ) {
return null;
}
return $sort;
}
}
/** @deprecated class alias since 1.43 */
class_alias( WikiTextStructure::class, 'WikiTextStructure' );