Skip to content

Instantly share code, notes, and snippets.

@stronk7
Last active March 2, 2021 23:01
Show Gist options
  • Save stronk7/7d6e5fc984d0bacd07f8bbf2c4f680a1 to your computer and use it in GitHub Desktop.
Save stronk7/7d6e5fc984d0bacd07f8bbf2c4f680a1 to your computer and use it in GitHub Desktop.
Look for all the print_error() / moodle_exception() uses, detecting lang strings used, or literals or missing lang strings
#!/usr/bin/env bash
# This script must be put in dirroot, so it's able to find lang files in expected locations.
basepath=${1:-.} # Defaults to current directory, relative dir to look for uses.
pe=0 # print_error() calls
petra=0 # translated print_error() calls
pelit=0 # literal passed in print_error() calls
me=0 # moodle_exception() calls
metra=0 # translated moodle_exception calls
melit=0 # literal passed in moodle_exception() calls
# Let's use another descriptor different from stdin (that some commands within the loop may consume)
regexppe="\\\\?print_error"
regexpex="new +\\\\?moodle_exception"
while IFS= read -r line <&3
do
ispe=
if [[ "$line" =~ $regexppe ]]; then
ispe=1
((pe++))
elif [[ "$line" =~ $regexpex ]]; then
((me++))
else
continue
fi
langstr=
module=
[[ "$line" =~ [^\']*[\']([^\']+)[\']([^\']+[\']([^\']*)[\'])?|[^\"]*[\"]([^\"]+)[\"]([^\"]+[\"]([^\"]*)[\"])? ]]
langstr=${BASH_REMATCH[4]:-${BASH_REMATCH[1]}}
module=${BASH_REMATCH[6]:-${BASH_REMATCH[3]}}
module=${module#core_}
if [[ -z "$module" ]] || [[ "$module" == "moodle" ]] || [[ "$module" == "core" ]]; then
module=error
fi
litfound=
trafound=
nofound=" STRING NOT FOUND"
if [[ $langstr =~ ' ' ]]; then
litfound=" LITERAL FOUND"
nofound=
else
langfiles=$(find . -name "$module.php" | grep '/lang/en/');
if [[ -n "${langfiles}" ]]; then
if grep -q "string\['$langstr'\]" $(find . -name "$module.php" | grep '/lang/en/'); then
trafound=" TRANSLATION FOUND"
nofound=
fi
else
nofound=" MODULE NOT FOUND"
fi
fi
if [[ -n $ispe ]]; then
if [[ -n $litfound ]]; then
((pelit++))
elif [[ -n $trafound ]]; then
((petra++))
fi
else
if [[ -n $litfound ]]; then
((melit++))
elif [[ -n $trafound ]]; then
((metra++))
fi
fi
echo "${nofound}${litfound}${trafound}: $line";
done 3< <(ag "(new +\\\\?moodle_exception|print_error)\([^'\"\$]*['\"]([^'\"]*)['\"]( *, *['\"]([^'\"]*)['\"])?[^\)\n]*[\)\n]" -o --nofilename --nobreak --php ${basepath})
if [[ $pe -gt 0 ]]; then
echo "print_error calls: $pe, translated: $petra ($(($petra*100/$pe))%), literals: $pelit ($(($pelit*100/$pe))%)"
fi
if [[ $me -gt 0 ]]; then
echo "new moodle_exception calls: $me, translated: $metra ($(($metra*100/$me))%), literals: $melit ($(($melit*100/$me))%)"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment