Bug 21826: Use subfield map in LinkBibHeadingsToAuthorities
[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 strict;
23 use warnings;
24 use 5.010;
25
26 use MARC::Record;
27 use MARC::Field;
28 use C4::Context;
29 use Module::Load;
30 use Carp;
31
32
33 =head1 NAME
34
35 C4::Heading
36
37 =head1 SYNOPSIS
38
39  use C4::Heading;
40  my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode);
41  my $thesaurus = $heading->thesaurus();
42  my $type = $heading->type();
43  my $display_heading = $heading->display_form();
44  my $search_form = $heading->search_form();
45
46 =head1 DESCRIPTION
47
48 C<C4::Heading> implements a simple class to representing
49 headings found in bibliographic and authority records.
50
51 =head1 METHODS
52
53 =head2 new_from_bib_field
54
55   my $heading = C4::Heading->new_from_bib_field($field, $frameworkcode, [, $marc_flavour]);
56
57 Given a C<MARC::Field> object containing a heading from a 
58 bib record, create a C<C4::Heading> object.
59
60 The optional second parameter is the MARC flavour (i.e., MARC21
61 or UNIMARC); if this parameter is not supplied, it is
62 taken from the Koha application context.
63
64 If the MARC field supplied is not a valid heading, undef
65 is returned.
66
67 =cut
68
69 sub new_from_bib_field {
70     my $class         = shift;
71     my $field         = shift;
72     my $frameworkcode = shift;
73     my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
74
75     my $marc_handler = _marc_format_handler($marcflavour);
76
77     my $tag = $field->tag();
78     return unless $marc_handler->valid_bib_heading_tag( $tag, $frameworkcode );
79     my $self = {};
80
81     $self->{'field'} = $field;
82     (
83         $self->{'auth_type'},   $self->{'thesaurus'},
84         $self->{'search_form'}, $self->{'display_form'},
85         $self->{'match_type'}
86     ) = $marc_handler->parse_heading($field);
87
88     bless $self, $class;
89     return $self;
90 }
91
92 =head2 auth_type
93
94   my $auth_type = $heading->auth_type();
95
96 Return the auth_type of the heading.
97
98 =cut
99
100 sub auth_type {
101     my $self = shift;
102     return $self->{'auth_type'};
103 }
104
105 =head2 field
106
107   my $field = $heading->field();
108
109 Return the MARC::Field the heading is based on.
110
111 =cut
112
113 sub field {
114     my $self = shift;
115     return $self->{'field'};
116 }
117
118 =head2 display_form
119
120   my $display = $heading->display_form();
121
122 Return the "canonical" display form of the heading.
123
124 =cut
125
126 sub display_form {
127     my $self = shift;
128     return $self->{'display_form'};
129 }
130
131 =head2 search_form
132
133   my $search_form = $heading->search_form();
134
135 Return the "canonical" search form of the heading.
136
137 =cut
138
139 sub search_form {
140     my $self = shift;
141     return $self->{'search_form'};
142 }
143
144 =head2 authorities
145
146   my $authorities = $heading->authorities([$skipmetadata]);
147
148 Return a list of authority records for this 
149 heading. If passed a true value for $skipmetadata,
150 SearchAuthorities will return only authids.
151
152 =cut
153
154 sub authorities {
155     my $self         = shift;
156     my $skipmetadata = shift;
157     my ( $results, $total ) = _search( $self, 'match-heading', $skipmetadata );
158     return $results;
159 }
160
161 =head2 preferred_authorities
162
163   my $preferred_authorities = $heading->preferred_authorities;
164
165 Return a list of authority records for headings
166 that are a preferred form of the heading.
167
168 =cut
169
170 sub preferred_authorities {
171     my $self = shift;
172     my $skipmetadata = shift || undef;
173     my ( $results, $total ) = _search( 'see-from', $skipmetadata );
174     return $results;
175 }
176
177 =head2 valid_bib_heading_subfield
178
179     if (C4::Heading::valid_bib_heading_subfield('100', 'e', '')) ...
180
181 =cut
182
183 sub valid_bib_heading_subfield {
184     my $tag           = shift;
185     my $subfield      = shift;
186     my $marcflavour   = @_ ? shift : C4::Context->preference('marcflavour');
187
188     my $marc_handler = _marc_format_handler($marcflavour);
189
190     return $marc_handler->valid_bib_heading_subfield( $tag, $subfield );
191 }
192
193 =head1 INTERNAL METHODS
194
195 =head2 _search
196
197 =cut
198
199 sub _search {
200     my $self         = shift;
201     my $index        = shift || undef;
202     my $skipmetadata = shift || undef;
203     my @marclist;
204     my @and_or;
205     my @excluding = [];
206     my @operator;
207     my @value;
208
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 ($self->{'thesaurus'}) {
217     #        push @marclist, 'thesaurus';
218     #        push @and_or, 'and';
219     #        push @excluding, '';
220     #        push @operator, 'is';
221     #        push @value, $self->{'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     return $searcher->search_auth_compat( $search_query, 0, 20, $skipmetadata );
241 }
242
243 =head1 INTERNAL FUNCTIONS
244
245 =head2 _marc_format_handler
246
247 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
248 depending on the selected MARC flavour.
249
250 =cut
251
252 sub _marc_format_handler {
253     my $marcflavour = uc shift;
254     $marcflavour = 'MARC21' if ( $marcflavour eq 'NORMARC' );
255     my $pname = "C4::Heading::$marcflavour";
256     load $pname;
257     return $pname->new();
258 }
259
260 =head1 AUTHOR
261
262 Koha Development Team <http://koha-community.org/>
263
264 Galen Charlton <galen.charlton@liblime.com>
265
266 =cut
267
268 1;