#!/bin/bash # file: gsl__post_checkers # Folder: /var/lib/gsl/scripts # By echolib (XMPP: im@echolib.re) # License: GNU AFFERO GENERAL PUBLIC LICENSE Version 3, 19 November 2007 #====================================================================== # Get arguments from COMMAND (check) # $2 -F (to force) or File # $3 File #====================================================================== check__OPTIONS() { gsl_proc="check" while test "$2" do case "$2" in -F) gsl_force_check=true log_info_force="Force " ;; -N) unset gsl_force_check gsl_check_newer=true ;; *".gsl") if__file "$2" post gsl_post="$2" post_only="$2" ;; *) gsl__invalid_option "$2" "-F|-N|(FILE)" ;; esac shift done if ! [[ "$gsl_post" ]];then log_info_all=" ALL" [[ $gsl_check_newer ]] \ && log_info_all+=" NEWER" fi log__process_begin "${log_info_force}Check${log_info_all}" posts__loop check # For logs ; set post or unset if all [[ $log_info_all ]] && unset gsl_post [[ $post_only ]] && gsl_post="$post_only" log__process_end "${log_info_force}Check${log_info_all}" } #====================================================================== # From post__loop() ; get and CHECK headers values # Called from read__line_with() in gsl__do_commons # $1: marker # $2: type (meta/metas) #====================================================================== check__headers_from_filter() { case "$4" in meta) check__meta_header "$3" ;; metas|content) case "$3" in "title") check__titles ;; "link") check__link ;; "abbr") check__abbr ;; "image") check__file_image ;; "flink") check__file_link ;; "fcode") check__file_code ;; "fbrut") check__file_brut ;; esac ;; esac } #====================================================================== # Call in posts_loop() for process before Checkers() # Check if error in meta marker # From get__needed_headers() in gsl__do_commons # $1: marker #====================================================================== check__meta_header() { if ! [[ "$header_f1" ]];then gsl__logs_print -e -h \ "$1" \ "Not set" \ "$PWD/$gsl_post" return 1 fi case "$1" in # TYPE "$gsl_marker_type") gsl_post_type="$header_f1" case "$gsl_post_type" in page|post) gsl__logs_print -i -h \ "$1" \ "Set to '$gsl_post_type'" \ "${PWD}/$gsl_post" ;; *) gsl__logs_print -e -h \ "$1" \ "Wrong '$gsl_post_type' ; $gsl_log_act_type" \ "${PWD}/$gsl_post" ;; esac ;; # CSS "$gsl_marker_css") gsl_post_css=" $header_f1" gsl__logs_print -i -h \ "$1" \ "Body template: $header_f1" \ "$PWD/$gsl_post" ;; # Title "$gsl_marker_title") gsl_post_title="$header_f1" ;; # DATE "$gsl_marker_date") gsl_post_date="$header_f1" case "$gsl_post_date" in [0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]) gsl_date_m=`awk -F- '{print $2}' <<<"$gsl_post_date"` if (( "$gsl_date_m" > 12 ));then gsl__logs_print -e -h \ "$1" \ "'$gsl_post_date' ; Month: $gsl_date_m > 12..." \ "${PWD}/$gsl_post" return fi # timestamp date to db too (sidebar sort) false_time=`date +%T` gsl_date_y=`awk -F- '{print $1}' <<<"$gsl_post_date"` gsl_date_d=`awk -F- '{print $3}' <<<"$gsl_post_date"` date_epoch="$gsl_date_y-$gsl_date_m-$gsl_date_d $false_time" date_epoch=`date +%s -u -d "$date_epoch"` ;; *) gsl__logs_print -e -h \ "$1" \ "Mismatch '$gsl_post_date' ; not YYYY-MM-DD" \ "${PWD}/$gsl_post" ;; esac ;; # SLUG "$gsl_marker_slug") gsl_post_slug="$header_f1" gsl_post_slug_src="$gsl_post_slug" gsl_hash_slug_src=`cksum <<<"$gsl_post_slug_src"` # Type page with "/" in if [[ "$gsl_post_type" == "page" ]] && \ [[ `grep '/' <<<"$gsl_post_slug"` ]];then gsl_post_slug=${gsl_post_slug////-} gsl__logs_print -w -h \ "$1" \ "Has '/' for type $gsl_post_type ; Replaced with '-'" \ "${PWD}/$gsl_post" fi # Ensure no ending / for slug gsl_post_slug=${gsl_post_slug/%\//} # Has space gsl_post_slug=${gsl_post_slug// /-} gsl_hash_slug=`cksum <<<"$gsl_post_slug"` if ! [[ "$gsl_hash_slug" == "$gsl_hash_slug_src" ]];then mark_slug_src="$gsl_marker_slug $gsl_post_slug_src" mark_slug_new="$gsl_marker_slug $gsl_post_slug" sed -i \ "s^$mark_slug_src^$mark_slug_new^" \ "$gsl_post" gsl__logs_print -w -h \ "$1" \ "Changed to: '$gsl_post_slug'" \ "${PWD}/$gsl_post" article__sum "$gsl_post" fi # Already used slug while read -r "file_db" do gsl_slug_db_hash=` awk -F= '/article_Hash_DB/ {print $2}' \ "$file_db"` gsl_slug_db_type=` awk -F= '/article_Type/ {print $2}' \ "$file_db"` ! [[ "$gsl_slug_db_type" == "$gsl_post_type" ]] \ && continue [[ "$gsl_slug_db_hash" == "$article_Hash_DB" ]] \ && continue gsl__logs_print -e -h \ "$1" \ "'$header_f1' already in use ; change it" \ "$file_db" break done < <(grep -sl "article_Slug=\"$gsl_post_slug\"" \ $gsl_dir_db_domain/*.db) ;; # AUTHOR "$gsl_marker_author") gsl_post_author="$header_f1" if ! [[ `grep "^$gsl_post_author$" "$gsl_file_domain_authors"` ]];then gsl__logs_print -e -h \ "$1" \ "'$gsl_post_author' ; not registred !" \ "${PWD}/$gsl_post" fi ;; # INFO "$gsl_marker_info") gsl_post_info="$header_f1" if (( ${#gsl_post_info} <= 18 ));then gsl__logs_print -w -h \ "$1" \ "'$gsl_post_info' ; not too short ?" \ "${PWD}/$gsl_post" fi ;; # TAGS "$gsl_marker_tags") gsl_post_tags="$header_f1" gsl_stat_tags=`awk -F, '{print NF}' <<<$gsl_post_tags` ;; esac } #====================================================================== # -------------------------------- # Do all checkers From: loop_posts # -------------------------------- #====================================================================== Checkers() { get__needed_headers "check" "$gsl_tmp_head" ! [[ $gsl_do_check ]] && return stats__init # init stats values gsl_tmp_db=`mktemp` # Create tmp DB for found files to add then to DB # Check for links read__line_with "^$gsl_marker_link" "check" \ "link" "metas" "$gsl_tmp_head" # Check abbrs read__line_with "^$gsl_marker_abbr" "check" \ "abbr" "metas" "$gsl_tmp_head" # Check images read__line_with "^$gsl_marker_image" "check" \ "image" "metas" "$gsl_tmp_head" # Check files read__line_with "^$gsl_marker_flink" "check" \ "flink" "metas" "$gsl_tmp_head" # Check filesCODE read__line_with "^$gsl_marker_fcode" "check" \ "fcode" "metas" "$gsl_tmp_head" # Check for brut code file read__line_with "^$gsl_marker_fbrut" "check" \ "fbrut" "metas" "$gsl_tmp_head" # Check for content titles read__line_with "^#[1-6]" "check" \ "title" "content" "$gsl_tmp_post" # Independant modules check__paragraphs "$gsl_tmp_post" # Check for paragraphs markers check__lists "$gsl_tmp_post" # Check for lists markers check__blockquote "$gsl_tmp_post" # Check for blockquote markers check__bsis "$gsl_tmp_post" # Check paired & stats check__icode "$gsl_tmp_post" # Check paired & stats for inline-code # Errors => remove from DB and srv WIP [[ "$gsl_checker_err" ]] \ && article_Errors="yes" \ || article_Errors="no" Stat__words "$gsl_tmp_post" DB__set_status chk # Remove temp files rm -f "$gsl_tmp_post" \ "$gsl_tmp_head" \ "$gsl_tmp_db" } #---------------------------------------------------------------------- # ------------------------------------------------- # Modules checkers # ------------------------------------------------- #---------------------------------------------------------------------- #====================================================================== # Checking titles #====================================================================== check__titles() { ((gsl_stat_titles++)) mark_title=`awk '{print $1}' <<<"$gsl_line"` content_title=`awk '{print $2}' <<<"$gsl_line"` post_ln=$((ln + gsl_post_begin - 1)) [[ "$content_title" ]] && return gsl__logs_print -e -c \ "Title" \ "Line: $post_ln ; No content for $mark_title" \ "${PWD}/$gsl_post" } #====================================================================== # Check link #====================================================================== check__link() { if ! [[ "$header_f1" && "$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_link" \ "Line: $ln not set ; $gsl_log_act_link" \ "${PWD}/$gsl_post" return fi if ! [[ "$header_f3" ]];then gsl__logs_print -w -h \ "$gsl_marker_link" \ "Line: $ln No Alt-Text ; $gsl_log_act_link" \ "${PWD}/$gsl_post" fi if ! [[ `grep "\<_$header_f1\>" "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_link" \ "Line: $ln ; Unused: '_$header_f1'" \ "${PWD}/$gsl_post" return fi # For online stats, convert /... url # article not yet in www ; do not check status and count case "$header_f2" in "/"*) [[ $gsl_srv_www_file ]] \ && header_f2="$site_url$header_f2" ;; esac # Get Online status case "$header_f2" in "http"*) url_status=` timeout 2 \ curl -o /dev/null --silent --head --write-out \ '%{http_code}' \ "$header_f2" 2>/dev/null \ || url_status="T"` ;; *) url_status=NS ;; esac # Analyse Online Status case "$url_status" in 1*|2*|3*) gsl__logs_print -i -h \ "$gsl_marker_link" \ "Online: $url_status ; ${header_f2:0:30}..." \ "${PWD}/$gsl_post" ((gsl_stat_links_online++)) ;; NS) gsl__logs_print -w -h \ "$gsl_marker_link" \ "No status ; ${header_f2:0:30}..." \ "${PWD}/$gsl_post" ;; T) gsl__logs_print -w -h \ "$gsl_marker_link" \ "Timeout ; ${header_f2:0:30}..." \ "${PWD}/$gsl_post" ;; ""|*) gsl__logs_print -w -h \ "$gsl_marker_link" \ "Offline: $url_status ; ${header_f2:0:30}..." \ "${PWD}/$gsl_post" ;; esac # Stat: Count ((gsl_stat_links++)) } #====================================================================== # Check paragraphs markers #====================================================================== check__paragraphs() { gsl_post_po=` grep "^$gsl_mark_p_start$\|^$gsl_mark_p_start " "$1" \ | wc -l` gsl_post_pc=` grep "^$gsl_mark_p_close$" "$1" \ | wc -l` if (( "$gsl_post_po" == 0 ));then gsl__logs_print -w -c \ "Paragraphs" \ "Unused '$gsl_mark_p_start' & '$gsl_mark_p_close' at begining lines" \ "${PWD}/$gsl_post" gsl_stat_p="0" elif (( "$gsl_post_po" == "$gsl_post_pc" ));then gsl_stat_p="$gsl_post_po" else gsl__logs_print -e -c \ "Paragraphs" \ "Mismatch '$gsl_mark_p_start' & '$gsl_mark_p_close' ; not paired" \ "${PWD}/$gsl_post" fi } #====================================================================== # Check paragraphs markers #====================================================================== check__lists() { gsl_post_lo=`grep "^<<$\|^<< " "$1" | wc -l` gsl_post_lc=`grep "^>>$" "$1" | wc -l` if (( "$gsl_post_lo" == "$gsl_post_lc" ));then gsl_stat_lists="$gsl_post_lo" else gsl__logs_print -e -c \ "Lists" \ "Mismatch '<<' & '>>' ; not paired" \ "${PWD}/$gsl_post" fi } #====================================================================== # Check blockquotes markers #====================================================================== check__blockquote() { gsl_post_bq=`grep "^$gsl_mark_blockquote" "$1" | wc -l` gsl_post_bq_paired=$(( $gsl_post_bq % 2 )) if [[ "$gsl_post_bq_paired" -eq 0 ]];then gsl_stat_bq=$(( gsl_post_bq / 2 )) else gsl__logs_print -e -c \ "Blockquote" \ "Mismatch: '$gsl_mark_blockquote' ; not paired" \ "${PWD}/$gsl_post" fi } #====================================================================== # Check ABBR #====================================================================== check__abbr() { if ! [[ "$header_f1" && "$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_abbr" \ "Line: $ln not set ; $gsl_log_act_abbr" \ "${PWD}/$gsl_post" # must be in caps lock elif ! [[ "$header_f1" == ${header_f1^^} ]];then gsl__logs_print -e -c \ "$gsl_marker_abbr" \ "Line: $ln ; '$header_f1' not UPPER case" \ "${PWD}/$gsl_post" # ! forget space ↓ elif ! [[ `grep -E " $header_f1|^$header_f1 " "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_abbr" \ "Line: $ln ; Unused: '$header_f1'" \ "${PWD}/$gsl_post" else ((gsl_stat_abbrs++)) fi } #====================================================================== # Check Flink #====================================================================== check__file_link() { if ! [[ "$header_f1" && "$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_flink" \ "Line: $ln not set ; $gsl_log_act_flink" \ "${PWD}/$gsl_post" return fi if ! [[ "$header_f3" ]];then gsl__logs_print -w -h \ "$gsl_marker_flink" \ "Line: $ln No Alt-Text ; $gsl_log_act_flink" \ "${PWD}/$gsl_post" fi if ! [[ `grep "\<__$header_f1\>" "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_flink" \ "Unused: '__$header_f1'" \ "${PWD}/$gsl_post" gsl_flink_err=true fi if ! [[ -f "$gsl_dir_domain_files/$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_flink" \ "Not found: $header_f2" \ "$gsl_dir_domain_docs/$header_f2" gsl_flink_err=true fi [[ $gsl_flink_err ]] && return # Register file to DB printf '%s\n' \ "# Flink_URI:$gsl_dir_domain_fles/$header_f2" \ >> "$gsl_tmp_db" # Stat ((gsl_stat_flinks++)) } #====================================================================== # Check images #====================================================================== check__file_image() { if ! [[ "$header_f1" && "$header_f2" && "$header_f3" ]];then gsl__logs_print -e -h \ "$gsl_marker_image" \ "Line: $ln not set ; $gsl_log_act_image" \ "${PWD}/$gsl_post" return fi if ! [[ `grep "_image:$header_f1" "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_image" \ "Unused: '_image:$header_f1'" \ "${PWD}/$gsl_post" gsl_image_err=true fi if ! [[ -f "$gsl_dir_domain_images/$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_image" \ "Not found: $header_f2" \ "$gsl_dir_domain_images/$header_f2" gsl_image_err=true fi [[ $gsl_image_err ]] && return # Register file to DB printf '%s\n' \ "# Image_URI:$gsl_dir_domain_images/$header_f2" \ >> "$gsl_tmp_db" # Stat ((gsl_stat_images++)) } #====================================================================== # Check fcode: file code to convert and import in content #====================================================================== check__file_code() { if ! [[ "$header_f1" && "$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_fcode" \ "Line: $ln not set ; $gsl_log_act_fcode" \ "${PWD}/$gsl_post" return fi if ! [[ "$header_f3" ]];then gsl__logs_print -w -h \ "$gsl_marker_fcode" \ "Line: $ln No Alt-Text ; $gsl_log_act_fcode" \ "${PWD}/$gsl_post" fi if ! [[ `grep "$gsl_mark_fcode:$header_f1" "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_fcode" \ "Unused: '$gsl_mark_fcode:$header_f1'" \ "${PWD}/$gsl_post" gsl_fcode_err=true fi if ! [[ -f "$gsl_dir_domain_files/$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_fcode" \ "Not found: $header_f2" \ "$gsl_dir_domain_precodes/$header_f2" gsl_fcode_err=true fi [[ $gsl_fcode_err ]] && return # Register file to DB printf '%s\n' \ "# Fcode_URI:$gsl_dir_domain_files/$header_f2" \ >> "$gsl_tmp_db" # Stat ((gsl_stat_fcodes++)) } #====================================================================== # Check fbrut : brut file to import in content #====================================================================== check__file_brut() { if ! [[ "$header_f1" && "$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_fbrut" \ "Line: $ln not set ; $gsl_log_act_fbrut" \ "${PWD}/$gsl_post" return fi if ! [[ `grep "$gsl_mark_fbrut:$header_f1" "$gsl_tmp_post"` ]];then gsl__logs_print -e -c \ "$gsl_marker_fbrut" \ "Unused: '$gsl_mark_fbrut:$header_f1'" \ "${PWD}/$gsl_post" gsl_fbrut_err=true fi if ! [[ -f "$gsl_dir_domain_files/$header_f2" ]];then gsl__logs_print -e -h \ "$gsl_marker_fbrut" \ "Not found: $header_f2" \ "$gsl_dir_domain_files/$header_f2" gsl_fbrut_err=true fi [[ $gsl_fcode_err ]] && return # Register file to DB printf '%s\n' \ "# Fbrut_URI:$gsl_dir_domain_files/$header_f2" \ >> "$gsl_tmp_db" # Stat ((gsl_stat_fbruts++)) } #====================================================================== # Check bsis + stats # $1: file (gsl_tmp_post) #====================================================================== check__bsis() { while read -r "ln" "gsl_line" do ln=$(( ln + $gsl_post_begin - 1 )) unset line_has_bold \ line_has_strong \ line_has_em \ line_has_cross \ line_has_del case "$gsl_line" in *"$gsl_mark_bold"*|\ *"$gsl_mark_strike"*|\ *"$gsl_mark_em"*|\ *"$gsl_mark_strong"*|\ *"$gsl_mark_del"*) check__bsis_stats ;; esac if [[ $line_has_bold ]];then bold_paired=$(( $found_bold % 2 )) if ! [[ "$bold_paired" -eq 0 ]];then gsl__logs_print -e -c \ "Bold" \ "Line: $ln ; $found_bold '$gsl_mark_bold' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_bolds=$(( gsl_stat_bolds + found_bold / 2 )) fi fi if [[ $line_has_strong ]];then strong_paired=$(( $found_strong % 2 )) if ! [[ "$strong_paired" -eq 0 ]];then gsl__logs_print -e -c \ "Strong" \ "Line: $ln ; $found_strong '$gsl_mark_strong' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_strongs=$(( gsl_stat_strongs + found_strong / 2 )) fi fi if [[ $line_has_em ]];then em_paired=$(( $found_em % 2 )) if ! [[ "$em_paired" -eq 0 ]];then gsl__logs_print -e -c \ "Emphasis" \ "Line: $ln ; $found_em '$gsl_mark_em' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_italics=$(( gsl_stat_italics + found_em / 2 )) fi fi if [[ $line_has_cross ]];then cross_paired=$(( $found_cross % 2 )) if ! [[ "$cross_paired" -eq 0 ]];then gsl__logs_print -e -c \ "Cross" \ "Line: $ln ; $found_cross '$gsl_mark_cross' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_strikes=$(( gsl_stat_strikes + found_cross / 2 )) fi fi if [[ $line_has_del ]];then del_paired=$(( $found_del % 2 )) if ! [[ "$del_paired" -eq 0 ]];then gsl__logs_print -e -c \ "Del" \ "Line: $ln ; $found_del '$gsl_mark_del' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_dels=$(( gsl_stat_dels + found_del / 2 )) fi fi done < <(cat -n "$1") } check__bsis_stats() { found_bold=0 found_strong=0 found_em=0 found_cross=0 found_del=0 for (( i=0; i<${#gsl_line}; i++ )) do case "${gsl_line:$i:1}" in "$gsl_mark_icode") [[ $code_open ]] \ && unset code_open \ || code_open=true ;; "$gsl_mark_bold") [[ $code_open ]] && continue ((found_bold++)) line_has_bold=true ;; *"$gsl_mark_strong"*) [[ $code_open ]] && continue ((found_strong++)) line_has_strong=true ;; *"$gsl_mark_em"*) [[ $code_open ]] && continue ((found_em++)) line_has_em=true ;; *"$gsl_mark_cross"*) [[ $code_open ]] && continue ((found_cross++)) line_has_cross=true ;; *"$gsl_mark_del"*) [[ $code_open ]] && continue ((found_del++)) line_has_del=true ;; esac done } #====================================================================== # Check iCode + stats # $1: file (gsl_tmp_post) #====================================================================== check__icode() { while read -r "ln" "gsl_line" do ln=$(( ln + $gsl_post_begin - 1 )) unset line_has_icode case "$gsl_line" in *"$gsl_mark_icode"*) found_icode=0 for (( i=0; i<${#gsl_line}; i++ )) do case "${gsl_line:$i:1}" in "$gsl_mark_icode") ((found_icode++)) line_has_icode=true ;; esac done ;; esac if [[ $line_has_icode ]];then icode_paired=$(( $found_icode % 2 )) if ! [[ "$icode_paired" -eq 0 ]];then gsl__logs_print -e -c \ "i-Code" \ "Line: $ln ; $found_icode '$gsl_mark_icode' ; not paired" \ "$PWD/$gsl_post" else gsl_stat_icodes=$(( gsl_stat_icodes + found_icode / 2 )) fi fi done < <(cat -n "$1") }