_binary From a29aaf5adb0a74ad8706a178d79d29a01e695ff4 Mon Sep 17 00:00:00 2001
From: Sandhya Vissapragada <sandyvissa@gmail.com>
Date: Tue, 30 Oct 2012 18:43:13 -0700
Subject: [PATCH] git add scripts/suggest.js git add views/search_view.php

---
 scripts/suggest.js    |  175 ++++++++++++++++++++++++++++++++++++++++++++-----
 views/search_view.php |   34 +++++-----
 2 files changed, 172 insertions(+), 37 deletions(-)

diff --git a/scripts/suggest.js b/scripts/suggest.js
index dc5275f..c4a8c5f 100644
--- a/scripts/suggest.js
+++ b/scripts/suggest.js
@@ -51,7 +51,6 @@ FONT_HEIGHT = 24;
  * what the trie object loaded says in loadTrie
  */
 END_OF_TERM_MARKER = " ";
-
 /**
  * Steps to follow every time a key is up from the user end
  * Handles up/dowm arrow keys
@@ -60,6 +59,8 @@ END_OF_TERM_MARKER = " ";
  * @return String text_field Current value from the search box
  *
  */
+
+
 function onTypeTerm(event, text_field) 
 {
     var key_code_pressed;
@@ -69,6 +70,19 @@ function onTypeTerm(event, text_field)
     var suggest_dropdown = elt("suggest-dropdown");
     var scroll_pos = 0;
     var tmp_pos = 0;
+    localeTerms = new Object();
+    localTermsPresent = false;
+    localSuggest = true;
+    search_list_array = new Object();
+ 
+    //To find the length of an associative array
+    Object.size = function(obj) {
+    var size = 0, key;
+    for (key in obj) {
+        if (obj.hasOwnProperty(key)) size++;
+    }
+    return size;
+    };
 
     concat_term = "";
     if(window.event) { // IE8 and earlier
@@ -90,7 +104,46 @@ function onTypeTerm(event, text_field)
     if (key_code_pressed != KeyCodes.DOWN_ARROW && 
             key_code_pressed != KeyCodes.UP_ARROW) {
         search_list = "";
-        termSuggest(dictionary, input_term);
+
+    // First search the local storage to fetch the suggestions
+    if (localStorage) {
+         if (localStorage[locale] != null) {
+           split = localStorage[locale].split(" ");
+             localeTerms = JSON.parse(split[1]);
+
+         local_dict = JSON.parse(localStorage[locale].split(" ", 1));
+            if (local_dict != null) {
+            termSuggest(local_dict, input_term);
+            localTermsPresent = true;
+            }
+
+         }
+            else  count =0; 
+     }
+    var sortedLocal = SortLocalTerms();
+     
+     if (Object.size(search_list_array) > 0)
+     {
+        search_list = "";         
+         for(var i = 0; i < sortedLocal.length ; i++) {         
+            var splitArray = sortedLocal[i].split('*');     
+            
+            if(search_list_array[splitArray[1]] != null)
+            search_list += search_list_array[splitArray[1]];
+         }
+        
+     }
+     
+     // Now search the actual dictionary trie
+     localSuggest = false;
+     termSuggest(dictionary, input_term);
+
+     // insert nbsp of the number of suggestions are less than MAX_DISPLAY
+     short_max = MAX_DISPLAY - count;
+     for(var i = 0; i < short_max; i++) {
+        search_list += "<li><span class='unselected'>&nbsp;</span></li>";
+    }
+
         if(count < 1) {
             search_list = "";
         }
@@ -148,6 +201,89 @@ function onTypeTerm(event, text_field)
 }
 
 /**
+ * To update the local storage with the previous query terms and
+ * create a trie on those terms
+ *
+ * @param None * 
+ */
+function updateLocalStorage() {
+
+     var trie_to_store;
+     trie_storage = {};
+     var store_term = elt("query-field").value;
+     var freq, k=0;
+     var sortedLocaleTerms = new Array();
+     if(localStorage) { 
+                
+         if (localeTerms[store_term] == null) {
+            localeTerms[store_term] = 1;
+         } else {
+            freq = parseInt(localeTerms[store_term]);
+            freq++;
+            localeTerms[store_term] = freq;             
+         }     
+
+         for(var key in localeTerms) {
+            sortedLocaleTerms[k] = key;
+            k++;
+         }        
+          sortedLocaleTerms.sort();
+
+            for(var i=0; i< sortedLocaleTerms.length; i++) {
+             var trie_word = sortedLocaleTerms[i];        
+             var letters = trie_word.split(""),cur = trie_storage;
+
+    	        for ( var j = 0; j < letters.length; j++ ) {
+	        	    var letter = encode(letters[j]);
+                    var pos = cur[ letter ];
+
+		            if ( pos == null ) {        
+                        if (j === letters.length - 1)
+                            { cur = cur[ letter ] = {'$' : '$'};
+                         } else {
+                            cur = cur[ letter ] = {}; 
+                         }
+                    } else if (pos === 0) {
+		        	    cur = cur[ letter ] = { '$' : '$' };
+                    } else {
+		        	    cur = cur[ letter ];
+		            }
+                }
+            	
+        }
+    }
+    trie_to_store = JSON.stringify( trie_storage );
+    localStorage.setItem(locale,trie_to_store + " " + JSON.stringify(localeTerms)); 
+}
+
+/* Sort the local storage words based of number of times they are queried
+ *
+ */
+function SortLocalTerms() {
+ var localStorageArray = new Array();
+ 
+ if(Object.size(localeTerms) > 0) {
+    var j=0;
+    for(var key in localeTerms) {
+    localStorageArray[j] = localeTerms[key] +"*"+  key;
+    j++;
+    }
+}
+
+localStorageArray.sort(sortFunction);
+localStorageArray.reverse();
+return localStorageArray;
+}
+
+
+function sortFunction(a, b){
+var splitArray1 = a.split('*');
+var splitArray2 = b.split('*'); 
+ var val1 = parseInt(splitArray1[0]);
+ var val2 = parseInt(splitArray2[0]);
+return (val1 - val2);
+}
+/**
  * To select an suggest value while up/down arrow keys are being used
  * and place in the search box
  *
@@ -196,21 +332,24 @@ function getTrieTerms(trie_array, parent_word, highlighted_word)
                 getTrieTerms(trie_array[key], parent_word + key, 
                         highlighted_word + key);
             } else {
-                search_terms = concat_term.trim() + " " + decode(parent_word);
-                search_terms = search_terms.trim();
-                highlighted_terms = concat_term.trim() + " " 
-                    + decode(highlighted_word) + "</b>";
-                search_list += "<li><span id='term" +count+
+                if (localTermsPresent == false || (localeTerms[decode(parent_word)] == null && localTermsPresent == true))  {
+                    search_terms = concat_term.trim() + " " + decode(parent_word);
+                    search_terms = search_terms.trim();
+                    highlighted_terms = concat_term.trim() + " " + decode(highlighted_word) + "</b>";
+                    search_string =  "<li><span id='term" +count+
                     "' class='unselected' onclick = 'void(0)' " +
                     "title='"+search_terms+"' " +
                     "onmouseover='setSelectedTerm(\""+count+"\",\"selected\")'" +
                     "onmouseout='setSelectedTerm(\""+count+"\",\"unselected\")'" +
                     "onmouseup='termClick(\""+search_terms+"\",this.id)'"+
                     ">" + highlighted_terms + "</span></li>";
-                count++;
+                    search_list += search_string;           
+                    search_list_array[decode(parent_word)] = search_string;
+                    count++;                     
+                }
+                }
             }
         }
-    }
 }
 
 /**
@@ -230,7 +369,7 @@ function exist(trie_array, term)
         tmp = getUnicodeCharAndNextOffset(term, i);
         if(tmp == false) return false;
         next_char = tmp[0];
-        i = tmp[1];
+        i = tmp[1]; 
         enc_char = encode(next_char);
         trie_array = trie_array[enc_char];
         if(trie_array == null) {
@@ -252,11 +391,14 @@ function exist(trie_array, term)
 function termSuggest(trie_array, term)
 {
     last_word = false;
-    count = 0;
-    search_list = "";
+    if (localSuggest == true) {
+        count = 0;
+        search_list = ""; 
+    
+    }
     // For US english ignore the case
     if(locale == 'en-US') {
-    term = term.toLowerCase();
+        term = term.toLowerCase();
     }
     var tmp;
     if(trie_array == null) {
@@ -275,10 +417,6 @@ function termSuggest(trie_array, term)
         trie_array = trie_array[term];
     }
     getTrieTerms(trie_array, term, term + "<b>");
-    short_max = MAX_DISPLAY - count;
-    for(var i = 0; i < short_max; i++) {
-        search_list += "<li><span class='unselected'>&nbsp;</span></li>";
-    }
 }
 
 /* wrappers to save typing */
@@ -291,7 +429,7 @@ function encode(str)
 {
     str = encodeURIComponent(str);
     str = str.replace(/\'/g, '%27'); // encodeURIComponent doesnt convert '
-    return str;
+    return str;    
 }
 
 /**
@@ -342,6 +480,7 @@ function getUnicodeCharAndNextOffset(str, i)
 function loadTrie() 
 {
     var request = makeRequest();
+    local_store_count = 0; 
     if(request) {
         request.onreadystatechange = function() {
             if (request.readyState == 4 && request.status == 200) {
diff --git a/views/search_view.php b/views/search_view.php
index ce7f432..d3e45a4 100755
--- a/views/search_view.php
+++ b/views/search_view.php
@@ -97,8 +97,8 @@ class SearchView extends View implements CrawlConstants
         }
         ?>
 
-        <h1 class="logo"><a href="./?<?php 
-            e(CSRF_TOKEN."=".$data[CSRF_TOKEN])?>"><img 
+        <h1 class="logo"><a href="./?YIOOP_TOKEN=<?php 
+            e($data['YIOOP_TOKEN'])?>"><img 
             src="<?php e($logo); ?>" alt="<?php e(tl('search_view_title'));
                  ?>"
             /></a>
@@ -112,14 +112,14 @@ class SearchView extends View implements CrawlConstants
         ?>
 
         <div class="searchbox">
-            <form id="search-form" method="get" action='?'>
+            <form id="search-form" method="get" action='?' onsubmit="updateLocalStorage()" >
             <p>
             <?php if(isset($data["SUBSEARCH"]) && $data["SUBSEARCH"] != "") { ?>
             <input type="hidden" name="s" value="<?php 
                 e($data['SUBSEARCH']); ?>" />
             <?php } ?>
-            <input type="hidden" name="<?php e(CSRF_TOKEN); ?>" value="<?php 
-                e($data[CSRF_TOKEN]); ?>" />
+            <input type="hidden" name="YIOOP_TOKEN" value="<?php 
+                e($data['YIOOP_TOKEN']); ?>" />
             <input type="hidden" name="its" value="<?php e($data['its']); ?>" />
             <input type="text" <?php if(WORD_SUGGEST) { ?>
                 autocomplete="off"  onkeyup="onTypeTerm(event, this)"
@@ -185,7 +185,7 @@ class SearchView extends View implements CrawlConstants
                 <?php
                 $subsearch = (isset($data["SUBSEARCH"])) ? $data["SUBSEARCH"] :
                     "";
-                $base_query = "?".CSRF_TOKEN."=".$data[CSRF_TOKEN].
+                $base_query = "?YIOOP_TOKEN={$data['YIOOP_TOKEN']}".
                         "&amp;c=search";
                 if(isset($page['IMAGES'])) {
                     $this->imagesHelper->render($page['IMAGES'], 
@@ -194,17 +194,14 @@ class SearchView extends View implements CrawlConstants
                     continue;
                 } else if(isset($page['FEEDS'])){
                     $this->feedsHelper->render($page['FEEDS'], 
-                        $base_query, $data['QUERY'],  $subsearch,
-                        $data['OPEN_IN_TABS']);
+                        $base_query, $data['QUERY'],  $subsearch);
                     e( "           </div>");
                     continue;
                 }
                 ?>
 
                 <h2>
-                <a href="<?php  e(htmlentities($url));  ?>" rel="nofollow"
-                 <?php if($data["OPEN_IN_TABS"]) { 
-                    ?> target="_blank" <?php }?> ><?php
+                <a href="<?php  e(htmlentities($url));  ?>" rel="nofollow"><?php
                  if(isset($page[self::THUMB]) && $page[self::THUMB] != 'NULL') {
                     ?><img src="<?php e($page[self::THUMB]); ?>" alt="<?php 
                         e($title); ?>"  /> <?php
@@ -220,7 +217,7 @@ class SearchView extends View implements CrawlConstants
                 </h2>
                 <?php if($check_video) {
                     $this->videourlHelper->render($url,
-                        $data['VIDEO_SOURCES'], $data["OPEN_IN_TABS"]);
+                        $data['VIDEO_SOURCES']);
                 }
                 ?>
                 <p class="echolink" <?php e($subtitle); ?>><?php 
@@ -241,7 +238,7 @@ class SearchView extends View implements CrawlConstants
                           in_array("NONE", $page[self::ROBOT_METAS])))) {
                         $aux_link_flag = true;
                     ?>
-                    <a href="?<?php e(CSRF_TOKEN."=".$data[CSRF_TOKEN]);
+                    <a href="?YIOOP_TOKEN=<?php e($data['YIOOP_TOKEN']);
                             ?>&amp;c=search&amp;a=cache&amp;q=<?php 
                             e($data['QUERY']); ?>&amp;arg=<?php 
                             e(urlencode($url)); 
@@ -261,7 +258,7 @@ class SearchView extends View implements CrawlConstants
                     if(SIMILAR_LINK) {
                         $aux_link_flag = true;
                     ?>
-                    <a href="?<?php e(CSRF_TOKEN."=".$data[CSRF_TOKEN]);
+                    <a href="?YIOOP_TOKEN=<?php e($data['YIOOP_TOKEN']);
                         ?>&amp;c=search&amp;a=related&amp;arg=<?php 
                         e(urlencode($url)); ?>&amp;<?php
                         ?>its=<?php e($page[self::CRAWL_TIME]); ?>" 
@@ -273,7 +270,7 @@ class SearchView extends View implements CrawlConstants
                     if(IN_LINK) {
                         $aux_link_flag = true;
                     ?>
-                    <a href="?<?php e(CSRF_TOKEN."=".$data[CSRF_TOKEN]);
+                    <a href="?YIOOP_TOKEN=<?php e($data['YIOOP_TOKEN']);
                         ?>&amp;c=search&amp;q=<?php 
                         e(urlencode("link:".$url)); ?>&amp;<?php
                         ?>its=<?php e($page[self::CRAWL_TIME]); ?>" 
@@ -285,7 +282,7 @@ class SearchView extends View implements CrawlConstants
                     if(IP_LINK && isset($page[self::IP_ADDRESSES])){
                     foreach($page[self::IP_ADDRESSES] as $address) {?>
 
-                    <a href="?<?php e(CSRF_TOKEN."=".$data[CSRF_TOKEN]);
+                    <a href="?YIOOP_TOKEN=<?php e($data['YIOOP_TOKEN']);
                             ?>&amp;c=search&amp;q=<?php
                             e(urlencode('ip:'.$address));?>&amp;<?php
                             ?>its=<?php e($data['its']); ?>" 
@@ -313,8 +310,7 @@ class SearchView extends View implements CrawlConstants
             <?php 
             } //end foreach
             $this->paginationHelper->render(
-                $data['PAGING_QUERY']."&amp;".CSRF_TOKEN."=".
-                    $data[CSRF_TOKEN],
+                $data['PAGING_QUERY']."&amp;YIOOP_TOKEN=".$data['YIOOP_TOKEN'],
                 $data['LIMIT'], $data['RESULTS_PER_PAGE'], $data['TOTAL_ROWS']);
             ?>
 
@@ -326,7 +322,7 @@ class SearchView extends View implements CrawlConstants
         <div class="landing-footer">
             <div><b><?php e($data['INDEX_INFO']);?></b> <?php
             if(isset($data["HAS_STATISTICS"]) && $data["HAS_STATISTICS"]) { 
-            ?>[<a href="index.php?<?php e(CSRF_TOKEN."=".$data[CSRF_TOKEN]);
+            ?>[<a href="index.php?YIOOP_TOKEN=<?php e($data['YIOOP_TOKEN']);
                 ?>&amp;c=statistics&amp;its=<?php e($data['its']);?>"><?php 
                 e(tl('search_view_more_statistics')); ?></a>]
             <?php 
-- 
1.7.9.4

