diff --git a/README.md b/README.md index 7b3b658..cebf42f 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ Features --------- - Identify and remove orphan **category images** (reading data from the default "image" attribute and **all custom attributes** of type "image"). -- Identify and remove orphan **product images** (reading data from media_gallery). +- Identify and remove orphan **product images** (reading data from media_gallery) and **product images cache**. - Identify and remove orphan **WYSIWYG images and files** (reading used images/files from cms_block, cms_page, core_email_template tables and all /skin/frontend CSS files). - Check before delete: you can review (and download) the identified images before removing them. - Possibility to **blacklist folders and/or files** (with wildcard support) not to ever identify them as orphans. diff --git a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Grid.php b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Grid.php index ca9ebb6..9253135 100644 --- a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Grid.php +++ b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Grid.php @@ -57,7 +57,9 @@ protected function _prepareColumns() 'options' => array( 3 => $this->__('Category'), 4 => $this->__('Product'), - 98 => $this->__('WYSIWYG'), + -3 => $this->__('Category Cache'), + -4 => $this->__('Product Cache'), + -98 => $this->__('WYSIWYG'), ) )); diff --git a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Renderer/Image.php b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Renderer/Image.php index e23d9da..600f477 100644 --- a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Renderer/Image.php +++ b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Fbimagecleaner/Renderer/Image.php @@ -34,7 +34,13 @@ public function render(Varien_Object $row) case 4: $url .= "catalog/product/"; break; - case 98: + case -3: + $url .= "catalog/category/cache/"; + break; + case -4: + $url .= "catalog/product/cache/"; + break; + case -98: $url .= "wysiwyg/"; break; } diff --git a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php index 1c0555b..c542515 100644 --- a/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php +++ b/app/code/community/Fballiano/ImageCleaner/Block/Adminhtml/Image.php @@ -52,6 +52,13 @@ public function __construct() ) ); + $this->_addButton( + 'sync_product_cache', array( + 'label' => Mage::helper('fballiano_imagecleaner')->__('Sync Products Cache'), + 'onclick' => "setLocation('{$this->getUrl('*/*/syncproductcache')}')" + ) + ); + $this->_addButton( 'flush_media_tmp', array( 'label' => Mage::helper('fballiano_imagecleaner')->__('Flush media/tmp'), diff --git a/app/code/community/Fballiano/ImageCleaner/Helper/Data.php b/app/code/community/Fballiano/ImageCleaner/Helper/Data.php index 1c29d99..634e4f3 100644 --- a/app/code/community/Fballiano/ImageCleaner/Helper/Data.php +++ b/app/code/community/Fballiano/ImageCleaner/Helper/Data.php @@ -48,7 +48,7 @@ public function getMediaDirByEntityTypeId($entity_type_id) if ($entity_type_id == 3) return "{$media_dir}catalog/category/"; if ($entity_type_id == 4) return "{$media_dir}catalog/product/"; - if ($entity_type_id == 98) return "{$media_dir}wysiwyg/"; + if ($entity_type_id == -98) return "{$media_dir}wysiwyg/"; return $media_dir; } diff --git a/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php b/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php index bb623fc..9d0e43e 100644 --- a/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php +++ b/app/code/community/Fballiano/ImageCleaner/controllers/Adminhtml/FbimagecleanerController.php @@ -122,9 +122,57 @@ public function syncproductAction() $this->_redirect('*/*'); } + public function syncproductCacheAction() + { + $entity_type_id = -4; + $media_dir = Mage::getBaseDir('media') . '/catalog/product/cache'; + $media_dir_nocache = Mage::getBaseDir('media') . '/catalog/product'; + $resource = Mage::getSingleton('core/resource'); + $db = $resource->getConnection('core_read'); + + if (!file_exists($media_dir) || !is_dir($media_dir)) { + Mage::getSingleton('adminhtml/session')->addError($this->__('"media/catalog/product/cache" folder does not exist.')); + $this->_redirect('*/*'); + return; + } + + $fs_images = Mage::helper('fballiano_imagecleaner')->scandirRecursive($media_dir); + $fs_images = str_replace("{$media_dir}/", '', $fs_images); + + $unused_images = array(); + foreach ($fs_images as $fs_image) { + if (strpos($fs_image, '/placeholder/') !== false) { + continue; + } + + $fs_image_path_nocache = explode('/', $fs_image); + $fs_image_path_nocache = array_slice($fs_image_path_nocache, -3); + $fs_image_path_nocache = implode('/', $fs_image_path_nocache); + if (!file_exists("{$media_dir_nocache}/{$fs_image_path_nocache}")) { + $unused_images[] = $fs_image; + } + } + + if ($unused_images) { + $cleaner_table = $resource->getTableName('fb_imagecleaner_image'); + $already_seen_images = $db->fetchCol("SELECT path FROM {$cleaner_table} WHERE entity_type_id={$entity_type_id}"); + $unused_images = array_diff($unused_images, $already_seen_images); + if ($unused_images) { + foreach ($unused_images as $unused_image) { + $db->insert($cleaner_table, array( + 'entity_type_id' => $entity_type_id, + 'path' => $unused_image + )); + } + } + } + + $this->_redirect('*/*'); + } + public function syncwysiwygAction() { - $entity_type_id = 98; + $entity_type_id = -98; $media_dir = Mage::getBaseDir('media') . '/wysiwyg'; $resource = Mage::getSingleton('core/resource'); $db = $resource->getConnection('core_read'); diff --git a/app/code/community/Fballiano/ImageCleaner/etc/config.xml b/app/code/community/Fballiano/ImageCleaner/etc/config.xml index c0aa8a5..f0247e0 100644 --- a/app/code/community/Fballiano/ImageCleaner/etc/config.xml +++ b/app/code/community/Fballiano/ImageCleaner/etc/config.xml @@ -2,7 +2,7 @@ - 0.9.0 + 1.1.0 diff --git a/app/code/community/Fballiano/ImageCleaner/sql/fballiano_imagecleaner_setup/upgrade-1.0.0-1.1.0.php b/app/code/community/Fballiano/ImageCleaner/sql/fballiano_imagecleaner_setup/upgrade-1.0.0-1.1.0.php new file mode 100644 index 0000000..b14c9df --- /dev/null +++ b/app/code/community/Fballiano/ImageCleaner/sql/fballiano_imagecleaner_setup/upgrade-1.0.0-1.1.0.php @@ -0,0 +1,29 @@ +startSetup(); +$installer->run(" +TRUNCATE {$this->getTable('fb_imagecleaner_image')}; +ALTER TABLE `{$this->getTable( 'fb_imagecleaner_image' )}` MODIFY COLUMN `entity_type_id` SMALLINT SIGNED;"); +$installer->endSetup();