Skip to content

Commit

Permalink
Added support for product images cache
Browse files Browse the repository at this point in the history
  • Loading branch information
fballiano committed Sep 23, 2023
1 parent aae1bc3 commit ab54703
Show file tree
Hide file tree
Showing 8 changed files with 98 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
)
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Fballiano/ImageCleaner/Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion app/code/community/Fballiano/ImageCleaner/etc/config.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<config>
<modules>
<Fballiano_ImageCleaner>
<version>0.9.0</version>
<version>1.1.0</version>
</Fballiano_ImageCleaner>
</modules>
<global>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/**
* FBalliano
*
* NOTICE OF LICENSE
*
* This source file is subject to the Open Software License (OSL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/osl-3.0.php
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade this Module to
* newer versions in the future.
*
* @category FBalliano
* @package FBalliano_ImageCleaner
* @copyright Copyright (c) 2021 Fabrizio Balliano (http://fabrizioballiano.it)
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
*/

/* @var $installer Mage_Core_Model_Resource_Setup */
$installer = $this;
$installer->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();

0 comments on commit ab54703

Please sign in to comment.