--- trunk/lib/WebPAC/Normalize.pm 2006/10/05 12:57:51 736 +++ trunk/lib/WebPAC/Normalize.pm 2007/04/01 21:47:47 813 @@ -10,7 +10,7 @@ tag search display marc marc_indicators marc_repeatable_subfield marc_compose marc_leader - marc_duplicate marc_remove + marc_duplicate marc_remove marc_count marc_original_order rec1 rec2 rec @@ -19,6 +19,9 @@ save_into_lookup split_rec_on + + get set + count /; use warnings; @@ -39,11 +42,11 @@ =head1 VERSION -Version 0.22 +Version 0.27 =cut -our $VERSION = '0.22'; +our $VERSION = '0.27'; =head1 SYNOPSIS @@ -164,7 +167,7 @@ =cut -my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators); +my ($out, $marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader); my ($marc_record_offset, $marc_fetch_offset) = (0, 0); sub _get_ds { @@ -181,7 +184,7 @@ sub _clean_ds { my $a = {@_}; - ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators) = (); + ($out,$marc_record, $marc_encoding, $marc_repeatable_subfield, $marc_indicators, $marc_leader) = (); ($marc_record_offset, $marc_fetch_offset) = (0,0); $marc_encoding = $a->{marc_encoding}; } @@ -286,13 +289,15 @@ =cut +my $fetch_pos; + sub _get_marc_fields { my $arg = {@_}; warn "### _get_marc_fields arg: ", dump($arg), $/ if ($debug > 2); - my $offset = $marc_fetch_offset; + $fetch_pos = $marc_fetch_offset; if ($arg->{offset}) { - $offset = $arg->{offset}; + $fetch_pos = $arg->{offset}; } elsif($arg->{fetch_next}) { $marc_fetch_offset++; } @@ -301,9 +306,9 @@ warn "### full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 2); - my $marc_rec = $marc_record->[ $offset ]; + my $marc_rec = $marc_record->[ $fetch_pos ]; - warn "## _get_marc_fields (at offset: $offset) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); + warn "## _get_marc_fields (at offset: $fetch_pos) -- marc_record = ", dump( @$marc_rec ), $/ if ($debug > 1); return if (! $marc_rec || ref($marc_rec) ne 'ARRAY' || $#{ $marc_rec } < 0); @@ -324,7 +329,7 @@ if ($debug) { warn "## marc_repeatable_subfield = ", dump( $marc_repeatable_subfield ), $/ if ( $marc_repeatable_subfield ); - warn "## marc_record[$offset] = ", dump( $marc_rec ), $/; + warn "## marc_record[$fetch_pos] = ", dump( $marc_rec ), $/; warn "## sorted_marc_record = ", dump( \@sorted_marc_record ), $/; warn "## subfield count = ", dump( $u ), $/; } @@ -405,6 +410,19 @@ return \@m; } +=head2 _get_marc_leader + +Return leader from currently fetched record by L + + print WebPAC::Normalize::_get_marc_leader(); + +=cut + +sub _get_marc_leader { + die "no fetch_pos, did you called _get_marc_fields first?" unless ( defined( $fetch_pos ) ); + return $marc_leader->[ $fetch_pos ]; +} + =head2 _debug Change level of debug warnings @@ -487,9 +505,15 @@ my ($offset,$value) = @_; if ($offset) { - $out->{' leader'}->{ $offset } = $value; + $marc_leader->[ $marc_record_offset ]->{ $offset } = $value; } else { - return $out->{' leader'}; + + if (defined($marc_leader)) { + die "marc_leader not array = ", dump( $marc_leader ) unless (ref($marc_leader) eq 'ARRAY'); + return $marc_leader->[ $marc_record_offset ]; + } else { + return; + } } } @@ -621,9 +645,11 @@ my $m = $marc_record->[ -1 ]; die "can't duplicate record which isn't defined" unless ($m); push @{ $marc_record }, dclone( $m ); - warn "## marc_duplicate = ", dump(@$marc_record), $/ if ($debug > 1); + push @{ $marc_leader }, dclone( marc_leader() ); + warn "## marc_duplicate = ", dump(@$marc_leader, @$marc_record), $/ if ($debug > 1); $marc_record_offset = $#{ $marc_record }; warn "## marc_record_offset = $marc_record_offset", $/ if ($debug > 1); + } =head2 marc_remove @@ -635,6 +661,10 @@ This will erase field C<200> or C<200^a> from current MARC record. + marc_remove('*'); + +Will remove all fields in current MARC record. + This is useful after calling C or on it's own (but, you should probably just remove that subfield definition if you are not using C). @@ -652,39 +682,47 @@ warn "### marc_remove before = ", dump( $marc ), $/ if ($debug > 2); - my $i = 0; - foreach ( 0 .. $#{ $marc } ) { - last unless (defined $marc->[$i]); - warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); - if ($marc->[$i]->[0] eq $f) { - if (! defined $sf) { - # remove whole field - splice @$marc, $i, 1; - warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); - $i--; - } else { - foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { - my $o = ($j * 2) + 3; - if ($marc->[$i]->[$o] eq $sf) { - # remove subfield - splice @{$marc->[$i]}, $o, 2; - warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); - # is record now empty? - if ($#{ $marc->[$i] } == 2) { - splice @$marc, $i, 1; - warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); - $i--; - }; + if ($f eq '*') { + + delete( $marc_record->[ $marc_record_offset ] ); + warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); + + } else { + + my $i = 0; + foreach ( 0 .. $#{ $marc } ) { + last unless (defined $marc->[$i]); + warn "#### working on ",dump( @{ $marc->[$i] }), $/ if ($debug > 3); + if ($marc->[$i]->[0] eq $f) { + if (! defined $sf) { + # remove whole field + splice @$marc, $i, 1; + warn "#### slice \@\$marc, $i, 1 = ",dump( @{ $marc }), $/ if ($debug > 3); + $i--; + } else { + foreach my $j ( 0 .. (( $#{ $marc->[$i] } - 3 ) / 2) ) { + my $o = ($j * 2) + 3; + if ($marc->[$i]->[$o] eq $sf) { + # remove subfield + splice @{$marc->[$i]}, $o, 2; + warn "#### slice \@{\$marc->[$i]}, $o, 2 = ", dump( @{ $marc }), $/ if ($debug > 3); + # is record now empty? + if ($#{ $marc->[$i] } == 2) { + splice @$marc, $i, 1; + warn "#### slice \@\$marc, $i, 1 = ", dump( @{ $marc }), $/ if ($debug > 3); + $i--; + }; + } } } } + $i++; } - $i++; - } - warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); + warn "### marc_remove($f", $sf ? ",$sf" : "", ") after = ", dump( $marc ), $/ if ($debug > 2); - $marc_record->[ $marc_record_offset ] = $marc; + $marc_record->[ $marc_record_offset ] = $marc; + } warn "## full marc_record = ", dump( @{ $marc_record }), $/ if ($debug > 1); } @@ -759,6 +797,18 @@ warn "## marc_record = ", dump( $marc_record ),$/ if ($debug > 1); } +=head2 marc_count + +Return number of MARC records created using L. + + print "created ", marc_count(), " records"; + +=cut + +sub marc_count { + return $#{ $marc_record }; +} + =head1 Functions to extract data from input @@ -880,6 +930,9 @@ @v = rec('200') @v = rec('200','a') +If rec() returns just single value, it will +return scalar, not array. + =cut sub rec { @@ -889,7 +942,9 @@ } elsif ($#_ == 1) { @out = rec2(@_); } - if (@out) { + if ($#out == 0 && ! wantarray) { + return $out[0]; + } elsif (@out) { return @out; } else { return ''; @@ -1017,7 +1072,7 @@ sub lookup { my ($what, $database, $input, $key, $having) = @_; - confess "lookup needs 5 arguments: what, database, input, key, having" unless ($#_ == 4); + confess "lookup needs 5 arguments: what, database, input, key, having\n" unless ($#_ == 4); warn "## lookup ($database, $input, $key)", $/ if ($debug > 1); return unless (defined($lookup->{$database}->{$input}->{$key})); @@ -1031,7 +1086,7 @@ foreach my $h ( @having ) { if (defined($lookup->{$database}->{$input}->{$key}->{$h})) { - warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n"; + warn "lookup for $database/$input/$key/$h return ",dump($lookup->{$database}->{$input}->{$key}->{$h}),"\n" if ($debug); $mfns->{$_}++ foreach keys %{ $lookup->{$database}->{$input}->{$key}->{$h} }; } } @@ -1040,7 +1095,7 @@ my @mfns = sort keys %$mfns; - warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n"; + warn "# lookup loading $database/$input/$key mfn ", join(",",@mfns)," having ",dump(@having),"\n" if ($debug); my $old_rec = $rec; my @out; @@ -1048,13 +1103,13 @@ foreach my $mfn (@mfns) { $rec = $load_row_coderef->( $database, $input, $mfn ); - warn "got $database/$input/$mfn = ", dump($rec), $/; + warn "got $database/$input/$mfn = ", dump($rec), $/ if ($debug); my @vals = $what->(); push @out, ( @vals ); - warn "lookup for mfn $mfn returned ", dump(@vals), $/; + warn "lookup for mfn $mfn returned ", dump(@vals), $/ if ($debug); } # if (ref($lookup->{$k}) eq 'ARRAY') { @@ -1065,9 +1120,13 @@ $rec = $old_rec; - warn "## lookup returns = ", dump(@out), $/; + warn "## lookup returns = ", dump(@out), $/ if ($debug); - return @out; + if ($#out == 0) { + return $out[0]; + } else { + return @out; + } } =head2 save_into_lookup @@ -1239,5 +1298,45 @@ } } +my $hash; + +=head2 set + + set( key => 'value' ); + +=cut + +sub set { + my ($k,$v) = @_; + warn "## set ( $k => ", dump($v), " )", $/ if ( $debug ); + $hash->{$k} = $v; +}; + +=head2 get + + get( 'key' ); + +=cut + +sub get { + my $k = shift || return; + my $v = $hash->{$k}; + warn "## get $k = ", dump( $v ), $/ if ( $debug ); + return $v; +} + +=head2 count + + if ( count( @result ) == 1 ) { + # do something if only 1 result is there + } + +=cut + +sub count { + warn "## count ",dump(@_),$/ if ( $debug ); + return @_ . ''; +} + # END 1;