8 |
tag search display |
tag search display |
9 |
marc marc_indicators marc_repeatable_subfield |
marc marc_indicators marc_repeatable_subfield |
10 |
marc_compose marc_leader |
marc_compose marc_leader |
11 |
|
marc_duplicate marc_remove |
12 |
|
|
13 |
rec1 rec2 rec |
rec1 rec2 rec |
14 |
regex prefix suffix surround |
regex prefix suffix surround |
23 |
#use base qw/WebPAC::Common/; |
#use base qw/WebPAC::Common/; |
24 |
use Data::Dump qw/dump/; |
use Data::Dump qw/dump/; |
25 |
use Encode qw/from_to/; |
use Encode qw/from_to/; |
26 |
|
use Storable qw/dclone/; |
27 |
|
|
28 |
# debugging warn(s) |
# debugging warn(s) |
29 |
my $debug = 0; |
my $debug = 0; |
35 |
|
|
36 |
=head1 VERSION |
=head1 VERSION |
37 |
|
|
38 |
Version 0.09 |
Version 0.10 |
39 |
|
|
40 |
=cut |
=cut |
41 |
|
|
42 |
our $VERSION = '0.09'; |
our $VERSION = '0.10'; |
43 |
|
|
44 |
=head1 SYNOPSIS |
=head1 SYNOPSIS |
45 |
|
|
119 |
|
|
120 |
=cut |
=cut |
121 |
|
|
122 |
my ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); |
123 |
|
my ($marc_record_offset, $marc_fetch_offset) = (0, 0); |
124 |
|
|
125 |
sub _get_ds { |
sub _get_ds { |
126 |
return $out; |
return $out; |
137 |
sub _clean_ds { |
sub _clean_ds { |
138 |
my $a = {@_}; |
my $a = {@_}; |
139 |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); |
140 |
|
($marc_record_offset, $marc_fetch_offset) = (0,0); |
141 |
$marc_encoding = $a->{marc_encoding}; |
$marc_encoding = $a->{marc_encoding}; |
142 |
} |
} |
143 |
|
|
186 |
which might not be what you have in mind. If you need repeatable subfield, |
which might not be what you have in mind. If you need repeatable subfield, |
187 |
define it using C<marc_repeatable_subfield> like this: |
define it using C<marc_repeatable_subfield> like this: |
188 |
|
|
189 |
.... |
marc_repeatable_subfield('900','a'); |
190 |
|
marc('900','a', rec('200','a') ); |
191 |
|
marc('900','b', rec('200','b') ); |
192 |
|
marc('900','c', rec('200','c') ); |
193 |
|
|
194 |
|
will create: |
195 |
|
|
196 |
|
900a-1 900a-2 900a-3 900b-1 900c-1 |
197 |
|
900b-2 |
198 |
|
|
199 |
|
There is also support for returning next or specific using: |
200 |
|
|
201 |
|
while (my $mf = WebPAC::Normalize:_get_marc_fields( fetch_next => 1 ) ) { |
202 |
|
# do something with $mf |
203 |
|
} |
204 |
|
|
205 |
|
will always return fields from next MARC record or |
206 |
|
|
207 |
|
my $mf = WebPAC::Normalize::_get_marc_fields( offset => 42 ); |
208 |
|
|
209 |
|
will return 42th copy record (if it exists). |
210 |
|
|
211 |
=cut |
=cut |
212 |
|
|
213 |
sub _get_marc_fields { |
sub _get_marc_fields { |
214 |
|
|
215 |
return if (! $marc_record || ref($marc_record) ne 'ARRAY' || $#{ $marc_record } < 0); |
my $arg = {@_}; |
216 |
|
warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); |
217 |
|
my $offset = $marc_fetch_offset; |
218 |
|
if ($arg->{offset}) { |
219 |
|
$offset = $arg->{offset}; |
220 |
|
} elsif($arg->{fetch_next}) { |
221 |
|
$marc_fetch_offset++; |
222 |
|
} |
223 |
|
|
224 |
|
return if (! $marc_record || ref($marc_record) ne 'ARRAY'); |
225 |
|
|
226 |
|
warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); |
227 |
|
|
228 |
|
my $marc_rec = $marc_record->[ $offset ]; |
229 |
|
|
230 |
|
warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); |
231 |
|
|
232 |
|
return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); |
233 |
|
|
234 |
# first, sort all existing fields |
# first, sort all existing fields |
235 |
# XXX might not be needed, but modern perl might randomize elements in hash |
# XXX might not be needed, but modern perl might randomize elements in hash |
236 |
my @sorted_marc_record = sort { |
my @sorted_marc_record = sort { |
237 |
$a->[0] . $a->[3] cmp $b->[0] . $b->[3] |
$a->[0] . ( $a->[3] || '' ) cmp $b->[0] . ( $b->[3] || '') |
238 |
} @{ $marc_record }; |
} @{ $marc_rec }; |
239 |
|
|
240 |
@sorted_marc_record = @{ $marc_record }; ### FIXME disable sorting |
@sorted_marc_record = @{ $marc_rec }; ### FIXME disable sorting |
241 |
|
|
242 |
# output marc fields |
# output marc fields |
243 |
my @m; |
my @m; |
244 |
|
|
245 |
# count unique field-subfields (used for offset when walking to next subfield) |
# count unique field-subfields (used for offset when walking to next subfield) |
246 |
my $u; |
my $u; |
247 |
map { $u->{ $_->[0] . $_->[3] }++ } @sorted_marc_record; |
map { $u->{ $_->[0] . ( $_->[3] || '') }++ } @sorted_marc_record; |
248 |
|
|
249 |
if ($debug) { |
if ($debug) { |
250 |
warn "## marc_repeatable_subfield ", dump( $marc_repeatable_subfield ), $/; |
warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); |
251 |
warn "## marc_record ", dump( $marc_record ), $/; |
warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; |
252 |
warn "## sorted_marc_record ", dump( \@sorted_marc_record ), $/; |
warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; |
253 |
warn "## subfield count ", dump( $u ), $/; |
warn "## subfield count = ", dump( $u ), $/; |
254 |
} |
} |
255 |
|
|
256 |
my $len = $#sorted_marc_record; |
my $len = $#sorted_marc_record; |
268 |
# mark it visited |
# mark it visited |
269 |
$visited->{$i}++; |
$visited->{$i}++; |
270 |
|
|
271 |
my $row = $sorted_marc_record[$i]; |
my $row = dclone( $sorted_marc_record[$i] ); |
272 |
|
|
273 |
# field and subfield which is key for |
# field and subfield which is key for |
274 |
# marc_repeatable_subfield and u |
# marc_repeatable_subfield and u |
275 |
my $fsf = $row->[0] . $row->[3]; |
my $fsf = $row->[0] . ( $row->[3] || '' ); |
276 |
|
|
277 |
if ($debug > 1) { |
if ($debug > 1) { |
278 |
|
|
340 |
sub _debug { |
sub _debug { |
341 |
my $l = shift; |
my $l = shift; |
342 |
return $debug unless defined($l); |
return $debug unless defined($l); |
343 |
warn "debug level $l" if ($l > 0); |
warn "debug level $l",$/ if ($l > 0); |
344 |
$debug = $l; |
$debug = $l; |
345 |
} |
} |
346 |
|
|
422 |
Save value for MARC field |
Save value for MARC field |
423 |
|
|
424 |
marc('900','a', rec('200','a') ); |
marc('900','a', rec('200','a') ); |
425 |
|
marc('001', rec('000') ); |
426 |
|
|
427 |
=cut |
=cut |
428 |
|
|
430 |
my $f = shift or die "marc needs field"; |
my $f = shift or die "marc needs field"; |
431 |
die "marc field must be numer" unless ($f =~ /^\d+$/); |
die "marc field must be numer" unless ($f =~ /^\d+$/); |
432 |
|
|
433 |
my $sf = shift or die "marc needs subfield"; |
my $sf; |
434 |
|
if ($f >= 10) { |
435 |
|
$sf = shift or die "marc needs subfield"; |
436 |
|
} |
437 |
|
|
438 |
foreach (@_) { |
foreach (@_) { |
439 |
my $v = $_; # make var read-write for Encode |
my $v = $_; # make var read-write for Encode |
440 |
next unless (defined($v) && $v !~ /^\s*$/); |
next unless (defined($v) && $v !~ /^\s*$/); |
441 |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
442 |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); |
443 |
push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ]; |
if (defined $sf) { |
444 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $i1, $i2, $sf => $v ]; |
445 |
|
} else { |
446 |
|
push @{ $marc_record->[ $marc_record_offset ] }, [ $f, $v ]; |
447 |
|
} |
448 |
} |
} |
449 |
} |
} |
450 |
|
|
506 |
|
|
507 |
while (@_) { |
while (@_) { |
508 |
my $sf = shift or die "marc_compose $f needs subfield"; |
my $sf = shift or die "marc_compose $f needs subfield"; |
509 |
my $v = shift or die "marc_compose $f needs value for subfield $sf"; |
my $v = shift; |
510 |
|
|
511 |
next unless (defined($v) && $v !~ /^\s*$/); |
next unless (defined($v) && $v !~ /^\s*$/); |
512 |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); |
513 |
push @$m, ( $sf, $v ); |
push @$m, ( $sf, $v ); |
514 |
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ) if ($debug > 1); |
warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); |
515 |
} |
} |
516 |
|
|
517 |
warn "## marc_compose(d) ", dump( $m ) if ($debug > 1); |
warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); |
518 |
|
|
519 |
push @{ $marc_record }, $m; |
push @{ $marc_record->[ $marc_record_offset ] }, $m if ($#{$m} > 2); |
520 |
} |
} |
521 |
|
|
522 |
|
=head2 marc_duplicate |
523 |
|
|
524 |
|
Generate copy of current MARC record and continue working on copy |
525 |
|
|
526 |
|
marc_duplicate(); |
527 |
|
|
528 |
|
Copies can be accessed using C<< _get_marc_fields( fetch_next => 1 ) >> or |
529 |
|
C<< _get_marc_fields( offset => 42 ) >>. |
530 |
|
|
531 |
|
=cut |
532 |
|
|
533 |
|
sub marc_duplicate { |
534 |
|
my $m = $marc_record->[ -1 ]; |
535 |
|
die "can't duplicate record which isn't defined" unless ($m); |
536 |
|
push @{ $marc_record }, dclone( $m ); |
537 |
|
warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); |
538 |
|
$marc_record_offset = $#{ $marc_record }; |
539 |
|
warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); |
540 |
|
} |
541 |
|
|
542 |
|
=head2 marc_remove |
543 |
|
|
544 |
|
Remove some field or subfield from MARC record. |
545 |
|
|
546 |
|
marc_remove('200'); |
547 |
|
marc_remove('200','a'); |
548 |
|
|
549 |
|
This will erase field C<200> or C<200^a> from current MARC record. |
550 |
|
|
551 |
|
This is useful after calling C<marc_duplicate> or on it's own (but, you |
552 |
|
should probably just remove that subfield definition if you are not |
553 |
|
using C<marc_duplicate>). |
554 |
|
|
555 |
|
FIXME: support fields < 10. |
556 |
|
|
557 |
|
=cut |
558 |
|
|
559 |
|
sub marc_remove { |
560 |
|
my ($f, $sf) = @_; |
561 |
|
|
562 |
|
die "marc_remove needs record number" unless defined($f); |
563 |
|
|
564 |
|
my $marc = $marc_record->[ $marc_record_offset ]; |
565 |
|
|
566 |
|
warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); |
567 |
|
|
568 |
|
foreach my $i ( 0 .. $#{ $marc } ) { |
569 |
|
last unless (defined $marc->[$i]); |
570 |
|
warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); |
571 |
|
if ($marc->[$i]->[0] eq $f) { |
572 |
|
if (! defined $sf) { |
573 |
|
# remove whole field |
574 |
|
splice @$marc, $i, 1; |
575 |
|
warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); |
576 |
|
$i--; |
577 |
|
} else { |
578 |
|
foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { |
579 |
|
my $o = ($j * 2) + 3; |
580 |
|
if ($marc->[$i]->[$o] eq $sf) { |
581 |
|
# remove subfield |
582 |
|
splice @{$marc->[$i]}, $o, 2; |
583 |
|
warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); |
584 |
|
# is record now empty? |
585 |
|
if ($#{ $marc->[$i] } == 2) { |
586 |
|
splice @$marc, $i, 1; |
587 |
|
warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); |
588 |
|
$i--; |
589 |
|
}; |
590 |
|
} |
591 |
|
} |
592 |
|
} |
593 |
|
} |
594 |
|
} |
595 |
|
|
596 |
|
warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); |
597 |
|
|
598 |
|
$marc_record->[ $marc_record_offset ] = $marc; |
599 |
|
|
600 |
|
warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); |
601 |
|
} |
602 |
|
|
603 |
=head1 Functions to extract data from input |
=head1 Functions to extract data from input |
604 |
|
|
617 |
|
|
618 |
sub rec1 { |
sub rec1 { |
619 |
my $f = shift; |
my $f = shift; |
620 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
621 |
return unless (defined($rec) && defined($rec->{$f})); |
return unless (defined($rec) && defined($rec->{$f})); |
622 |
|
warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); |
623 |
if (ref($rec->{$f}) eq 'ARRAY') { |
if (ref($rec->{$f}) eq 'ARRAY') { |
624 |
return map { |
return map { |
625 |
if (ref($_) eq 'HASH') { |
if (ref($_) eq 'HASH') { |
789 |
die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); |
die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); |
790 |
|
|
791 |
my ($fld, $sf, $regex, $part) = @_; |
my ($fld, $sf, $regex, $part) = @_; |
792 |
warn "### regex ", ref($regex), $regex if ($debug > 2); |
warn "### regex ", ref($regex), $regex, $/ if ($debug > 2); |
793 |
|
|
794 |
my @r = rec( $fld, $sf ); |
my @r = rec( $fld, $sf ); |
795 |
my $v = shift @r; |
my $v = shift @r; |
796 |
warn "### first rec($fld,$sf) = ",dump($v) if ($debug > 2); |
warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); |
797 |
|
|
798 |
|
return '' if( ! defined($v) || $v =~ /^\s*$/); |
799 |
|
|
800 |
my @s = split( $regex, $v ); |
my @s = split( $regex, $v ); |
801 |
warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s) if ($debug > 1); |
warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1); |
802 |
if ($part > 0) { |
if ($part && $part > 0) { |
803 |
return $s[ $part - 1 ]; |
return $s[ $part - 1 ]; |
804 |
} else { |
} else { |
805 |
return @s; |
return @s; |