2022-07-20 19:37:21 +02:00
|
|
|
#!/bin/bash
|
|
|
|
# Name: Statique Littérateur
|
|
|
|
# Type: Article manager
|
|
|
|
# file: manage__articles
|
|
|
|
# Folder: /var/lib/stl/scripts/
|
|
|
|
# By echolib (XMPP: im@echolib.re)
|
|
|
|
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
|
|
|
|
|
2022-08-23 16:48:33 +02:00
|
|
|
#------------
|
|
|
|
# funny stats
|
|
|
|
#------------
|
2022-08-24 12:38:38 +02:00
|
|
|
# lines: 258
|
2022-08-23 19:14:41 +02:00
|
|
|
# functions: 8
|
2022-08-23 16:48:33 +02:00
|
|
|
#-----------------------------------------------------------------------
|
|
|
|
|
|
|
|
#**********************************************************************
|
|
|
|
|
|
|
|
#======================================================================
|
|
|
|
# Argument remove
|
|
|
|
# Remove article in wip/, www/ if wip, publish
|
|
|
|
# $1: remove
|
|
|
|
# $2: argument
|
|
|
|
#======================================================================
|
|
|
|
remove__OPTIONS() {
|
|
|
|
case "$2" in
|
|
|
|
wip) wip_remove=true;srv="WIP" ;;
|
|
|
|
www) www_remove=true;srv="WWW" ;;
|
|
|
|
*) noarg "$2" "Use: wip,www (ARTICLE)" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
case "$3" in
|
|
|
|
*".stl") article__hash "$3" ;;
|
|
|
|
*) noarg "$3" "Use: (ARTICLE).stl" ;;
|
|
|
|
esac
|
|
|
|
|
|
|
|
if__article_db "$article_db"
|
|
|
|
if ! [[ $db_exists ]];then
|
|
|
|
echo "! $this_article must be checked first"
|
|
|
|
exit
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Checking wip status
|
|
|
|
if [[ $wip_remove ]];then
|
|
|
|
! [[ $article_wip_hash ]] \
|
|
|
|
&& echo "! $3 is not in wip/ server" \
|
|
|
|
&& exit
|
|
|
|
|
|
|
|
dir_target="$domain_dir_wip"
|
|
|
|
|
|
|
|
# Checking www status
|
|
|
|
elif [[ $www_remove ]];then
|
|
|
|
[[ $stl_server == "server" ]] \
|
|
|
|
&& echo "! STL install: $stl_install. Unuse www/" \
|
|
|
|
&& exit
|
|
|
|
|
|
|
|
! [[ $article_www_hash ]] \
|
|
|
|
&& echo "! Article '$3' is not in www/ server" \
|
|
|
|
&& exit
|
|
|
|
|
|
|
|
dir_target="$domain_dir_www"
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Remove .html, .stl from server
|
2022-08-23 19:14:41 +02:00
|
|
|
remove__this_file "$dir_target$article_uri_srv"
|
|
|
|
remove__this_file "$dir_target$article_uri_src"
|
2022-08-23 16:48:33 +02:00
|
|
|
|
|
|
|
# in manage__publish: remove included files used by article
|
2022-08-23 19:14:41 +02:00
|
|
|
get__article_files_included
|
2022-08-23 16:48:33 +02:00
|
|
|
|
|
|
|
# Remove empty directories
|
|
|
|
find "$dir_target" -type d -empty -delete
|
|
|
|
|
|
|
|
# Remove www hash to DB
|
|
|
|
db__srv_status_hash "$srv" Unset
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:38:38 +02:00
|
|
|
|
2022-08-23 19:14:41 +02:00
|
|
|
#======================================================================
|
|
|
|
# Remove this file
|
|
|
|
# $1: file
|
|
|
|
#======================================================================
|
|
|
|
remove__this_file() {
|
|
|
|
if [[ -f "$1" ]];then
|
|
|
|
rm -f "$1"
|
|
|
|
this_article="$1"
|
|
|
|
log__add -w "$srv" -rm \
|
|
|
|
"file from server"
|
|
|
|
fi
|
|
|
|
}
|
2022-07-20 19:37:21 +02:00
|
|
|
|
2022-08-24 12:38:38 +02:00
|
|
|
|
2022-07-20 19:37:21 +02:00
|
|
|
#======================================================================
|
|
|
|
# Argument read
|
|
|
|
# $1: read
|
|
|
|
# $2: argument
|
|
|
|
#======================================================================
|
|
|
|
read__OPTIONS() {
|
|
|
|
! [[ "$2" ]] \
|
|
|
|
&& noarg "Cannot be empty"
|
|
|
|
|
2022-08-24 12:38:38 +02:00
|
|
|
sl="-n"
|
2022-07-20 19:37:21 +02:00
|
|
|
case "$2" in
|
2022-08-23 16:48:33 +02:00
|
|
|
*".stl") article__hash "$2" ;;
|
|
|
|
conf) domain__get;uri_article="$stl_file_pwd_conf" ;;
|
|
|
|
sidebar) domain__get;uri_article="$domain_file_sidebar_load" ;;
|
2022-08-24 12:38:38 +02:00
|
|
|
license) uri_article="$stl_dir_help/LICENSE";unset sl ;;
|
2022-08-23 16:48:33 +02:00
|
|
|
*) noarg "$2" ;;
|
2022-07-20 19:37:21 +02:00
|
|
|
esac
|
2022-08-23 16:48:33 +02:00
|
|
|
|
|
|
|
if__file "$uri_article" read
|
2022-08-24 12:38:38 +02:00
|
|
|
if [[ $uri_article == "$stl_dir_help/LICENSE" ]];then
|
|
|
|
echo && stl license
|
|
|
|
fi
|
2022-08-23 19:14:41 +02:00
|
|
|
exit
|
2022-07-20 19:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#======================================================================
|
2022-08-23 16:48:33 +02:00
|
|
|
# Argument edit
|
2022-07-20 19:37:21 +02:00
|
|
|
# $1: edit
|
|
|
|
# $2: argument
|
|
|
|
#======================================================================
|
|
|
|
edit__OPTIONS() {
|
|
|
|
! [[ "$2" ]] \
|
|
|
|
&& noarg "Cannot be empty"
|
|
|
|
|
2022-08-23 16:48:33 +02:00
|
|
|
domain__get
|
2022-07-20 19:37:21 +02:00
|
|
|
case "$2" in
|
2022-08-23 16:48:33 +02:00
|
|
|
*".stl") article__hash "$2" ;;
|
|
|
|
conf) uri_article="$domain_conf" ;;
|
|
|
|
sidebar) uri_article="$domain_file_sidebar_load" ;;
|
|
|
|
*) noarg "$2" ;;
|
2022-07-20 19:37:21 +02:00
|
|
|
esac
|
2022-08-23 16:48:33 +02:00
|
|
|
|
|
|
|
nano --linenumbers "$uri_article"
|
|
|
|
exit
|
2022-07-20 19:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#======================================================================
|
|
|
|
# Split article in 2 files : metas + content
|
|
|
|
# Get begining article content line
|
|
|
|
# $1: article > $uri_article
|
|
|
|
#======================================================================
|
|
|
|
split_article() {
|
|
|
|
article_tmp_head=`mktemp` # Metas
|
|
|
|
article_tmp_post=`mktemp` # Content
|
|
|
|
|
|
|
|
article_begin=`
|
|
|
|
grep -n "^-----" "$1" \
|
|
|
|
| head -n 1 \
|
|
|
|
| awk -F: '{print $1}'`
|
|
|
|
|
|
|
|
if ! [[ $article_begin ]];then
|
|
|
|
log__add -e -C -A \
|
|
|
|
"No separator before content: use '-----'"
|
|
|
|
return
|
|
|
|
fi
|
|
|
|
|
|
|
|
# Create file with metas only
|
|
|
|
awk -v end="$article_begin" \
|
|
|
|
'NR >=1 && NR < end-1 {print}' \
|
|
|
|
"$1" \
|
|
|
|
> "$article_tmp_head"
|
|
|
|
|
|
|
|
((article_begin++))
|
|
|
|
|
|
|
|
# Create file with content only
|
|
|
|
awk -v end="$article_begin" \
|
|
|
|
'NR >= end {print}' \
|
|
|
|
"$1" \
|
|
|
|
> "$article_tmp_post"
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:38:38 +02:00
|
|
|
|
2022-07-20 19:37:21 +02:00
|
|
|
#======================================================================
|
|
|
|
# Get article line for article_tmp_post. Count from begin - 1
|
|
|
|
#======================================================================
|
|
|
|
article__line() {
|
|
|
|
ln=$(( ln + article_begin - 1 ))
|
|
|
|
}
|
|
|
|
|
2022-08-24 12:38:38 +02:00
|
|
|
|
2022-07-20 19:37:21 +02:00
|
|
|
#======================================================================
|
|
|
|
# Get article hash + article_db ID from uri
|
|
|
|
# $1: file *.stl from argument
|
|
|
|
#======================================================================
|
|
|
|
article__hash() {
|
|
|
|
domain__get
|
|
|
|
this_article="$1"
|
2022-08-23 16:48:33 +02:00
|
|
|
this_article_bkp="$1"
|
2022-07-20 19:37:21 +02:00
|
|
|
uri_article="$domain_dir_articles/$1"
|
|
|
|
if__file "$uri_article"
|
|
|
|
|
2022-08-23 16:48:33 +02:00
|
|
|
# Get Hash, Size, Uri
|
2022-07-20 19:37:21 +02:00
|
|
|
while read -r "H" "S" "U"
|
|
|
|
do
|
|
|
|
article_hash="$H"
|
|
|
|
article_size="$S"
|
|
|
|
article_id=`echo "$U" | cksum | awk '{print $1}'`
|
|
|
|
article_db="$article_id.db"
|
|
|
|
article_log="$article_id.log"
|
|
|
|
done < <(cksum "$uri_article")
|
2022-08-08 17:34:11 +02:00
|
|
|
|
|
|
|
if [[ $article_size -lt 10 ]];then
|
|
|
|
echo "! This article is nearly empty..."
|
|
|
|
exit
|
|
|
|
|
|
|
|
elif [[ $article_size -lt 60 ]];then
|
|
|
|
echo "! This article is too small to be checked"
|
|
|
|
exit
|
|
|
|
fi
|
2022-07-20 19:37:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#======================================================================
|
|
|
|
# Get values from OPTIONAL metas
|
|
|
|
# $1: marker
|
|
|
|
# $2: from split mktemp > $article_tmp_head
|
|
|
|
# $3: check | make
|
|
|
|
# $4: $article_tmp_post
|
|
|
|
#======================================================================
|
|
|
|
get__content_metas() {
|
|
|
|
unset meta
|
|
|
|
|
|
|
|
while IFS=: read -r "ln" "content"
|
|
|
|
do
|
|
|
|
meta=true
|
|
|
|
header_f0=`awk -F"$1" '{print $2}' <<<"$content"`
|
|
|
|
header_f1=`awk -F" : " '{print $1}' <<<"$header_f0"`
|
|
|
|
header_f2=`awk -F" : " '{print $2}' <<<"$header_f0"`
|
|
|
|
header_f3=`awk -F" : " '{print $3}' <<<"$header_f0"`
|
|
|
|
|
|
|
|
case "$3" in
|
|
|
|
check)
|
|
|
|
case "$1" in
|
|
|
|
"^link: ") check__link ;;
|
|
|
|
"^image: ") check__image ;;
|
|
|
|
"^code: ") check__precode ;;
|
|
|
|
"^brut: ") check__brut ;;
|
|
|
|
"^abbr: ") check__abbr ;;
|
|
|
|
"^file: ") check__link_file ;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
|
|
|
|
make)
|
|
|
|
case "$1" in
|
|
|
|
"^abbr: ") make__abbrs "$4" ;;
|
|
|
|
"^link: ") make__links "$4" ;;
|
|
|
|
"^file: ") make__links_file "$4" ;;
|
|
|
|
"^image: ") make__images "$4" ;;
|
|
|
|
"^code: ") make__precode "$4" ;;
|
|
|
|
"^brut: ") make__brut "$4" ;;
|
|
|
|
esac
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done < <(grep -n "$1" "$2" )
|
|
|
|
}
|