From e6423f1f96c45411d4b42fce51ff41c20894aa22 Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 6 May 2013 14:46:09 -0400
Subject: [PATCH] Resolving VUFIND-709 (Issue with searches followed by
 question marks).

---
 .../VuFindSearch/Backend/Solr/QueryBuilder.php    | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
index fd2bb45b430..92c4fa22fe9 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/QueryBuilder.php
@@ -374,11 +374,16 @@ class QueryBuilder
             $string = '[* TO *]';
         }
 
-        // If the query ends in a question mark, the user may not really intend to
-        // use the question mark as a wildcard -- let's account for that possibility
-        if (substr($string, -1) == '?') {
-            $string = "({$string}) OR (" . substr($string, 0, strlen($string) - 1)
-                . ")";
+        // If the query ends in a non-escaped question mark, the user may not really
+        // intend to use the question mark as a wildcard -- let's account for that
+        // possibility
+        if (substr($query, -1) == '?' && substr($query, -2) != '\?') {
+            // Make sure all question marks are properly escaped (first unescape
+            // any that are already escaped to prevent double-escapes, then escape
+            // all of them):
+            $strippedQuery
+                = str_replace('?', '\?', str_replace('\?', '?', $query));
+            $query = "({$query}) OR (" . $strippedQuery . ")";
         }
 
         return $handler
-- 
GitLab