]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
90840603cd6ec7e1881b197f92994d2bbd4e3882
[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
22 I="$_DATA/index"
23 words="$( GET q | awk '
24   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
25           if ( length("¡") == 1 )  # Utf-8 aware AWK
26           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
27           else                     # UTF-8 Hack
28           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
29           fi
30         }
31         { for (n = 1; n <= NF; n++) printf "%s  ", tolower($n); }
32 ')"
33
34 for w in ${words}; do
35   [ ! -f "$I/$w" ] && continue
36
37   while read date doc freq num total; do
38     P="$_DATA/pages$(UNSTRING "$doc")"
39     d="$(stat -c %Y -- "$P/#index.flag" 2>&-)"
40     [ "$d" -le "$date" ] 2>&- || continue
41
42     printf '%s  %f\n' "$doc" "$freq"
43   done <"$I/$w"
44 done \
45 | awk '
46       { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] + $2 : $2; }
47   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
48         for (d in cnt) if ( cnt[d] == m ) printf "%f    %s\n", weight[d], d;
49       }
50 ' \
51 | sort -nr \
52 | while read freq doc; do
53   page="$(UNSTRING "$doc")"
54   [ "${page%*/\[*\]/*}" != "$page" ] && continue
55   if [ "$LANGUAGE_DEFAULT" ]; then
56     [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
57     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
58   fi
59   acl_read "$page" || continue
60   printf '%s\n' "$page"
61 done \
62 | theme_search "${words%        }"