From e3a34617b275e10a8610bdda694e1d18c9e9e7df Mon Sep 17 00:00:00 2001 From: Stephan Gambke Date: Mon, 18 Aug 2014 15:55:51 +0000 Subject: [PATCH] Fix highlighting of results when the search result does not return termMatches If the search engine does not know the concept of serch terms the search result object will just return an empty array as defined in class SearchResultSet. In this case SearchHighlighter::highlightSimple will place a span between each and every byte (yes, byte, not character, it will break multibyte chars). This patch will just output the first few lines of a page if no search terms are available for highlighting. Note: Highlighting the page name in the case of title matches is questionable, IMHO. It might make more sense for this case as well to just return the first few lines of the page. Change-Id: I276418f271855fb99443188f51cc076289c6ba0d --- includes/search/SearchHighlighter.php | 17 +++++++++++++++++ includes/search/SearchResult.php | 11 ++++++++--- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/includes/search/SearchHighlighter.php b/includes/search/SearchHighlighter.php index 69534624c8..7e5f685930 100644 --- a/includes/search/SearchHighlighter.php +++ b/includes/search/SearchHighlighter.php @@ -554,4 +554,21 @@ class SearchHighlighter { return $extract; } + + /** + * Returns the first few lines of the text + * + * @param string $text + * @param int $contextlines max number of returned lines + * @param int $contextchars average number of characters per line + * @return string + */ + public function highlightNone( $text, $contextlines, $contextchars) { + $match = array(); + $text = ltrim( $text ) . "\n"; // make sure the preg_match may find the last line + $text = str_replace( "\n\n", "\n", $text); // remove empty lines + preg_match( "/^(.*\n){0,$contextlines}/", $text , $match); + $text = htmlspecialchars( substr( trim( $match[0] ), 0, $contextlines * $contextchars ) ); // trim and limit to max number of chars + return str_replace( "\n", '
', $text ); + } } diff --git a/includes/search/SearchResult.php b/includes/search/SearchResult.php index d51bff7cdd..1d28691c83 100644 --- a/includes/search/SearchResult.php +++ b/includes/search/SearchResult.php @@ -166,11 +166,16 @@ class SearchResult { // TODO: make highliter take a content object. Make ContentHandler a factory for SearchHighliter. list( $contextlines, $contextchars ) = SearchEngine::userHighlightPrefs(); + $h = new SearchHighlighter(); - if ( $wgAdvancedSearchHighlighting ) { - return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars ); + if ( count( $terms ) > 0 ) { + if ( $wgAdvancedSearchHighlighting ) { + return $h->highlightText( $this->mText, $terms, $contextlines, $contextchars ); + } else { + return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars ); + } } else { - return $h->highlightSimple( $this->mText, $terms, $contextlines, $contextchars ); + return $h->highlightNone( $this->mText, $contextlines, $contextchars ); } } -- 2.20.1