Guest User

Untitled

a guest
Jan 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.95 KB | None | 0 0
  1. <?php
  2. require_once 'abstract.php';
  3.  
  4. class Mage_Shell_CheckImages extends Mage_Shell_Abstract
  5. {
  6. const CATALOG_PRODUCT = '/catalog/product';
  7.  
  8. const CACHE = '/cache/';
  9. const PLACEHOLDER = '/placeholder/';
  10.  
  11. protected function _glob_recursive($pattern, $flags = 0)
  12. {
  13. $files = glob($pattern, $flags);
  14. foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) {
  15. $files = array_merge($files, $this->_glob_recursive($dir . '/' . basename($pattern), $flags));
  16. }
  17. return $files;
  18. }
  19.  
  20. public function run()
  21. {
  22. if ($this->getArg('help')) {
  23. echo $this->usageHelp();
  24. return;
  25. }
  26.  
  27. $media = Mage::getBaseDir('media');
  28. $debug = $this->getArg('debug');
  29. $dryrun = $this->getArg('dry') ? true : false ;
  30. $includeCache = $this->getArg('cache');
  31.  
  32. $imagesOnDb = array();
  33. $imagesOnDisk = array();
  34. $setup = new Mage_Core_Model_Resource_Setup('core_setup');
  35. /** @var Varien_Db_Adapter_Pdo_Mysql $connection */
  36. $connection = $setup->getConnection();
  37.  
  38. $entityTypeIdSql = sprintf(
  39. "SELECT `entity_type_id` FROM `%s` WHERE `entity_type_code` = :entity_type_code;",
  40. $setup->getTable('eav/entity_type')
  41. );
  42. $entityTypeId = (int)$connection->fetchOne($entityTypeIdSql, array('entity_type_code' => Mage_Catalog_Model_Product::ENTITY));
  43. if (!$entityTypeId) {
  44. throw new RuntimeException(sprintf(
  45. "Could not find entity type code for entity %s",
  46. Mage_Catalog_Model_Product::ENTITY
  47. ));
  48. }
  49.  
  50. $sql = "SELECT DISTINCT value
  51. FROM (
  52. SELECT value
  53. FROM `{$setup->getTable('catalog_product_entity_media_gallery')}`
  54. WHERE attribute_id
  55. IN (SELECT attribute_id FROM `{$setup->getTable('eav_attribute')}` WHERE `attribute_code` in ('media_gallery') AND entity_type_id = :entity_type_id)
  56. UNION
  57. SELECT value
  58. FROM `{$setup->getTable('catalog_product_entity_varchar')}`
  59. WHERE attribute_id
  60. IN (SELECT attribute_id FROM `{$setup->getTable('eav_attribute')}` WHERE `attribute_code` in ('image','small_image','thumbnail') AND entity_type_id = :entity_type_id)
  61. ) AS T";
  62. $result = $connection->query($sql, array('entity_type_id' => $entityTypeId));
  63. foreach ($result->fetchAll() as $rec) {
  64. $imagesOnDb[$rec['value']] = 1;
  65. }
  66. $imagesOnDb = array_keys($imagesOnDb);
  67. if ($debug) print_r(array_slice($imagesOnDb, 0, 100));
  68.  
  69. if ($debug) echo $media . "/*\n";
  70. $skip = strlen($media . self::CATALOG_PRODUCT);
  71. foreach ($this->_glob_recursive($media . self::CATALOG_PRODUCT . '/*', GLOB_MARK) as $img) {
  72. if (substr($img, -1) != '/') {
  73. if ((substr($img, $skip, 13) != self::PLACEHOLDER) && ($includeCache || (substr($img, $skip, 7) != self::CACHE))) {
  74. $imagesOnDisk[] = substr($img, $skip);
  75. }
  76. }
  77. }
  78. if ($debug) print_r(array_slice($imagesOnDisk, 0, 100));
  79.  
  80. $imagesToDelete = array_diff($imagesOnDisk, $imagesOnDb);
  81. if ($debug) {
  82. print_r($imagesToDelete);
  83. echo count($imagesOnDisk)." images on Disk\n";
  84. echo count($imagesOnDb)." images on DB\n";
  85. echo count($imagesToDelete)." images to delete\n";
  86. } else {
  87. foreach ($imagesToDelete as $x) {
  88. if ($dryrun) {
  89. echo 'rm '.$media . self::CATALOG_PRODUCT . $x.PHP_EOL;
  90. } else {
  91. @unlink($media . self::CATALOG_PRODUCT . $x);
  92. }
  93. }
  94. }
  95.  
  96. if ($debug) {
  97. echo "\r\n";
  98. }
  99.  
  100. }
  101.  
  102. public function usageHelp()
  103. {
  104. return <<<USAGE
  105. Usage: php -f delete_unused_images.php
  106. debug debug mode
  107. cache remove cache images too
  108. dry dryrun
  109.  
  110.  
  111. USAGE;
  112. }
  113. }
  114.  
  115. $shell = new Mage_Shell_CheckImages();
  116. $shell->run();
Add Comment
Please, Sign In to add comment