|
#!/bin/bash |
|
|
|
# clearCache.sh |
|
# Version 0.6 / (c) Bernhard Kraft <kraftb@think-open.at> |
|
# This script is licensed under the GNU GPL v2.0 |
|
|
|
# This script needs "typo3-instance-sqlshell.sh" installed in |
|
# a location pointed to by PATH environment variable. |
|
# The script "typo3-instance-sqlshell.sh" can be found here: |
|
# https://gist.github.com/kraftb/a3becef65ad853243fe2 |
|
|
|
|
|
# Get TYPO3 location |
|
INSTANCEDIR="$1" |
|
|
|
# 1. Are we on MAC OS? |
|
OS="linux" |
|
if [ `echo $OSTYPE | grep -c darwin` -gt 0 ]; then |
|
OS="darwin" |
|
fi |
|
|
|
# Try to determine absolute INSTANCEDIR loaction using "realpath" |
|
REALPATH_BIN=`which realpath 2> /dev/null` |
|
if [ ! -x "$REALPATH_BIN" ]; then |
|
REALPATH_BIN="/usr/bin/realpath" |
|
fi |
|
|
|
if [ ! -x "$REALPATH_BIN" ] |
|
then |
|
echo "Warning: No \"realpath\" installed. Can not determine absolute path of INSTANCEDIR." |
|
echo "Using fallback method." |
|
INSTANCEDIR="`dirname \"$INSTANCEDIR\"`/`basename \"$INSTANCEDIR\"`" |
|
else |
|
INSTANCEDIR=`$REALPATH_BIN "$INSTANCEDIR"` |
|
fi |
|
|
|
# Determine PHP executable |
|
PHP_BIN=`which php5 2> /dev/null` |
|
if [ -z "$PHP_BIN" ] |
|
then |
|
PHP_BIN=`which php 2> /dev/null` |
|
fi |
|
|
|
if [ -z "$PHP_BIN" ] |
|
then |
|
echo "Warning: No PHP binary found. Disabling OPcache clearing." |
|
fi |
|
|
|
# Determine which cache system(s) is/are enabled |
|
if [ -n "$PHP_BIN" ] |
|
then |
|
APC_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "apc.enabled" | grep "On"` |
|
OPCACHE_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "opcache.enable" | grep "On"` |
|
XCACHE_ENABLED=`echo "<?php phpinfo();" | $PHP_BIN | grep "xcache.cacher" | grep "On"` |
|
fi |
|
|
|
|
|
# Output usage message |
|
function usage { |
|
printf "Usage: `basename $0` [pathToTYPO3-Instance]\n\n" |
|
printf "pathToTYPO3-Instance must be a directory\n\n" |
|
exit 1 |
|
} |
|
|
|
|
|
# ---------------- Check for TYPO3 webroot ----------------------- |
|
if [ ! -d "$1" ] |
|
then |
|
usage |
|
fi |
|
|
|
if [ ! -d "$1/typo3conf" ] |
|
then |
|
usage |
|
fi |
|
|
|
if [ ! -d "$1/typo3temp" ] |
|
then |
|
usage |
|
fi |
|
|
|
if [ ! -d "$1/fileadmin" ] |
|
then |
|
usage |
|
fi |
|
|
|
# ---------------- Clear file caches --------------------------------- |
|
|
|
rmrf_safe() { |
|
## Try to "delete" DELPATH as atomically as possible by renaming |
|
## the existing directory and then recreating it |
|
DELPATH="$1" |
|
|
|
# Path does not exist |
|
if [ ! -e "$DELPATH" ]; then |
|
return 0 |
|
fi |
|
|
|
echo $DELPATH |
|
|
|
if [ $OS == "darwin" ] |
|
then |
|
OWNER_GROUP=`stat -f %u:%g "$DELPATH"` |
|
ACCESS=`stat -f %A "$DELPATH"` |
|
else |
|
OWNER_GROUP=`stat --format=%u:%g "$DELPATH"` |
|
ACCESS=`stat --format=%a "$DELPATH"` |
|
fi |
|
|
|
rm -rf "$DELPATH-old" |
|
|
|
mv "$DELPATH" "$DELPATH-old" && \ |
|
mkdir "$DELPATH" && \ |
|
chown $OWNER_GROUP "$DELPATH" && \ |
|
chmod 0$ACCESS "$DELPATH" |
|
|
|
## Clean up: Remove the renamed typo3temp directory |
|
rm -rf "$DELPATH-old" |
|
} |
|
|
|
# Remove everything in typo3temp/* |
|
DELPATH1="$INSTANCEDIR/typo3temp" |
|
rmrf_safe "$DELPATH1" |
|
# Remove everything in typo3conf/autoload/* |
|
DELPATH2="$INSTANCEDIR/typo3conf/autoload" |
|
rmrf_safe "$DELPATH2" |
|
|
|
|
|
# ---------------- Clear database cache --------------------------------- |
|
TABLES=`echo "show tables like 'cf_%';" | typo3-instance-sqlshell.sh "$INSTANCEDIR" | tail -n +2` |
|
CLEARCMD="" |
|
|
|
for i in $TABLES; do |
|
CLEARCMD+="TRUNCATE $i;" |
|
done |
|
|
|
echo $CLEARCMD | typo3-instance-sqlshell.sh "$INSTANCEDIR" |
|
|
|
|
|
# ---------------- Reset opcaches (apc, opcache, xcache) ---------------- |
|
if [ -n "$APC_ENABLED" ] |
|
then |
|
echo "<?php apc_clear_cache();" | $PHP_BIN |
|
fi |
|
|
|
if [ -n "$OPCACHE_ENABLED" ] |
|
then |
|
echo "<?php opcache_reset();" | $PHP_BIN |
|
fi |
|
|
|
if [ -n "$XCACHE_ENABLED" ] |
|
then |
|
echo "<?php xcache_clear_cache();" | $PHP_BIN |
|
fi |
|
|
|
|