Changeset 27312


Ignore:
Timestamp:
05/07/13 17:07:03 (8 years ago)
Author:
ak19
Message:

get_metadata_array which takes a JSON string and returns one, implemented for getting index metadata. archives, import and live will be implemented later after testing the current implementation against the javascript for retrieving user added comments.

File:
1 edited

Legend:

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

    r27295 r27312  
    6666        'optional-args'   => [ ] },
    6767
     68    "get-metadata-array" => { # where param can be ONE of: index (default), import, archives, live
     69        'compulsory-args' => [ "json" ],
     70        'optional-args'   => [ "where" ],
     71        'help-string' => [
     72        '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"}]}]'
     73        ]},
     74
    6875    #SET METHODS
    6976    "set-live-metadata" => {
     
    238245    $self->get_index_metadata(@_);
    239246}
     247
     248# JSON version that will get the requested metadata values
     249# from the requested source (index, import, archives or live)
     250# One of the params is a JSON string and the return value is JSON too
     251# http://forums.asp.net/t/1844684.aspx/1 - Web api method return json in string
     252sub get_metadata_array
     253{
     254    my $self = shift @_;
     255
     256    my $where = $self->{'where'};
     257    if (!$where) { 
     258    $where = "index"; # default behaviour is to get the values from index
     259    }
     260
     261    # Only when setting metadata do we perform authentication and do we lock the collection
     262    # not when getting metadata
     263
     264    # for get_meta_array, the where param can only be ONE of import, archives, index, live
     265    if($where =~ m/index/) {
     266    $self->_get_index_metadata_array(@_);
     267    }
     268    # still to be implemented for archives, import and live
     269}
     270
     271# takes a JSON string and returns a JSON string
     272sub _get_index_metadata_array
     273{
     274    my $self = shift @_;
     275
     276    my $collect   = $self->{'collect'};
     277    my $gsdl_cgi  = $self->{'gsdl_cgi'};
     278    my $site = $self->{'site'};
     279    my $collect_dir = $gsdl_cgi->get_collection_dir($site);
     280
     281    # look up additional args   
     282    my $infodbtype = $self->{'infodbtype'};
     283   
     284    # To people who know $collect_tail please add some comments
     285    # Obtain the path to the database
     286    my $collect_tail = $collect;
     287    $collect_tail =~ s/^.*[\/\\]//;
     288    my $index_text_directory = &util::filename_cat($collect_dir,$collect,"index","text");
     289    my $infodb_file_path = &dbutil::get_infodb_file_path($infodbtype, $collect_tail, $index_text_directory);
     290
     291    my $json_str      = $self->{'json'};
     292    my $doc_array = decode_json $json_str;
     293
     294    my $json_result_str = "[";
     295    my $first_doc_rec = 1;
     296
     297    foreach my $doc_array_rec ( @$doc_array ) {
     298   
     299    my $docid     = $doc_array_rec->{'docid'};
     300   
     301    if($first_doc_rec) {
     302        $first_doc_rec = 0;
     303    } else {
     304        $json_result_str .= ",";
     305    }
     306    $json_result_str = $json_result_str . "{docid:\"" . $docid . "\""; 
     307
     308    my $metatable = $doc_array_rec->{'metatable'}; # a subarray, or need to generate an error saying JSON structure is wrong
     309    $json_result_str = $json_result_str . ",metatable:[";
     310
     311    my $first_rec = 1;
     312    foreach my $metatable_rec ( @$metatable ) { # the subarray metatable is an array of hashmaps       
     313        if($first_rec) {
     314        $first_rec = 0;
     315        } else {
     316        $json_result_str .= ",";       
     317        }
     318       
     319        my $metaname  = $metatable_rec->{'metaname'};
     320        $json_result_str .= "{metaname:\"$metaname\",metavals:[";
     321
     322        my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined
     323        if(!defined $metapos) {
     324        $metapos = "all";
     325        }
     326
     327         # Read the docid entry
     328        my $doc_rec = &dbutil::read_infodb_entry($infodbtype, $infodb_file_path, $docid);
     329 
     330        # Basically loop through and unescape_html the values
     331        foreach my $k (keys %$doc_rec) {
     332        my @escaped_v = ();
     333        foreach my $v (@{$doc_rec->{$k}}) {
     334            my $ev = &ghtml::unescape_html($v);
     335            push(@escaped_v, $ev);
     336        }
     337        $doc_rec->{$k} = \@escaped_v;
     338        }
     339
     340        # Obtain the specified metadata value(s)
     341        my $metavalue;
     342
     343        if($metapos ne "all") { # get the value at a single metapos
     344
     345        $metavalue = $doc_rec->{$metaname}->[$metapos];     
     346        $json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}";
     347
     348        } else {
     349        my $first_metaval = 1;
     350        $metapos = 0;
     351        $metavalue = $doc_rec->{$metaname}->[$metapos];
     352
     353        while (defined $metavalue) {
     354            if($first_metaval) {       
     355            $first_metaval = 0;
     356            } else {
     357            $json_result_str .= ",";
     358            }
     359       
     360            $json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}";
     361
     362            $metapos++;
     363            $metavalue = $doc_rec->{$metaname}->[$metapos];         
     364        }
     365        }
     366
     367        $json_result_str .= "]}"; # close metavals array and metatable record
     368    }
     369   
     370    $json_result_str .= "]}"; # close metatable array and docid record
     371    }
     372
     373    $json_result_str .= "]"; # close array of docids
     374
     375    $gsdl_cgi->generate_ok_message($json_result_str);   
     376}
     377
    240378
    241379sub get_index_metadata
Note: See TracChangeset for help on using the changeset viewer.