Changeset 27336


Ignore:
Timestamp:
05/14/13 20:45:03 (8 years ago)
Author:
ak19
Message:
  1. Fixed regex to retrieve collect_tail. It's unused at present, but appeared to be wrong. 2. Added metadataaction::get-live-metadata-array() subroutine. Tested and works, but not sure if this is exactly how this particular method is meant to behave.
File:
1 edited

Legend:

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

    r27333 r27336  
    259259    }
    260260
    261     # Only when setting metadata do we perform authentication and do we lock the collection
     261    # Only when setting metadata do we perform authentication and do we lock the collection,
    262262    # not when getting metadata
    263263
     
    273273    }
    274274    elsif($where =~ m/live/) {
    275 #       $self->_get_live_metadata_array(@_);
    276     }
    277 }
    278 
     275        $self->_get_live_metadata_array(@_);
     276    }
     277}
     278
     279# Unused at present. Added for completion. Tested.
    279280sub _get_import_metadata_array {
    280281   
     
    480481}
    481482
    482 # takes a JSON string and returns a JSON string
    483 # resulting string is of the form:
    484 # [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
    485 sub _get_index_metadata_array
     483
     484# Unused at present. Added for completion. Tested, but not sure if it retrieves metadata in the manner it's expected to.
     485sub _get_live_metadata_array
    486486{
    487487    my $self = shift @_;
     
    498498    # Obtain the path to the database
    499499    my $collect_tail = $collect;
    500     $collect_tail =~ s/^.*[\/\\]//;
     500    $collect_tail =~ s/^.*[\/|\\]//;
    501501    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    502     my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     502    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
    503503
    504504    my $json_str      = $self->{'json'};
     
    518518    }
    519519    $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\""; 
    520 
     520   
    521521    my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
    522522    $json_result_str = $json_result_str . ",\"metatable\":[";
    523 
     523   
    524524    my $first_rec = 1;
    525525    foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps       
     
    532532        my $metaname  = $metatable_rec->{'metaname'};
    533533        $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
     534       
     535        # Generate the dbkey
     536        my $dbkey = "$docid.$metaname";
     537       
     538        # metapos for get_live_metadata is always assumed to be "all".
     539        # It's always going to get all the lines of metavalues associated with a metaname
     540        # (It's the metaname itself that should contain an increment number, if there are to be multiple values).
     541        #my $metapos = "all";
     542        my $metapos = $metatable_rec->{'metapos'} || 0; # Can be 0... 1|all|undefined. Defaults to 0 if undefined/false
     543        my $metavalue = "";
     544       
     545        # Obtain the content of the key
     546        my $cmd = "gdbmget $infodb_file_path $dbkey";
     547        if (open(GIN,"$cmd |") != 0) { # Success.
     548       
     549        binmode(GIN,":utf8");
     550        # Read everything in and concatenate them into $metavalue       
     551        my $line;
     552        my $first_metaval = 1;
     553        my $pos = 0;
     554        while (defined ($line=<GIN>)) {
     555            chomp($line); # Get rid off the tailing newlines
     556           
     557            if($metapos eq "all") {
     558            if($first_metaval) {       
     559                $first_metaval = 0;
     560            } else {
     561                $json_result_str .= ",";
     562            }           
     563            $metavalue = $line;
     564            $json_result_str .= "{\"metapos\":\"$pos\",\"metavalue\":\"$metavalue\"}";
     565            } elsif($metapos == $pos) {
     566            $metavalue = $line;
     567            $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}";
     568            last;
     569            } # else, the current $pos is not the required $metapos
     570            $pos += 1;
     571        }
     572        close(GIN);
     573        } # else open cmd == 0 (failed) and metavals array will be empty [] for this metaname
     574       
     575        $json_result_str .= "]}"; # close metavals array and metatable record
     576    }
     577   
     578    $json_result_str .= "]}"; # close metatable array and docid record
     579    }
     580
     581    $json_result_str .= "]"; # close array of docids
     582   
     583    $gsdl_cgi->generate_ok_message($json_result_str."\n");   
     584}
     585
     586
     587# Takes a JSON string and returns a JSON string
     588# Request string is of the form:
     589# http://localhost:8283/greenstone/cgi-bin/metadata-server.pl?a=get-metadata-array&c=demo&where=index&json=[{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metapos":"all"},{"metaname":"usertimestamp","metapos":"all"}, {"metaname":"usercomment","metapos":"all"}]}]
     590# Resulting string is of the form:
     591# [{"docid":"HASHc5bce2d6d3e5b04e470ec9","metatable":[{"metaname":"username","metavals":[{"metapos":"0","metavalue":"me"},{"metapos":"1","metavalue":"admin"}]},{"metaname":"usertimestamp","metavals":[{"metapos":"0","metavalue":"1367900586888"},{"metapos":"1","metavalue":"1367900616574"}]},{"metaname":"usercomment","metavals":[{"metapos":"0","metavalue":"Hi"},{"metapos":"1","metavalue":"Hello"}]}]}]
     592sub _get_index_metadata_array
     593{
     594    my $self = shift @_;
     595
     596    my $collect   = $self->{'collect'};
     597    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     598    my $site = $self->{'site'};
     599    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     600
     601    # look up additional args   
     602    my $infodbtype = $self->{'infodbtype'};
     603   
     604    # To people who know $collect_tail please add some comments
     605    # Obtain the path to the database
     606    my $collect_tail = $collect;
     607    $collect_tail =~ s/^.*[\/|\\]//;
     608    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     609    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     610
     611    my $json_str      = $self->{'json'};
     612    my $doc_array = decode_json $json_str;
     613
     614    my $json_result_str = "[";
     615    my $first_doc_rec = 1;
     616
     617    foreach my $doc_array_rec ( @$doc_array ) {
     618   
     619    my $docid     = $doc_array_rec->{'docid'};
     620   
     621    if($first_doc_rec) {
     622        $first_doc_rec = 0;
     623    } else {
     624        $json_result_str .= ",";
     625    }
     626    $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\""; 
     627
     628    my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
     629    $json_result_str = $json_result_str . ",\"metatable\":[";
     630
     631    my $first_rec = 1;
     632    foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps       
     633        if($first_rec) {
     634        $first_rec = 0;
     635        } else {
     636        $json_result_str .= ",";       
     637        }
     638       
     639        my $metaname  = $metatable_rec->{'metaname'};
     640        $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":[";
    534641
    535642        my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
     
    613720    # Obtain path to the database
    614721    my $collect_tail = $collect;
    615     $collect_tail =~ s/^.*[\/\\]//;
     722    $collect_tail =~ s/^.*[\/|\\]//;
    616723    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    617724    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     
    8981005    # Obtain path to the database
    8991006    my $collect_tail = $collect;
    900     $collect_tail =~ s/^.*[\/\\]//;
     1007    $collect_tail =~ s/^.*[\/|\\]//;
    9011008    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    9021009    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
     
    9531060    # Obtain path to the database
    9541061    my $collect_tail = $collect;
    955     $collect_tail =~ s/^.*[\/\\]//;
     1062    $collect_tail =~ s/^.*[\/|\\]//;
    9561063    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    9571064    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     
    14341541    # Obtain path to the database
    14351542    my $collect_tail = $collect;
    1436     $collect_tail =~ s/^.*[\/\\]//;
     1543    $collect_tail =~ s/^.*[\/|\\]//;
    14371544    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    14381545    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
     
    29783085    # Obtain the live gdbm_db path
    29793086    my $collect_tail = $collect;
    2980     $collect_tail =~ s/^.*[\/\\]//;
     3087    $collect_tail =~ s/^.*[\/|\\]//;
    29813088    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    29823089    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, "live-$collect_tail", $index_text_directory);
     
    30893196    # Obtain the path to the database
    30903197    my $collect_tail = $collect;
    3091     $collect_tail =~ s/^.*[\/\\]//;
     3198    $collect_tail =~ s/^.*[\/|\\]//;
    30923199    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    30933200    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     
    32853392    # Obtain path to the database
    32863393    my $collect_tail = $collect;
    3287     $collect_tail =~ s/^.*[\/\\]//;
     3394    $collect_tail =~ s/^.*[\/|\\]//;
    32883395    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
    32893396    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
Note: See TracChangeset for help on using the changeset viewer.