Changeset 27173


Ignore:
Timestamp:
04/11/13 18:56:35 (8 years ago)
Author:
ak19
Message:

First commit to do with setting and removing import metadata. 1. Now set_import_meta adds a new FileSet into metadata.xml when there's no FileSet for a given filename. 2. As metadata.xml doesn't support subsections, this prints a warning message. 3. empty strings used for toplevel subsections instead of undef, not only to avoid warnings but also because this makes the check for whether a current subsection is equal to the subsection we're looking for easier when the topsection is also a string (as is the case elsewhere). 4. Fixed references to a generate_error_message subroutine of gsdlcgi which ought to be generate_error instead. Second commit aims to add support for metapos and prevmetavalue to the import_metadata subroutines set and remove and maybe get too, to mirror what the equivalent archives_metadata subroutines do.

File:
1 edited

Legend:

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

    r27168 r27173  
    317317    if (!defined $docid)
    318318    {
    319         $gsdl_cgi->generate_error_message("No docid (d=...) specified.\n");
     319        $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
    320320    }
    321321
     
    660660    # want either d= or f=
    661661    my $docid  = $self->{'d'};
     662    my ($docid_root,$docid_secnum);
     663    if(defined $docid) {   
     664    ($docid_root,$docid_secnum) = ($docid =~ m/^(.*?)(\..*)?$/);   
     665    # as yet no support for setting subsection metadata in metadata.xml
     666    if ((defined $docid_secnum) && ($docid_secnum !~ m/^\s*$/)) {
     667        $gsdl_cgi->generate_ok_message("*** Unable to set import metadata for subsections at this stage.\n");
     668        return;
     669    }
     670    }
     671
    662672    my $import_file  = $self->{'f'};
    663673    if ((!defined $docid) && (!defined $import_file)) {
     
    684694    if (defined $docid) {
    685695    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
    686     my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
     696    my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid_root);
    687697
    688698    # This now stores the full pathname
     
    706716    # TODO: This doesn't handle the common metadata (where FileName doesn't point to a single file)
    707717    $self->edit_metadata_xml($gsdl_cgi, $metadata_xml_filename,
    708                              $metaname, $metavalue, $metamode, $import_tailname);
     718                 $metaname, $metavalue, $metamode, $import_tailname);
    709719
    710720
     
    13001310
    13011311    my $curr_secnum = $parser->{'parameters'}->{'curr_section_num'};
    1302     my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'};
     1312    my $opt_doc_secnum = $parser->{'parameters'}->{'secnum'} || "";
    13031313   
    13041314    print STDERR "**** Processing a closing </Description> tag \n";
     
    13291339        }
    13301340
    1331         # If get to here and metamode is override, the this means there
     1341        # If we get to here and metamode is override, this means there
    13321342        # was no existing value to overide => treat as an append operation
    13331343       
     
    18091819        rules => \@rules,
    18101820        style => 'filter',
    1811         output_encoding => 'utf8'
     1821        output_encoding => 'utf8',
     1822#    normalisespaces => 1, # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
     1823        stripspaces => 2|0|0 # ineffectual
    18121824    );
    18131825   
     
    18491861    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
    18501862
    1851     if (!($parser->{'parameters'}->{'secid'} eq $parser->{'parameters'}->{'curr_section_num'}))
     1863    # For comparisons, toplevel section is indicated by ""
     1864    my $curr_sec_num = $parser->{'parameters'}->{'curr_section_num'} || "";
     1865    my $secid = $parser->{'parameters'}->{'secid'} || "";
     1866
     1867    if (!($secid eq $curr_sec_num))
    18521868    {
    18531869        # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
     
    18931909
    18941910    # Report error if we don't see FileName tag before this
    1895     die "Fatal Error: Unexpected metadata.xml structure. Undefind current_file, possiblely encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
     1911    die "Fatal Error: Unexpected metadata.xml structure. Undefined current_file, possibly encountered Description before FileName" if (!defined($parser->{'parameters'}->{'current_file'}));
    18961912   
    18971913    # Don't do anything if we are not in the right FileSet
     
    19011917    # file. 
    19021918    # So, skip anything with a .* in it as it is too general
     1919#   print STDERR "@@@@ Skipping entry in metadata.xml where FileName=.* as it is too general\n";
    19031920    return [$tagname => $attrHash];
    19041921    }
     
    19161933    $attrHash->{'_content'} = $metavalue;
    19171934
    1918 ##  print STDERR "**** overrideing metadata.xml\n";
     1935##  print STDERR "**** overriding metadata.xml\n";
    19191936
    19201937    # Don't want it to wipe out any other pieces of metadata
     
    20162033}
    20172034
     2035sub mxml_directorymetadata
     2036{
     2037    my ($tagname, $attrHash, $contextArray, $parentDataArray, $parser) = @_;
     2038
     2039    # if we haven't processed the metadata when we reach the end of metadata.xml
     2040    # it's because there's no particular FileSet element whose FileName matched
     2041    # In which case, add a new FileSet for this FileName
     2042    my $metamode = $parser->{'parameters'}->{'metamode'};
     2043    if($metamode ne "done") {
     2044   
     2045    if ($metamode eq "override") {
     2046        print "No metadata value to override.  Switching 'metamode' to accumulate\n";
     2047    }
     2048
     2049    # If we get to here and metamode is override, this means there
     2050    # was no existing value to overide => treat as an append operation
     2051   
     2052    # Create a new FileSet element and append to DirectoryMetadata
     2053    # <FileSet>
     2054    # <FileName>src_file</FileName>
     2055    # <Description>
     2056    # <Metadata mode="" name="">metavalue</Metadata>
     2057    # </Description>
     2058    # </FileSet>
     2059    my $src_file = $parser->{'parameters'}->{'src_file'};
     2060    my $metaname = $parser->{'parameters'}->{'metaname'};
     2061    my $metavalue = $parser->{'parameters'}->{'metavalue'};
     2062    my $metadata_attr = {
     2063        '_content' => $metavalue,
     2064        'name' => $metaname,
     2065        'mode' => "accumulate"
     2066    };
     2067    my $append_metadata = [ "Metadata" => $metadata_attr ];
     2068    my $description_attr->{'_content'} = [ "\n\t\t   ", $append_metadata, "\n\t\t"];
     2069    my $description_element = [ "Description" => $description_attr ];
     2070
     2071    #_content is not an attribute, it's special and holds the children of this element
     2072    # including the textnode value embedded in this element if any.
     2073    my $filename_attr = {'_content' => $src_file};
     2074    my $filename_element = [ "FileName" => $filename_attr ];
     2075
     2076    my $fileset_attr = {};
     2077    $fileset_attr->{'_content'} = [ "\n\t\t", $filename_element,"\n\t\t",$description_element ,"\n\t" ];
     2078    my $fileset = [ "FileSet" => $fileset_attr ]; #my $fileset = [ "FileSet" => {} ];
     2079   
     2080   
     2081    # get children of dirmeta, and push the new FileSet element onto it
     2082    print "Appending metadata to metadata.xml\n";
     2083    my $dirmeta_content = $attrHash->{'_content'};
     2084    if (ref($dirmeta_content)) {
     2085        # got some existing interesting nested content
     2086        #push(@$dirmeta_content, "    ", $fileset ,"\n        ");
     2087        push(@$dirmeta_content, "\t", $fileset ,"\n");
     2088    }
     2089    else {
     2090        #description_content is most likely a string such as "\n"
     2091        #$attrHash->{'_content'} = [$dirmeta_content, "    ", $fileset ,"\n" ];
     2092        $attrHash->{'_content'} = [$dirmeta_content, "\t", $fileset ,"\n" ];
     2093    }   
     2094
     2095    $parser->{'parameters'}->{'metamode'} = "done";
     2096    }
     2097    # RAW is [$tagname => $attrHash] not $tagname => $attrHash!!
     2098    return [$tagname => $attrHash];
     2099}
     2100
    20182101
    20192102sub edit_metadata_xml
     
    20282111      'Metadata' => \&mxml_metadata,
    20292112      'Description' => \&mxml_description,
    2030           'FileSet' => \&mxml_fileset);
     2113          'FileSet' => \&mxml_fileset,
     2114      'DirectoryMetadata' => \&mxml_directorymetadata);
    20312115
    20322116    # use XML::Rules to add it in (read in and out again)
    20332117    my $parser = XML::Rules->new(rules => \@rules,
    20342118                 style => 'filter',
    2035                                  output_encoding => 'utf8');
     2119                                 output_encoding => 'utf8',
     2120                 stripspaces => 2|0|0); # http://search.cpan.org/~jenda/XML-Rules-1.16/lib/XML/Rules.pm
    20362121
    20372122    if (!-e $metadata_xml_filename) {
     
    22762361    if ((!defined $docid) || ($docid =~ m/^\s*$/))
    22772362    {
    2278         $gsdl_cgi->generate_error_message("No docid (d=...) specified.\n");
     2363        $gsdl_cgi->generate_error("No docid (d=...) specified.\n");
    22792364    }
    22802365   
     
    22982383    if((!defined $import_filename) || ($import_filename =~ m/^\s*$/))
    22992384    {
    2300         $gsdl_cgi->generate_error_message("There is no metadata\n");
     2385        $gsdl_cgi->generate_error("There is no metadata\n");
    23012386    }
    23022387   
     
    23582443        rules => \@rules,
    23592444        style => 'filter',
    2360         output_encoding => 'utf8'
     2445        output_encoding => 'utf8',
     2446     #normalisespaces => 1,
     2447            stripspaces => 2|0|0 # ineffectual
    23612448    );
    23622449   
     
    23962483    if ((defined $parser->{'parameters'}->{'metavalue'}) && ($parser->{'parameters'}->{'src_file'} eq $parser->{'parameters'}->{'current_file'}) && ($attrHash->{'name'} eq $parser->{'parameters'}->{'metaname'}) && ($attrHash->{'_content'} eq $parser->{'parameters'}->{'metavalue'}))
    23972484    {
    2398         return [];
     2485        return [];
    23992486    }
    24002487
Note: See TracChangeset for help on using the changeset viewer.