From b4397ac91fa17972c4820da7d3acf80ecd6f93ba Mon Sep 17 00:00:00 2001 From: =?utf8?q?Paul=20H=C3=A4nsch?= Date: Wed, 28 Feb 2024 15:57:10 +0100 Subject: [PATCH] bugfix: accidentally wiped search index when pruning --- searchindex.sh | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/searchindex.sh b/searchindex.sh index 162fad1..28bd76a 100755 --- a/searchindex.sh +++ b/searchindex.sh @@ -65,18 +65,25 @@ help() { script is called, if it can be determined. --data SITE_DIR - Point to the location of your site installation. I.e. the directory - containing your "pages/" and "index/" dir. Defaults to the - environment variable "\$_DATA" or the working directory. + Point to the location of your site installation. I.e. the + directory containing your "pages/" and "index/" dir. Defaults to + the environment variable "\$_DATA" or the working directory. --force Add pages to index even if they seem to be indexed already. - --loction /PAGE + --location /PAGE Index only the given page and its children. The path is given relative to the web root, i.e. without the DATA and "page/" directory. + --purge + During pruning, remove empty database files. This will look + tidyer in the index/ directory, but the operation does not + guarantee atomicity and can disrupt index operations happening at + the same time. Use it carefully and maybe don't use it in + automatic runs via cron. + -v Be more verbose. EOF @@ -90,6 +97,7 @@ while [ $# -gt 0 ]; do case $1 in --location) location="${2}"; shift 2;; --verbose|-v) verb=true; shift 1;; --force) force=true; shift 1;; + --purge) purge=true; shift 1;; --help) help 0 2>&1;; prune|index) [ ! "$cmd" ] && cmd="$1" || help 1 @@ -112,6 +120,8 @@ fi . "$_EXEC/cgilite/storage.sh" prune() { + local v l d date location freq num total + for word in "$_DATA/index"/*; do [ "$word" = "$_DATA/index/*" ] && continue @@ -120,7 +130,7 @@ prune() { mv -- "$word" "${word}.$$" while read -r date location freq num total; do - l="$_DATA/pages$(UNSTRING "$location")#index.flag" + l="$_DATA/pages$(UNSTRING "$location")/#index.flag" d="$(stat -c %Y "$l")" 2>&- if [ "$date" -ge "$d" ] 2>&-; then @@ -131,10 +141,15 @@ prune() { fi done <"${word}.$$" >>"${word}" rm -- "${word}.$$" + if [ "${purge}" -a -f "${word}" -a ! -s "${word}" ]; then + rm -- "${word}" + [ "$verb" ] && printf '\rRemoving empty search key "%s"\n' "${word}" >&2 + fi done } index() { + local PATH_INFO _DATE SEARCH_INDEX location v export PATH_INFO="" _DATE="$(date +%s)" SEARCH_INDEX=true if [ "$location" ]; then -- 2.39.2