#!/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 ;; "icode") check__icode ;; "strong") check__strong ;; "bold") check__bold ;; "em") check__emphasis ;; "strike") check__strike ;; 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 "$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 ;; "$gsl_marker_css") gsl_post_css=" $header_f1" gsl__logs_print -i -h \ "$1" \ "Body template: $header_f1" \ "$PWD/$gsl_post" ;; "$gsl_marker_title") gsl_post_title="$header_f1" ;; "$gsl_marker_date") gsl_post_date="$header_f1" if [[ `grep -i "fr" <<<"$site_lang"` ]];then gsl_test_date='^[0-9][0-9]-[0-9][0-9]-[0-9][0-9][0-9][0-9]+$' gsl_log_content="DD-MM-YYYY" else gsl_test_date='^[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]+$' gsl_log_content="YYYY-MM-DD" fi if [[ "$gsl_post_date" =~ $gsl_test_date ]];then gsl_date_m=`awk -F- '{print $2}' <<<"$gsl_post_date"` (( "$gsl_date_m" > 12 )) \ && gsl__logs_print -e -h \ "$1" \ "'$gsl_post_date' ; Month: $gsl_date_m > 12..." \ "${PWD}/$gsl_post" else gsl__logs_print -e -h \ "$1" \ "Mismatch '$gsl_post_date' ; not $gsl_log_content" \ "${PWD}/$gsl_post" fi # timestamp date to db too (sidebar sort) false_time=`date +%T` if [[ `grep -i "fr" <<<"$site_lang"` ]];then gsl_date_y=`awk -F- '{print $3}' <<<"$gsl_post_date"` gsl_date_d=`awk -F- '{print $1}' <<<"$gsl_post_date"` else gsl_date_y=`awk -F- '{print $1}' <<<"$gsl_post_date"` gsl_date_d=`awk -F- '{print $3}' <<<"$gsl_post_date"` fi date_epoch="$gsl_date_y-$gsl_date_m-$gsl_date_d $false_time" date_epoch=`date +%s -u -d "$date_epoch"` ;; "$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) ;; "$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 ;; "$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 ;; "$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" # Check icode marks match read__line_with "$gsl_mark_icode" "check" \ "icode" "content" "$gsl_tmp_post" # Check strong marks match read__line_with "$gsl_mark_strong" "check" \ "strong" "content" "$gsl_tmp_post" # Check bold marks match read__line_with "$gsl_mark_bold" "check" \ "bold" "content" "$gsl_tmp_post" # Check emphasis marks match read__line_with "$gsl_mark_em" "check" \ "em" "content" "$gsl_tmp_post" # Check strike (line-through) marks match read__line_with "$gsl_mark_strike" "check" \ "strike" "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 # 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 # 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 iCode marks match nbr + stats #====================================================================== check__icode() { post_ln=$((ln + gsl_post_begin - 1)) mark_icode_nbr="${gsl_line//[^$gsl_mark_icode]}" mark_icode_nbr=${#mark_icode_nbr} mark_icode_paired=$(( $mark_icode_nbr % 2 )) if [[ "$mark_icode_paired" -eq 0 ]];then stat_icode_nbr=$(( mark_icode_nbr / 2 )) gsl_stat_icodes=$(( gsl_stat_icodes + stat_icode_nbr )) else gsl__logs_print -e -c \ "iCode" \ "Line: $post_ln ; '$gsl_mark_icode...$gsl_mark_icode' not paired" \ "$PWD/$gsl_post" fi } #====================================================================== # Check stronng marks match nbr + stats #====================================================================== check__strong() { post_ln=$((ln + gsl_post_begin - 1)) mark_strong_nbr="${gsl_line//[^$gsl_mark_strong]}" mark_strong_nbr=${#mark_strong_nbr} mark_strong_paired=$(( $mark_strong_nbr % 2 )) if [[ "$mark_strong_paired" -eq 0 ]];then stat_strong_nbr=$(( mark_strong_nbr / 2 )) gsl_stat_strongs=$(( gsl_stat_strongs + stat_strong_nbr )) else gsl__logs_print -e -c \ "Strong" \ "Line: $post_ln ; '$gsl_mark_strong...$gsl_mark_strong' not paired" \ "$PWD/$gsl_post" fi } #====================================================================== # Check Bold marks match nbr + stats #====================================================================== check__bold() { post_ln=$((ln + gsl_post_begin - 1)) mark_bold_nbr="${gsl_line//[^$gsl_mark_bold]}" mark_bold_nbr=${#mark_bold_nbr} mark_bold_paired=$(( $mark_bold_nbr % 2 )) if [[ "$mark_bold_paired" -eq 0 ]];then stat_bold_nbr=$(( mark_bold_nbr / 2 )) gsl_stat_bolds=$(( gsl_stat_bolds + stat_bold_nbr )) else gsl__logs_print -e -c \ "Bold" \ "Line: $post_ln ; '$gsl_mark_bold...$gsl_mark_bold' not paired" \ "$PWD/$gsl_post" fi } #====================================================================== # Check Bold marks match nbr + stats #====================================================================== check__emphasis() { post_ln=$((ln + gsl_post_begin - 1)) mark_em_nbr="${gsl_line//[^$gsl_mark_em]}" mark_em_nbr=${#mark_em_nbr} mark_em_paired=$(( $mark_em_nbr % 2 )) if [[ "$mark_em_paired" -eq 0 ]];then stat_em_nbr=$(( mark_em_nbr / 2 )) gsl_stat_italics=$(( gsl_stat_italics + stat_em_nbr )) else gsl__logs_print -e -c \ "Emphasis" \ "Line: $post_ln ; '$gsl_mark_em...$gsl_mark_em' not paired" \ "$PWD/$gsl_post" fi } #====================================================================== # Check strike (lt) marks match nbr + stats #====================================================================== check__strike() { post_ln=$((ln + gsl_post_begin - 1)) mark_lt_nbr="${gsl_line//[^$gsl_mark_strike]}" mark_lt_nbr=${#mark_lt_nbr} mark_lt_paired=$(( $mark_lt_nbr % 2 )) if [[ "$mark_lt_paired" -eq 0 ]];then stat_lt_nbr=$(( mark_lt_nbr / 2 )) gsl_stat_strikes=$(( gsl_stat_strikes + stat_lt_nbr )) else gsl__logs_print -e -c \ "Strike" \ "Line: $post_ln ; '$gsl_mark_strike...$gsl_mark_strike' not paired" \ "$PWD/$gsl_post" fi }