]> git.plutz.net Git - shellwiki/blobdiff - handlers/40_search.sh
search list: check if page content exists
[shellwiki] / handlers / 40_search.sh
index 5d0b1f698d14987c7d79843557669a5b8adc3925..038545711b5f2143bfb04e82f3635b2b93b97996 100644 (file)
 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 
+[ "$SEARCH_INDEX" != true ] && return 1
 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
 
 . "$_EXEC/cgilite/storage.sh"
+. "$_EXEC/db23.sh"
 
 I="$_DATA/index"
 words="$( GET q | awk '
@@ -30,19 +32,50 @@ words="$( GET q | awk '
        { for (n = 1; n <= NF; n++) printf "%s  ", tolower($n); }
 ')"
 
+searchteaser() {
+  local file="$1" words db3_data
+  local w l nc nl hits mhits cont mcont
+  shift 1; words="$*"
+
+  for w in ${words}; do
+    grep -hiwnF "$w" "$file"
+  done \
+  | sort -t: -k1 -n \
+  | { nc=-1 hits=0 mhits=0
+    while read -r l; do
+      nl="$nc" nc="${l%%:*}"
+      if [ $nc -eq $nl ]; then
+        hits=$((hits + 1))
+      elif [ $nc -eq $((nl + 1 )) ]; then
+        hits=$((hits + 1))
+        cont="${cont}${BR}${l#*:}"
+      elif [ $hits -gt $mhits ]; then
+        mhits="$hits" mcont="$cont"
+        hits=1 cont="${l#*:}"
+      else
+        hits=1 cont="${l#*:}"
+      fi
+    done
+
+    [ $hits -gt $mhits ] \
+    && STRING "$cont" \
+    || STRING "$mcont"
+  }
+}
+
 for w in ${words}; do
   [ ! -f "$I/$w" ] && continue
 
   while read date doc freq num total; do
     P="$_DATA/pages$(UNSTRING "$doc")"
-    d="$(stat -c %Y -- "$P/#index.flag")"
-    [ "$d" -gt "$date" ] && continue
+    d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
+    [ "$d" -le "$date" -a -f "$P/#page.md" ] 2>&- || continue
 
     printf '%s %f\n' "$doc" "$freq"
   done <"$I/$w"
 done \
 | awk '
-      { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] * $2 : $2; }
+      { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
         for (d in cnt) if ( cnt[d] == m ) printf "%f   %s\n", weight[d], d;
       }
@@ -56,6 +89,6 @@ done \
     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
   fi
   acl_read "$page" || continue
-  printf '%s\n' "$page"
+  printf '%s   %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
 done \
 | theme_search "${words%       }"