Ignore:
Timestamp:
2011-05-20T16:34:22+12:00 (13 years ago)
Author:
davidb
Message:

Introduction of actions that take an array of items (e.g. an array of OIDs or filenames). In adding in this ability, we have started to make use of JSON.

Another action added in is the ability to control building using a manifest file (its fields passed in using JSON). Also the ability to delete files in the archives directory (i.e. when a collection is beeing used in an 'onlyadd' way). Still needs to the more general case to be implemented.

/DB/

File:
1 edited

Legend:

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

    r23766 r24071  
    3333use ghtml;
    3434
     35use JSON;
     36
    3537
    3638BEGIN {
     
    5759                     'optional-args'   => [ "metapos" ] },
    5860
     61    "set-metadata-array"     => { 'compulsory-args' => [ "json" ],
     62                     'optional-args'   => [ ] },
     63                     
    5964    "set-archives-metadata" => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
    6065                     'optional-args'   => [ "metapos", "metamode" ]
     
    6267                },
    6368
     69    "set-archives-metadata-array" => { 'compulsory-args' => [ "json" ],
     70                     'optional-args'   => [ "metamode" ]
     71                },
     72               
    6473    "set-import-metadata"   => { 'compulsory-args' => [ "metaname", "metavalue" ],
    6574                     'optional-args'   => [ "d", "f", "metamode" ]
     
    292301}
    293302
     303sub set_metadata_entry
     304{
     305    my $self = shift @_;
     306    my ($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue) = @_;
     307   
     308    # To people who know $collect_tail please add some comments
     309    # Obtain path to the database
     310    my $collect_tail = $collect;
     311    $collect_tail =~ s/^.*[\/\\]//;
     312    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     313    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     314   
     315#   print STDERR "**** infodb file path = $infodb_file_path\n";
     316#   print STDERR "***** infodb type = $infodbtype\n";
     317   
     318    # Read the docid entry
     319    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
     320   
     321    # Set the metadata value
     322    if (defined $metapos) {
     323        $doc_rec->{$metaname}->[$metapos] = $metavalue;
     324    }
     325    else {
     326        $doc_rec->{$metaname} = [ $metavalue ];
     327    }
     328 
     329    my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
     330   
     331    return $status;
     332   
     333}
    294334
    295335sub set_metadata
     
    325365    my $infodbtype = $self->{'infodbtype'};
    326366   
    327     # To people who know $collect_tail please add some comments
    328     # Obtain path to the database
    329     my $collect_tail = $collect;
    330     $collect_tail =~ s/^.*[\/\\]//;
    331     my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    332     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
    333    
    334     print STDERR "**** infodb file path = $infodb_file_path\n";
    335     print STDERR "***** infodb type = $infodbtype\n";
    336    
    337     # Read the docid entry
    338     my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
    339    
    340     # Set the metadata value
    341     if (defined $metapos) {
    342     $doc_rec->{$metaname}->[$metapos] = $metavalue;
    343     }
    344     else {
    345     $doc_rec->{$metaname} = [ $metavalue ];
    346     }
    347  
    348     my $status = &dbutil::set_infodb_entry($infodbtype, $infodb_file_path,$docid,$doc_rec);
     367    my $status = $self->set_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue);
     368   
    349369    if ($status != 0) {
    350370        # Catch error if set infodb entry failed
     
    358378    }
    359379    else {
    360     my $mess = "set-document-metadata successful: Key[$docid]\n";
     380    my $mess = "set-metadata successful: Key[$docid]\n";
    361381    $mess .= "  $metaname";
    362382    $mess .= "->[$metapos]" if (defined $metapos);
     
    369389    $self->unlock_collection($username, $collect);
    370390}
     391
     392
     393
     394
     395sub set_metadata_array
     396{
     397    my $self = shift @_;
     398
     399    my $username  = $self->{'username'};
     400    my $collect   = $self->{'collect'};
     401    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     402    my $gsdlhome  = $self->{'gsdlhome'};
     403
     404    if ($baseaction::authentication_enabled) {
     405    # Ensure the user is allowed to edit this collection
     406    &authenticate_user($gsdl_cgi, $username, $collect);
     407    }
     408
     409    my $site = $self->{'site'};
     410    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     411   
     412    $gsdl_cgi->checked_chdir($collect_dir);
     413
     414    # Obtain the collect dir
     415    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     416
     417    # Make sure the collection isn't locked by someone else
     418    $self->lock_collection($username, $collect);
     419
     420    # look up additional args
     421   
     422    my $infodbtype = $self->{'infodbtype'};
     423   
     424    my $json_str      = $self->{'json'};
     425    my $doc_array = decode_json $json_str;
     426   
     427   
     428    my $global_status = 0;
     429    my $global_mess = "";
     430   
     431    my @all_docids = ();
     432   
     433    foreach my $doc_array_rec ( @$doc_array ) {
     434       
     435        my $docid     = $doc_array_rec->{'docid'};
     436        my $metaname  = $doc_array_rec->{'metaname'};
     437        my $metapos   = $doc_array_rec->{'metapos'};
     438        my $metavalue = $doc_array_rec->{'metavalue'};
     439       
     440        push(@all_docids,$docid);
     441       
     442        my $status = $self->set_metadata_entry($collect_dir,$collect,$infodbtype,$docid,$metaname,$metapos,$metavalue);
     443       
     444        if ($status != 0) {
     445            # Catch error if set infodb entry failed
     446            $global_status = $status;
     447            $global_mess .= "Failed to set metadata key: $docid\n";
     448            $global_mess .= "Exit status: $status\n";
     449            $global_mess .= "System Error Message: $!\n";
     450            $global_mess .= "-" x 20;
     451        }
     452    }
     453
     454    if ($global_status != 0) {
     455        $global_mess .= "PATH: $ENV{'PATH'}\n";
     456        $gsdl_cgi->generate_error($global_mess);
     457    }
     458    else {
     459        my $mess = "set-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
     460        $gsdl_cgi->generate_ok_message($mess);
     461    }
     462   
     463    # Release the lock once it is done
     464    $self->unlock_collection($username, $collect);
     465}
     466
    371467
    372468
     
    554650}
    555651
     652sub set_archives_metadata_entry
     653{
     654    my $self = shift @_;
     655    my ($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,$metaname,$metapos,$metavalue,$metamode) = @_;
     656   
     657    # Obtain the doc.xml path for the specified docID
     658    my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
     659
     660    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
     661    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
     662    my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
     663   
     664    # The $doc_xml_file is relative to the archives, and now let's get the full path
     665    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");   
     666    my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
     667   
     668    # Edit the doc.xml file with the specified metadata name, value and position.
     669    # TODO: there is a potential problem here as this edit_doc_xml function
     670    # is assuming the simple doc.xml situation where there is only one Section and no SubSections.
     671    # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
     672   
     673    print STDERR "** away to call edit_doc_xml\n";
     674   
     675    $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
     676            $metaname,$metavalue,$metapos,$metamode,$docid_secnum);
     677           
     678    print STDERR "*** finished edit_doc_xml\n";
     679   
     680    return 0; # return 0 for now to indicate no error
     681           
     682}
     683
    556684
    557685sub set_archives_metadata
     
    565693    my $infodbtype = $self->{'infodbtype'};
    566694   
     695
     696   
    567697    if ($baseaction::authentication_enabled) {
    568     # Ensure the user is allowed to edit this collection
    569     $self->authenticate_user($username, $collect);
    570     }
    571 
     698        # Ensure the user is allowed to edit this collection
     699        $self->authenticate_user($username, $collect);
     700    }
     701
     702    my $site = $self->{'site'};
     703       
    572704    # Obtain the collect and archive dir   
    573     my $site = $self->{'site'};
    574705    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
    575     ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
    576706   
    577707    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
     
    593723    # delete any existing values)
    594724    $metamode = "accumulate";
    595     }
    596    
    597     # Obtain the doc.xml path for the specified docID
    598     my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
    599 
    600     my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
    601     my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
    602     my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
    603    
    604     # The $doc_xml_file is relative to the archives, and now let's get the full path
    605     my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");   
    606     my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
    607    
    608     # Edit the doc.xml file with the specified metadata name, value and position.
    609     # TODO: there is a potential problem here as this edit_doc_xml function
    610     # is assuming the simple doc.xml situation where there is only one Section and no SubSections.
    611     # Running import.pl -groupsize will cause this to have multiple sections in one doc.xml
    612     $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
    613             $metaname,$metavalue,$metapos,$metamode,$docid_secnum);
    614    
     725    }
     726   
     727    my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
     728                $metaname,$metapos,$metavalue,$metamode);
     729   
    615730    # Release the lock once it is done
    616731    $self->unlock_collection($username, $collect);
    617732
    618     my $mess = "set-archives-metadata successful: Key[$docid]\n";
    619     $mess .= "  $metaname";
    620     $mess .= "->[$metapos]" if (defined $metapos);
    621     $mess .= " = $metavalue";
    622     $mess .= " ($metamode)\n";
    623    
    624     $gsdl_cgi->generate_ok_message($mess); 
     733    if ($status == 0) {
     734        my $mess = "set-archives-metadata successful: Key[$docid]\n";
     735        $mess .= "  $metaname";
     736        $mess .= "->[$metapos]" if (defined $metapos);
     737        $mess .= " = $metavalue";
     738        $mess .= " ($metamode)\n";
     739   
     740        $gsdl_cgi->generate_ok_message($mess); 
     741    }
     742    else {
     743        my $mess .= "Failed to set archives metadata key: $docid\n";
     744        $mess .= "Exit status: $status\n";
     745        $mess .= "System Error Message: $!\n";
     746        $mess .= "-" x 20 . "\n";
     747       
     748        $gsdl_cgi->generate_error($mess);
     749    }
     750}
     751
     752
     753sub set_archives_metadata_array
     754{
     755    my $self = shift @_;
     756
     757    my $username  = $self->{'username'};
     758    my $collect   = $self->{'collect'};
     759    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     760    my $gsdlhome  = $self->{'gsdlhome'};
     761
     762    if ($baseaction::authentication_enabled) {
     763    # Ensure the user is allowed to edit this collection
     764    &authenticate_user($gsdl_cgi, $username, $collect);
     765    }
     766
     767    my $site = $self->{'site'};
     768    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     769   
     770    $gsdl_cgi->checked_chdir($collect_dir);
     771
     772    # Obtain the collect dir
     773    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     774
     775    # Make sure the collection isn't locked by someone else
     776    $self->lock_collection($username, $collect);
     777
     778    # look up additional args
     779   
     780    my $infodbtype = $self->{'infodbtype'};
     781
     782   my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
     783   
     784    my $json_str      = $self->{'json'};
     785    my $doc_array = decode_json $json_str;
     786   
     787   
     788    my $global_status = 0;
     789    my $global_mess = "";
     790   
     791    my @all_docids = ();
     792   
     793    foreach my $doc_array_rec ( @$doc_array ) {
     794       
     795        my $docid     = $doc_array_rec->{'docid'};
     796        my $metaname  = $doc_array_rec->{'metaname'};
     797        my $metapos   = $doc_array_rec->{'metapos'};
     798        my $metamode   = $self->{'metamode'};
     799        my $metavalue = $doc_array_rec->{'metavalue'};
     800       
     801        # Some sanity checks
     802        $metapos = 0 if (!defined $metapos);
     803           
     804        if ((!defined $metamode) || ($metamode =~ m/^\s*$/)) {
     805            # make "accumulate" the default (less destructive, as won't actually
     806            # delete any existing values)
     807            $metamode = "accumulate";
     808        }
     809   
     810        push(@all_docids,$docid);
     811       
     812        my $status = $self->set_archives_metadata_entry($gsdl_cgi,$archive_dir, $collect_dir,$collect, $infodbtype,$docid,
     813                $metaname,$metapos,$metavalue,$metamode);
     814       
     815        if ($status != 0) {
     816            # Catch error if set infodb entry failed
     817            $global_status = $status;
     818            $global_mess .= "Failed to set metadata key: $docid\n";
     819            $global_mess .= "Exit status: $status\n";
     820            $global_mess .= "System Error Message: $!\n";
     821            $global_mess .= "-" x 20 . "\n";
     822        }
     823    }
     824
     825    if ($global_status != 0) {
     826        $global_mess .= "PATH: $ENV{'PATH'}\n";
     827        $gsdl_cgi->generate_error($global_mess);
     828    }
     829    else {
     830        my $mess = "set-archives-metadata-array successful: Keys[ ".join(", ",@all_docids)."]\n";
     831        $gsdl_cgi->generate_ok_message($mess);
     832    }
     833   
     834    # Release the lock once it is done
     835    $self->unlock_collection($username, $collect);
    625836}
    626837
Note: See TracChangeset for help on using the changeset viewer.