r9089@llin: dpavlin | 2005-11-24 12:47:02 +0100
[webpac2] / lib / WebPAC / Normalize.pm
index c555131..1cb9f93 100644 (file)
@@ -11,11 +11,11 @@ WebPAC::Normalize - data mungling for normalisation
 
 =head1 VERSION
 
-Version 0.01
+Version 0.02
 
 =cut
 
-our $VERSION = '0.01';
+our $VERSION = '0.02';
 
 =head1 SYNOPSIS
 
@@ -122,13 +122,7 @@ C<conf/normalize/*.xml>.
 
 This structures are used to produce output.
 
- my @ds = $webpac->data_structure($rec);
-
-B<Note: historical oddity follows>
-
-This method will also set C<< $webpac->{'currnet_filename'} >> if there is
-C<< <filename> >> tag and C<< $webpac->{'headline'} >> if there is
-C<< <headline> >> tag.
+ my $ds = $webpac->data_structure($rec);
 
 =cut
 
@@ -140,12 +134,18 @@ sub data_structure {
        my $rec = shift;
        $log->logconfess("need HASH as first argument!") if ($rec !~ /HASH/o);
 
+       $log->debug("data_structure rec = ", sub { Dumper($rec) });
+
+       $log->logdie("need unique ID (mfn) in field 000 of record ", sub { Dumper($rec) } ) unless (defined($rec->{'000'}));
+
+       my $mfn = $rec->{'000'}->[0] || $log->logdie("field 000 isn't array!");
+
        my $cache_file;
 
        if ($self->{'db'}) {
-               my @ds = $self->{'db'}->load_ds($rec);
-               $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper(@ds) });
-               return @ds if ($#ds > 0);
+               my $ds = $self->{'db'}->load_ds( $mfn );
+               $log->debug("load_ds( rec = ", sub { Dumper($rec) }, ") = ", sub { Dumper($ds) });
+               return $ds if ($ds);
                $log->debug("cache miss, creating");
        }
 
@@ -160,7 +160,7 @@ sub data_structure {
                $self->{tags_by_order} = \@sorted_tags;
        }
 
-       my @ds;
+       my $ds;
 
        $log->debug("tags: ",sub { join(", ",@sorted_tags) });
 
@@ -171,7 +171,10 @@ sub data_structure {
 #print "field $field [",$self->{'tag'},"] = ",Dumper($self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}});
 
                foreach my $tag (@{$self->{'import_xml'}->{'indexer'}->{$field}->{$self->{'tag'}}}) {
-                       my $format = $tag->{'value'} || $tag->{'content'};
+                       my $format;
+
+                       $log->logdie("expected tag HASH and got $tag") unless (ref($tag) eq 'HASH');
+                       $format = $tag->{'value'} || $tag->{'content'};
 
                        $log->debug("format: $format");
 
@@ -192,22 +195,13 @@ sub data_structure {
                                @v = map { $self->apply_format($tag->{'format_name'},$tag->{'format_delimiter'},$_) } @v;
                        }
 
-                       if ($field eq 'filename') {
-                               $self->{'current_filename'} = join('',@v);
-                               $log->debug("filename: ",$self->{'current_filename'});
-                       } elsif ($field eq 'headline') {
-                               $self->{'headline'} .= join('',@v);
-                               $log->debug("headline: ",$self->{'headline'});
-                               next; # don't return headline in data_structure!
-                       }
-
                        # delimiter will join repeatable fields
                        if ($tag->{'delimiter'}) {
                                @v = ( join($tag->{'delimiter'}, @v) );
                        }
 
                        # default types 
-                       my @types = qw(display swish);
+                       my @types = qw(display search);
                        # override by type attribute
                        @types = ( $tag->{'type'} ) if ($tag->{'type'});
 
@@ -240,14 +234,14 @@ sub data_structure {
 
                        # TODO: name_sigular, name_plural
                        my $name = $self->{'import_xml'}->{'indexer'}->{$field}->{'name'};
-                       $row->{'name'} = $name ? $self->_x($name) : $field;
+                       my $row_name = $name ? $self->_x($name) : $field;
 
                        # post-sort all values in field
                        if ($self->{'import_xml'}->{'indexer'}->{$field}->{'sort'}) {
                                $log->warn("sort at field tag not implemented");
                        }
 
-                       push @ds, $row;
+                       $ds->{$row_name} = $row;
 
                        $log->debug("row $field: ",sub { Dumper($row) });
                }
@@ -255,14 +249,15 @@ sub data_structure {
        }
 
        $self->{'db'}->save_ds(
-               ds => \@ds,
-               current_filename => $self->{'current_filename'},
-               headline => $self->{'headline'},
+               id => $mfn,
+               ds => $ds,
        ) if ($self->{'db'});
 
-       $log->debug("ds: ", sub { Dumper(@ds) });
+       $log->debug("ds: ", sub { Dumper($ds) });
 
-       return @ds;
+       $log->logconfess("data structure returned is not array any more!") if wantarray;
+
+       return $ds;
 
 }
 
@@ -535,7 +530,7 @@ sub get_data {
                if ($sf && $$rec->{$f}->[$i]->{$sf}) {
                        $$found++ if (defined($$found));
                        return $$rec->{$f}->[$i]->{$sf};
-               } elsif ($$rec->{$f}->[$i]) {
+               } elsif (! $sf && $$rec->{$f}->[$i]) {
                        $$found++ if (defined($$found));
                        # it still might have subfield, just
                        # not specified, so we'll dump all
@@ -548,6 +543,8 @@ sub get_data {
                        } else {
                                return $$rec->{$f}->[$i];
                        }
+               } else {
+                       return '';
                }
        } else {
                return '';