Changeset 27313

Show
Ignore:
Timestamp:
07.05.2013 19:32:42 (6 years ago)
Author:
ak19
Message:

Now the existing user comments to be displayed are all retrieved in one go using the new get_metadata_array subroutine in metadataaction.pm via the new gsajaxapi method getMetadataArray which take a JSON string and return one. This loads user comments much faster, and doesn't get that much slower if the number of comments stored in the index database gets larger. 2 bugfixes to metadataaction.pm's recently added get_metadata_array subroutine: if no metapos supplied it defaults to 0 like the other get_meta functions instead of defaulting to the keyword 'all'. The fieldnames in the JSON string returned also needed to be inside double quotes in order to be successfully parsed back into a JSON object on the Javascript side. Replaced the old loadUserComments() javascript function in style.dm, which now calls the new gsajaxapi.getMetadataArray() post method.

Location:
main/trunk/greenstone2
Files:
3 modified

Legend:

Unmodified
Added
Removed
  • main/trunk/greenstone2/macros/style.dm

    r27310 r27313  
    264264        doc_id = doc_id.substring(0, period); 
    265265    \} 
    266      
    267     var metapos=0; // counter variable 
    268     var username = null; 
    269     var timestamp = null; 
    270     var comment = null; 
    271      
    272     var comments_arraymap = []; // array of comment records with timestamp, username and comment fields 
    273  
    274     // First retrieve all the usercomments (usernames, associated usertimestamps and usercomments) 
    275     // from the archives folder, where it is arranged easier than in index because it's in sequence already. 
    276  
    277     username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 
    278      
    279     // Keep looping, retrieving username, comment and timestamp 
    280     // Store each triplet in the comments array. Stop when there's no further usernames in archives 
    281  
    282     while(username) \{  //&& !username.indexOf("ERROR") == -1) \{  
    283      
    284         var errorIndex = username.indexOf("ERROR"); 
    285         if(errorIndex != -1) \{ 
    286            var endIndex = username.indexOf("("); 
    287            var error = username.substring(errorIndex,endIndex); 
    288            var errormessage=document.createTextNode("Error retrieving comments. " + error); 
    289            usercommentdiv.appendChild(errormessage); 
    290            //alert(username); 
    291            break; 
    292         \} 
    293      
    294         timestamp = gsapi.getDocumentMetadata(doc_id, "usertimestamp", metapos); 
    295         comment = gsapi.getDocumentMetadata(doc_id, "usercomment", metapos); 
    296  
    297         //alert("username: " + username + "\\ntime: " + timestamp + "\\ncomment: " + comment); 
    298  
    299         // array of records (arraymap) 
    300         // http://stackoverflow.com/questions/8190658/sorting-objects-in-a-javascript-array-by-a-key-value 
    301         // http://stackoverflow.com/questions/4969121/in-javascript-is-there-an-easy-way-to-sort-key-value-pairs-by-the-value-and-re 
    302         // http://stackoverflow.com/questions/6298169/how-to-create-a-map-object-in-a-javascript 
    303  
    304         // object with member vars t, u and c. 
    305         var comment_record = \{ 
    306             t: timestamp, 
    307             u: username, 
    308             c: comment 
    309         \}; 
    310  
    311         comments_arraymap.push(comment_record); 
    312              
    313         metapos++; 
    314         username = gsapi.getDocumentMetadata(doc_id, "username", metapos); 
     266 
     267    var username_rec = \{ 
     268        metaname: "username", 
     269        metapos: "all" 
     270    \}; 
     271 
     272    var timestamp_rec = \{ 
     273        metaname: "usertimestamp", 
     274        metapos: "all" 
     275    \}; 
     276 
     277    var comment_rec = \{ 
     278        metaname: "usercomment", 
     279        metapos: "all" 
     280    \}; 
     281 
     282    var doc_rec = \{ 
     283        docid: doc_id, 
     284        metatable: [username_rec, timestamp_rec, comment_rec]        
     285    \}; 
     286 
     287    var docArray = [doc_rec]; 
     288    //alert(JSON.stringify(docArray)); 
     289 
     290    var json_result_str = gsapi.getMetadataArray(docArray, "index"); 
     291//  alert(json_result_str); 
     292    var result = JSON.parse(json_result_str); 
     293    // result contains only one docrec (result[0]), since we asked for the usercomments of one docid 
     294    var metatable = result[0].metatable; 
     295//  alert(JSON.stringify(metatable)); 
     296 
     297    var i = 0; 
     298    var looping = true; 
     299    var print_heading = true; 
     300 
     301    // metatable[0] = list of usernames, metatable[1] = list of timestamps, metatable[2] = list of comments  
     302    // the 3 lists/arrays should be of even length. Assuming this, loop as long as there's another username 
     303    while(looping) \{ 
     304        var metaval_rec = metatable[0].metavals[i]; 
     305        if(metaval_rec == undefined) \{ 
     306            looping = false; 
     307        \}  
     308        else \{ 
     309 
     310            if(print_heading) \{ 
     311                 var heading=document.createElement("div"); 
     312                 var attr=document.createAttribute("class"); 
     313                 attr.nodeValue="usercommentheading"; 
     314                 heading.setAttributeNode(attr); 
     315                 var txt=document.createTextNode("_text-usercomments-section_"); 
     316                 heading.appendChild(txt); 
     317                 usercommentdiv.appendChild(heading); 
     318 
     319                 print_heading = false; 
     320            \} 
     321 
     322                var username = metaval_rec.metavalue; 
     323            var timestamp = metatable[1].metavals[i].metavalue;   
     324            var comment = metatable[2].metavals[i].metavalue;  
     325 
     326            //alert("Comment: " + username + " " + timestamp + " " + comment); 
     327 
     328            // No need to sort by time, as the meta are already stored sorted  
     329            // and hence retrieved in the right order by using the i (metapos) counter 
     330            // If sorting the array of comment records, which would be by timestamp, see 
     331            // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort 
     332 
     333             // for each usercomment, create a child div with the username, timestamp and comment 
     334             displayInUserCommentList(usercommentdiv, username, timestamp, comment); 
     335              
     336             i++; 
     337         \}      
    315338    \} 
    316339 
    317     // No need to sort by time,  
    318     // as the array is already stored sorted and hence retrieved in the right order by using metapos counter 
    319     // If sorting the array of comment records, which would be by timestamp, see 
    320     // https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/sort 
    321  
    322     //comments_arraymap.sort(function(rec1, rec2) \{ 
    323     // return rec1.t-rec2.t; // sort by timestamp 
    324     //\}); 
    325      
    326     //alert("Found " + metapos + " number of comments in the archives folder for doc " + doc_id); 
    327      
    328     // create the output HTML for all comments in one go now 
    329     // (we do this here for all values together to hopefully avoid reloading the page for each comment) 
    330  
    331     if(metapos > 0) \{  
    332         var heading=document.createElement("div"); 
    333         var attr=document.createAttribute("class"); 
    334         attr.nodeValue="usercommentheading"; 
    335         heading.setAttributeNode(attr); 
    336         var txt=document.createTextNode("_text-usercomments-section_"); 
    337         heading.appendChild(txt); 
    338         usercommentdiv.appendChild(heading); 
    339     \} 
    340  
    341     for(var i = 0; i < metapos; i++) \{ 
    342         var comment_record = comments_arraymap[i]; 
    343  
    344         username = comment_record.u; 
    345         timestamp = comment_record.t; 
    346         comment = comment_record.c; 
    347  
    348         // for each usercomment, create a child div with the username, timestamp and comment 
    349         displayInUserCommentList(usercommentdiv, username, timestamp, comment); 
    350     \} 
    351  
    352      
    353340    \} 
     341 
    354342 
    355343    function displayInUserCommentList(usercommentdiv, username, timestamp, comment) \{ 
  • main/trunk/greenstone2/perllib/cgiactions/metadataaction.pm

    r27312 r27313  
    270270 
    271271# takes a JSON string and returns a JSON string 
     272# resulting string is of the form: 
     273# [{"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"}]}]}] 
    272274sub _get_index_metadata_array 
    273275{ 
     
    304306        $json_result_str .= ","; 
    305307    } 
    306     $json_result_str = $json_result_str . "{docid:\"" . $docid . "\"";   
     308    $json_result_str = $json_result_str . "{\"docid\":\"" . $docid . "\"";   
    307309 
    308310    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:["; 
     311    $json_result_str = $json_result_str . ",\"metatable\":["; 
    310312 
    311313    my $first_rec = 1; 
     
    318320         
    319321        my $metaname  = $metatable_rec->{'metaname'}; 
    320         $json_result_str .= "{metaname:\"$metaname\",metavals:["; 
     322        $json_result_str .= "{\"metaname\":\"$metaname\",\"metavals\":["; 
    321323 
    322324        my $metapos   = $metatable_rec->{'metapos'}; # 0... 1|all|undefined 
    323325        if(!defined $metapos) { 
    324         $metapos = "all"; 
     326        $metapos = 0; 
    325327        } 
    326328 
     
    344346 
    345347        $metavalue = $doc_rec->{$metaname}->[$metapos];      
    346         $json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}"; 
     348        $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}"; 
    347349 
    348350        } else { 
     
    358360            } 
    359361         
    360             $json_result_str .= "{metapos:\"$metapos\",metavalue:\"$metavalue\"}"; 
     362            $json_result_str .= "{\"metapos\":\"$metapos\",\"metavalue\":\"$metavalue\"}"; 
    361363 
    362364            $metapos++; 
  • main/trunk/greenstone2/web/script/gsajaxapi.js

    r27281 r27313  
    271271    } 
    272272 
     273    this.getMetadataArray = function(docArray,where) 
     274    { 
     275    docArrayJSON = JSON.stringify(docArray); 
     276 
     277    var mdserver = this.metadataserverURL(); 
     278     
     279    var params = "a=" + escape("get-metadata-array"); //"a=set-metadata-array"; 
     280    if(where != null) { 
     281        params += "&where=" + escape(where); // if where not specified, meta-server will default to setting index meta 
     282        //} else { 
     283        //    params += "&where=import|archives|index"; 
     284    } 
     285    params += "&c="+escape(collect_); 
     286    params += "&json="+escape(docArrayJSON); 
     287     
     288    //this.urlGetSync(mdserver + "?" + params); 
     289    return this.urlPostSync(mdserver,params);    
     290    } 
     291 
     292 
    273293    this._setMetadata = function(mode,docid,metaname,metapos,metavalue,metamode) 
    274294    {