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.
Find Drupal 7.x rich texts (HTML) tags and shortcodes.
#!/bin/bash -e
# Prevent errors in a pipeline from being masked
set -o pipefail
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.
# 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" "img")
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
@theodorosploumis
Copy link
Author

Example results:

Reports details
---------------
Drupal version: 7.82
Date: 2023-01-19 16:49:13

Tags inside fields
------------------
field_country (table: 25)
body (iframe: 46)
body (script: 47)
body (style: 9)
body (font: 32)
body (table: 107)
body (embed: 12)

Shortcodes inside fields
-------------------------
field_file_tree (filetree: 122)
body (link: 12)

@cherouvim
Copy link

You may want to integrate the following at the top:

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment