From 675f1701c987cca3426129a22a1669aba1798574 Mon Sep 17 00:00:00 2001 From: Aryeh Gregor Date: Thu, 17 Apr 2008 15:59:49 +0000 Subject: [PATCH] The problem also applies to all the other regex special chars: try it out with ., |, etc. Use preg_quote(). --- RELEASE-NOTES | 4 ++-- includes/SearchEngine.php | 10 ++++++---- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 33d9d0c5f7..5b7509df47 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -190,8 +190,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 13756) Don't show the form and navigation links of Special:Newpages if the page is included * When hiding things on WhatLinksHere, generated URLs should hide them too -* Properly escape search terms with forward slashes so they appear highlighted - in search results. +* Properly escape search terms with regex chars so they appear highlighted in + search results === API changes in 1.13 === diff --git a/includes/SearchEngine.php b/includes/SearchEngine.php index 864be9d7a9..6af2bb38a0 100644 --- a/includes/SearchEngine.php +++ b/includes/SearchEngine.php @@ -545,7 +545,7 @@ class SearchResult { } /** - * @param array $terms terms to highlight + * @param array $terms Terms to highlight (unescaped) * @return string highlighted text snippet, null (and not '') if not supported */ function getTextSnippet($terms){ @@ -559,7 +559,7 @@ class SearchResult { * Default implementation of snippet extraction * * @param string $text - * @param array $terms + * @param array $terms Terms to highlight (unescaped) * @param int $contextlines * @param int $contextchars * @return string @@ -569,9 +569,11 @@ class SearchResult { $fname = __METHOD__; $lines = explode( "\n", $text ); - + + foreach( $terms as $index => $term ) { + $terms[$index] = preg_quote( $term, '/' ); + } $terms = implode( '|', $terms ); - $terms = str_replace( '/', "\\/", $terms); $max = intval( $contextchars ) + 1; $pat1 = "/(.*)($terms)(.{0,$max})/i"; -- 2.20.1