Bug 34532: Silence warns in Patroncard.pm
[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 Modern::Perl;
21
22 use MARC::Field;
23 use C4::Context;
24 use Module::Load qw( load );
25 use List::Util qw( none );
26
27 =head1 NAME
28
29 C4::Heading
30
31 =head1 SYNOPSIS
32
33  use C4::Heading;
34  my $heading = C4::Heading->new_from_field($field, $frameworkcode);
35  my $thesaurus = $heading->thesaurus();
36  my $type = $heading->type();
37  my $display_heading = $heading->display_form();
38  my $search_form = $heading->search_form();
39
40 =head1 DESCRIPTION
41
42 C<C4::Heading> implements a simple class to representing
43 headings found in bibliographic and authority records.
44
45 =head1 METHODS
46
47 =head2 new_from_field
48
49   my $heading = C4::Heading->new_from_field($field, $frameworkcode, [, $auth]);
50
51 Given a C<MARC::Field> object containing a heading from a 
52 bib record, create a C<C4::Heading> object.
53
54 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
55
56 If the MARC field supplied is not a valid heading, undef
57 is returned.
58
59 =cut
60
61 sub new_from_field {
62     my $class         = shift;
63     my $field         = shift;
64     my $frameworkcode = shift; #FIXME this is not used?
65     my $auth          = shift;
66     my $marcflavour   = C4::Context->preference('marcflavour');
67     my $marc_handler = _marc_format_handler($marcflavour);
68
69     my $tag = $field->tag();
70     return unless $marc_handler->valid_heading_tag( $tag, $frameworkcode, $auth );
71     my $self = {};
72
73     $self->{'field'} = $field;
74     (
75         $self->{'auth_type'},   $self->{'thesaurus'},
76         $self->{'search_form'}, $self->{'display_form'},
77         $self->{'match_type'}
78     ) = $marc_handler->parse_heading($field, $auth );
79
80     bless $self, $class;
81     return $self;
82 }
83
84 =head2 auth_type
85
86   my $auth_type = $heading->auth_type();
87
88 Return the auth_type of the heading.
89
90 =cut
91
92 sub auth_type {
93     my $self = shift;
94     return $self->{'auth_type'};
95 }
96
97 =head2 field
98
99   my $field = $heading->field();
100
101 Return the MARC::Field the heading is based on.
102
103 =cut
104
105 sub field {
106     my $self = shift;
107     return $self->{'field'};
108 }
109
110 =head2 display_form
111
112   my $display = $heading->display_form();
113
114 Return the "canonical" display form of the heading.
115
116 =cut
117
118 sub display_form {
119     my $self = shift;
120     return $self->{'display_form'};
121 }
122
123 =head2 search_form
124
125   my $search_form = $heading->search_form();
126
127 Return the "canonical" search form of the heading.
128
129 =cut
130
131 sub search_form {
132     my $self = shift;
133     return $self->{'search_form'};
134 }
135
136 =head2 authorities
137
138   my $authorities = $heading->authorities([$skipmetadata]);
139
140 Return a list of authority records for this 
141 heading. If passed a true value for $skipmetadata,
142 SearchAuthorities will return only authids.
143
144 =cut
145
146 sub authorities {
147     my $self         = shift;
148     my $skipmetadata = shift;
149     my ( $results, $total ) = _search( $self, 'match-heading', $skipmetadata );
150     return $results;
151 }
152
153 =head2 preferred_authorities
154
155   my $preferred_authorities = $heading->preferred_authorities;
156
157 Return a list of authority records for headings
158 that are a preferred form of the heading.
159
160 =cut
161
162 sub preferred_authorities {
163     my $self = shift;
164     my $skipmetadata = shift || undef;
165     my ( $results, $total ) = _search( 'see-from', $skipmetadata );
166     return $results;
167 }
168
169 =head2 valid_heading_subfield
170
171     if (C4::Heading::valid_heading_subfield('100', 'e', '')) ...
172
173 Check if the given subfield is valid for the given field.
174
175 =cut
176
177 sub valid_heading_subfield {
178     my $tag           = shift;
179     my $subfield      = shift;
180     my $marcflavour   = C4::Context->preference('marcflavour');
181     my $auth          = shift;
182
183     my $marc_handler = _marc_format_handler($marcflavour);
184     return $marc_handler->valid_heading_subfield( $tag, $subfield, $auth );
185 }
186
187 =head1 INTERNAL METHODS
188
189 =head2 _search
190
191 =cut
192
193 sub _search {
194     my $self         = shift;
195     my $index        = shift || undef;
196     my $skipmetadata = shift || undef;
197     my $thesaurus = $self->{thesaurus};
198     my $subject_heading_thesaurus = '';
199     my @marclist;
200     my @and_or;
201     my @excluding = [];
202     my @operator;
203     my @value;
204
205     my $check_thesaurus = C4::Context->preference('LinkerConsiderThesaurus');
206
207     # FIXME: We specify values for @and_or and @excluding
208     # but these fields are not used anywhere and should be removed
209     if ($index) {
210         push @marclist, $index;
211         push @and_or,   'AND';
212         push @operator, $self->{'match_type'};
213         push @value,    $self->{'search_form'};
214     }
215
216     if ( $check_thesaurus && $thesaurus ) {
217         push @marclist, 'thesaurus';
218         push @and_or, 'and';
219         push @excluding, '';
220         push @operator, 'is';
221         push @value, $thesaurus;
222     }
223
224     require Koha::SearchEngine::QueryBuilder;
225     require Koha::SearchEngine::Search;
226
227     # Use state variables to avoid recreating the objects every time.
228     # With Elasticsearch this also avoids creating a massive amount of
229     # ES connectors that would eventually run out of file descriptors.
230     state $builder = Koha::SearchEngine::QueryBuilder->new(
231         { index => $Koha::SearchEngine::AUTHORITIES_INDEX } );
232     state $searcher = Koha::SearchEngine::Search->new(
233         {index => $Koha::SearchEngine::AUTHORITIES_INDEX} );
234
235     my $search_query = $builder->build_authorities_query_compat(
236         \@marclist, \@and_or, \@excluding, \@operator,
237         \@value,    $self->{'auth_type'},
238         'AuthidAsc'
239     );
240
241     my ( $matched_auths, $total ) = $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
242     # Some auth records may not contain the 040$f to specify their source
243     # This is legal, so we do a fallback search
244     if (
245            $check_thesaurus
246         && !$total
247         && $thesaurus
248         && none { $_ eq $thesaurus } (
249             'lcsh',         'lcac', 'mesh', 'nal',
250             'notspecified', 'cash', 'rvm',  'sears',
251             'aat'
252         )
253       )
254     {
255         pop @value;
256         push @value, 'notdefined';
257         $search_query =
258           $builder->build_authorities_query_compat( \@marclist, \@and_or,
259             \@excluding, \@operator, \@value, $self->{'auth_type'},
260             'AuthidAsc' );
261         ( $matched_auths, $total ) =
262           $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
263     }
264     return ( $matched_auths, $total );
265
266 }
267
268 =head1 INTERNAL FUNCTIONS
269
270 =head2 _marc_format_handler
271
272 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
273 depending on the selected MARC flavour.
274
275 =cut
276
277 sub _marc_format_handler {
278     my $marcflavour = uc shift;
279     my $pname = "C4::Heading::$marcflavour";
280     load $pname;
281     return $pname->new();
282 }
283
284 =head1 AUTHOR
285
286 Koha Development Team <http://koha-community.org/>
287
288 Galen Charlton <galen.charlton@liblime.com>
289
290 =cut
291
292 1;