Bug 19724: Add timestamp to biblio_metadata and deletedbiblio_metadata
[koha.git] / Koha / Util / MARC.pm
blob63477ea4e3e37cc658687363d22fd6ad57c9059e
1 package Koha::Util::MARC;
3 # Copyright 2013 C & P Bibliography Services
5 # This file is part of Koha.
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.
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.
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.
20 use Modern::Perl;
21 use MARC::Record;
23 =head1 NAME
25 Koha::Util::MARC - utility class with routines for working with MARC records
27 =head1 METHODS
29 =head2 createMergeHash
31 Create a hash to use when merging MARC records
33 =cut
35 sub createMergeHash {
36 my ( $record, $tagslib ) = @_;
38 return unless $record;
40 my @array;
41 my @fields = $record->fields();
43 foreach my $field (@fields) {
44 my $fieldtag = $field->tag();
45 if ( $fieldtag < 10 ) {
46 if (
47 !defined($tagslib)
48 || ( defined( $tagslib->{$fieldtag} )
49 && $tagslib->{$fieldtag}->{'@'}->{'tab'} >= 0 )
52 push @array, {
53 tag => $fieldtag,
54 key => _createKey(),
55 value => $field->data(),
59 else {
60 my @subfields = $field->subfields();
61 my @subfield_array;
62 foreach my $subfield (@subfields) {
63 if (
64 !defined($tagslib)
65 || ( defined $tagslib->{$fieldtag}
66 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }
67 && defined $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'}
68 && $tagslib->{$fieldtag}->{ @$subfield[0] }->{'tab'} >= 0 )
71 push @subfield_array, {
72 subtag => @$subfield[0],
73 subkey => _createKey(),
74 value => @$subfield[1],
80 if (
82 !defined($tagslib) || ( defined $tagslib->{$fieldtag}
83 && defined $tagslib->{$fieldtag}->{'tab'}
84 && $tagslib->{$fieldtag}->{'tab'} >= 0 )
86 && @subfield_array
89 push @array, {
90 tag => $fieldtag,
91 key => _createKey(),
92 indicator1 => $field->indicator(1),
93 indicator2 => $field->indicator(2),
94 subfield => [@subfield_array],
100 return [@array];
103 =head2 _createKey
105 Create a random value to set it into the input name
107 =cut
109 sub _createKey {
110 return int(rand(1000000));
113 =head2 getAuthorityAuthorizedHeading
115 Retrieve the authorized heading from a MARC authority record
117 =cut
119 sub getAuthorityAuthorizedHeading {
120 my ( $record, $schema ) = @_;
121 return unless ( ref $record eq 'MARC::Record' );
122 if ( $schema eq 'unimarc' ) {
124 # construct UNIMARC summary, that is quite different from MARC21 one
125 # accepted form
126 foreach my $field ( $record->field('2..') ) {
127 return $field->as_string('abcdefghijlmnopqrstuvwxyz');
130 else {
131 foreach my $field ( $record->field('1..') ) {
132 my $tag = $field->tag();
133 next if "152" eq $tag;
135 # FIXME - 152 is not a good tag to use
136 # in MARC21 -- purely local tags really ought to be
137 # 9XX
138 if ( $tag eq '100' ) {
139 return $field->as_string('abcdefghjklmnopqrstvxyz68');
141 elsif ( $tag eq '110' ) {
142 return $field->as_string('abcdefghklmnoprstvxyz68');
144 elsif ( $tag eq '111' ) {
145 return $field->as_string('acdefghklnpqstvxyz68');
147 elsif ( $tag eq '130' ) {
148 return $field->as_string('adfghklmnoprstvxyz68');
150 elsif ( $tag eq '148' ) {
151 return $field->as_string('abvxyz68');
153 elsif ( $tag eq '150' ) {
154 return $field->as_string('abvxyz68');
156 elsif ( $tag eq '151' ) {
157 return $field->as_string('avxyz68');
159 elsif ( $tag eq '155' ) {
160 return $field->as_string('abvxyz68');
162 elsif ( $tag eq '180' ) {
163 return $field->as_string('vxyz68');
165 elsif ( $tag eq '181' ) {
166 return $field->as_string('vxyz68');
168 elsif ( $tag eq '182' ) {
169 return $field->as_string('vxyz68');
171 elsif ( $tag eq '185' ) {
172 return $field->as_string('vxyz68');
174 else {
175 return $field->as_string();
179 return;