--- trunk/lib/WebPAC/Normalize.pm 2006/06/30 20:43:09 551 +++ trunk/lib/WebPAC/Normalize.pm 2006/07/03 14:30:22 571 @@ -3,13 +3,17 @@ @EXPORT = qw/ _set_rec _set_lookup _get_ds _clean_ds + _debug tag search display marc marc_indicators marc_repeatable_subfield + marc_compose marc_leader rec1 rec2 rec regex prefix suffix surround first lookup join_with + + split_rec_on /; use warnings; @@ -29,11 +33,11 @@ =head1 VERSION -Version 0.06 +Version 0.09 =cut -our $VERSION = '0.06'; +our $VERSION = '0.09'; =head1 SYNOPSIS @@ -153,16 +157,32 @@ $marc->add_fields( WebPAC::Normalize:_get_marc_fields() ); - - We are using I which detect repeatable fields only from sequence of field/subfield data generated by normalization. -Repeatable field is created if there is second occurence of same subfield or -if any of indicators are different. This is sane for most cases except for -non-repeatable fields with repeatable subfields. +Repeatable field is created when there is second occurence of same subfield or +if any of indicators are different. + +This is sane for most cases. Something like: -You can change behaviour of that using C. + 900a-1 900b-1 900c-1 + 900a-2 900b-2 + 900a-3 + +will be created from any combination of: + + 900a-1 900a-2 900a-3 900b-1 900b-2 900c-1 + +and following rules: + + marc('900','a', rec('200','a') ); + marc('900','b', rec('200','b') ); + marc('900','c', rec('200','c') ); + +which might not be what you have in mind. If you need repeatable subfield, +define it using C like this: + +.... =cut @@ -176,6 +196,8 @@ $a->[0] . $a->[3] cmp $b->[0] . $b->[3] } @{ $marc_record }; + @sorted_marc_record = @{ $marc_record }; ### FIXME disable sorting + # output marc fields my @m; @@ -266,6 +288,21 @@ return @m; } +=head2 _debug + +Change level of debug warnings + + _debug( 2 ); + +=cut + +sub _debug { + my $l = shift; + return $debug unless defined($l); + warn "debug level $l",$/ if ($l > 0); + $debug = $l; +} + =head1 Functions to create C Those functions generally have to first in your normalization file. @@ -320,11 +357,31 @@ $out->{$name}->{search} = \@o; } +=head2 marc_leader + +Setup fields within MARC leader or get leader + + marc_leader('05','c'); + my $leader = marc_leader(); + +=cut + +sub marc_leader { + my ($offset,$value) = @_; + + if ($offset) { + $out->{' leader'}->{ $offset } = $value; + } else { + return $out->{' leader'}; + } +} + =head2 marc Save value for MARC field marc('900','a', rec('200','a') ); + marc('001', rec('000') ); =cut @@ -332,14 +389,21 @@ my $f = shift or die "marc needs field"; die "marc field must be numer" unless ($f =~ /^\d+$/); - my $sf = shift or die "marc needs subfield"; + my $sf; + if ($f >= 10) { + $sf = shift or die "marc needs subfield"; + } foreach (@_) { my $v = $_; # make var read-write for Encode next unless (defined($v) && $v !~ /^\s*$/); from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); - push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ]; + if (defined $sf) { + push @{ $marc_record }, [ $f, $i1, $i2, $sf => $v ]; + } else { + push @{ $marc_record }, [ $f, $v ]; + } } } @@ -379,6 +443,41 @@ @{ $marc_indicators->{$f} } = ($i1,$i2); } +=head2 marc_compose + +Save values for each MARC subfield explicitly + + marc_compose('900', + 'a', rec('200','a') + 'b', rec('201','a') + 'a', rec('200','b') + 'c', rec('200','c') + ); + +=cut + +sub marc_compose { + my $f = shift or die "marc_compose needs field"; + die "marc_compose field must be numer" unless ($f =~ /^\d+$/); + + my ($i1,$i2) = defined($marc_indicators->{$f}) ? @{ $marc_indicators->{$f} } : (' ',' '); + my $m = [ $f, $i1, $i2 ]; + + while (@_) { + my $sf = shift or die "marc_compose $f needs subfield"; + my $v = shift; + + next unless (defined($v) && $v !~ /^\s*$/); + from_to($v, 'iso-8859-2', $marc_encoding) if ($marc_encoding); + push @$m, ( $sf, $v ); + warn "## ++ marc_compose($f,$sf,$v) ", dump( $m ),$/ if ($debug > 1); + } + + warn "## marc_compose(d) ", dump( $m ),$/ if ($debug > 1); + + push @{ $marc_record }, $m if ($#{$m} > 2); +} + =head1 Functions to extract data from input @@ -397,7 +496,9 @@ sub rec1 { my $f = shift; + warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); return unless (defined($rec) && defined($rec->{$f})); + warn "rec1($f) = ", dump( $rec->{$f} ), $/ if ($debug > 1); if (ref($rec->{$f}) eq 'ARRAY') { return map { if (ref($_) eq 'HASH') { @@ -548,5 +649,41 @@ return join($d, grep { defined($_) && $_ ne '' } @_); } +=head2 split_rec_on + +Split record subfield on some regex and take one of parts out + + $a_before_semi_column = + split_rec_on('200','a', /\s*;\s*/, $part); + +C<$part> is optional number of element. First element is +B<1>, not 0! + +If there is no C<$part> parameter or C<$part> is 0, this function will +return all values produced by splitting. + +=cut + +sub split_rec_on { + die "split_rec_on need (fld,sf,regex[,part]" if ($#_ < 2); + + my ($fld, $sf, $regex, $part) = @_; + warn "### regex ", ref($regex), $regex, $/ if ($debug > 2); + + my @r = rec( $fld, $sf ); + my $v = shift @r; + warn "### first rec($fld,$sf) = ",dump($v),$/ if ($debug > 2); + + return '' if( ! defined($v) || $v =~ /^\s*$/); + + my @s = split( $regex, $v ); + warn "## split_rec_on($fld,$sf,$regex,$part) = ",dump(@s),$/ if ($debug > 1); + if ($part && $part > 0) { + return $s[ $part - 1 ]; + } else { + return @s; + } +} + # END 1;