259 lines
6.4 KiB
Bash
259 lines
6.4 KiB
Bash
#!/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
|
|
|
|
#------------
|
|
# funny stats
|
|
#------------
|
|
# lines: 258
|
|
# functions: 8
|
|
#-----------------------------------------------------------------------
|
|
|
|
#**********************************************************************
|
|
|
|
#======================================================================
|
|
# 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
|
|
remove__this_file "$dir_target$article_uri_srv"
|
|
remove__this_file "$dir_target$article_uri_src"
|
|
|
|
# in manage__publish: remove included files used by article
|
|
get__article_files_included
|
|
|
|
# Remove empty directories
|
|
find "$dir_target" -type d -empty -delete
|
|
|
|
# Remove www hash to DB
|
|
db__srv_status_hash "$srv" Unset
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# 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
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Argument read
|
|
# $1: read
|
|
# $2: argument
|
|
#======================================================================
|
|
read__OPTIONS() {
|
|
! [[ "$2" ]] \
|
|
&& noarg "Cannot be empty"
|
|
|
|
sl="-n"
|
|
case "$2" in
|
|
*".stl") article__hash "$2" ;;
|
|
conf) domain__get;uri_article="$stl_file_pwd_conf" ;;
|
|
sidebar) domain__get;uri_article="$domain_file_sidebar_load" ;;
|
|
license) uri_article="$stl_dir_help/LICENSE";unset sl ;;
|
|
*) noarg "$2" ;;
|
|
esac
|
|
|
|
if__file "$uri_article" read
|
|
if [[ $uri_article == "$stl_dir_help/LICENSE" ]];then
|
|
echo && stl license
|
|
fi
|
|
exit
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Argument edit
|
|
# $1: edit
|
|
# $2: argument
|
|
#======================================================================
|
|
edit__OPTIONS() {
|
|
! [[ "$2" ]] \
|
|
&& noarg "Cannot be empty"
|
|
|
|
domain__get
|
|
case "$2" in
|
|
*".stl") article__hash "$2" ;;
|
|
conf) uri_article="$domain_conf" ;;
|
|
sidebar) uri_article="$domain_file_sidebar_load" ;;
|
|
*) noarg "$2" ;;
|
|
esac
|
|
|
|
nano --linenumbers "$uri_article"
|
|
exit
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# 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"
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Get article line for article_tmp_post. Count from begin - 1
|
|
#======================================================================
|
|
article__line() {
|
|
ln=$(( ln + article_begin - 1 ))
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# Get article hash + article_db ID from uri
|
|
# $1: file *.stl from argument
|
|
#======================================================================
|
|
article__hash() {
|
|
domain__get
|
|
this_article="$1"
|
|
this_article_bkp="$1"
|
|
uri_article="$domain_dir_articles/$1"
|
|
if__file "$uri_article"
|
|
|
|
# Get Hash, Size, Uri
|
|
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")
|
|
|
|
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
|
|
}
|
|
|
|
|
|
#======================================================================
|
|
# 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" )
|
|
}
|