]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
Merge commit '93889092574b7deb38da16f67bd5e3d744a94b86'
[shellwiki] / handlers / 40_search.sh
1 #!/bin/sh
2
3 [ "${PATH_INFO%\[search\]}" = "$PATH_INFO" ] && return 1
4
5 . "$_EXEC/cgilite/storage.sh"
6
7 I="$_DATA/index"
8 words="$( GET q | awk '
9   BEGIN { # Field separator FS should include punctuation, including Unicode Block U+2000 - U+206F
10           if ( length("ยก") == 1 )  # Utf-8 aware AWK
11           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '[\342\200\200-\342\201\257]')"')+";
12           else                     # UTF-8 Hack
13           FS = "([] \\t\\n\\r!\"#'\''()*+,./:;<=>?\\\\^_`{|}~[-]|%[0-9A-Fa-f]{2}|'"$(printf '\342\200[\200-\277]|\342\201[\201-\257]')"')+";
14           fi
15         }
16         { for (n = 1; n <= NF; n++) printf "%s  ", tolower($n); }
17 ')"
18
19 for w in ${words}; do
20   [ ! -f "$I/$w" ] && continue
21
22   while read date doc freq num total; do
23     P="$_DATA/pages$(UNSTRING "$doc")"
24     d="$(stat -c %Y -- "$P/#index.flag")"
25     [ "$d" -gt "$date" ] && continue
26
27     printf '%s  %f\n' "$doc" "$freq"
28   done <"$I/$w"
29 done \
30 | awk '
31       { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] * $2 : $2; }
32   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
33         for (d in cnt) if ( cnt[d] == m ) printf "%f    %s\n", weight[d], d;
34       }
35 ' \
36 | sort -nr \
37 | while read freq doc; do
38   page="$(UNSTRING "$doc")"
39   [ "${page%*/\[*\]/*}" != "$page" ] && continue
40   if [ "$LANGUAGE_DEFAULT" ]; then
41     [ -d "${_DATA}/pages/${page}/:${LANGUAGE}/" ] && continue
42     [ "${page%/:*/}" = "${page%/:${LANGUAGE}/}" ] || continue
43   fi
44   acl_read "$page" || continue
45   printf '%s\n' "$page"
46 done \
47 | theme_search "${words%        }"