]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
f057e45fee3a15cbfa6cb4120b03fccfc6d651b8
[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   acl_read "$page" || continue
40   printf '<li><a href="%s">%s</a></li>' "$(URL "$page")" "$(HTML "$page")"
41 done \
42 | theme_page - <<-EOF
43         <article>
44           <h1>$(_ "Search results")</h1>
45           <form method=GET>
46             <input class="search" name="q" placeholder="$(_ Search)" value="$(HTML $words)"><button class="search" type="submit">$(_ Search)</button>
47           </form>
48           <ol class="searchresults">
49             $(cat)
50           </ol>
51         </article>
52         EOF