Skip to content

Instantly share code, notes, and snippets.

@ginkgomzd
Forked from axon-obriend/civimail-template-diffs
Last active April 1, 2018 20:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ginkgomzd/0ae45009afda8b6d85aca9e03a57df2b to your computer and use it in GitHub Desktop.
Save ginkgomzd/0ae45009afda8b6d85aca9e03a57df2b to your computer and use it in GitHub Desktop.
Compare any modified CiviCRM workflow templates to the originals
#!/usr/bin/env bash
DIR_DEFAULTS="./tmp.default"
DIR_MODIFIEDS="./tmp.modified"
test -d "$DIR_DEFAULTS" && echo "ERROR: $DIR_DEFAULTS would be deleted" && exit 1
mkdir "$DIR_DEFAULTS"
test -d "$DIR_MODIFIEDS" && echo "ERROR: $DIR_DEFAULTS would be deleted" && exit 1
mkdir "$DIR_MODIFIEDS"
TID=`mysql --skip-column-names --database=civicrm -e "
SELECT workflow_id
FROM civicrm_msg_template
WHERE workflow_id IS NOT NULL
GROUP BY workflow_id
HAVING COUNT(*)>1"`
if [ -z "$1" ]; then
DIFF_DIR=$(pwd)/diffed
else
DIFF_DIR="$1"
fi
if [ ! -d "$DIFF_DIR" ]; then
mkdir -p "$DIFF_DIR"
fi
for ID in $TID; do
TNAME=`mysql --skip-column-names --database=civicrm -e "
SELECT msg_title
FROM civicrm_msg_template
WHERE workflow_id=$ID AND is_default=1"`
echo Comparing $TNAME
for X in text html; do
# civicrm_msg_template columns CiviSpeak:
# - is_default = active
# - is_active = enabled
# - is_reserved = distro version
cat > "$DIR_DEFAULTS/$TNAME.$X" <<EOF
`mysql --skip-column-names --database=civicrm -e "
SELECT msg_$X
FROM civicrm_msg_template
WHERE workflow_id=$ID AND is_reserved=1"`
EOF
cat > "$DIR_MODIFIEDS/$TNAME.$X" <<EOF
`mysql --skip-column-names --database=civicrm -e "
SELECT msg_$X
FROM civicrm_msg_template
WHERE workflow_id=$ID AND is_reserved=0 and is_default=1"`
EOF
# replace new-line entity with actual new-line
sed -i -e 's/\\n/\n/g' "$DIR_DEFAULTS/$TNAME.$X"
sed -i -e 's/\\n/\n/g' "$DIR_MODIFIEDS/$TNAME.$X"
diff -NBuw "$DIR_DEFAULTS/$TNAME.$X" "$DIR_MODIFIEDS/$TNAME.$X" > "$DIFF_DIR/$TNAME.$X.diff"
#reversed: recommended where modified is older than default
# diff -NBuw "$DIR_MODIFIEDS/$TNAME.$X" "$DIR_DEFAULTS/$TNAME.$X" > "$DIFF_DIR/$TNAME.$X.diff"
done
done
echo clean-up: "$DIR_DEFAULTS" "$DIR_MODIFIEDS"
rm -rf "$DIR_DEFAULTS" "$DIR_MODIFIEDS"
# Every system message is automatically copied.
# So, delete empty diffs.
# shellcheck disable=SC2038
find $DIFF_DIR -name '*diff' -size 0 | xargs -d '\n' rm
echo
echo "DIFFERENCES FOUND:"
ls -1 "$DIFF_DIR"
@ginkgomzd
Copy link
Author

Changes from upstream:

  • create diffs in a sub-directory, defaults to "./diffed", or first supplied argument
  • change working directories, and clean up afterwards.
  • refuse to run if working directories already exist
  • correct for backwards meaning of "default" in civicrm schema.
  • adjust diff command options
  • Delete empty diff's since every template gets copied by CiviCRM. (How does it know which is overridden??)
  • quieter output
  • move set TNAME out of nested loop
  • list contents of output directory to show differences found

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