<?php
require_once 'abstract.php';
class Shell_CleanCache extends Mage_Shell_Abstract
{
/**
* Cleans image cache using catalog/product_image model.
*
*/
protected function cleanImageCache()
{
try {
echo "Cleaning image cache... ";
echo Mage::getModel('catalog/product_image')->clearCache();
echo "[OK]" . PHP_EOL . PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
/**
* Cleans magento data cache:
* - config,
* - layout,
* - block_html
* - translate,
* - collections,
* - eav,
* - config_api
*/
protected function cleanDataCache()
{
try {
echo "Cleaning data cache:" . PHP_EOL;
$types = Mage::app()->getCacheInstance()->getTypes();
foreach ($types as $type => $data) {
echo "Removing $type ... ";
echo Mage::app()->getCacheInstance()->clean($data["tags"]) ? "[OK]" : "[ERROR]";
echo PHP_EOL;
}
echo PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
protected function cleanMergedJSCSS()
{
try {
echo "Cleaning merged JS/CSS... ";
Mage::getModel('core/design_package')->cleanMergedJsCss();
Mage::dispatchEvent('clean_media_cache_after');
echo "[OK]" . PHP_EOL . PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
protected function cleanStoredCache()
{
try {
echo "Cleaning stored cache... ";
echo Mage::app()->getCacheInstance()->clean() ? "[OK]" : "[ERROR]";
echo PHP_EOL . PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
/**
* Does a rmdir on:
* - cache,
* - var/full_page_cache
* - var/minifycache
* - session dir
*
* @todo verify these functions
* @todo check var dir for any other cleanable subdirs.
*/
protected function cleanFiles()
{
try {
echo "Cleaning files:" . PHP_EOL;
echo "Cache... ";
$this->_rrmdirContent(Mage::getBaseDir('cache'));
echo "[OK]" . PHP_EOL;
$full_page_dir = Mage::getBaseDir('var') . DIRECTORY_SEPARATOR . 'full_page_cache';
echo "Full page cache... ";
$this->_rrmdirContent($full_page_dir);
echo "[OK]" . PHP_EOL;
}
$minify_dir = Mage::getBaseDir('var') . DIRECTORY_SEPARATOR . 'minifycache';
echo "Minify cache... ";
$this->_rrmdirContent($minify_dir);
echo "[OK]" . PHP_EOL;
}
echo "Session... ";
$this->_rrmdirContent(Mage::getBaseDir('session'));
echo "[OK]" . PHP_EOL;
echo PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
protected function cleanAccelerator()
{
try {
echo "Cleaning accelerator... ";
accelerator_reset();
echo "[OK]" . PHP_EOL . PHP_EOL;
} catch (Exception $e) {
die("[ERROR:" . $e->getMessage() . "]" . PHP_EOL
);
}
}
protected function cleanAll()
{
$this->cleanImageCache();
$this->cleanDataCache();
$this->cleanStoredCache();
$this->cleanMergedJSCSS();
$this->cleanFiles();
$this->cleanAccelerator();
}
}
/**
* Run script
*
*/
public function run()
{
Mage::app('admin')->setUseSessionInUrl(false);
Mage::getConfig()->init();
$caches = array('image', 'data', 'stored', 'js_css', 'files');
if ($this->getArg('info')) {
echo 'Allowed caches: ' . PHP_EOL;
foreach ($caches as $cache) {
echo "\t" . $cache . PHP_EOL;
}
}
if ($this->getArg('all')) {
$this->cleanAll();
}
if ($this->getArg('clean') && in_array($this->getArg('clean'), $caches)) {
switch ($this->getArg('clean')) {
case 'image':
$this->cleanImageCache();
break;
case 'data':
$this->cleanDataCache();
break;
case 'stored':
$this->cleanStoredCache();
break;
case 'js_css':
$this->cleanMergedJSCSS();
break;
case 'files':
$this->cleanFiles();
break;
}
} else {
echo $this->usageHelp();
}
}
/**
* Removes all elements contained in the given directory
* @param string $dir directory containing elements to remove
*/
private function _rrmdirContent($dir)
{
foreach ($items as $item) {
$path = $dir . DIRECTORY_SEPARATOR . $item;
}
}
/**
* Removes a directory and all elements contained
* @param string $dir directory to remove
*/
private function _rrmdir($dir)
{
foreach ($objects as $object) {
$path = $dir . DIRECTORY_SEPARATOR . $object;
}
}
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php cleanCache.php -- [options]
--clean <cache> Clean <cache>. Any of [image|data|stored|js_css|files]
all Clean all caches
info Show allowed caches
help This help
USAGE;
}
}
$shell = new Shell_CleanCache();
$shell->run();