From 26bba845a3b308d7212b67f58d5439cfbc234021 Mon Sep 17 00:00:00 2001
From: Eoin Kilfeather <ekilfeather@gmail.com>
Date: Fri, 25 May 2018 14:10:39 +0100
Subject: [PATCH] Improve standalone slash handling (#1182)

- Rather than eliminating standalone slashes, quote them. This still prevents syntax errors, but it causes less loss of information.
---
 .../Backend/Solr/LuceneSyntaxHelper.php            | 14 ++++++++++++--
 .../VuFindTest/Backend/Solr/QueryBuilderTest.php   |  3 +--
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/LuceneSyntaxHelper.php b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/LuceneSyntaxHelper.php
index b69955e4304..39c4ecbf140 100644
--- a/module/VuFindSearch/src/VuFindSearch/Backend/Solr/LuceneSyntaxHelper.php
+++ b/module/VuFindSearch/src/VuFindSearch/Backend/Solr/LuceneSyntaxHelper.php
@@ -487,11 +487,21 @@ class LuceneSyntaxHelper
     {
         // Freestanding hyphens and slashes can cause problems:
         $lookahead = self::$insideQuotes;
+        // remove freestanding hyphens
         $input = preg_replace(
-            '/(\s+[-\/]$|\s+[-\/]\s+|^[-\/]\s+)' . $lookahead . '/',
+            '/(\s+[-]$|\s+[-]\s+|^[-]\s+)' . $lookahead . '/',
+            ' ', $input
+        );
+        // wrap quotes on standalone slashes
+        $input = preg_replace(
+            '/(\s+[\/]\s+)' . $lookahead . '/',
+            ' "/" ', $input
+        );
+        // remove trailing and leading slashes
+        $input = preg_replace(
+            '/(\s+[\/]$|^[\/]\s+)' . $lookahead . '/',
             ' ', $input
         );
-
         // A proximity of 1 is illegal and meaningless -- remove it:
         $input = preg_replace('/~1(\.0*)?$/', '', $input);
         $input = preg_replace('/~1(\.0*)?\s+' . $lookahead . '/', ' ', $input);
diff --git a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
index 3c7285b2ce1..30ea55f23d7 100644
--- a/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
+++ b/module/VuFindSearch/tests/unit-tests/src/VuFindTest/Backend/Solr/QueryBuilderTest.php
@@ -75,10 +75,9 @@ class QueryBuilderTest extends \VuFindTest\Unit\TestCase
             ['^10', '10'],                       // invalid boosts
             ['test^ test^6', 'test test6'],      // invalid boosts
             ['test^1 test^2', 'test^1 test^2'],  // valid boosts
-            ['this / that', 'this that'],        // freestanding slash
+            ['this / that', 'this "/" that'],    // freestanding slash
             ['/ this', 'this'],                  // leading slash
             ['title /', 'title'],                // trailing slash
-            ['this - that', 'this that'],        // freestanding hyphen
             ['- this', 'this'],                  // leading hyphen
             ['title -', 'title'],                // trailing hyphen
             ['AND', 'and'],                      // freestanding operator
-- 
GitLab