Skip to content

Instantly share code, notes, and snippets.

@theodorosploumis
Last active February 28, 2023 11:36
Show Gist options
  • Save theodorosploumis/fdc6d992df29f357e6934cbe5db53b00 to your computer and use it in GitHub Desktop.
Save theodorosploumis/fdc6d992df29f357e6934cbe5db53b00 to your computer and use it in GitHub Desktop.

Revisions

  1. theodorosploumis revised this gist Feb 28, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drupal7_rich_fields_report.sh
    Original file line number Diff line number Diff line change
    @@ -29,7 +29,7 @@ drupal_rich_text_fields=$(eval "$ddev_cmd $drush_cmd sqlq --db-prefix 'SELECT fi
    readarray -t array_fields <<<"$drupal_rich_text_fields"

    # Tags or Shortcode inside HTML body to search for
    tags=("iframe" "script" "style" "font" "table" "drupal-entity" "media" "embed")
    tags=("iframe" "script" "style" "font" "table" "drupal-entity" "media" "embed" "img")
    shortcodes=("quote" "img" "highlight" "button" "dropcap" "item" "clear" "link" "filetree")

    function find-value-tag() {
  2. theodorosploumis revised this gist Feb 7, 2023. 1 changed file with 1 addition and 2 deletions.
    3 changes: 1 addition & 2 deletions drupal7_rich_fields_report.sh
    Original file line number Diff line number Diff line change
    @@ -2,8 +2,7 @@

    # Prevent errors in a pipeline from being masked
    set -o pipefail
    # Exit on missing variable
    set -u
    set -e

    # A script that generates a simple txt report for all the rich texts (that allow HTML value) on a Drupal 7.x project.
    # The report is searching inside each field for special HTML tags or shortcodes.
  3. theodorosploumis revised this gist Feb 7, 2023. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion drupal7_rich_fields_report.sh
    Original file line number Diff line number Diff line change
    @@ -39,7 +39,7 @@ function find-value-tag() {
    sign="<"

    # Add a 3rd argument just to use bracket on regex
    if [ "$3" != "" ]
    if [[ "$3" != "" ]]
    then
    sign="["
    fi
  4. theodorosploumis revised this gist Jan 20, 2023. 1 changed file with 5 additions and 0 deletions.
    5 changes: 5 additions & 0 deletions drupal7_rich_fields_report.sh
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,10 @@
    #!/bin/bash -e

    # Prevent errors in a pipeline from being masked
    set -o pipefail
    # Exit on missing variable
    set -u

    # A script that generates a simple txt report for all the rich texts (that allow HTML value) on a Drupal 7.x project.
    # The report is searching inside each field for special HTML tags or shortcodes.
    # The script is useful for Migration processes as also as for a general overview of the HTML tags found inside fields.
  5. theodorosploumis revised this gist Jan 19, 2023. No changes.
  6. theodorosploumis created this gist Jan 19, 2023.
    86 changes: 86 additions & 0 deletions drupal7_rich_fields_report.sh
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,86 @@
    #!/bin/bash -e

    # A script that generates a simple txt report for all the rich texts (that allow HTML value) on a Drupal 7.x project.
    # The report is searching inside each field for special HTML tags or shortcodes.
    # The script is useful for Migration processes as also as for a general overview of the HTML tags found inside fields.
    # Requires drush 8.x, php. Can also work with ddev.

    # Check ddev command
    ddev_cmd="ddev"
    if ! type "ddev" &> /dev/null; then
    ddev_cmd=""
    fi

    # Check drush command. Inside ddev server there is a drush8 only command.
    drush_cmd="drush"
    if type "drush8" &> /dev/null; then
    drush_cmd="drush8"
    fi

    # Variables
    current_date=$(date +"%Y-%m-%d %T")
    drupal_version=$(eval "$ddev_cmd $drush_cmd status --fields='Drupal version'")
    drupal_rich_text_fields=$(eval "$ddev_cmd $drush_cmd sqlq --db-prefix 'SELECT field_name FROM {field_config} WHERE type = \"text_with_summary\" OR type = \"text_long\" ORDER BY \"field_name\";'")
    # Need to convert the \n separated lines into a bash array. See https://stackoverflow.com/q/26634978/1365264
    readarray -t array_fields <<<"$drupal_rich_text_fields"

    # Tags or Shortcode inside HTML body to search for
    tags=("iframe" "script" "style" "font" "table" "drupal-entity" "media" "embed")
    shortcodes=("quote" "img" "highlight" "button" "dropcap" "item" "clear" "link" "filetree")

    function find-value-tag() {
    table=$1
    element=$2
    sign="<"

    # Add a 3rd argument just to use bracket on regex
    if [ "$3" != "" ]
    then
    sign="["
    fi
    command="$ddev_cmd $drush_cmd sqlq --db-prefix \"SELECT COUNT(entity_id) FROM {field_data_"$table"} WHERE "$table"_value LIKE '%$sign$element%'\""
    eval "$command"
    }

    # Debug variables
    #echo "${tags[*]}"
    #echo "$array_fields"

    printf "\n"
    echo "Reports details"
    echo "---------------"
    echo "$drupal_version"
    echo "Date: $current_date"
    printf "\n"

    echo "Tags inside fields"
    echo "-------------------"

    for field in "${array_fields[@]}"
    do
    for tag in "${tags[@]}"
    do
    count=$(eval "find-value-tag $field $tag")
    if [[ ! $count -eq 0 ]]
    then
    echo "$field ($tag: $count)"
    fi
    done
    done

    printf "\n"

    echo "Shortcodes inside fields"
    echo "-------------------------"

    for field in "${array_fields[@]}"
    do
    for code in "${shortcodes[@]}"
    do
    count=$(eval "find-value-tag $field $code bracket")
    if [[ ! $count -eq 0 ]]
    then
    echo "$field ($code: $count)"
    fi
    done
    done