3 # Copyright 2023 Paul Hänsch
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.
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.
17 export _EXEC="${_EXEC:-${0%/*}/}" _DATA="${_DATA:-.}"
18 verb="" v=0 cmd="" force="" location=""
26 ${0##*/} prune [--exec "INSTALL_DIR"] [--data "SITE_DIR"] [-v]
28 ${0##*/} index [--exec "INSTALL_DIR"] [--data "SITE_DIR"] \\
29 [--location "/PAGE"] [--force] [-v]
34 Remove outdated records from the database. This is usually
35 more time consuming than index creation. It is generally
36 save to run pruning while the wiki is online, even when
37 pages are being updated. Although in rare cases a search
38 operation may return incomplete results while running on
39 a database being pruned.
41 Pruning mode should be called regularly via cron.
44 Add pages to the search index. Pages with a current index
45 will be skipped unless the --force option is provided.
46 Optionally a --location can be provided to add only a
47 part of the document tree.
49 When running indexing and pruning together, indexing should be run
50 first and pruning afterwards.
52 Pruning becomes necessary with page updates, not during mere read
53 operation. On a medium traffic installation pruning should be run
55 Pruning the index more often than daily will rarely be necessary
56 and with low traffic sites monthly maintenance may be completely
62 Point to the location of your shellwiki installation. Without
63 this optin, the location will be read from the environments
64 variable "\$_EXEC", or will default to the path at which the
65 script is called, if it can be determined.
68 Point to the location of your site installation. I.e. the directory
69 containing your "pages/" and "index/" dir. Defaults to the
70 environment variable "\$_DATA" or the working directory.
73 Add pages to index even if they seem to be indexed already.
76 Index only the given page and its children. The path is given
77 relative to the web root, i.e. without the DATA and "page/"
87 while [ $# -gt 0 ]; do case $1 in
88 --exec|-e) _EXEC="${2%/}"; shift 2;;
89 --data|-d) _DATA="${2%/}"; shift 2;;
90 --verbose|-v) verb=true; shift 1;;
91 --force) force=true; shift 1;;
94 [ ! "$cmd" ] && cmd="$1" || help 1
99 if ! [ -d "$_DATA/pages/" -a -d "$_DATA/index/" ]; then
100 printf 'ERROR: %s\nTry --help\n' "\"${_DATA}\" does not seem to be a valid site directory" >&2
103 if ! [ -x "$_EXEC/parsers/40_indexer.sh" -a -x "$_EXEC/cgilite/storage.sh" ]; then
104 printf 'ERROR: %s\nTry --help\n' "could not determine shellwiki installation path (tried \"$_EXEC\")" >&2
107 if [ ! "$cmd" ]; then
111 . "$_EXEC/cgilite/storage.sh"
114 for word in "$_DATA/index"/*; do
115 [ "$word" = "$_DATA/index/*" ] && continue
117 [ "$verb" ] && printf "%${v}s\r%s\r" "" "${word##*/}" >&2
119 mv -- "$word" "${word}.$$"
121 while read -r date location freq num total; do
122 l="$_DATA/pages$(UNSTRING "$location")#index.flag"
123 d="$(stat -c %Y "$l")" 2>&-
125 if [ "$date" -ge "$d" ] 2>&-; then
126 printf '%i %s %f %i %i\n' \
127 "$date" "$location" "$freq" "$num" "$total"
128 elif [ "$verb" ]; then
129 printf "\rRemoving \"%s\" from \"%s\"\n" "$location" "${word##*/}" >&2
131 done <"${word}.$$" >>"${word}"
137 export PATH_INFO="" _DATE="$(date +%s)"
139 if [ "$location" ]; then
140 location="${location#/}" location="${location%/}"
141 printf %s\\n "/${location}/"
142 find "$_DATA/pages/" -type d -path "$_DATA/pages/${location}/*" -not -name "#*" -printf "/%P/\n"
144 find "$_DATA/pages/" -type d -not -name "#*" -printf "/%P/\n"
146 | while read PATH_INFO; do
147 [ "$force" ] && rm -f -- "$_DATA/pages/$PATH_INFO/#index.flag"
148 if [ "$_DATA/pages/$PATH_INFO/#page.md" -nt "$_DATA/pages/$PATH_INFO/#index.flag" \
149 -o -f "$_DATA/pages/$PATH_INFO/#page.md" \
150 -a ! -f "$_DATA/pages/$PATH_INFO/#index.flag" ] 2>&-
152 [ "$verb" ] && printf "%${v}s\r%s\r" "" "$PATH_INFO" >&2
154 "$_EXEC/parsers/40_indexer.sh" <"$_DATA/pages/$PATH_INFO/#page.md" >/dev/null