gsl-statique-litterateur/var/lib/gsl/scripts/gsl__post_checkers

614 lines
14 KiB
Bash

#!/bin/bash
# file: gsl__post_checkers
# Folder: /var/lib/gsl/scripts
# By echolib
# License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007
#----------------------------------------------------------------------
# --------------------------------------------------
# From: gsl__loop_posts | MAIN checkers from modules
# --------------------------------------------------
#----------------------------------------------------------------------
gsl__all_checkers() {
# Init Stats
gsl_stat_abbr=0
gsl_stat_abbrs=0
gsl_stat_link=0
gsl_stat_links=0
gsl_stat_file=0
gsl_count_files=0
gsl_stat_image=0
gsl_stat_fcode=0
gsl_db_tmp=`mktemp`
gsl__check_h1 || return
gsl__post_content_only
gsl__check_type
gsl__check_headers
gsl__check_paragraphs "$gsl_tmp_post"
gsl__check_blockquote "$gsl_tmp_post"
gsl__do_header check "$gsl_marker_link" "$gsl_post"
gsl__do_header check "$gsl_marker_abbr" "$gsl_post"
gsl__do_header check "$gsl_marker_file" "$gsl_post"
gsl__do_header check "$gsl_marker_image" "$gsl_post"
gsl__do_header check "$gsl_marker_code" "$gsl_post"
gsl__stat_words "$gsl_tmp_post"
gsl__do_strongs_bolds stats "$gsl_tmp_post"
gsl__do_italics stats "$gsl_tmp_post"
gsl__do_icode stats "$gsl_tmp_post"
rm -f "$gsl_tmp_post"
echo -ne "\r\033[2K"
gsl__db_set_post_status chk
rm -f "$gsl_db_tmp"
}
#----------------------------------------------------------------------
# -------------------------------------------------
# Modules checkers
# -------------------------------------------------
#----------------------------------------------------------------------
#======================================================================
# Check Post for begining content
#======================================================================
gsl__check_h1() {
echo -ne ": Searching for begining #1..."
gsl_post_begin=`
grep -n "#1" $gsl_post \
| head -1 \
| awk -F: '{print $1}'`
if ! [[ "$gsl_post_begin" ]];then
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Begins" \
"Unused: #1 TITLE" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# Check/Set Post Type
#======================================================================
gsl__check_type() {
echo -ne "\r\033[2K: Searching for Post TYPE..."
case "$gsl_post_type" in
page|post)
gsl__logs_print \
"$gsl_log_i" \
"Header" \
"$gsl_marker_type" \
"$gsl_post Set as $gsl_post_type" \
"${PWD}/$gsl_post"
;;
*)
gsl_post_type="post"
sed -i "1i$gsl_marker_type$gsl_post_type" \
"$gsl_post" && \
gsl__logs_print \
"$gsl_log_w" \
"Header" \
"$gsl_marker_type" \
"$gsl_post Set as Default $gsl_post_type" \
"${PWD}/$gsl_post"
gsl__get_sum "$gsl_post"
;;
esac
}
#======================================================================
# Check for paragraphs ( and ) | $1: File
#======================================================================
gsl__check_paragraphs() {
echo -ne "\r\033[2K: Searching for Paragraphs..."
gsl_post_p_open=`
awk -v op="$gsl_mo_p" \
'$1 == op' \
"$1" \
| wc -l`
gsl_post_p_close=`
awk -v cp="$gsl_mc_p" \
'$1 == cp' \
"$1" \
| wc -l`
if (( "$gsl_post_p_open" == 0 ));then
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Paragraphs" \
"Unused: ( and ) at begining lines" \
"${PWD}/$gsl_post"
gsl_checker_err=true
return
fi
if (( "$gsl_post_p_open" == "$gsl_post_p_close" ));then
gsl_stat_p="$gsl_post_p_open"
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Paragraphs" \
"Mismatch: (=$gsl_post_p_open ; )=$gsl_post_p_close" \
"${PWD}/$gsl_post"
gsl_checker_err=true
return
fi
}
#======================================================================
# Check for blockquotes: --- | $1: File
#======================================================================
gsl__check_blockquote() {
echo -ne "\r\033[2K: Searching for BLOCKQUOTEs..."
gsl_post_blockquotes_nbr=`
awk -v bq="$gsl_mark_blockquote" \
'$1 == bq' \
"$1" \
| wc -l`
gsl_post_blockquotes_nbr_r=$(( $gsl_post_blockquotes_nbr % 2 ))
if [[ "$gsl_post_blockquotes_nbr_r" -eq 0 ]];then
gsl_stat_bq=$(( $gsl_post_blockquotes_nbr / 2 ))
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Citations" \
"Mismatch: --- not paired" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# From gsl__do_header: Check Link
#======================================================================
gsl__check_link() {
# Get & Check Header CONTENT
gsl__check_header_fields \
"Header" "$gsl_marker_link" "$gsl_log_act_link" \
|| return
echo -ne "\r\033[2K: Searching for Links... $gsl_post_hf_1"
# No ALT TEXT ?
gsl__check_header_field3 "Header" "$gsl_marker_link"
gsl_count_links=`
gsl__get_content_line "_$gsl_post_hf_1" "$gsl_post" | wc -l`
if (( "$gsl_count_links" > 0 ));then
# Check URL
case "$gsl_post_hf_2" in
http*)
gsl_url_status=`
timeout 2 curl -o /dev/null --silent --head --write-out \
'%{http_code}' \
"$gsl_post_hf_2" 2>/dev/null`
;;
*)
gsl_url_status=408
;;
esac
case "$gsl_url_status" in
1*|2*|3*)
gsl__logs_print \
"$gsl_log_i" \
"Header" \
"$gsl_marker_link" \
"Online:$gsl_url_status ${gsl_post_hf_2:0:40}..." \
"${PWD}/$gsl_post"
;;
4*)
gsl__logs_print \
"$gsl_log_w" \
"Header" \
"$gsl_marker_link" \
"Not checked: ${gsl_post_hf_2:0:40}..." \
"${PWD}/$gsl_post"
gsl_checker_war=true
;;
""|*)
gsl__logs_print \
"$gsl_log_w" \
"Header" \
"$gsl_marker_link" \
"Offline:$gsl_url_status ${gsl_post_hf_2:0:40}..." \
"${PWD}/$gsl_post"
# Decomment below if you want the offline link avoiding convertion
#gsl_checker_err=true
gsl_checker_war=true
return
;;
esac
# Stats
((gsl_stat_link++))
gsl_stat_links=$(( gsl_stat_links + gsl_count_links ))
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Link" \
"Unused: _$gsl_post_hf_1" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# From gsl__do_header: Check ABBR
#======================================================================
gsl__check_abbr() {
gsl__check_header_fields \
"Content" "$gsl_log_h_abbr" "$gsl_log_act_abbr" \
|| return
echo -ne "\r\033[2K: Searching for ABBRs... $gsl_post_hf_1"
gsl_count_abbrs=`
gsl__get_content_line "$gsl_post_hf_1" "$gsl_post" | wc -l`
if (( "$gsl_count_abbrs" > 0 ));then
((gsl_stat_abbr++))
gsl_stat_abbrs=$(( gsl_stat_abbrs + gsl_count_abbrs ))
else
# Missing CONTENT
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"$gsl_marker_abbr" \
"Unused: $gsl_post_header_field_1" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# From gsl__do_header: Check Files
#======================================================================
gsl__check_file() {
gsl__check_header_fields \
"Content" "$gsl_log_h_file" "$gsl_log_act_file" \
|| return
echo -ne "\r\033[2K: Searching for Content Files... $gsl_post_hf_1"
gsl__check_header_field3 "Content" "$gsl_log_h_file" "$gsl_marker_file"
# File exists in folder
if ! [[ -f "$gsl_dir_domain_files/$gsl_post_hf_2" ]];then
gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_file" \
"Not found: $gsl_post_hf_2" \
"$gsl_dir_domain_files/$gsl_post_hf_2"
gsl_checker_err=true
else
# Register file to DB with hash
gsl_file_csum=`
cksum "$gsl_dir_domain_files/$gsl_post_hf_2" \
| awk '{print $1}'`
printf '%s\n' \
"File:$gsl_dir_domain_images/$gsl_post_hf_2:$gsl_file_csum" \
>> "$gsl_db_tmp"
fi
# Exists in Content
gsl_count_files=`
gsl__get_content_line "__$gsl_post_hf_1" "$gsl_post" | wc -l`
if (( $gsl_count_files > 0 ));then
((gsl_stat_file++))
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"$gsl_log_c_file" \
"Unused: __$gsl_post_hf_1" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# From gsl__do_header: Check Files
#======================================================================
gsl__check_image() {
gsl__check_header_fields \
"Header" "$gsl_marker_image" "$gsl_log_act_image" \
|| return
gsl__check_header_field3 \
"Header" "$gsl_marker_image" "$gsl_marker_image" \
|| return
echo -ne "\r\033[2K: Searching for Images... $gsl_post_hf_1"
# File exists in folder
if ! [[ -f "$gsl_dir_domain_images/$gsl_post_hf_2" ]];then
gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_image" \
"Not found: $gsl_post_hf_2" \
"$gsl_dir_domain_images/$gsl_post_hf_2"
gsl_checker_err=true
else
# Register file to DB with hash
gsl_file_csum=`
cksum "$gsl_dir_domain_images/$gsl_post_hf_2" \
| awk '{print $1}'`
printf '%s\n' \
"Image:$gsl_dir_domain_images/$gsl_post_hf_2:$gsl_file_csum" \
>> "$gsl_db_tmp"
fi
# Exists in Content
gsl_count_images=`
gsl__get_content_line "_image:$gsl_post_hf_1" "$gsl_post" | wc -l`
if (( $gsl_count_images > 0 ));then
((gsl_stat_image++))
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Image" \
"Unused: _image:$gsl_post_hf_1" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# From gsl__do_header: Check Files
#======================================================================
gsl__check_fcode() {
gsl__check_header_fields \
"Header" "$gsl_log_h_code" "$gsl_log_act_code" \
|| return
echo -ne "\r\033[2K: Searching for File Codes... $gsl_post_hf_1"
gsl__check_header_field3 "Content" "$gsl_log_h_code" "$gsl_marker_code" \
# File exists in folder
if ! [[ -f "$gsl_dir_domain_files/$gsl_post_hf_2" ]];then
gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_code" \
"Not found: $gsl_post_hf_2" \
"$gsl_dir_domain_files/$gsl_post_hf_2"
gsl_checker_err=true
else
# Register file to DB with hash
gsl_file_csum=`
cksum "$gsl_dir_domain_files/$gsl_post_hf_2" \
| awk '{print $1}'`
printf '%s\n' \
"FCode:$gsl_dir_domain_filess/$gsl_post_hf_2:$gsl_file_csum" \
>> "$gsl_db_tmp"
fi
# Exists in Content
gsl_count_fcodes=`
gsl__get_content_line "_code:$gsl_post_hf_1" "$gsl_post" | wc -l`
if (( $gsl_count_fcodes > 0 ));then
((gsl_stat_fcode++))
else
gsl__logs_print \
"$gsl_log_e" \
"Content" \
"Code" \
"Unused: _code:$gsl_post_hf_1" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
}
#======================================================================
# Stat: Count words
#======================================================================
gsl__stat_words() {
echo -ne "\r\033[2K: Counting Words..."
gsl_stat_words=`cat "$1" | wc -w`
gsl_stat_words=$(( $gsl_stat_words - 2*$gsl_stat_p ))
}
#======================================================================
# Check Post for MISSING & VALID Content HEADERS
#======================================================================
gsl__check_headers() {
echo -ne "\r\033[2K: Searching HEADERS..."
#--------------------------------
# Check if Missing NEEDED HEADERS
#--------------------------------
# Title
echo -ne "\r\033[2K: Searching HEADERS... $gsl_header_title"
gsl__check_needed_headers \
"$gsl_header_title" \
"$gsl_marker_title" \
"POST TITLE"
# Slug
echo -ne "\r\033[2K: Searching HEADERS... $gsl_header_slug"
gsl__check_needed_headers \
"$gsl_header_slug" \
"$gsl_marker_slug" \
"POST-TITLE" \
&& gsl_slug_err=true
# Author
echo -ne "\r\033[2K: Searching HEADERS... $gsl_header_author"
gsl__check_needed_headers \
"$gsl_header_author" \
"$gsl_marker_author" \
"REGISTRED NAME" \
&& gsl_author_err=true
# Date
echo -ne "\r\033[2K: Searching HEADERS... $gsl_header_date"
# Date format check in fr or INternational ?
if [[ `grep -i "fr" <<<"$gsl_site_lang"` ]];then
gsl_log_content="DD-MM-YYYY"
gsl_test_date='^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]+$'
else
gsl_log_content="YYYY-MM-DD"
gsl_test_date='^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]+$'
fi
gsl__check_needed_headers \
"$gsl_header_date" \
"$gsl_marker_date" \
"$gsl_log_content" \
&& gsl_date_err=true
# Description
echo -ne "\r\033[2K: Searching HEADERS... ${gsl_header_info:0:40}..."
gsl__check_needed_headers \
"$gsl_header_info" \
"$gsl_marker_info" \
"DESCRIPTION"
# Tags
echo -ne "\r\033[2K: Searching HEADERS... ${gsl_header_tags:0:40}..."
gsl__check_needed_headers \
"$gsl_header_tags" \
"$gsl_marker_tags" \
"TAG1,OTHER TAG2,TAG3"
#--------------------------------
# Check for VALID Content HEADERS
#--------------------------------
# Author registred
if ! [[ "$gsl_author_err" ]] && \
! [[ `grep "$gsl_header_author" "$gsl_file_auth_ndd"` ]];then
gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_author" \
"$gsl_header_author not registred for domain $gsl_find_domain" \
"${PWD}/$gsl_post"
gsl_checker_err=true
fi
# Date Format YYYY-MM-DD
if ! [[ "$gsl_date_err" ]] && \
! [[ "$gsl_header_date" =~ $gsl_test_date ]];then
gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_date" \
"$gsl_header_date not $gsl_log_content" \
"${PWD}/$gsl_post"
gsl_checker_err=true
gsl_date_mismatch=true
fi
if ! [[ $gsl_date_mismatch ]];then
gsl_date_m=`awk -F- '{print $2}' <<<"$gsl_header_date"`
[[ "$gsl_date_m" -gt 12 ]] \
&& gsl__logs_print \
"$gsl_log_e" \
"Header" \
"$gsl_marker_date" \
"$gsl_header_date : Month > 12..." \
"${PWD}/$gsl_post" \
&& gsl_checker_err=true
fi
# Slug format title-post
if ! [[ "$gsl_slug_err" ]];then
gsl_header_slug_test=${gsl_header_slug// /-}
if ! [[ "$gsl_header_slug" == "$gsl_header_slug_test" ]];then
gsl_new_header_slug="$gsl_marker_slug$gsl_header_slug_test"
sed -i \
"s|$gsl_marker_slug$gsl_header_slug|$gsl_new_header_slug|" \
"$gsl_post" && \
gsl__logs_print \
"$gsl_log_w" \
"Header" \
"$gsl_marker_slug" \
"Changed: $gsl_header_slug_test" \
"${PWD}/$gsl_post"
gsl__get_sum "$gsl_post"
gsl_header_slug=$gsl_header_slug_test
gsl_checker_war=true
fi
fi
}