]> git.plutz.net Git - shellwiki/blob - handlers/40_search.sh
search handler
[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 |tr '] \t\n\r!\"#'\''()*+,./:;<=>?\\^_`{|}~[-' ' ')"
9
10 for w in ${words}; do
11   [ ! -f "$I/$w" ] && continue
12
13   while read date doc freq num total; do
14     P="$_DATA/pages$(UNSTRING "$doc")"
15     d="$(stat -c %Y -- "$P/#page.md")"
16     [ "$d" -gt "$date" ] && continue
17
18     printf '%s  %f\n' "$doc" "$freq"
19   done <"$I/$w"
20 done \
21 | awk '
22       { cnt[$1]++; weight[$1] = weight[$1] ? weight[$1] * $2 : $2; }
23   END { m = 0; for (d in cnt) m = ( m < cnt[d] ) ? cnt[d] : m;
24         for (d in cnt) if ( cnt[d] == m ) printf "%f    %s\n", weight[d], d;
25       }
26 ' \
27 | sort -nr \
28 | while read freq doc; do
29   page="$(UNSTRING "$doc")"
30   acl_read "$page" || continue
31   printf '<li><a href="%s">%s</a></li>' "$(URL "$page")" "$(HTML "$page")"
32 done \
33 | theme_page - <<-EOF
34         <article>
35           <h1>$(_ "Search results:")</h1>
36           <ol class="searchresults">
37             $(cat)
38           </ol>
39         </article>
40         EOF