Bug 5202: merge authorities from the authority file and reservoir
[koha.git] / Koha / Util / MARC.pm
1 package Koha::Util::MARC;
2
3 # Copyright 2013 C & P Bibliography Services
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 3 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
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21 use MARC::Record;
22
23 =head1 NAME
24
25 Koha::Util::MARC - utility class with routines for working with MARC records
26
27 =head1 METHODS
28
29 =head2 createMergeHash
30
31 Create a hash to use when merging MARC records
32
33 =cut
34
35 sub createMergeHash {
36     my ( $record, $tagslib ) = @_;
37
38     return unless $record;
39
40     my @array;
41     my @fields = $record->fields();
42
43     foreach my $field (@fields) {
44         my $fieldtag = $field->tag();
45         if ( $fieldtag < 10 ) {
46             if ( !defined($tagslib)
47                 || $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
48             {
49                 push @array,
50                   {
51                     field => [
52                         {
53                             tag   => $fieldtag,
54                             key   => _createKey(),
55                             value => $field->data(),
56                         }
57                     ]
58                   };
59             }
60         }
61         else {
62             my @subfields = $field->subfields();
63             my @subfield_array;
64             foreach my $subfield (@subfields) {
65                 if ( !defined($tagslib)
66                     || $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
67                 {
68                     push @subfield_array,
69                       {
70                         subtag => @$subfield[0],
71                         subkey => _createKey(),
72                         value  => @$subfield[1],
73                       };
74                 }
75
76             }
77
78             if ( !defined($tagslib) || @subfield_array )
79             {
80                 push @array,
81                   {
82                     field => [
83                         {
84                             tag        => $fieldtag,
85                             key        => _createKey(),
86                             indicator1 => $field->indicator(1),
87                             indicator2 => $field->indicator(2),
88                             subfield   => [@subfield_array],
89                         }
90                     ]
91                   };
92             }
93
94         }
95     }
96     return [@array];
97 }
98
99 =head2 _createKey
100
101 Create a random value to set it into the input name
102
103 =cut
104
105 sub _createKey {
106     return int(rand(1000000));
107 }
108
109 =head2 getAuthorityAuthorizedHeading
110
111 Retrieve the authorized heading from a MARC authority record
112
113 =cut
114
115 sub getAuthorityAuthorizedHeading {
116     my ( $record, $schema ) = @_;
117     return unless ( ref $record eq 'MARC::Record' );
118     if ( $schema eq 'unimarc' ) {
119
120         # construct UNIMARC summary, that is quite different from MARC21 one
121         # accepted form
122         foreach my $field ( $record->field('2..') ) {
123             return $field->as_string('abcdefghijlmnopqrstuvwxyz');
124         }
125     }
126     else {
127         foreach my $field ( $record->field('1..') ) {
128             my $tag = $field->tag();
129             next if "152" eq $tag;
130
131             # FIXME - 152 is not a good tag to use
132             # in MARC21 -- purely local tags really ought to be
133             # 9XX
134             if ( $tag eq '100' ) {
135                 return $field->as_string('abcdefghjklmnopqrstvxyz68');
136             }
137             elsif ( $tag eq '110' ) {
138                 return $field->as_string('abcdefghklmnoprstvxyz68');
139             }
140             elsif ( $tag eq '111' ) {
141                 return $field->as_string('acdefghklnpqstvxyz68');
142             }
143             elsif ( $tag eq '130' ) {
144                 return $field->as_string('adfghklmnoprstvxyz68');
145             }
146             elsif ( $tag eq '148' ) {
147                 return $field->as_string('abvxyz68');
148             }
149             elsif ( $tag eq '150' ) {
150                 return $field->as_string('abvxyz68');
151             }
152             elsif ( $tag eq '151' ) {
153                 return $field->as_string('avxyz68');
154             }
155             elsif ( $tag eq '155' ) {
156                 return $field->as_string('abvxyz68');
157             }
158             elsif ( $tag eq '180' ) {
159                 return $field->as_string('vxyz68');
160             }
161             elsif ( $tag eq '181' ) {
162                 return $field->as_string('vxyz68');
163             }
164             elsif ( $tag eq '182' ) {
165                 return $field->as_string('vxyz68');
166             }
167             elsif ( $tag eq '185' ) {
168                 return $field->as_string('vxyz68');
169             }
170             else {
171                 return $field->as_string();
172             }
173         }
174     }
175     return;
176 }
177
178 1;