Bug 17600: Standardize our EXPORT_OK
[srvgit] / 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
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 =head1 NAME
23
24 Koha::Util::MARC - utility class with routines for working with MARC records
25
26 =head1 METHODS
27
28 =head2 createMergeHash
29
30 Create a hash to use when merging MARC records
31
32 =cut
33
34 sub createMergeHash {
35     my ( $record, $tagslib ) = @_;
36
37     return unless $record;
38
39     my @array;
40     my @fields = $record->fields();
41
42     foreach my $field (@fields) {
43         my $fieldtag = $field->tag();
44         if ( $fieldtag < 10 ) {
45             if (
46                 !defined($tagslib)
47                 || ( defined( $tagslib->{$fieldtag} )
48                     && $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
49               )
50             {
51                 push @array, {
52                     tag   => $fieldtag,
53                     key   => _createKey(),
54                     value => $field->data(),
55                 };
56             }
57         }
58         else {
59             my @subfields = $field->subfields();
60             my @subfield_array;
61             foreach my $subfield (@subfields) {
62                 if (
63                     !defined($tagslib)
64                     || (   defined $tagslib->{$fieldtag}
65                         && defined $tagslib->{$fieldtag}->{ @$subfield[0] }
66                         && defined $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'}
67                         && $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
68                   )
69                 {
70                     push @subfield_array, {
71                         subtag => @$subfield[0],
72                         subkey => _createKey(),
73                         value  => @$subfield[1],
74                     };
75                 }
76
77             }
78
79             if (
80                 (
81                     !defined($tagslib) || ( defined $tagslib->{$fieldtag}
82                         && defined $tagslib->{$fieldtag}->{'tab'}
83                         && $tagslib->{$fieldtag}->{'tab'} >= 0 )
84                 )
85                 && @subfield_array
86               )
87             {
88                 push @array, {
89                       tag        => $fieldtag,
90                       key        => _createKey(),
91                       indicator1 => $field->indicator(1),
92                       indicator2 => $field->indicator(2),
93                       subfield   => [@subfield_array],
94                   };
95             }
96
97         }
98     }
99     return [@array];
100 }
101
102 =head2 _createKey
103
104 Create a random value to set it into the input name
105
106 =cut
107
108 sub _createKey {
109     return int(rand(1000000));
110 }
111
112 =head2 getAuthorityAuthorizedHeading
113
114 Retrieve the authorized heading from a MARC authority record
115
116 =cut
117
118 sub getAuthorityAuthorizedHeading {
119     my ( $record, $schema ) = @_;
120     return unless ( ref $record eq 'MARC::Record' );
121     if ( $schema eq 'unimarc' ) {
122
123         # construct UNIMARC summary, that is quite different from MARC21 one
124         # accepted form
125         foreach my $field ( $record->field('2..') ) {
126             return $field->as_string('abcdefghijlmnopqrstuvwxyz');
127         }
128     }
129     else {
130         foreach my $field ( $record->field('1..') ) {
131             my $tag = $field->tag();
132             next if "152" eq $tag;
133
134             # FIXME - 152 is not a good tag to use
135             # in MARC21 -- purely local tags really ought to be
136             # 9XX
137             if ( $tag eq '100' ) {
138                 return $field->as_string('abcdefghjklmnopqrstvxyz68');
139             }
140             elsif ( $tag eq '110' ) {
141                 return $field->as_string('abcdefghklmnoprstvxyz68');
142             }
143             elsif ( $tag eq '111' ) {
144                 return $field->as_string('acdefghklnpqstvxyz68');
145             }
146             elsif ( $tag eq '130' ) {
147                 return $field->as_string('adfghklmnoprstvxyz68');
148             }
149             elsif ( $tag eq '148' ) {
150                 return $field->as_string('abvxyz68');
151             }
152             elsif ( $tag eq '150' ) {
153                 return $field->as_string('abvxyz68');
154             }
155             elsif ( $tag eq '151' ) {
156                 return $field->as_string('avxyz68');
157             }
158             elsif ( $tag eq '155' ) {
159                 return $field->as_string('abvxyz68');
160             }
161             elsif ( $tag eq '180' ) {
162                 return $field->as_string('vxyz68');
163             }
164             elsif ( $tag eq '181' ) {
165                 return $field->as_string('vxyz68');
166             }
167             elsif ( $tag eq '182' ) {
168                 return $field->as_string('vxyz68');
169             }
170             elsif ( $tag eq '185' ) {
171                 return $field->as_string('vxyz68');
172             }
173             else {
174                 return $field->as_string();
175             }
176         }
177     }
178     return;
179 }
180
181 =head2 set_marc_field
182
183     set_marc_field($record, $marcField, $value);
184
185 Set the value of $marcField to $value in $record. If the field exists, it will
186 be updated. If not, it will be created.
187
188 =head3 Parameters
189
190 =over 4
191
192 =item C<$record>
193
194 MARC::Record object
195
196 =item C<$marcField>
197
198 the MARC field to modify, a string in the form of 'XXX$y'
199
200 =item C<$value>
201
202 the value
203
204 =back
205
206 =cut
207
208 sub set_marc_field {
209     my ($record, $marcField, $value) = @_;
210
211     if ($marcField) {
212         my ($fieldTag, $subfieldCode) = split /\$/, $marcField;
213         if( !$subfieldCode ) {
214             warn "set_marc_field: Invalid marcField format: $marcField\n";
215             return;
216         }
217         my $field = $record->field($fieldTag);
218         if ($field) {
219             $field->update($subfieldCode => $value);
220         } else {
221             $field = MARC::Field->new($fieldTag, ' ', ' ',
222                 $subfieldCode => $value);
223             $record->append_fields($field);
224         }
225     }
226 }
227
228 1;