Ignore:
Timestamp:
09/07/09 11:42:04 (11 years ago)
Author:
davidb
Message:

further development of metadata action

File:
1 edited

Legend:

Unmodified
Added
Removed
  • gsdl/trunk/perllib/cgiactions/metadataaction.pm

    r19499 r20538  
    4545my $action_table =
    4646{
    47     "get-live-metadata"    => { 'compulsory-args' => [ "d", "metaname" ],
    48                     'optional-args'   => [] },
    49 
    50     "get-metadata"         => { 'compulsory-args' => [ "d", "metaname" ],
    51                     'optional-args'   => [ "metapos" ] },
    52 
    53     "set-live-metadata"    => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
    54                     'optional-args'   => [ ] },
    55 
    56     "set-metadata"         => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
    57                     'optional-args'   => [ "metapos" ] },
    58 
    59     "set-import-metadata"  => { 'compulsory-args' => [ "metaname", "metavalue" ],
    60                     'optional-args'   => [ "d", "f", "metamode" ]
    61                    # metamode can be "accumulate", "override", or "unique-id"
     47    "get-live-metadata"     => { 'compulsory-args' => [ "d", "metaname" ],
     48                     'optional-args'   => [] },
     49
     50    "get-metadata"          => { 'compulsory-args' => [ "d", "metaname" ],
     51                     'optional-args'   => [ "metapos" ] },
     52
     53    "set-live-metadata"     => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
     54                     'optional-args'   => [ ] },
     55
     56    "set-metadata"          => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
     57                     'optional-args'   => [ "metapos" ] },
     58
     59    "set-archives-metadata" => { 'compulsory-args' => [ "d", "metaname", "metavalue" ],
     60                     'optional-args'   => [ "metapos" ]
     61
    6262                },
    63     "remove-live-metadata" => { 'compulsory-args' => [ "d", "metaname" ],
    64                     'optional-args'   => [ ] },
    65 
    66     "remove-metadata"      => { 'compulsory-args' => [ "d", "metaname" ],
    67                     'optional-args'   => [ "metapos" ] }
     63
     64    "set-import-metadata"   => { 'compulsory-args' => [ "metaname", "metavalue" ],
     65                     'optional-args'   => [ "d", "f", "metamode" ]
     66                    # metamode can be "accumulate", "override", or "unique-id"
     67                 },
     68
     69
     70    "remove-live-metadata"  => { 'compulsory-args' => [ "d", "metaname" ],
     71                     'optional-args'   => [ ] },
     72
     73    "remove-metadata"       => { 'compulsory-args' => [ "d", "metaname" ],
     74                     'optional-args'   => [ "metapos" ] }
    6875};
    6976
     
    353360
    354361
    355 
    356 
    357 
    358 sub mxml_metadata
     362sub dxml_metadata
    359363{
    360364
     
    383387
    384388
    385 sub mxml_description
     389sub dxml_description
    386390{
    387391
     
    414418}
    415419
     420sub edit_xml_file
     421{
     422    my $self = shift @_;
     423
     424    my ($gsdl_cgi, $filename, $rules, $options) = @_;
     425
     426    # use XML::Rules to add it in (read in and out again)
     427
     428    my $parser = XML::Rules->new(rules => $rules,
     429                 style => 'filter' );
     430
     431    my $xml_in = "";
     432    if (!open(MIN,"<$filename")) {
     433    $gsdl_cgi->generate_error("Unable to read in $filename: $!");
     434    }
     435    else {
     436    my $line;
     437    while (defined ($line=<MIN>)) {
     438        $xml_in .= $line;
     439    }
     440    close(MIN);
     441   
     442   
     443    my $xml_out = "";
     444    $parser->filter($xml_in,\$xml_out, $options);
     445   
     446    if (!open(MOUT,">$filename")) {
     447        $gsdl_cgi->generate_error("Unable to write out to $filename: $!");
     448    }
     449    else {
     450        print MOUT $xml_out;
     451        close(MOUT);
     452       
     453    }
     454    }
     455}
     456
     457
     458
     459sub edit_doc_xml
     460{
     461    my $self = shift @_;
     462
     463    my ($gsdl_cgi, $doc_xml_filename, $metaname, $metavalue, $metapos) = @_;
     464
     465    # use XML::Rules to add it in (read in and out again)
     466
     467    my @rules =
     468    ( _default => 'raw extended',
     469      'Metadata' => \&dxml_metadata,
     470      'Description' => \&dxml_description );
     471     
     472    my $options = { 'metaname'  => $metaname,
     473            'metapos'   => $metapos,
     474            'metavalue' => $metavalue };
     475
     476    $self->edit_xml_file($gsdl_cgi,$doc_xml_filename,\@rules,$options);
     477}
     478
     479
     480sub set_archives_metadata
     481{
     482    my $self = shift @_;
     483
     484    my $username  = $self->{'username'};
     485    my $collect   = $self->{'collect'};
     486    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     487    my $gsdlhome  = $self->{'gsdlhome'};
     488
     489
     490    # don't user authenticate for now
     491    if ($baseaction::authentication_enabled) {
     492    # Ensure the user is allowed to edit this collection
     493    $self->authenticate_user($username, $collect);
     494    }
     495
     496    my $collect_dir = &util::filename_cat($gsdlhome, "collect");
     497#    $gsdl_cgi->checked_chdir($collect_dir);
     498
     499    my $archive_dir = &util::filename_cat($collect_dir,$collect,"archives");
     500
     501    # Make sure the collection isn't locked by someone else
     502    $self->lock_collection($username, $collect);
     503
     504    # look up additional args
     505
     506    my $docid  = $self->{'d'};
     507
     508    my $metaname   = $self->{'metaname'};
     509    my $metavalue  = $self->{'metavalue'};
     510    my $metapos    = $self->{'metapos'};
     511
     512    $metapos = 0 if (!defined $metapos);
     513
     514    my $doc_db = "archiveinf-doc.gdb";
     515    my $arcinfo_doc_filename = &util::filename_cat ($archive_dir, $doc_db);
     516
     517    my $doc_rec = GDBMUtils::gdbmRecordToHash($arcinfo_doc_filename,$docid);
     518
     519    my $doc_xml_file = $doc_rec->{'doc-file'}->[0];
     520
     521   
     522    my $archives_dir = &util::filename_cat($collect_dir,$collect,"archives");
     523   
     524    my $doc_xml_filename = &util::filename_cat($archives_dir,$doc_xml_file);
     525
     526   
     527    $self->edit_doc_xml($gsdl_cgi,$doc_xml_filename,
     528            $metaname,$metavalue,$metapos);
     529
     530}
     531
     532
     533
     534
     535sub mxml_metadata
     536{
     537
     538    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
     539
     540
     541    my $metaname = $parser->{'parameters'}->{'metaname'};
     542    my $metamode = $parser->{'parameters'}->{'metamode'};
     543
     544    my $name_attr = $attrHash->{'name'};
     545
     546    if (($name_attr eq $metaname) && ($metamode eq "override")) {
     547
     548    my $metavalue = $parser->{'parameters'}->{'metavalue'};
     549
     550    $attrHash->{'_content'} = $metavalue;
     551
     552    # Don't want it to wipe out any other pieces of metadata
     553    $parser->{'parameters'}->{'metamode'} = "done";
     554
     555    }
     556
     557    # raw extended
     558    return (':'.$tagname => $attrHash, [$tagname => $attrHash]);
     559}
     560
     561
     562sub mxml_description
     563{
     564
     565    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
     566
     567    my $metamode = $parser->{'parameters'}->{'metamode'};
     568
     569
     570    if ($metamode eq "accumulate") {
     571    # tack a new metadata tag on to the end of the <Metadata>+ block
     572
     573    my $metaname = $parser->{'parameters'}->{'metaname'};
     574    my $metavalue = $parser->{'parameters'}->{'metavalue'};
     575   
     576
     577    my $metadata_attr = { '_content' => $metavalue,
     578                  'name'     => $metaname,
     579                  'mode'     => "accumulate" };
     580
     581    my $append_metadata = [ "Metadata" => $metadata_attr ];
     582
     583    my $description_content = $attrHash->{'_content'};
     584
     585    push(@$description_content,"    ", $append_metadata ,"\n        ");
     586
     587    }
     588
     589    # raw
     590    return $tagname => $attrHash;
     591}
     592
    416593sub edit_metadata_xml
    417594{
    418595    my $self = shift @_;
    419596
    420     my ($gsdl_cgi, $metadata_xml_filename, $file, $metaname, $metavalue, $metamode) = @_;
     597    my ($gsdl_cgi, $metadata_xml_filename, $metaname, $metavalue, $metamode) = @_;
    421598
    422599    # use XML::Rules to add it in (read in and out again)
     
    458635    }
    459636    }
    460 
    461 
    462 
    463 }
     637}
     638
    464639
    465640sub set_import_metadata
     
    524699    my $import_filename = &util::filename_cat($collect_dir,$collect,$import_file);
    525700
    526 
    527     # Assuming that the metadata field is being indexed, then
    528     # **** "touch" (in the Unix sense) $import_filename so we know it needs to
    529     # be reindexed?
    530     # (to be implemented)
    531701   
    532702    # figure out correct metadata.xml file
     
    537707    my $metadata_xml_filename = &util::filename_cat($import_dirname,"metadata.xml");
    538708
    539     $self->edit_metadata_xml($gsdl_cgi,$metadata_xml_filename,$import_tailname,
     709    $self->edit_metadata_xml($gsdl_cgi,$metadata_xml_filename,
    540710                 $metaname,$metavalue,$metamode);
    541711
Note: See TracChangeset for help on using the changeset viewer.