Fix highlighting of results when the search result does not return termMatches
[lhc/web/wiklou.git] / includes / search / SearchHighlighter.php
index b8b2bae..7e5f685 100644 (file)
@@ -117,7 +117,7 @@ class SearchHighlighter {
                                                }
                                                $offset = $endMatches[0][1] + strlen( $endMatches[0][0] );
                                        }
-                                       if ( ! $found ) {
+                                       if ( !$found ) {
                                                // couldn't find appropriate closing tag, skip
                                                $this->splitAndAdd( $textExt, $count, substr( $text, $start, strlen( $matches[0][0] ) ) );
                                                $start += strlen( $matches[0][0] );
@@ -184,7 +184,7 @@ class SearchHighlighter {
                        $succ = true;
                        // check if first text contains all terms
                        foreach ( $terms as $term ) {
-                               if ( ! preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) {
+                               if ( !preg_match( "/$patPre" . $term . "$patPost/ui", $firstText ) ) {
                                        $succ = false;
                                        break;
                                }
@@ -194,7 +194,7 @@ class SearchHighlighter {
                                $offsets[$first] = 0;
                        }
                }
-               if ( ! $snippets ) {
+               if ( !$snippets ) {
                        // match whole query on text
                        $this->process( $pat1, $textExt, $left, $contextchars, $snippets, $offsets );
                        // match whole query on templates/tables/images
@@ -279,7 +279,7 @@ class SearchHighlighter {
 
                $processed = array();
                foreach ( $terms as $term ) {
-                       if ( ! isset( $processed[$term] ) ) {
+                       if ( !isset( $processed[$term] ) ) {
                                $pat3 = "/$patPre(" . $term . ")$patPost/ui"; // highlight word
                                $extract = preg_replace( $pat3,
                                        "\\1<span class='searchmatch'>\\2</span>\\3", $extract );
@@ -295,7 +295,7 @@ class SearchHighlighter {
        /**
         * Split text into lines and add it to extracts array
         *
-        * @param array $extracts index -> $line
+        * @param array $extracts Index -> $line
         * @param int $count
         * @param string $text
         */
@@ -447,6 +447,7 @@ class SearchHighlighter {
        /**
         * Basic wikitext removal
         * @protected
+        * @param string $text
         * @return mixed
         */
        function removeWiki( $text ) {
@@ -528,7 +529,7 @@ class SearchHighlighter {
                        }
                        ++$lineno;
                        $m = array();
-                       if ( ! preg_match( $pat1, $line, $m ) ) {
+                       if ( !preg_match( $pat1, $line, $m ) ) {
                                continue;
                        }
                        --$contextlines;
@@ -553,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", '<br>', $text );
+       }
 }