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 );
+ }
}
// 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 );
}
}