improve authority heading matching (partial fix for 1905)
[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 under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along with
17 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
18 # Suite 330, Boston, MA  02111-1307 USA
19
20 use strict;
21 use MARC::Record;
22 use MARC::Field;
23 use C4::Context;
24 use C4::Heading::MARC21;
25 use C4::Search;
26
27 our $VERSION = 3.00;
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);
37 my $thesaurus = $heading->thesaurus();
38 my $type = $heading->type();
39 my $display_heading = $heading->display();
40 my $search_string = $heading->search_string();
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 =over 4
52
53 my $heading = C4::Heading->new_from_bib_field($field[, $marc_flavour]);
54
55 =back
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 $marcflavour = @_ ? shift : C4::Context->preference('marcflavour');
73
74     my $marc_handler = _marc_format_handler($marcflavour);
75
76     my $tag = $field->tag();
77     return unless $marc_handler->valid_bib_heading_tag($tag);
78     my $self = {};
79    
80     ($self->{'auth_type'}, $self->{'subject_added_entry'}, $self->{'series_added_entry'}, $self->{'main_entry'},
81      $self->{'thesaurus'}, $self->{'search_form'}, $self->{'display_form'}) =
82         $marc_handler->parse_heading($field);
83
84     bless $self, $class;
85     return $self;
86 }
87
88 =head2 display_form
89
90 =over 4
91
92 my $display = $heading->display_form();
93
94 =back
95
96 Return the "canonical" display form of the heading.
97
98 =cut
99
100 sub display_form {
101     my $self = shift;
102     return $self->{'display_form'};
103 }
104
105 =head2 authorities
106
107 =over 4
108
109 my $authorities = $heading->authorities;
110
111 =back
112
113 Return a list of authority records for this 
114 heading.
115
116 =cut
117
118 sub authorities {
119     my $self = shift;
120     my $query = qq(Match-heading,ext="$self->{'search_form'}");
121     $query .= $self->_query_limiters();
122     my $results = SimpleSearch($query, "authorityserver");
123     return $results;
124 }
125
126 =head2 preferred_authorities
127
128 =over 4
129
130 my $preferred_authorities = $heading->preferred_authorities;
131
132 =back
133
134 Return a list of authority records for headings
135 that are a preferred form of the heading.
136
137 =cut
138
139 sub preferred_authorities {
140     my $self = shift;
141     my $query = "Match-heading-see-from,ext='$self->{'search_form'}'";
142     $query .= $self->_query_limiters();
143     my $results = SimpleSearch($query, "authorityserver");
144     return $results;
145 }
146
147 =head1 INTERNAL METHODS
148
149 =head2 _query_limiters
150
151 =cut
152
153 sub _query_limiters {
154     my $self = shift;
155
156     my $limiters = " AND at='$self->{'auth_type'}'";
157     if ($self->{'subject_added_entry'}) {
158         $limiters .= " AND Heading-use-subject-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
159         $limiters .= " AND Subject-heading-thesaurus=$self->{'thesaurus'}";
160     }
161     if ($self->{'series_added_entry'}) {
162         $limiters .= " AND Heading-use-series-added-entry=a"; # FIXME -- is this properly in C4::Heading::MARC21?
163     }
164     if (not $self->{'subject_added_entry'} and not $self->{'series_added_entry'}) {
165         $limiters .= " AND Heading-use-main-or-added-entry=a" # FIXME -- is this properly in C4::Heading::MARC21?
166     }
167     return $limiters;
168 }
169
170 =head1 INTERNAL FUNCTIONS
171
172 =head2 _marc_format_handler
173
174 Returns a C4::Heading::MARC21 or C4::Heading::UNIMARC object
175 depending on the selected MARC flavour.
176
177 =cut
178
179 sub _marc_format_handler {
180     my $marcflavour = shift;
181
182     if ($marcflavour eq 'UNIMARC') {
183         return C4::Heading::UNIMARC->new();
184     } else {
185         return C4::Heading::MARC21->new();
186     }
187
188 }
189
190 =head1 AUTHOR
191
192 Koha Developement team <info@koha.org>
193
194 Galen Charlton <galen.charlton@liblime.com>
195
196 =cut
197
198 1;