Changeset 27333


Ignore:
Timestamp:
05/14/13 14:25:55 (8 years ago)
Author:
ak19
Message:
  1. Added the metadataaction::get_import_metadata_array() subroutine for completion and tested it. 2. A couple of unicode subroutines print warnings when parameters are undefined. Since they can be undefined sometimes, added a test for this so as to avoid the unnecessary warnings.
Location:
main/trunk/greenstone2/perllib
Files:
2 edited

Legend:

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

    r27324 r27333  
    270270    }
    271271    elsif($where =~ m/import/) {
    272 #   $self->_get_import_metadata_array(@_);
     272    $self->_get_import_metadata_array(@_);
    273273    }
    274274    elsif($where =~ m/live/) {
     
    277277}
    278278
     279sub _get_import_metadata_array {
     280   
     281    my $self = shift @_;
     282
     283    my $collect   = $self->{'collect'};
     284    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     285    my $site = $self->{'site'};
     286    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     287   
     288    # look up additional args
     289    my $infodbtype = $self->{'infodbtype'};
     290   
     291    my $archive_dir = &util::filename_cat($collect_dir, $collect, "archives");
     292    my $arcinfo_doc_filename = &dbutil::get_infodb_file_path($infodbtype, "archiveinf-doc", $archive_dir);
     293    my $json_str      = $self->{'json'};
     294    my $doc_array = decode_json $json_str;
     295
     296    my $json_result_str = "[";
     297    my $first_doc_rec = 1;
     298    foreach my $doc_array_rec ( @$doc_array ) {
     299   
     300    my $docid = $doc_array_rec->{'docid'}; # no subsection metadata support in metadata.xml, only toplevel meta
     301   
     302    if($first_doc_rec) {
     303        $first_doc_rec = 0;
     304    } else {
     305        $json_result_str .= ",";
     306    }
     307    $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\""; 
     308
     309    my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
     310    $json_result_str = $json_result_str . ",\"metatable\":[";
     311
     312    my $first_rec = 1;
     313    foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps       
     314
     315        # Read the docid entry     
     316        my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $arcinfo_doc_filename, $docid);
     317        # This now stores the full pathname
     318        my $import_filename = $doc_rec->{'src-file'}->[0];
     319
     320        # figure out correct metadata.xml file [?]
     321        # Assuming the metadata.xml file is next to the source file
     322        # Note: This will not work if it is using the inherited metadata from the parent folder
     323        my ($import_tailname, $import_dirname) = File::Basename::fileparse($import_filename);
     324        my $metadata_xml_filename = &util::filename_cat($import_dirname, "metadata.xml");
     325
     326
     327        if($first_rec) {
     328        $first_rec = 0;
     329        } else {
     330        $json_result_str .= ",";       
     331        }
     332       
     333        my $metaname  = $metatable_rec->{'metaname'};
     334        $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
     335
     336        my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
     337        if(!defined $metapos) {
     338        $metapos = 0;
     339        }
     340
     341        # Obtain the specified metadata value(s)
     342        my $metavalue;
     343
     344        if($metapos ne "all") { # get the value at a single metapos
     345        $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
     346
     347        #print STDERR "**** Metafilename, metaname, metapos, sec_num: $metadata_xml_filename, $metaname, $metapos, $import_tailname\n";
     348       
     349        $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
     350
     351        } else {
     352        my $first_metaval = 1;
     353        $metapos = 0;
     354        $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
     355
     356        while (defined $metavalue && $metavalue ne "") {
     357            if($first_metaval) {       
     358            $first_metaval = 0;
     359            } else {
     360            $json_result_str .= ",";
     361            }
     362       
     363            $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
     364
     365            $metapos++;
     366            $metavalue = $self->get_metadata_from_metadata_xml($gsdl_cgi, $metadata_xml_filename, $metaname, $metapos, $import_tailname);
     367        }
     368        }
     369
     370        $json_result_str .= "]}"; # close metavals array and metatable record
     371    }
     372   
     373    $json_result_str .= "]}"; # close metatable array and docid record
     374    }
     375
     376    $json_result_str .= "]"; # close array of docids
     377    $gsdl_cgi->generate_ok_message($json_result_str."\n");
     378}
    279379
    280380# Unused method, but included for completion. Tested, works. Takes a JSON string and returns a JSON string.
     
    654754            if($parser->{'parameters'}->{'metapos'} > 0) {
    655755            print STDERR "@@@@ WARNING: non-zero metapos.\n";
    656             print STDERR "@@@@ Assuming SIMPLE collection and proceeding to retrieve the import meta at".$parser->{'parameters'}->{'metapos'}.".\n";
     756            print STDERR "@@@@ Assuming SIMPLE collection and proceeding to retrieve the import meta at position: ".$parser->{'parameters'}->{'metapos'}.".\n";
    657757            }
    658758            $parser->{'pad'}->{'metavalue'} = $attrHash->{'_content'};
  • main/trunk/greenstone2/perllib/unicode.pm

    r27306 r27333  
    158158    my ($in) = @_;
    159159    my $out = [];
     160
     161    if(!defined $in) {
     162    return $out;
     163    }
    160164
    161165    my $i = 0;
     
    630634    my ($text,$and_numeric_entities) = @_;
    631635
    632     $text =~ s/\%([0-9A-F]{2})/pack('C', hex($1))/ige;
    633 
    634     if ((defined $and_numeric_entities) && ($and_numeric_entities)) {
    635     $text =~ s/\&\#x([0-9A-F]+);/pack('C', hex($1))/ige;
    636     $text =~ s/\&\#u?([0-9]+);/pack('C', $1)/ige;
     636    if(defined $text) {
     637    $text =~ s/\%([0-9A-F]{2})/pack('C', hex($1))/ige;
     638   
     639    if ((defined $and_numeric_entities) && ($and_numeric_entities)) {
     640        $text =~ s/\&\#x([0-9A-F]+);/pack('C', hex($1))/ige;
     641        $text =~ s/\&\#u?([0-9]+);/pack('C', $1)/ige;
     642    }
    637643    }
    638644
Note: See TracChangeset for help on using the changeset viewer.