If you look at the source for this module controller controller, you can see the code that they use to perform bulk removal
#File: app/code/local/Mage/Imaclean/controllers/Adminhtml/ImacleanController.php public function massDeleteAction() { $imacleanIds = $this->getRequest()->getParam('imaclean'); if(!is_array($imacleanIds)) { Mage::getSingleton('adminhtml/session')->addError(Mage::helper('adminhtml')->__('Please select item(s)')); } else { try { $model = Mage::getModel('imaclean/imaclean'); foreach ($imacleanIds as $imacleanId) { $model->load($imacleanId); unlink('media/catalog/product'. $model->getFilename()); $model->setId($imacleanId)->delete(); } Mage::getSingleton('adminhtml/session')->addSuccess( Mage::helper('adminhtml')->__( 'Total of %d record(s) were successfully deleted', count($imacleanIds) ) ); } catch (Exception $e) { Mage::getSingleton('adminhtml/session')->addError($e->getMessage()); } } $this->_redirect('*/*/index'); }
So, this controller action accepts many identifiers of the imaclean / imaclean model, uses these identifiers to perform the deletion. So, the key code in this action
$imacleanIds = $this->getRequest()->getParam('imaclean'); $model = Mage::getModel('imaclean/imaclean'); foreach ($imacleanIds as $imacleanId) { $model->load($imacleanId); unlink('media/catalog/product'. $model->getFilename()); $model->setId($imacleanId)->delete(); }
So you can reproduce the above code in standalone version with something like
//itterates through all 'imaclean/imaclean' models in the database $models = Mage::getModel('imaclean/imaclean')->getCollection(); foreach ($models as $model) { unlink('media/catalog/product'. $model->getFilename()); $model->setId($model->getId())->delete(); }
Finally, it appears that the imaclean / imaclean models are used to track which images are no longer needed. It looks like the module creates these (i.e., launches validation for unused images) in newAction using the compareList helper compareList method.
public function newAction(){ Mage::helper('imaclean')->compareList(); $this->_redirect('*/*/'); }
So, we can add this to the beginning of our script, as well as the de facto initialization of Magento, which should give us what we need.
#File: cleanup.php require_once "app/Mage.php"; $app = Mage::app("default"); Mage::helper('imaclean')->compareList(); $models = Mage::getModel('imaclean/imaclean')->getCollection(); foreach ($models as $model) { unlink('media/catalog/product'. $model->getFilename()); $model->setId($model->getId())->delete(); }
This should at least begin. Good luck
Alan storm
source share