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/explodeaction.pm

    r23768 r24071  
    3434use util;
    3535
     36use JSON;
     37
    3638use File::Basename;
    3739
     
    5052my $action_table =
    5153{
    52     "explode"          => { 'compulsory-args' => ["d"],
    53                 'optional-args'   => [] }
     54    "explode-document"          => { 'compulsory-args' => ["d"],
     55                'optional-args'   => [] },
     56    "delete-document"           => { 'compulsory-args' => ["d"],
     57                'optional-args'   => [ "onlyadd" ] },
     58    "delete-document-array"     => { 'compulsory-args' => ["json"],
     59                'optional-args'   => [ "onlyadd" ] }
     60               
     61               
    5462};
    5563
     
    257265
    258266
    259 sub explode
     267sub remove_docoids
     268{
     269    my $self = shift @_;
     270    my ($docids) = @_;
     271
     272    my $collect   = $self->{'collect'};
     273    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     274    my $infodb_type = $self->{'infodbtype'};
     275
     276    # Derive the archives and import directories
     277    my $site = $self->{'site'};
     278    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     279   
     280    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
     281
     282    # Obtain the doc.xml path for the specified docID
     283    my $arcinfo_doc_filename
     284    = &dbutil::get_infodb_file_path($infodb_type, "archiveinf-doc",
     285                    $archive_dir);
     286
     287    foreach my $docid (@$docids) {
     288
     289        my $doc_rec
     290            = &dbutil::read_infodb_entry($infodb_type, $arcinfo_doc_filename,
     291                     $docid);
     292
     293        my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
     294
     295        # The $doc_xml_file is relative to the archives, so need to do
     296        # a bit more work to make sure the right folder containing this
     297        # is moved to the right place in the import folder
     298
     299        my $assoc_path = dirname($doc_xml_file);
     300        my $archive_assoc_dir = &util::filename_cat($archive_dir,$assoc_path);
     301
     302        &util::rm_r($archive_assoc_dir)
     303    }
     304}
     305
     306
     307sub explode_document
    260308{
    261309    my $self = shift @_;
     
    309357
    310358
     359sub delete_document_entry
     360{
     361    my $self = shift @_;
     362    my ($docid_root,$opt_onlyadd) = @_;
     363   
     364    my $docid_keys = [];
     365    if ((defined $opt_onlyadd) && ($opt_onlyadd==1)) {
     366        # delete docoid archive folder
     367        push(@$docid_keys,$docid_root);
     368    }
     369    else {
     370        print STDERR "**** Not currently implemented for the general case!!\nDeleting 'archive' version only.";
     371       
     372        push(@$docid_keys,$docid_root);
     373       
     374        #my $orig_import_filenames = $self->docid_to_import_filenames($docid_root);
     375        #$docid_keys = $self->import_filenames_to_docids($orig_import_filenames);
     376        #my $expanded_import_filenames = $self->docid_to_import_filenames(@$docid_keys);
     377       
     378        # need to remove only the files that are not
     379       
     380        #$self->remove_import_filenames($expanded_import_filenames);
     381    }
     382   
     383    $self->remove_docoids($docid_keys);
     384}
     385   
     386
     387sub delete_document
     388{
     389    my $self = shift @_;
     390
     391    my $username  = $self->{'username'};
     392    my $collect   = $self->{'collect'};
     393    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     394    my $gsdl_home  = $self->{'gsdlhome'};
     395
     396    # Authenticate user if it is enabled
     397    if ($baseaction::authentication_enabled) {
     398    # Ensure the user is allowed to edit this collection
     399    &authenticate_user($gsdl_cgi, $username, $collect);
     400    }
     401   
     402    # Derive the archives dir   
     403    my $site = $self->{'site'};
     404    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     405   
     406    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
     407    ##my $archive_dir = &util::filename_cat($ENV{'GSDLCOLLECTDIR'},"archives");
     408
     409    # Make sure the collection isn't locked by someone else
     410    $self->lock_collection($username, $collect);
     411
     412    # look up additional args
     413    my $docid  = $self->{'d'};
     414    if ((!defined $docid) || ($docid =~ m/^\s*$/)) {
     415    $self->unlock_collection($username, $collect);
     416    $gsdl_cgi->generate_error("No docid (d=...) specified.");
     417    }
     418
     419    my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
     420
     421    my $onlyadd = $self->{'onlyadd'};
     422
     423    my $status = $self->delete_document_entry($docid_root,$onlyadd);   
     424
     425    # Release the lock once it is done
     426    $self->unlock_collection($username, $collect);
     427
     428    my $mess = "delete-document successful: Key[$docid_root]\n";
     429    $gsdl_cgi->generate_ok_message($mess);
     430
     431}
     432
     433
     434sub delete_document_array
     435{
     436    my $self = shift @_;
     437
     438    my $username  = $self->{'username'};
     439    my $collect   = $self->{'collect'};
     440    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     441    my $gsdlhome  = $self->{'gsdlhome'};
     442
     443    if ($baseaction::authentication_enabled) {
     444    # Ensure the user is allowed to edit this collection
     445    &authenticate_user($gsdl_cgi, $username, $collect);
     446    }
     447
     448    my $site = $self->{'site'};
     449    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     450   
     451    $gsdl_cgi->checked_chdir($collect_dir);
     452
     453    # Obtain the collect dir
     454    ## my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     455
     456    # Make sure the collection isn't locked by someone else
     457    $self->lock_collection($username, $collect);
     458
     459    # look up additional args
     460   
     461    my $json_str      = $self->{'json'};
     462    my $doc_array = decode_json $json_str;
     463
     464    my $onlyadd = $self->{'onlyadd'};
     465   
     466   
     467    my $global_status = 0;
     468    my $global_mess = "";
     469   
     470    my @all_docids = ();
     471   
     472    foreach my $doc_array_rec ( @$doc_array ) {
     473       
     474        my $docid     = $doc_array_rec->{'docid'};
     475       
     476        push(@all_docids,$docid);
     477       
     478        my ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);
     479         
     480        my $status = $self->delete_document_entry($docid_root,$onlyadd);   
     481       
     482        if ($status != 0) {
     483            # Catch error if set infodb entry failed
     484            $global_status = $status;
     485            $global_mess .= "Failed to delete document key: $docid\n";
     486            $global_mess .= "Exit status: $status\n";
     487            $global_mess .= "System Error Message: $!\n";
     488            $global_mess .= "-" x 20;
     489        }
     490    }
     491
     492    if ($global_status != 0) {
     493        $global_mess .= "PATH: $ENV{'PATH'}\n";
     494        $gsdl_cgi->generate_error($global_mess);
     495    }
     496    else {
     497        my $mess = "delete-document-array successful: Keys[ ".join(", ",@all_docids)."]\n";
     498        $gsdl_cgi->generate_ok_message($mess);
     499    }
     500   
     501    # Release the lock once it is done
     502    $self->unlock_collection($username, $collect);
     503}
     504
     505
    311506
    3125071;
Note: See TracChangeset for help on using the changeset viewer.