authorities - new module - C4::Heading
[koha.git] / C4 / Heading / MARC21.pm
1 package C4::Heading::MARC21;
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
24 our $VERSION = 3.00;
25
26 =head1 NAME
27
28 C4::Heading::MARC21
29
30 =head1 SYNOPSIS
31
32 use C4::Heading::MARC21;
33
34 =head1 DESCRIPTION
35
36 This is an internal helper class used by
37 C<C4::Heading> to parse headings data from
38 MARC21 records.  Object of this type
39 do not carry data, instead, they only
40 dispatch functions.
41
42 =head1 DATA STRUCTURES
43
44 FIXME - this should be moved to a configuration file.
45
46 =head2 bib_heading_fields
47
48 =cut
49
50 my $bib_heading_fields = {
51     '100' => { auth_type => 'PERSO_NAME', subfields => 'abcdefghjklmnopqrst', main_entry => 1 },
52     '110' => { auth_type => 'CORPO_NAME', subfields => 'abcdefghklmnoprst', main_entry => 1 },
53     '111' => { auth_type => 'MEETI_NAME', subfields => 'acdefghjklnpqst', main_entry => 1 },
54     '130' => { auth_type => 'UNIF_TITLE', subfields => 'adfghklmnoprst', main_entry => 1 },
55     '600' => { auth_type => 'PERSO_NAME', subfields => 'abcdefghjklmnopqrstvxyz', subject => 1 },
56     '610' => { auth_type => 'CORPO_NAME', subfields => 'abcdefghklmnoprstvxyz', subject => 1 },
57     '611' => { auth_type => 'MEETI_NAME', subfields => 'acdefghjklnpqstvxyz', subject => 1 },
58     '630' => { auth_type => 'UNIF_TITLE', subfields => 'adfghklmnoprstvxyz', subject => 1 },
59     '648' => { auth_type => 'CHRON_TERM', subfields => 'avxyz', subject => 1 },
60     '650' => { auth_type => 'TOPIC_TERM', subfields => 'abvxyz', subject => 1 },
61     '651' => { auth_type => 'GEOGR_NAME', subfields => 'avxyz', subject => 1 },
62     '655' => { auth_type => 'GENRE/FORM', subfields => 'avxyz', subject => 1 },
63     '700' => { auth_type => 'PERSO_NAME', subfields => 'abcdefghjklmnopqrst' },
64     '710' => { auth_type => 'CORPO_NAME', subfields => 'abcdefghklmnoprst' },
65     '711' => { auth_type => 'MEETI_NAME', subfields => 'acdefghjklnpqst' },
66     '730' => { auth_type => 'UNIF_TITLE', subfields => 'adfghklmnoprst' },
67     '800' => { auth_type => 'PERSO_NAME', subfields => 'abcdefghjklmnopqrst', series => 1 },
68     '810' => { auth_type => 'CORPO_NAME', subfields => 'abcdefghklmnoprst', series => 1 },
69     '811' => { auth_type => 'MEETI_NAME', subfields => 'acdefghjklnpqst', series => 1 },
70     '830' => { auth_type => 'UNIF_TITLE', subfields => 'adfghklmnoprst', series => 1 },
71 };
72
73 =head2 subdivisions
74
75 =cut
76
77 my %subdivisions = (
78     'v' => 'formsubdiv',
79     'x' => 'generalsubdiv',
80     'y' => 'chronologicalsubdiv',
81     'z' => 'geographicsubdiv',
82 );
83
84 =head1 METHODS
85
86 =head2 new
87
88 =over 4
89
90 my $marc_handler = C4::Heading::MARC21->new();
91
92 =back
93
94 =cut
95
96 sub new {
97     my $class = shift;
98     return bless {}, $class;
99 }
100
101 =head2 valid_bib_heading_tag
102
103 =cut
104
105 sub valid_bib_heading_tag {
106     my $self = shift;
107     my $tag = shift;
108
109     if (exists $bib_heading_fields->{$tag}) {
110         return 1
111     } else {
112         return 0;
113     }
114
115 }
116
117 =head2 parse_heading
118
119 =cut
120
121 sub parse_heading {
122     my $self = shift;
123     my $field = shift;
124
125     my $tag = $field->tag;
126     my $field_info = $bib_heading_fields->{$tag};
127
128     my $auth_type = $field_info->{'auth_type'};
129     my $subject = $field_info->{'subject'} ? 1 : 0;
130     my $series = $field_info->{'series'} ? 1 : 0;
131     my $main_entry = $field_info->{'main_entry'} ? 1 : 0;
132     my $thesaurus = $subject ? _get_subject_thesaurus($field) : "lcsh"; # use 'lcsh' for names, UT, etc.
133     my $search_heading = _get_search_heading($field, $field_info->{'subfields'});
134     my $display_heading = _get_display_heading($field, $field_info->{'subfields'});
135
136     return ($auth_type, $subject, $series, $main_entry, $thesaurus, $search_heading, $display_heading);
137 }
138
139 =head1 INTERNAL FUNCTIONS
140
141 =head2 _get_subject_thesaurus
142
143 =cut
144
145 sub _get_subject_thesaurus {
146     my $field = shift;
147     my $ind2 = $field->indicator(2);
148
149     my $thesaurus = "notdefined";
150     if ($ind2 eq '0') {
151         $thesaurus = "lcsh";
152     } elsif ($ind2 eq '1') {
153         $thesaurus = "lcac";
154     } elsif ($ind2 eq '2') {
155         $thesaurus = "mesh";
156     } elsif ($ind2 eq '3') {
157         $thesaurus = "nal";
158     } elsif ($ind2 eq '4') {
159         $thesaurus = "notspecified";
160     } elsif ($ind2 eq '5') {
161         $thesaurus = "cash";
162     } elsif ($ind2 eq '6') {
163         $thesaurus = "rvm";
164     } elsif ($ind2 eq '7') {
165         my $sf2 = $field->subfield('2');
166         $thesaurus = $sf2 if defined($sf2);
167     }
168
169     return $thesaurus;
170 }
171
172 =head2 _get_search_heading
173
174 =cut
175
176 sub _get_search_heading {
177     my $field = shift;
178     my $subfields = shift;
179
180     my $heading = "";
181     my @subfields = $field->subfields();
182     my $first = 1;
183     for (my $i = 0; $i <= $#subfields; $i++) {
184         my $code = $subfields[$i]->[0];
185         my $value = $subfields[$i]->[1];
186         next unless $subfields =~ qr/$code/;
187         if ($first) {
188             $first = 0;
189             $heading = $value;
190         } else {
191             if (exists $subdivisions{$code}) {
192                 $heading .= " $subdivisions{$code} $value";
193             } else {
194                 $heading .= " $value";
195             }
196         }
197     }
198     return $heading;
199 }
200
201 =head2 _get_display_heading
202
203 =cut
204
205 sub _get_display_heading {
206     my $field = shift;
207     my $subfields = shift;
208
209     my $heading = "";
210     my @subfields = $field->subfields();
211     my $first = 1;
212     for (my $i = 0; $i <= $#subfields; $i++) {
213         my $code = $subfields[$i]->[0];
214         my $value = $subfields[$i]->[1];
215         next unless $subfields =~ qr/$code/;
216         if ($first) {
217             $first = 0;
218             $heading = $value;
219         } else {
220             if (exists $subdivisions{$code}) {
221                 $heading .= "--$value";
222             } else {
223                 $heading .= " $value";
224             }
225         }
226     }
227     return $heading;
228 }
229
230 =head1 AUTHOR
231
232 Koha Developement team <info@koha.org>
233
234 Galen Charlton <galen.charlton@liblime.com>
235
236 =cut
237
238 1;