Ignore:
Timestamp:
2011-03-02T16:13:39+13:00 (13 years ago)
Author:
davidb
Message:

Changed to using 'txt2db' to set a single entry so it is not effected by length of data that needs to be pushed through the command line of 'gdbmset' or is effected by the issue of quotes in the data that need to be escaped

File:
1 edited

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/dbutil/gdbm.pm

    r23399 r23758  
    158158
    159159
    160 sub set_infodb_entry
     160sub set_infodb_entry_OLD
    161161{
    162162    my $infodb_file_path = shift(@_);
     
    164164    my $infodb_map = shift(@_);
    165165 
    166     # Protect metadtaa values that go inside quotes for gdbmset
     166    # Protect metadata values that go inside quotes for gdbmset
    167167    foreach my $k (keys %$infodb_map) {
    168168      my @escaped_v = ();
     
    184184    # Generate the record string
    185185    my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
    186     ## print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
     186##    print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
    187187
    188188    # Store it into GDBM
    189189    my $cmd = "gdbmset \"$infodb_file_path\" \"$infodb_key\" \"$serialized_infodb_map\"";
    190190    my $status = system($cmd);
     191
     192    return $status;
    191193 
    192     return $status;
     194}
     195
     196
     197
     198sub set_infodb_entry
     199{
     200    my $infodb_file_path = shift(@_);
     201    my $infodb_key = shift(@_);
     202    my $infodb_map = shift(@_);
    193203 
     204    # HTML escape anything that is not part of the "contains" metadata value
     205    foreach my $k (keys %$infodb_map) {
     206      my @escaped_v = ();
     207      foreach my $v (@{$infodb_map->{$k}}) {
     208        if ($k eq "contains") {
     209          push(@escaped_v, $v);
     210        }
     211        else {
     212          my $ev = &ghtml::unescape_html($v);
     213          push(@escaped_v, $ev);
     214        }
     215      }
     216      $infodb_map->{$k} = \@escaped_v;
     217    }
     218   
     219    # Generate the record string
     220    my $serialized_infodb_map = &dbutil::convert_infodb_hash_to_string($infodb_map);
     221###    print STDERR "**** ser dr\n$serialized_infodb_map\n\n\n";
     222
     223    # Store it into GDBM using 'txt2db -append' which despite its name
     224    # actually replaces the record if it already exists
     225
     226    my $cmd = "txt2db -append \"$infodb_file_path\"";
     227
     228    my $status = undef;
     229    if(!open(GOUT, "| $cmd"))
     230    {
     231    print STDERR "Error: gdbm::set_infodb_entry() failed to open pipe to: $cmd\n";
     232    print STDERR "       $!\n";
     233    $status = -1;
     234    }
     235    else {
     236    binmode(GOUT,":utf8");
     237   
     238    print GOUT "[$infodb_key]\n";
     239    print GOUT "$serialized_infodb_map\n";
     240
     241    close(GOUT);
     242    $status = 0; # as in exit status of cmd OK
     243    }
     244
     245    return $status; 
    194246}
    195247
Note: See TracChangeset for help on using the changeset viewer.