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