]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
fancier search results with teasers and page titles
[shellwiki] / handlers / 40_search.sh
1 #!/bin/sh
2
3 # Copyright 2023 Paul Hänsch
4
5 # Permission to use, copy, modify, and/or distribute this software for any
6 # purpose with or without fee is hereby granted, provided that the above
7 # copyright notice and this permission notice appear in all copies.
8
9 # THE SOFTWARE IS PROVIDED “AS IS” AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 # WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 # MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
12 # SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 # ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
15 # IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16
17 [ "$SEARCH_INDEX" != true ] && return 1
18 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
19
20 . "$_EXEC/cgilite/storage.sh"
21 . "$_EXEC/db23.sh"
22
23 I="$_DATA/index"
24 words="$( GET q | awk '
25   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
26           if ( length("¡") == 1 )  # Utf-8 aware AWK
27           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
28           else                     # UTF-8 Hack
29           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
30           fi
31         }
32         { for (n = 1; n <= NF; n++) printf "%s  ", tolower($n); }
33 ')"
34
35 searchteaser() {
36   local file="$1" words db3_data
37   local w l nc nl hits mhits cont mcont
38   shift 1; words="$*"
39
40   for w in ${words}; do
41     grep -hiwnF "$w" "$file"
42   done \
43   | sort -t: -k1 -n \
44   | { nc=-1 hits=0 mhits=0
45     while read -r l; do
46       nl="$nc" nc="${l%%:*}"
47       if [ $nc -eq $nl ]; then
48         hits=$((hits + 1))
49       elif [ $nc -eq $((nl + 1 )) ]; then
50         hits=$((hits + 1))
51         cont="${cont}${BR}${l#*:}"
52       elif [ $hits -gt $mhits ]; then
53         mhits="$hits" mcont="$cont"
54         hits=1 cont="${l#*:}"
55       else
56         hits=1 cont="${l#*:}"
57       fi
58     done
59
60     [ $hits -gt $mhits ] \
61     && STRING "$cont" \
62     || STRING "$mcont"
63   }
64 }
65
66 for w in ${words}; do
67   [ ! -f "$I/$w" ] && continue
68
69   while read date doc freq num total; do
70     P="$_DATA/pages$(UNSTRING "$doc")"
71     d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
72     [ "$d" -le "$date" ] 2>&- || continue
73
74     printf '%s  %f\n' "$doc" "$freq"
75   done <"$I/$w"
76 done \
77 | awk '
78       { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
79   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
80         for (d in cnt) if ( cnt[d] == m ) printf "%f    %s\n", weight[d], d;
81       }
82 ' \
83 | sort -nr \
84 | while read freq doc; do
85   page="$(UNSTRING "$doc")"
86   [ "${page%*/\[*\]/*}" != "$page" ] && continue
87   if [ "$LANGUAGE_DEFAULT" ]; then
88     [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
89     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
90   fi
91   acl_read "$page" || continue
92   printf '%s    %s\n' "$doc" "$(searchteaser "$(mdfile "$page")" $words)"
93 done \
94 | theme_search "${words%        }"