From be2e63857d28b2a3610be739a38b1841256e100f Mon Sep 17 00:00:00 2001
From: Fridolin Somers <fridolin.somers@biblibre.com>
Date: Wed, 7 Jul 2021 15:42:13 -1000
Subject: [PATCH] Bug 28682: Protect search for analytics in record detail page

In record detail page, there is a search for analytics, see Bug 15851.

If for any reason this search fails, the page will be broken with 'Internal server error'.

This patch adds a protection with eval.

Test plan :
1) Use a Zebra install with no Elasticsearch configuration
2) Go to staff interface
3) Go to any record detail page : no error
4) Change preference 'SearchEngine' to 'Elasticsearch'
5) Refresh ecord detail page : check you have no error
6) Check a record with analytics link
7) Repeat 3 to 6 on OPAC
---
 catalogue/detail.pl | 29 +++++++++++++++--------------
 opac/opac-detail.pl | 29 +++++++++++++++--------------
 2 files changed, 30 insertions(+), 28 deletions(-)

diff --git a/catalogue/detail.pl b/catalogue/detail.pl
index c282ecf6b8..2550d60ec1 100755
--- a/catalogue/detail.pl
+++ b/catalogue/detail.pl
@@ -124,22 +124,23 @@ my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
 
 if ( $xslfile ) {
 
-    my $searcher = Koha::SearchEngine::Search->new(
-        { index => $Koha::SearchEngine::BIBLIOS_INDEX }
-    );
-    my $cleaned_title = $biblio->title;
-    $cleaned_title =~ tr|/||;
-    my $query =
-      ( C4::Context->preference('UseControlNumber') and $record->field('001') )
-      ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
-      : "Host-item:($cleaned_title)";
-    my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
-
-    warn "Warning from simple_search_compat: $err"
-        if $err;
+    my $analytics_count = 0;
+    eval {
+        my $searcher = Koha::SearchEngine::Search->new(
+            { index => $Koha::SearchEngine::BIBLIOS_INDEX }
+        );
+        my $cleaned_title = $biblio->title;
+        $cleaned_title =~ tr|/||;
+        my $query =
+          ( C4::Context->preference('UseControlNumber') and $record->field('001') )
+          ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
+          : "Host-item:($cleaned_title)";
+        my ( $err, $result, $analytics_count ) = $searcher->simple_search_compat( $query, 0, 0 );
+        warn "Warning from simple_search_compat: $err" if $err;
+    };
 
     my $variables = {
-        show_analytics_link => $count > 0 ? 1 : 0
+        show_analytics_link => $analytics_count > 0 ? 1 : 0
     };
 
     $template->param(
diff --git a/opac/opac-detail.pl b/opac/opac-detail.pl
index 249de81618..0f94445f94 100755
--- a/opac/opac-detail.pl
+++ b/opac/opac-detail.pl
@@ -180,23 +180,24 @@ my $sysxml = $xslfile ? C4::XSLT::get_xslt_sysprefs() : undef;
 
 if ( $xslfile ) {
 
-    my $searcher = Koha::SearchEngine::Search->new(
-        { index => $Koha::SearchEngine::BIBLIOS_INDEX }
-    );
-    my $cleaned_title = $biblio->title;
-    $cleaned_title =~ tr|/||;
-    my $query =
-      ( C4::Context->preference('UseControlNumber') and $record->field('001') )
-      ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
-      : "Host-item:($cleaned_title)";
-    my ( $err, $result, $count ) = $searcher->simple_search_compat( $query, 0, 0 );
-
-    warn "Warning from simple_search_compat: $err"
-        if $err;
+    my $analytics_count = 0;
+    eval {
+        my $searcher = Koha::SearchEngine::Search->new(
+            { index => $Koha::SearchEngine::BIBLIOS_INDEX }
+        );
+        my $cleaned_title = $biblio->title;
+        $cleaned_title =~ tr|/||;
+        my $query =
+          ( C4::Context->preference('UseControlNumber') and $record->field('001') )
+          ? 'rcn:'. $record->field('001')->data . ' AND (bib-level:a OR bib-level:b)'
+          : "Host-item:($cleaned_title)";
+        my ( $err, $result, $analytics_count ) = $searcher->simple_search_compat( $query, 0, 0 );
+        warn "Warning from simple_search_compat: $err" if $err;
+    };
 
     my $variables = {
         anonymous_session   => ($borrowernumber) ? 0 : 1,
-        show_analytics_link => $count > 0 ? 1 : 0
+        show_analytics_link => $analytics_count > 0 ? 1 : 0
     };
 
     my @plugin_responses = Koha::Plugins->call(
-- 
2.32.0