stl-statilque-litterateur/var/lib/stl/scripts/manage__sidebar
2022-08-24 12:38:38 +02:00

319 lines
8.1 KiB
Bash

#!/bin/bash
# Name: Statique Littérateur
# Type: Sidebar manager
# file: manage__sidebar
# Folder: /var/lib/stl/scripts/
# By echolib (XMPP: im@echolib.re)
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#------------
# funny stats
#------------
# lines: 319
# functions: 5
#-----------------------------------------------------------------------
#**********************************************************************
#======================================================================
# Sidebar: Manage arguments
# Check Article, WIP and Position
#======================================================================
sidebar__OPTIONS() {
[[ $sdb_load ]] || domain__get
case "$2" in
create)
domain__get
sidebar__create_HTML
return
;;
load)
sidebar__load
return
;;
add)
# Argument article
! [[ "$4" ]] \
&& noarg "$4" "Use: sidebar add (POS) (ARTICLE)" \
&& exit
# Position. Must be numeric: 1 to max_items
[[ "$3" == *[!0-9]* ]] \
&& noarg "$3" "Use numeric position: 1 to $domain_siddebar_items" \
&& exit
# Position mismatches values
(( "$3" == 0 || "$3" > $domain_sidebar_items )) \
&& noarg "$3" "Use position 1 to $domain_siddebar_items" \
&& exit
# Article
article__hash "$4"
if__article_db "$article_db"
# Article not yet checked
if ! [[ $db_exists ]];then
log__add -e -S -A \
"'$article_title' is not yet checked"
# sidebar load
[[ $sdb_load ]] && item=$(( item - 1 ))
return
fi
# article is checked and newer than WIP one
if [[ $old_wip ]];then
log__add -e -S -A \
"$sync_mess"
# sidebar load
[[ $sdb_load ]] && item=$(( item - 1 ))
return
fi
# WIP check
if ! [[ $article_wip_hash ]];then
log__add -e -S -A \
"'$article_title' is not yet converted in HTML"
# sidebar load
[[ $sdb_load ]] && item=$(( item - 1 ))
return
fi
# Already in sidebar at same position
if [[ "$3" == $sidebar_position ]];then
log__add -w -S -A \
"'$article_title' is already at position: $3"
return
fi
# Already in sidebar, different position
if [[ $sidebar_position ]];then
sed -i "s^sidebar_position=.*^sidebar_position=^" "$db_file"
if [[ -f "$domain_dir_sidebar/.$sidebar_position.sidebar" ]];then
rm -f "$domain_dir_sidebar/.$sidebar_position.sidebar"
log__add -w -S -rm \
"$article_title at position: $sidebar_position"
fi
fi
# Create HTML idem file
mkdir -p "$domain_dir_sidebar" # ensure articles/sidebar is creeated
position="$3"
sidebar_HTML_item="$domain_dir_sidebar/.$3.sidebar"
sidebar_add=true
;;
latest|oldest)
domain__get
;;
*)
noarg "$2" "Use: load, add, latest, oldest, create"
;;
esac
# Start process
sidebar__manage "$2" "$position" "$sidebar_HTML_item"
}
#======================================================================
# Sidebar: create /templates/sidebar.html
# $1: Process (add, replace...)
# $2: Position
# $3: file (.POS.sidebar) in wip template
#======================================================================
sidebar__manage() {
case "$1" in
latest|-l) sort_arg='-k2' ;;
oldest|-o) sort_arg='-k2r' ;;
esac
case "$1" in
latest|oldest)
sidebar_pos=0
while IFS=: read -r 'file' 'timestamp'
do
((position++))
(( $position > $domain_sidebar_items )) \
&& break
file=`basename "$file"`
db_file="$domain_db_articles/$file"
source "$db_file"
! [[ $article_wip_hash -gt 0 ]] \
&& continue
sidebar_HTML_item="$domain_dir_sidebar/.$position.sidebar"
sidebar__create_HTML_item "$sidebar_HTML_item"
done < <(grep -H "article_timestamp" "$domain_db_articles/"*.db \
| sort -t= $sort_arg )
;;
add)
sidebar__create_HTML_item "$3"
;;
esac
# sidebar load: do not create the complete sidebar now
[[ $sdb_load ]] && return
sidebar__create_HTML
}
#======================================================================
# Called from sidebar__create in gsl__post_makers
# file: /var/lib/gsl/domains/DOMAIN/X-tra/sidebar/[POSITION].html
# You can edit this HTML code for your needs
# It can get all Values from Database post
# $1: file ($sidebar_HTML_item)
#======================================================================
sidebar__create_HTML_item() {
create__HTML_sidebar_item "$1"
# Log added
log__add -i -S -A "$article_title added to position: $position"
# Unset sidebar position in DB if an article is at position
db_file_sidebar=`
grep -H "sidebar_position=$position" \
"$domain_db_articles/"*.db \
| awk -F: '{print $1}'`
# Add value to DB
sed -i "s^sidebar_position=.*^sidebar_position=$position^" "$db_file"
# If position is used by another article
if [[ $db_file_sidebar ]];then
source "$db_file_sidebar"
sed -i "s^sidebar_position=.*^sidebar_position=^" "$db_file_sidebar"
# Create log for this other article
uri_src=${article_uri_src:1}
article__hash "$uri_src"
log__add -w -S -rm "$article_title at position: $position"
fi
}
#======================================================================
# Create sidebar.html from files .(1-MAX).sidebar
#======================================================================
sidebar__create_HTML() {
if [[ -f "$domain_dir_wip_tpl/sidebar.html" ]];then
rm -f "$domain_dir_wip_tpl/sidebar.html"
this_article="$domain_dir_wip_tpl/sidebar.html"
log__add -w -S -rm \
"Create new"
fi
for i in `seq 1 $domain_sidebar_items`
do
[[ -f "$domain_dir_sidebar/.$i.sidebar" ]] \
&& cat "$domain_dir_sidebar/.$i.sidebar" \
>> "$domain_dir_wip_tpl/sidebar.html"
done
this_article="$domain_dir_wip_tpl/sidebar.html"
log__add -i -M -rs "Created items from 1 to $domain_sidebar_items"
}
#======================================================================
# Specific. Loading static stl.sidebar and create sidebar
#======================================================================
sidebar__load() {
sdb_load=true
domain__get
mkdir -p "$domain_dir_sidebar" # ensure articles/sidebar is creeated
# Not yet configured (for older versions)
! [[ "$domain_file_sidebar_load" ]] \
&& echo "! Unused configuration for sidebar. Type: stl update" \
&& exit
# Create, stl.sidebar file (if not exists)
if ! [[ -f "$domain_file_sidebar_load" ]];then
create__file_sidebar_load # manage__domain
echo "# Static sidebar file created: $domain_file_sidebar_load"
# Get sidebar hash, create default id file
sidebar_hash=`cksum "$domain_file_sidebar_load" | awk '{print $1}'`
touch "$domain_dir_sidebar/.id.sidebar"
echo "user_sidebar=$sidebar_hash" \
> "$domain_dir_sidebar/.id.sidebar"
exit
fi
# Get sidebar hash
sidebar_hash=`cksum "$domain_file_sidebar_load" | awk '{print $1}'`
# Check .id.sidebar file
if ! [[ -f "$domain_dir_sidebar/.id.sidebar" ]];then
touch "$domain_dir_sidebar/.id.sidebar"
echo "user_sidebar=$sidebar_hash" \
> "$domain_dir_sidebar/.id.sidebar"
fi
source "$domain_dir_sidebar/.id.sidebar"
case "$sidebar_hash" in
# Default file
"4269375654"|"2074433654")
echo "! Sidebar not yet configured: $domain_file_sidebar_load"
exit
;;
"$user_sidebar")
echo "! No changes in $domain_file_sidebar_load"
exit
;;
*)
echo "user_sidebar=$sidebar_hash" \
> "$domain_dir_sidebar/.id.sidebar"
;;
esac
# loop stl.sidebar lines
item=0
while read -r "line"
do
# Ignore comments, empty lines
[[ "$line" =~ ^'#' ]] || [[ -z "$line" ]] && continue
# Remove first / if found
[[ ${line::1} == '/' ]] && line=${line:1}
# Get from DB if exists
db_file=`
grep -H "article_uri_src='/$line'" $domain_db_articles/*.db \
| head -n 1 \
| awk -F: '{print $1}'`
# Article not yet checked
[[ "$db_file" ]] || continue
((item++))
sidebar__OPTIONS " " add "$item" "$line"
# Count to max definded items
(( $item > $domain_sidebar_items )) && break
done <"$domain_dir_sidebar/stl.sidebar"
# Create HTML sidebar if an article is added
[[ $sidebar_add ]] && sidebar__create_HTML
}