Bug 33282: Remove useless intercept
[srvgit] / C4 / Heading.pm
index dca4d1b..7541a56 100644 (file)
@@ -4,28 +4,25 @@ package C4::Heading;
 #
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 2 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
 
-use strict;
-use warnings;
-use MARC::Record;
 use MARC::Field;
 use C4::Context;
-use Module::Load;
-use Carp;
+use Module::Load qw( load );
 
-our $VERSION = 3.07.00.049;
 
 =head1 NAME
 
@@ -34,7 +31,7 @@ C4::Heading
 =head1 SYNOPSIS
 
  use C4::Heading;
- my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
+ my $heading = C4::Heading->new_from_field($field, $frameworkcode);
  my $thesaurus = $heading->thesaurus();
  my $type = $heading->type();
  my $display_heading = $heading->display_form();
@@ -47,32 +44,30 @@ headings found in bibliographic and authority records.
 
 =head1 METHODS
 
-=head2 new_from_bib_field
+=head2 new_from_field
 
-  my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
+  my $heading = C4::Heading->new_from_field($field, $frameworkcode, [, $auth]);
 
 Given a C<MARC::Field> object containing a heading from a 
 bib record, create a C<C4::Heading> object.
 
-The optional second parameter is the MARC flavour (i.e., MARC21
-or UNIMARC); if this parameter is not supplied, it is
-taken from the Koha application context.
+The optional third parameter is 'auth' - it is handled as boolean. If supplied we treat the field as an auth record field. Otherwise if it is a bib field. The fields checked are the same in a UNIMARC system and this parameter is ignored
 
 If the MARC field supplied is not a valid heading, undef
 is returned.
 
 =cut
 
-sub new_from_bib_field {
+sub new_from_field {
     my $class         = shift;
     my $field         = shift;
-    my $frameworkcode = shift;
-    my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
-
+    my $frameworkcode = shift; #FIXME this is not used?
+    my $auth          = shift;
+    my $marcflavour   = C4::Context->preference('marcflavour');
     my $marc_handler = _marc_format_handler($marcflavour);
 
     my $tag = $field->tag();
-    return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
+    return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth );
     my $self = {};
 
     $self->{'field'} = $field;
@@ -80,7 +75,7 @@ sub new_from_bib_field {
         $self->{'auth_type'},   $self->{'thesaurus'},
         $self->{'search_form'}, $self->{'display_form'},
         $self->{'match_type'}
-    ) = $marc_handler->parse_heading($field);
+    ) = $marc_handler->parse_heading($field, $auth );
 
     bless $self, $class;
     return $self;
@@ -171,6 +166,24 @@ sub preferred_authorities {
     return $results;
 }
 
+=head2 valid_heading_subfield
+
+    if (C4::Heading::valid_heading_subfield('100', 'e', '')) ...
+
+Check if the given subfield is valid for the given field.
+
+=cut
+
+sub valid_heading_subfield {
+    my $tag           = shift;
+    my $subfield      = shift;
+    my $marcflavour   = C4::Context->preference('marcflavour');
+    my $auth          = shift;
+
+    my $marc_handler = _marc_format_handler($marcflavour);
+    return $marc_handler->valid_heading_subfield( $tag, $subfield, $auth );
+}
+
 =head1 INTERNAL METHODS
 
 =head2 _search
@@ -181,6 +194,8 @@ sub _search {
     my $self         = shift;
     my $index        = shift || undef;
     my $skipmetadata = shift || undef;
+    my $ind2         = $self->{field}->{_ind2};
+    my $subject_heading_thesaurus = '';
     my @marclist;
     my @and_or;
     my @excluding = [];
@@ -189,24 +204,61 @@ sub _search {
 
     if ($index) {
         push @marclist, $index;
-        push @and_or,   'and';
+        push @and_or,   'AND';
         push @operator, $self->{'match_type'};
         push @value,    $self->{'search_form'};
     }
 
-    #    if ($self->{'thesaurus'}) {
-    #        push @marclist, 'thesaurus';
-    #        push @and_or, 'and';
-    #        push @excluding, '';
-    #        push @operator, 'is';
-    #        push @value, $self->{'thesaurus'};
-    #    }
-    require C4::AuthoritiesMarc;
-    return C4::AuthoritiesMarc::SearchAuthorities(
+    if ($self->{'thesaurus'}) {
+        if ($ind2 eq '0') {
+            $subject_heading_thesaurus = 'a';
+        } elsif ($ind2 eq '1') {
+            $subject_heading_thesaurus = 'b';
+        } elsif ($ind2 eq '2') {
+            $subject_heading_thesaurus = 'c';
+        } elsif ($ind2 eq '3') {
+            $subject_heading_thesaurus = 'd';
+        } elsif ($ind2 eq '4') {
+            $subject_heading_thesaurus = 'n';
+        } elsif ($ind2 eq '5') {
+            $subject_heading_thesaurus = 'k';
+        } elsif ($ind2 eq '6') {
+            $subject_heading_thesaurus = 'v';
+        } else {
+            $subject_heading_thesaurus = 'z';
+        }
+        push @marclist, 'thesaurus';
+        push @and_or, 'and';
+        push @excluding, '';
+        push @operator, 'is';
+        push @value, $subject_heading_thesaurus;
+    }
+
+    if ($ind2 eq '7') {
+        push @marclist, 'thesaurus-conventions';
+        push @and_or, 'and';
+        push @excluding, '';
+        push @operator, 'is';
+        push @value, $self->{'thesaurus'};
+    }
+
+    require Koha::SearchEngine::QueryBuilder;
+    require Koha::SearchEngine::Search;
+
+    # Use state variables to avoid recreating the objects every time.
+    # With Elasticsearch this also avoids creating a massive amount of
+    # ES connectors that would eventually run out of file descriptors.
+    state $builder = Koha::SearchEngine::QueryBuilder->new(
+        { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
+    state $searcher = Koha::SearchEngine::Search->new(
+        {index => $Koha::SearchEngine::AUTHORITIES_INDEX} );
+
+    my $search_query = $builder->build_authorities_query_compat(
         \@marclist, \@and_or, \@excluding, \@operator,
-        \@value,    0,        20,          $self->{'auth_type'},
-        'AuthidAsc',         $skipmetadata
+        \@value,    $self->{'auth_type'},
+        'AuthidAsc'
     );
+    return $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
 }
 
 =head1 INTERNAL FUNCTIONS
@@ -220,7 +272,6 @@ depending on the selected MARC flavour.
 
 sub _marc_format_handler {
     my $marcflavour = uc shift;
-    $marcflavour = 'MARC21' if ( $marcflavour eq 'NORMARC' );
     my $pname = "C4::Heading::$marcflavour";
     load $pname;
     return $pname->new();