From 98abaec7fd74ffa62e9033836dbf2aa61b3af2de Mon Sep 17 00:00:00 2001
From: Fridolin Somers <fridolin.somers@biblibre.com>
Date: Fri, 4 Aug 2023 10:51:22 -1000
Subject: [PATCH] Bug 34481: Add IncludeSeeAlsoFromInSearches like
 IncludeSeeFromInSearches
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Like IncludeSeeFromInSearches adds see from heading in search (4xx),
new system preference IncludeSeeAlsoFromInSearches adds see also from heading (5xx).

Test plan:
Test on both Elasticsearch and Zebra:
1)
1.1) Create an authority record with heading, see from heading 4xx and see also from heading 5xx
1.2) Use this authority in a biblio record
2)
2.1) Set IncludeSeeFromInSearches and IncludeSeeAlsoFromInSearches to "Don't include"
2.3) Rebuild search engine
2.4) Perform a search on heading text => you find the biblio record
2.5) Perform a search on see from heading => you do not find the biblio record
2.6) Perform a search on see also from heading => you do not find the biblio record
3)
3.1) Set IncludeSeeFromInSearches and IncludeSeeAlsoFromInSearches to "Include"
3.2) Rebuild search engine
3.3) Perform a search on heading text => you find the biblio record
3.4) Perform a search on see from heading => you find the biblio record
3.5) Perform a search on see also from heading => you find the biblio record

Signed-off-by: Frédéric Demians <f.demians@tamil.fr>
---
 Koha/Filter/MARC/EmbedSeeFromHeadings.pm      | 17 +++++++++++++--
 Koha/SearchEngine/Elasticsearch.pm            | 19 +++++++++++++++--
 .../data/mysql/atomicupdate/bug_34481.pl      | 17 +++++++++++++++
 installer/data/mysql/mandatory/sysprefs.sql   |  1 +
 .../modules/admin/preferences/searching.pref  |  7 +++++++
 misc/migration_tools/rebuild_zebra.pl         | 21 ++++++++++++++++---
 6 files changed, 75 insertions(+), 7 deletions(-)
 create mode 100755 installer/data/mysql/atomicupdate/bug_34481.pl

diff --git a/Koha/Filter/MARC/EmbedSeeFromHeadings.pm b/Koha/Filter/MARC/EmbedSeeFromHeadings.pm
index 9973db90de..93eaf44501 100644
--- a/Koha/Filter/MARC/EmbedSeeFromHeadings.pm
+++ b/Koha/Filter/MARC/EmbedSeeFromHeadings.pm
@@ -88,6 +88,19 @@ include in facets, sorting, or suggestion fields)
 sub fields {
     my ($self, $record) = @_;
 
+    my $params = $self->params;
+    my $other_headings = $params->{options}->{other_headings} || [ 'see_from' ];
+
+    my @auth_others;
+    foreach my $other_heading ( @$other_headings ) {
+        if ( $other_heading eq 'see_from' ) {
+            push @auth_others, '4..';
+        }
+        if ( $other_heading eq 'see_also_from' ) {
+            push @auth_others, '5..';
+        }
+    }
+
     my ($item_tag) = GetMarcFromKohaField( "items.itemnumber" );
     $item_tag ||= '';
 
@@ -102,8 +115,8 @@ sub fields {
         my $authority = Koha::MetadataRecord::Authority->get_from_authid($authid);
         next unless $authority;
         my $auth_marc = $authority->record;
-        my @seefrom = $auth_marc->field('4..');
-        foreach my $authfield (@seefrom) {
+        my @authfields = $auth_marc->field(@auth_others);
+        foreach my $authfield (@authfields) {
             my $tag = substr($field->tag(), 0, 1) . substr($authfield->tag(), 1, 2);
             next if MARC::Field->is_controlfield_tag($tag);
             my $newfield = MARC::Field->new($tag,
diff --git a/Koha/SearchEngine/Elasticsearch.pm b/Koha/SearchEngine/Elasticsearch.pm
index 3f26cbc027..9b168650e4 100644
--- a/Koha/SearchEngine/Elasticsearch.pm
+++ b/Koha/SearchEngine/Elasticsearch.pm
@@ -648,8 +648,23 @@ sub marc_records_to_documents {
             }
         }
 
-        if (C4::Context->preference('IncludeSeeFromInSearches') and $self->index eq 'biblios') {
-            foreach my $field (Koha::Filter::MARC::EmbedSeeFromHeadings->new->fields($record)) {
+        if ( $self->index eq $BIBLIOS_INDEX and ( C4::Context->preference('IncludeSeeFromInSearches') || C4::Context->preference('IncludeSeeAlsoFromInSearches') ) ) {
+            my $marc_filter = Koha::Filter::MARC::EmbedSeeFromHeadings->new;
+            my @other_headings;
+            if ( C4::Context->preference('IncludeSeeFromInSearches') ) {
+                push @other_headings, 'see_from';
+            }
+            if ( C4::Context->preference('IncludeSeeAlsoFromInSearches') ) {
+                push @other_headings, 'see_also_from';
+            }
+            $marc_filter->initialize(
+                {
+                    options => {
+                        other_headings => \@other_headings
+                    }
+                }
+            );
+            foreach my $field ($marc_filter->fields($record)) {
                 my $data_field_rules = $data_fields_rules->{$field->tag()};
                 if ($data_field_rules) {
                     my $subfields_mappings = $data_field_rules->{subfields};
diff --git a/installer/data/mysql/atomicupdate/bug_34481.pl b/installer/data/mysql/atomicupdate/bug_34481.pl
new file mode 100755
index 0000000000..e7a4f01038
--- /dev/null
+++ b/installer/data/mysql/atomicupdate/bug_34481.pl
@@ -0,0 +1,17 @@
+use Modern::Perl;
+
+return {
+    bug_number  => "34481",
+    description => "Add IncludeSeeAlsoFromInSearches like IncludeSeeFromInSearches",
+    up          => sub {
+        my ($args) = @_;
+        my ( $dbh, $out ) = @$args{qw(dbh out)};
+
+        $dbh->do(q{
+            INSERT IGNORE INTO systempreferences (`variable`,`value`,`options`,`explanation`,`type`)
+            VALUES ('IncludeSeeAlsoFromInSearches','0','','Include see-also-from references in searches.','YesNo')
+        });
+
+        say $out "Added new system preference 'IncludeSeeAlsoFromInSearches'";
+    },
+};
diff --git a/installer/data/mysql/mandatory/sysprefs.sql b/installer/data/mysql/mandatory/sysprefs.sql
index b1aba01bcb..a35e732763 100644
--- a/installer/data/mysql/mandatory/sysprefs.sql
+++ b/installer/data/mysql/mandatory/sysprefs.sql
@@ -319,6 +319,7 @@ INSERT INTO systempreferences ( `variable`, `value`, `options`, `explanation`, `
 ('ILS-DI:AuthorizedIPs','','Restricts usage of ILS-DI to some IPs','.','Free'),
 ('ImageLimit','5','','Limit images stored in the database by the Patron Card image manager to this number.','Integer'),
 ('IncludeSeeFromInSearches','0','','Include see-from references in searches.','YesNo'),
+('IncludeSeeAlsoFromInSearches','0','','Include see-also-from references in searches.','YesNo'),
 ('IndependentBranches','0',NULL,'If ON, increases security between libraries','YesNo'),
 ('IndependentBranchesPatronModifications','0', NULL, 'Show only modification request for the logged in branch','YesNo'),
 ('IndependentBranchesTransfers','0', NULL, 'Allow non-superlibrarians to transfer items between libraries','YesNo'),
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
index 86b9f54ae1..ef0fdc7751 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/preferences/searching.pref
@@ -65,6 +65,13 @@ Searching:
                   1: Include
                   0: "Don't include"
             - "<em>see from</em> (non-preferred form) headings in bibliographic searches. Please note: you will need to reindex your bibliographic database when changing this preference."
+        -
+            - pref: IncludeSeeAlsoFromInSearches
+              default: 0
+              choices:
+                  1: Include
+                  0: "Don't include"
+            - "<em>see also from</em> headings in bibliographic searches. Please note: you will need to reindex your bibliographic database when changing this preference."
         -
             - pref: EnableSearchHistory
               default: 0
diff --git a/misc/migration_tools/rebuild_zebra.pl b/misc/migration_tools/rebuild_zebra.pl
index ec19ce1f5c..941c5f8e2f 100755
--- a/misc/migration_tools/rebuild_zebra.pl
+++ b/misc/migration_tools/rebuild_zebra.pl
@@ -660,11 +660,26 @@ sub get_corrected_marc_record {
         elsif ( $record_type eq 'biblio' ) {
 
             my @filters;
+            my @other_headings;
             push @filters, 'EmbedItemsAvailability';
-            push @filters, 'EmbedSeeFromHeadings'
-                if C4::Context->preference('IncludeSeeFromInSearches');
+            if ( C4::Context->preference('IncludeSeeFromInSearches') || C4::Context->preference('IncludeSeeAlsoFromInSearches') ){
+                push @filters, 'EmbedSeeFromHeadings';
+                if ( C4::Context->preference('IncludeSeeFromInSearches') ) {
+                    push @other_headings, 'see_from';
+                }
+                if ( C4::Context->preference('IncludeSeeAlsoFromInSearches') ) {
+                    push @other_headings, 'see_also_from';
+                }
+            }
 
-            my $normalizer = Koha::RecordProcessor->new( { filters => \@filters } );
+            my $normalizer = Koha::RecordProcessor->new(
+                {
+                    filters => \@filters,
+                    options => {
+                        other_headings => \@other_headings
+                    }
+                }
+            );
             $marc = $normalizer->process($marc);
         }
         if ( C4::Context->preference("marcflavour") eq "UNIMARC" ) {
-- 
2.34.1