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