Bug 19365: (QA follow-up) Ensure we use a modern enough perl
[koha.git] / C4 / Heading.pm
1 package C4::Heading;
2
3 # Copyright (C) 2008 LibLime
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 use strict;
21 use warnings;
22 use 5.010;
23
24 use MARC::Record;
25 use MARC::Field;
26 use C4::Context;
27 use Module::Load;
28 use Carp;
29
30
31 =head1 NAME
32
33 C4::Heading
34
35 =head1 SYNOPSIS
36
37  use C4::Heading;
38  my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
39  my $thesaurus = $heading->thesaurus();
40  my $type = $heading->type();
41  my $display_heading = $heading->display_form();
42  my $search_form = $heading->search_form();
43
44 =head1 DESCRIPTION
45
46 C<C4::Heading> implements a simple class to representing
47 headings found in bibliographic and authority records.
48
49 =head1 METHODS
50
51 =head2 new_from_bib_field
52
53   my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
54
55 Given a C<MARC::Field> object containing a heading from a 
56 bib record, create a C<C4::Heading> object.
57
58 The optional second parameter is the MARC flavour (i.e., MARC21
59 or UNIMARC); if this parameter is not supplied, it is
60 taken from the Koha application context.
61
62 If the MARC field supplied is not a valid heading, undef
63 is returned.
64
65 =cut
66
67 sub new_from_bib_field {
68     my $class         = shift;
69     my $field         = shift;
70     my $frameworkcode = shift;
71     my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
72
73     my $marc_handler = _marc_format_handler($marcflavour);
74
75     my $tag = $field->tag();
76     return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
77     my $self = {};
78
79     $self->{'field'} = $field;
80     (
81         $self->{'auth_type'},   $self->{'thesaurus'},
82         $self->{'search_form'}, $self->{'display_form'},
83         $self->{'match_type'}
84     ) = $marc_handler->parse_heading($field);
85
86     bless $self, $class;
87     return $self;
88 }
89
90 =head2 auth_type
91
92   my $auth_type = $heading->auth_type();
93
94 Return the auth_type of the heading.
95
96 =cut
97
98 sub auth_type {
99     my $self = shift;
100     return $self->{'auth_type'};
101 }
102
103 =head2 field
104
105   my $field = $heading->field();
106
107 Return the MARC::Field the heading is based on.
108
109 =cut
110
111 sub field {
112     my $self = shift;
113     return $self->{'field'};
114 }
115
116 =head2 display_form
117
118   my $display = $heading->display_form();
119
120 Return the "canonical" display form of the heading.
121
122 =cut
123
124 sub display_form {
125     my $self = shift;
126     return $self->{'display_form'};
127 }
128
129 =head2 search_form
130
131   my $search_form = $heading->search_form();
132
133 Return the "canonical" search form of the heading.
134
135 =cut
136
137 sub search_form {
138     my $self = shift;
139     return $self->{'search_form'};
140 }
141
142 =head2 authorities
143
144   my $authorities = $heading->authorities([$skipmetadata]);
145
146 Return a list of authority records for this 
147 heading. If passed a true value for $skipmetadata,
148 SearchAuthorities will return only authids.
149
150 =cut
151
152 sub authorities {
153     my $self         = shift;
154     my $skipmetadata = shift;
155     my ( $results, $total ) = _search( $self, 'match-heading', $skipmetadata );
156     return $results;
157 }
158
159 =head2 preferred_authorities
160
161   my $preferred_authorities = $heading->preferred_authorities;
162
163 Return a list of authority records for headings
164 that are a preferred form of the heading.
165
166 =cut
167
168 sub preferred_authorities {
169     my $self = shift;
170     my $skipmetadata = shift || undef;
171     my ( $results, $total ) = _search( 'see-from', $skipmetadata );
172     return $results;
173 }
174
175 =head1 INTERNAL METHODS
176
177 =head2 _search
178
179 =cut
180
181 sub _search {
182     my $self         = shift;
183     my $index        = shift || undef;
184     my $skipmetadata = shift || undef;
185     my @marclist;
186     my @and_or;
187     my @excluding = [];
188     my @operator;
189     my @value;
190
191     if ($index) {
192         push @marclist, $index;
193         push @and_or,   'and';
194         push @operator, $self->{'match_type'};
195         push @value,    $self->{'search_form'};
196     }
197
198     #    if ($self->{'thesaurus'}) {
199     #        push @marclist, 'thesaurus';
200     #        push @and_or, 'and';
201     #        push @excluding, '';
202     #        push @operator, 'is';
203     #        push @value, $self->{'thesaurus'};
204     #    }
205
206     require Koha::SearchEngine::QueryBuilder;
207     require Koha::SearchEngine::Search;
208
209     # Use state variables to avoid recreating the objects every time.
210     # With Elasticsearch this also avoids creating a massive amount of
211     # ES connectors that would eventually run out of file descriptors.
212     state $builder = Koha::SearchEngine::QueryBuilder->new(
213         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
214     state $searcher = Koha::SearchEngine::Search->new(
215         {index => $Koha::SearchEngine::AUTHORITIES_INDEX} );
216
217     my $search_query = $builder->build_authorities_query_compat(
218         \@marclist, \@and_or, \@excluding, \@operator,
219         \@value,    $self->{'auth_type'},
220         'AuthidAsc'
221     );
222     return $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
223 }
224
225 =head1 INTERNAL FUNCTIONS
226
227 =head2 _marc_format_handler
228
229 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
230 depending on the selected MARC flavour.
231
232 =cut
233
234 sub _marc_format_handler {
235     my $marcflavour = uc shift;
236     $marcflavour = 'MARC21' if ( $marcflavour eq 'NORMARC' );
237     my $pname = "C4::Heading::$marcflavour";
238     load $pname;
239     return $pname->new();
240 }
241
242 =head1 AUTHOR
243
244 Koha Development Team <http://koha-community.org/>
245
246 Galen Charlton <galen.charlton@liblime.com>
247
248 =cut
249
250 1;