Changeset 21715

Show
Ignore:
Timestamp:
04.02.2010 13:04:56 (9 years ago)
Author:
mdewsnip
Message:

Adding some comments and error checking. By Jeffrey Ke.

Location:
main/trunk/greenstone2/perllib/cgiactions
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/perllib/cgiactions/baseaction.pm

    r21564 r21715  
    370370 
    371371 
     372# Release the gli.lck otherwise no one else will be able to use the collection again. 
     373sub unlock_collection 
     374{ 
     375    my $self = shift @_; 
     376    my ($username, $collection) = @_; 
     377    my $gsdl_cgi = $self->{'gsdl_cgi'}; 
     378 
     379    # Obtain the path to the collection GLI lock file 
     380    my $lock_file_path = &util::filename_cat($ENV{'GSDLHOME'}, "collect", $collection, "gli.lck"); 
     381 
     382    # If the lock file does exist, check if it is ours 
     383    if (-e $lock_file_path) 
     384    { 
     385    my $lock_file_content = ""; 
     386    open(LOCK_FILE, "<$lock_file_path"); 
     387    while (<LOCK_FILE>) { 
     388        $lock_file_content .= $_; 
     389    } 
     390    close(LOCK_FILE); 
     391 
     392    # Pick out the owner of the lock file 
     393    $lock_file_content =~ /\<User\>(.*?)\<\/User\>/; 
     394    my $lock_file_owner = $1; 
     395 
     396    # If we are the owner of this lock, we have the right to delete it 
     397    if ($lock_file_owner eq $username) { 
     398            unlink($lock_file_path ); 
     399    } 
     400        else { 
     401        $gsdl_cgi->generate_error("Collection is locked by: $lock_file_owner. Cannot be unlocked"); 
     402        } 
     403    } 
     404} 
     405 
     406 
    372407sub send_mail 
    373408{ 
  • main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm

    r21569 r21715  
    4343 
    4444 
    45  
    4645my $action_table = 
    4746{  
     
    8887 
    8988 
    90  
    9189sub get_live_metadata 
    9290{ 
     
    9896    my $gsdlhome  = $self->{'gsdlhome'}; 
    9997 
     98    # Note: Not sure why get_live_metadata doesn't need the authentication check 
     99 
     100    # Obtain the collect dir 
    100101    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    101 #    $gsdl_cgi->checked_chdir($collect_dir); 
    102  
    103102 
    104103    # Make sure the collection isn't locked by someone else 
     
    106105 
    107106    # look up additional args 
    108  
    109107    my $docid  = $self->{'d'}; 
    110108    if ((!defined $docid) || ($docid =~ m/^\s*$/)) { 
    111     $gsdl_cgi->generate_error("No docid (d=...) specified."); 
    112     } 
    113  
     109       $gsdl_cgi->generate_error("No docid (d=...) specified."); 
     110    } 
     111 
     112    # Generate the dbkey 
    114113    my $metaname  = $self->{'metaname'}; 
    115  
    116114    my $dbkey = "$docid.$metaname"; 
    117115 
     116    # To people who know $collect_tail please add some comments 
     117    # Obtain path to the database 
    118118    my $collect_tail = $collect; 
    119119    $collect_tail =~ s/^.*[\/|\\]//; 
    120  
    121120    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    122121    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory); 
    123  
     122     
     123    # Obtain the content of the key 
    124124    my $cmd = "gdbmget $infodb_file_path $dbkey"; 
    125  
    126125    if (open(GIN,"$cmd |") == 0) { 
     126        # Catch error if gdbmget failed 
    127127    my $mess = "Failed to get metadata key: $metaname\n"; 
    128128    $mess .= "$!\n"; 
     
    130130    $gsdl_cgi->generate_error($mess); 
    131131    } 
    132  
    133132    else { 
     133        # Read everything in and concatenate them into $metavalue 
    134134    my $metavalue = ""; 
    135  
    136135    my $line; 
    137136    while (defined ($line=<GIN>)) { 
     
    139138    } 
    140139    close(GIN); 
    141  
    142     chomp($metavalue); 
    143  
     140    chomp($metavalue); # Get rid off the tailing newlines 
    144141    $gsdl_cgi->generate_ok_message("$metavalue"); 
    145142    } 
    146 } 
    147  
     143 
     144    # Release the lock once it is done 
     145    $self->unlock_collection($username, $collect); 
     146} 
    148147 
    149148 
     
    157156    my $gsdlhome  = $self->{'gsdlhome'}; 
    158157 
    159  
     158    # Authenticate user if it is enabled 
    160159    if ($baseaction::authentication_enabled) { 
    161160    # Ensure the user is allowed to edit this collection 
     
    163162    } 
    164163 
     164    # Obtain the collect dir 
    165165    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    166 #    $gsdl_cgi->checked_chdir($collect_dir); 
    167  
    168166 
    169167    # Make sure the collection isn't locked by someone else 
     
    171169 
    172170    # look up additional args 
    173  
    174171    my $docid     = $self->{'d'}; 
    175172    my $metaname  = $self->{'metaname'}; 
    176173    my $metapos   = $self->{'metapos'}; 
    177174 
     175    # To people who know $collect_tail please add some comments 
     176    # Obtain path to the database 
    178177    my $collect_tail = $collect; 
    179178    $collect_tail =~ s/^.*[\/\\]//; 
    180  
    181179    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    182180    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory); 
     181 
     182    # Read the docid entry 
    183183    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid); 
    184184    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string); 
     185 
     186    # Basically loop through and unescape_html the values 
    185187    foreach my $k (keys %$doc_rec) { 
    186188    my @escaped_v = (); 
     
    189191        push(@escaped_v, $ev); 
    190192    } 
    191  
    192193    $doc_rec->{$k} = \@escaped_v; 
    193194    } 
    194195 
     196    # Obtain the specified metadata value 
    195197    $metapos = 0 if (!defined $metapos); 
    196  
    197198    my $metavalue = $doc_rec->{$metaname}->[$metapos]; 
    198  
    199199    $gsdl_cgi->generate_ok_message("$metavalue"); 
    200  
    201 } 
    202  
    203  
     200     
     201    # Release the lock once it is done 
     202    $self->unlock_collection($username, $collect); 
     203} 
    204204 
    205205 
     
    213213    my $gsdlhome  = $self->{'gsdlhome'}; 
    214214 
    215  
    216215    # don't user authenticate for now 
    217216    if ($baseaction::authentication_enabled) { 
     
    220219    } 
    221220 
     221    # Obtain the collect dir 
    222222    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    223 #    $gsdl_cgi->checked_chdir($collect_dir); 
    224  
    225223 
    226224    # Make sure the collection isn't locked by someone else 
     
    228226 
    229227    # look up additional args 
    230  
    231228    my $docid     = $self->{'d'}; 
     229    if ((!defined $docid) || ($docid =~ m/^\s*$/)) { 
     230      $gsdl_cgi->generate_error("No docid (d=...) specified."); 
     231    } 
     232    my $metavalue = $self->{'metavalue'}; 
     233 
     234    # Generate the dbkey     
    232235    my $metaname  = $self->{'metaname'}; 
    233     my $metavalue = $self->{'metavalue'}; 
    234  
    235236    my $dbkey = "$docid.$metaname"; 
    236237 
     238    # To people who know $collect_tail please add some comments 
     239    # Obtain path to the database 
    237240    my $collect_tail = $collect; 
    238241    $collect_tail =~ s/^.*[\/\\]//; 
    239  
    240242    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    241243    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory); 
    242244 
     245    # Set the new value 
    243246    my $cmd = "gdbmset \"$infodb_file_path\" \"$dbkey\" \"$metavalue\""; 
    244  
    245247    my $status = system($cmd); 
    246  
    247248    if ($status != 0) { 
     249        # Catch error if gdbmget failed 
    248250    my $mess = "Failed to set metadata key: $dbkey\n"; 
    249      
     251 
    250252    $mess .= "PATH: $ENV{'PATH'}\n"; 
    251253    $mess .= "cmd = $cmd\n"; 
     
    258260    $gsdl_cgi->generate_ok_message("set-live-metadata successful: Key[$metaname]=$metavalue"); 
    259261    } 
    260  
     262     
     263    # Release the lock once it is done 
     264    $self->unlock_collection($username, $collect); 
    261265} 
    262266 
     
    271275    my $gsdlhome  = $self->{'gsdlhome'}; 
    272276 
    273  
    274277    # don't user authenticate for now 
    275278    if ($baseaction::authentication_enabled) { 
     
    278281    } 
    279282 
     283    # Obtain the collect dir 
    280284    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    281 #    $gsdl_cgi->checked_chdir($collect_dir); 
    282  
    283285 
    284286    # Make sure the collection isn't locked by someone else 
     
    286288 
    287289    # look up additional args 
    288  
    289290    my $docid     = $self->{'d'}; 
    290291    my $metaname  = $self->{'metaname'}; 
     
    292293    my $metavalue = $self->{'metavalue'}; 
    293294 
     295    # To people who know $collect_tail please add some comments 
     296    # Obtain path to the database 
    294297    my $collect_tail = $collect; 
    295298    $collect_tail =~ s/^.*[\/\\]//; 
    296  
    297299    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    298300    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory); 
     301 
     302    # Read the docid entry 
    299303    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid); 
    300304    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string); 
     
    313317        } 
    314318    } 
    315  
    316319    $doc_rec->{$k} = \@escaped_v; 
    317320    } 
    318  
    319321    ## print STDERR "**** metavalue = $metavalue\n"; 
    320322 
     323    # Protect the quotes 
    321324    $metavalue =~ s/\"/\\\"/g; 
    322325 
     326    # Set the metadata value 
    323327    if (defined $metapos) { 
    324328    $doc_rec->{$metaname}->[$metapos] = $metavalue; 
     
    327331    $doc_rec->{$metaname} = [ $metavalue ]; 
    328332    } 
    329  
    330333    ## print STDERR "**** metavalue = $metavalue\n"; 
    331334 
     335    # Generate the record string 
    332336    my $serialized_doc_rec = &dbutil::convert_infodb_hash_to_string($doc_rec); 
    333  
    334337    ## print STDERR "**** ser dr\n$serialized_doc_rec\n\n\n"; 
    335338 
     339    # Store it into GDBM 
    336340    my $cmd = "gdbmset \"$infodb_file_path\" \"$docid\" \"$serialized_doc_rec\""; 
    337  
    338341    my $status = system($cmd); 
    339  
    340342    if ($status != 0) { 
     343        # Catch error if gdbmget failed 
    341344    my $mess = "Failed to set metadata key: $docid\n"; 
    342345     
     
    356359    $gsdl_cgi->generate_ok_message($mess); 
    357360    } 
    358  
    359 } 
    360  
     361     
     362    # Release the lock once it is done 
     363    $self->unlock_collection($username, $collect); 
     364} 
    361365 
    362366 
    363367sub dxml_metadata 
    364368{ 
    365  
    366369    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_; 
    367  
    368  
    369370    my $metaname = $parser->{'parameters'}->{'metaname'}; 
    370371    my $metamode = $parser->{'parameters'}->{'metamode'}; 
    371372 
     373    # Find the right metadata tag and checks if we are going to override it 
     374    # Note: This over writes the first metadata block it encountered. If there are multiple Sections in the doc.xml, it might not behave as you would expect 
    372375    my $name_attr = $attrHash->{'name'}; 
    373  
    374376    if (($name_attr eq $metaname) && ($metamode eq "override")) { 
    375  
     377        # Get the value and override the current value 
    376378    my $metavalue = $parser->{'parameters'}->{'metavalue'}; 
    377  
    378379    $attrHash->{'_content'} = $metavalue; 
    379380 
    380381    # Don't want it to wipe out any other pieces of metadata 
    381382    $parser->{'parameters'}->{'metamode'} = "done"; 
    382  
    383383    } 
    384384 
    385385    # raw extended 
     386    # Someone please write some comments on why adding ':'.$tagname => $attrHash 
    386387    return (':'.$tagname => $attrHash, [$tagname => $attrHash]); 
    387388} 
     
    390391sub dxml_description 
    391392{ 
    392  
    393393    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_; 
    394  
    395394    my $metamode = $parser->{'parameters'}->{'metamode'}; 
    396395 
    397  
     396    # Accumulate the metadata 
     397    # NOTE: This appends new metadata element to all description fields. 
     398    # If there are multiple Sections/SubSections, the new metadata block will get appended to all of them 
    398399    if ($metamode eq "accumulate") { 
    399400    # tack a new metadata tag on to the end of the <Metadata>+ block 
    400  
    401401    my $metaname = $parser->{'parameters'}->{'metaname'}; 
    402402    my $metavalue = $parser->{'parameters'}->{'metavalue'}; 
    403403     
    404  
    405404    my $metadata_attr = { '_content' => $metavalue,  
    406405                  'name'     => $metaname,  
     
    408407 
    409408    my $append_metadata = [ "Metadata" => $metadata_attr ]; 
    410  
    411409    my $description_content = $attrHash->{'_content'}; 
    412410 
    413411    push(@$description_content,"    ", $append_metadata ,"\n        "); 
    414  
    415412    } 
    416413 
     
    419416} 
    420417 
     418 
    421419sub edit_xml_file 
    422420{ 
    423421    my $self = shift @_; 
    424  
    425422    my ($gsdl_cgi, $filename, $rules, $options) = @_; 
    426423 
    427424    # use XML::Rules to add it in (read in and out again) 
    428  
    429425    my $parser = XML::Rules->new(rules => $rules,  
    430426                 style => 'filter' ); 
     
    435431    } 
    436432    else { 
     433        # Read all the text in 
    437434    my $line; 
    438435    while (defined ($line=<MIN>)) { 
     
    441438    close(MIN); 
    442439     
    443      
     440    # Matched lines will get handled by the call backs 
    444441    my $xml_out = ""; 
    445442    $parser->filter($xml_in,\$xml_out, $options); 
     
    450447    else { 
    451448        print MOUT $xml_out; 
    452         close(MOUT); 
    453          
    454     } 
    455     } 
    456 } 
    457  
     449        close(MOUT);         
     450    } 
     451    } 
     452} 
    458453 
    459454 
     
    461456{ 
    462457    my $self = shift @_; 
    463  
    464458    my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos) = @_; 
    465459 
    466460    # use XML::Rules to add it in (read in and out again) 
    467  
     461    # Set the call back functions 
    468462    my @rules =  
    469463    ( _default => 'raw extended', 
     
    471465      'Description' => \&dxml_description ); 
    472466       
     467    # Sets the parameters 
    473468    my $options = { 'metaname'  => $metaname, 
    474469            'metapos'   => $metapos, 
    475470            'metavalue' => $metavalue }; 
    476  
    477471    $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@rules,$options); 
    478472} 
     
    488482    my $gsdlhome  = $self->{'gsdlhome'}; 
    489483 
    490  
    491484    # don't user authenticate for now 
    492485    if ($baseaction::authentication_enabled) { 
     
    495488    } 
    496489 
     490    # Obtain the collect and archive dir     
    497491    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    498 #    $gsdl_cgi->checked_chdir($collect_dir); 
    499  
    500492    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
    501493 
     
    504496 
    505497    # look up additional args 
    506  
    507498    my $docid  = $self->{'d'}; 
    508  
    509499    my $metaname   = $self->{'metaname'}; 
    510500    my $metavalue  = $self->{'metavalue'}; 
    511501    my $metapos    = $self->{'metapos'}; 
    512  
    513502    $metapos = 0 if (!defined $metapos); 
    514  
     503     
     504    # Obtain the doc.xml path for the specified docID 
    515505    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archive_dir); 
    516  
    517506    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $arcinfo_doc_filename, $docid); 
    518     my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string); 
    519  
     507    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string);     
    520508    my $doc_xml_file = $doc_rec->{'doc-file'}->[0]; 
    521  
    522      
    523     my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
    524      
     509     
     510    # The $doc_xml_file is relative to the archives, and now let's get the full path 
     511    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");     
    525512    my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file); 
    526  
    527      
     513     
     514    # Edit the doc.xml file with the specified metadata name, value and position. 
     515    # TODO: there is a potential problem here as this edit_doc_xml function  
     516    # is assuming the simple doc.xml situation where there is only one Section and no SubSections. 
     517    # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml 
    528518    $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename, 
    529519            $metaname,$metavalue,$metapos); 
    530520 
    531 } 
    532  
    533  
     521    # Release the lock once it is done 
     522    $self->unlock_collection($username, $collect); 
     523} 
    534524 
    535525 
    536526sub mxml_metadata 
    537527{ 
    538  
    539528    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_; 
    540  
    541  
    542529    my $metaname = $parser->{'parameters'}->{'metaname'}; 
    543530    my $metamode = $parser->{'parameters'}->{'metamode'}; 
    544531 
     532    # Find the right metadata tag and checks if we are going to override it 
     533    # Note: This over writes the first metadata block it encountered even if it doesn't belong to the source file we specified 
    545534    my $name_attr = $attrHash->{'name'}; 
    546  
    547535    if (($name_attr eq $metaname) && ($metamode eq "override")) { 
    548  
     536        # Get the value and override the current value 
    549537    my $metavalue = $parser->{'parameters'}->{'metavalue'}; 
    550  
    551538    $attrHash->{'_content'} = $metavalue; 
    552539 
    553540    # Don't want it to wipe out any other pieces of metadata 
    554541    $parser->{'parameters'}->{'metamode'} = "done"; 
    555  
    556542    } 
    557543 
    558544    # raw extended 
     545    # Someone please write some comments on why adding ':'.$tagname => $attrHash 
    559546    return (':'.$tagname => $attrHash, [$tagname => $attrHash]); 
    560547} 
     
    563550sub mxml_description 
    564551{ 
    565  
    566552    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_; 
    567  
    568553    my $metamode = $parser->{'parameters'}->{'metamode'}; 
    569554 
    570  
     555    # Accumulate the metadata block to the end of the description block 
     556    # Note: This adds metadata block to all description blocks, so if there are  
     557    # multiple FileSets, it will add to all of them 
    571558    if ($metamode eq "accumulate") { 
    572559    # tack a new metadata tag on to the end of the <Metadata>+ block 
    573  
    574560    my $metaname = $parser->{'parameters'}->{'metaname'}; 
    575561    my $metavalue = $parser->{'parameters'}->{'metavalue'}; 
    576562     
    577  
    578563    my $metadata_attr = { '_content' => $metavalue,  
    579564                  'name'     => $metaname,  
     
    581566 
    582567    my $append_metadata = [ "Metadata" => $metadata_attr ]; 
    583  
    584568    my $description_content = $attrHash->{'_content'}; 
    585569 
    586570    push(@$description_content,"    ", $append_metadata ,"\n        "); 
    587  
    588571    } 
    589572 
     
    592575} 
    593576 
     577 
    594578sub edit_metadata_xml 
    595579{ 
    596580    my $self = shift @_; 
    597  
    598581    my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metavalue, $metamode) = @_; 
    599582 
    600     # use XML::Rules to add it in (read in and out again) 
    601  
     583    # Set the call-back functions for the metadata tags 
    602584    my @rules =  
    603585    ( _default => 'raw extended', 
    604586      'Metadata' => \&mxml_metadata, 
    605587      'Description' => \&mxml_description ); 
    606        
    607  
     588 
     589    # use XML::Rules to add it in (read in and out again) 
    608590    my $parser = XML::Rules->new(rules => \@rules,  
    609591                 style => 'filter' ); 
    610  
    611592 
    612593    my $xml_in = ""; 
     
    615596    } 
    616597    else { 
     598        # Read them in 
    617599    my $line; 
    618600    while (defined ($line=<MIN>)) { 
    619601        $xml_in .= $line; 
    620602    } 
    621     close(MIN); 
     603    close(MIN);  
    622604     
    623      
     605        # Filter with the call-back functions 
    624606    my $xml_out = ""; 
    625607    $parser->filter($xml_in,\$xml_out, { metaname => $metaname, 
    626608                         metavalue => $metavalue, 
    627                          metamode => $metamode } ); 
    628      
     609                                             metamode => $metamode } ); 
     610         
    629611    if (!open(MOUT,">$metadata_xml_filename")) { 
    630612        $gsdl_cgi->generate_error("Unable to write out to $metadata_xml_filename: $!"); 
     
    632614    else { 
    633615        print MOUT $xml_out; 
    634         close(MOUT); 
    635          
     616        close(MOUT);         
    636617    } 
    637618    } 
     
    642623{ 
    643624    my $self = shift @_; 
    644  
     625     
    645626    my $username  = $self->{'username'}; 
    646627    my $collect   = $self->{'collect'}; 
     
    648629    my $gsdlhome  = $self->{'gsdlhome'}; 
    649630 
    650  
    651631    # don't user authenticate for now 
    652632    if ($baseaction::authentication_enabled) { 
     
    655635    } 
    656636 
     637    # Obtain the collect and archive dir     
    657638    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    658 #    $gsdl_cgi->checked_chdir($collect_dir); 
    659  
    660639    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives"); 
    661  
    662640 
    663641    # Make sure the collection isn't locked by someone else 
     
    665643 
    666644    # look up additional args 
    667  
    668645    # want either d= or f= 
    669646    my $docid  = $self->{'d'}; 
    670647    my $import_file  = $self->{'f'}; 
    671  
    672648    if ((!defined $docid) && (!defined $import_file)) { 
    673649    $gsdl_cgi->generate_error("No docid (d=...) or import file (f=) specified."); 
    674650    }  
    675651 
     652    # Get the parameters and set default mode to "accumulate" 
    676653    my $metaname   = $self->{'metaname'}; 
    677654    my $metavalue  = $self->{'metavalue'}; 
    678655    my $metamode   = $self->{'metamode'}; 
    679  
    680656    if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) { 
    681657    # make "accumulate" the default (less destructive, as won't actually  
    682658    # delete any existing values) 
    683  
    684659    $metamode = "accumulate"; 
    685660    } 
    686661 
     662    # Obtain where the metadata.xml is from the archiveinfo-doc.gdb file 
     663    # If the doc oid is not specified, we assume the metadata.xml is next to the specified "f" 
    687664    my $metadata_xml_file; 
    688  
    689665    my $import_filename = undef; 
    690  
    691666    if (defined $docid) { 
    692667    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path("gdbm", "archiveinf-doc", $archive_dir); 
     
    698673    } 
    699674    else { 
    700     $import_filename = &util::filename_cat($collect_dir,$collect,$import_file); 
    701     } 
    702      
    703     # figure out correct metadata.xml file 
    704  
     675        $import_filename = &util::filename_cat($collect_dir,$collect,$import_file); 
     676    } 
     677     
     678     
     679    # figure out correct metadata.xml file [?] 
     680    # Assuming the metadata.xml file is next to the source file 
     681    # Note: This will not work if it is using the inherited metadata from the parent folder 
    705682    my ($import_tailname, $import_dirname)  
    706683    = File::Basename::fileparse($import_filename); 
    707  
    708684    my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml"); 
    709685 
     686    # Edit the metadata.xml 
     687    # Note: At moment it doesn't correctly on metadata.xml with multiple FileSets 
     688    # "accumulate" mode will add new metadata block to all FileSets 
     689    # "override" mode will over write the first encountered metadata block, even if it doesn't have the right source 
    710690    $self->edit_metadata_xml($gsdl_cgi,$metadata_xml_filename, 
    711691                 $metaname,$metavalue,$metamode); 
    712692 
    713 } 
    714  
    715  
    716  
     693    # Release the lock once it is done 
     694    $self->unlock_collection($username, $collect); 
     695} 
    717696 
    718697 
     
    726705    my $gsdlhome  = $self->{'gsdlhome'}; 
    727706 
    728  
    729707    if ($baseaction::authentication_enabled) { 
    730708    # Ensure the user is allowed to edit this collection 
     
    732710    } 
    733711 
     712    # Obtain the collect dir 
    734713    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    735 #    $gsdl_cgi->checked_chdir($collect_dir); 
    736  
    737714 
    738715    # Make sure the collection isn't locked by someone else 
     
    740717 
    741718    # look up additional args 
    742  
    743719    my $docid     = $self->{'d'}; 
     720    if ((!defined $docid) || ($docid =~ m/^\s*$/)) { 
     721      $gsdl_cgi->generate_error("No docid (d=...) specified."); 
     722    } 
     723     
     724    # Generate the dbkey 
    744725    my $metaname  = $self->{'metaname'}; 
    745  
    746726    my $dbkey = "$docid.$metaname"; 
    747727 
     728    # To people who know $collect_tail please add some comments 
     729    # Obtain the live gdbm_db path  
    748730    my $collect_tail = $collect; 
    749731    $collect_tail =~ s/^.*[\/\\]//; 
    750  
    751732    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    752733    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", "live-$collect_tail", $index_text_directory); 
    753734 
     735    # Remove the key 
    754736    my $cmd = "gdbmdel \"$infodb_file_path\" \"$dbkey\""; 
    755  
    756737    my $status = system($cmd); 
    757  
    758738    if ($status != 0) { 
     739        # Catch error if gdbmdel failed 
    759740    my $mess = "Failed to set metadata key: $dbkey\n"; 
    760741     
     
    782763    my $gsdlhome  = $self->{'gsdlhome'}; 
    783764 
    784  
    785765    # don't user authenticate for now 
    786766    if ($baseaction::authentication_enabled) { 
     
    789769    } 
    790770 
     771    # Obtain the collect dir 
    791772    my $collect_dir = &util::filename_cat($gsdlhome, "collect"); 
    792 #    $gsdl_cgi->checked_chdir($collect_dir); 
    793  
    794773 
    795774    # Make sure the collection isn't locked by someone else 
     
    797776 
    798777    # look up additional args 
    799  
    800778    my $docid     = $self->{'d'}; 
     779    if ((!defined $docid) || ($docid =~ m/^\s*$/)) { 
     780      $gsdl_cgi->generate_error("No docid (d=...) specified."); 
     781    } 
    801782    my $metaname  = $self->{'metaname'}; 
    802783    my $metapos   = $self->{'metapos'}; 
    803784 
     785    # To people who know $collect_tail please add some comments 
     786    # Obtain the path to the database 
    804787    my $collect_tail = $collect; 
    805788    $collect_tail =~ s/^.*[\/\\]//; 
    806  
    807789    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text"); 
    808790    my $infodb_file_path = &dbutil::get_infodb_file_path("gdbm", $collect_tail, $index_text_directory); 
     791 
     792    # Read the docid entry 
    809793    my $doc_rec_string = &dbutil::read_infodb_entry("gdbm", $infodb_file_path, $docid); 
    810794    my $doc_rec = &dbutil::convert_infodb_string_to_hash($doc_rec_string); 
     795 
     796    # Basically loop through and unescape_html the values 
    811797    foreach my $k (keys %$doc_rec) { 
    812798    my @escaped_v = (); 
     
    823809        } 
    824810    } 
    825  
    826811    $doc_rec->{$k} = \@escaped_v; 
    827812    } 
    828813 
     814    # Check to make sure the key does exist 
     815    if (!defined ($doc_rec->{$metaname})) { 
     816        $gsdl_cgi->generate_error("No metadata field \"" . $metaname . "\" in the specified document: [" . $docid . "]"); 
     817    } 
     818 
     819    # Obtain the specified metadata pos 
    829820    $metapos = 0 if (!defined $metapos); 
    830821 
    831822    # consider check key is defined before deleting? 
    832  
     823    # Loop through the metadata array and ignore the specified position 
    833824    my $filtered_metadata = []; 
    834     my $num_metadata_vals = scalar(@{$doc_rec->{$metaname}}); 
    835  
     825    my $num_metadata_vals = scalar(@{$doc_rec->{$metaname}});     
    836826    for (my $i=0; $i<$num_metadata_vals; $i++) { 
    837827    my $metavalue = shift(@{$doc_rec->{$metaname}}); 
     
    841831    } 
    842832    } 
    843  
    844833    $doc_rec->{$metaname} = $filtered_metadata; 
    845834 
     835    # Turn the record back to string 
    846836    my $serialized_doc_rec = &dbutil::convert_infodb_hash_to_string($doc_rec); 
    847837 
     838    # Store it back to the database 
    848839    my $cmd = "gdbmset \"$infodb_file_path\" \"$docid\" \"$serialized_doc_rec\""; 
    849  
    850840    my $status = system($cmd); 
    851  
    852841    if ($status != 0) { 
    853842    my $mess = "Failed to set metadata key: $docid\n"; 
     
    867856    $gsdl_cgi->generate_ok_message($mess); 
    868857    } 
    869  
    870 } 
    871  
    872  
    873  
    874  
     858} 
    875859 
    876860