Skip to content

Instantly share code, notes, and snippets.

@vpietri
Last active June 19, 2020 16:09
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save vpietri/376e6fc27a2bece3449f to your computer and use it in GitHub Desktop.
Save vpietri/376e6fc27a2bece3449f to your computer and use it in GitHub Desktop.
Magento shell script to run dataflow from the command line
<?php
require_once 'abstract.php';
/**
* Magento Dataflow Shell Script
*
* This file should be save in the shell directory of Magento
* Basic usage samples:
* php dataflow.php
* php dataflow.php list
* php dataflow.php --exec XX
*
* @author Vincent Pietri <www.vincent-pietri.fr>
*/
class Mage_Shell_Dataflow extends Mage_Shell_Abstract
{
/**
* Parse string with indexers and return array of indexer instances
*
* @param string $string
* @return array
*/
protected function _parseIndexerString($string)
{
if (!empty($string)) {
$codes = explode(',', $string);
$codes = array_map('trim', $codes);
$processes = $this->_getIndexer()->getProcessesCollectionByCodes($codes);
foreach($processes as $key => $process) {
if ($process->getIndexer()->getVisibility() === false) {
unset($processes[$key]);
}
}
if ($this->_getIndexer()->hasErrors()) {
echo implode(PHP_EOL, $this->_getIndexer()->getErrors()), PHP_EOL;
}
}
return $processes;
}
/**
* Run script
*
* @see: http://phpmysqltalk.com/1718-magento-dataflow-exportimport-form-the-command-line.html
* @see: http://www.maximehuran.fr/lancer-plusieurs-dataflow-magento-en-sequence-dans-un-cron/
*/
public function run()
{
$_SESSION = array();
if ($this->getArg('list')) {
$profileCollection = Mage::getModel('dataflow/profile')->getCollection();
foreach($profileCollection as $profile) {
echo 'Id: ' .str_pad($profile->getId(), 3, ' ', STR_PAD_RIGHT) . ' "' . $profile->getName() . '"' . PHP_EOL;
}
} else if ($this->getArg('exec')) {
$dataflowIds = explode(',', $this->getArg('exec'));
$dataflowIds = array_map('trim', $dataflowIds);
try {
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
foreach ($dataflowIds as $profileId) {
/* @var $process Mage_Index_Model_Process */
try {
$startTime = microtime(true);
if (!is_numeric($profileId)) {
throw new Exception('Id should be an integer');
}
$profile->load($profileId);
if (!$profile->getId()) {
throw new Exception('Unable to load Id');
}
Mage::register('current_convert_profile', $profile);
$profile->run();
echo '-- Start profile "'.$profile->getName().'"' . PHP_EOL;
foreach ($profile->getExceptions() as $e) {
echo 'Info: '.$e->getMessage() . PHP_EOL;
}
$batchModel = Mage::getSingleton('dataflow/batch');
$countData = 0;
$countDataProcessed = 0;
if ($batchModel->getId()) {
if ($batchModel->getAdapter()) {
$batchImportModel = $batchModel->getBatchImportModel();
$importIds = $batchImportModel->getIdCollection();
$adapter = Mage::getModel($batchModel->getAdapter());
$adapter->setBatchParams($batchModel->getParams());
foreach ($importIds as $importId) {
$countData++;
$batchImportModel->load($importId);
if (!$batchImportModel->getId()) {
echo 'Skip undefined row.' . PHP_EOL;
continue;
}
try {
$importData = $batchImportModel->getBatchData();
$adapter->saveRow($importData);
} catch (Exception $e) {
echo 'Info: '.$e->getMessage() . PHP_EOL;
continue;
}
$countDataProcessed++;
}
}
}
if ($countData) {
echo 'Info: '.Mage::helper('dataflow')->__('Processed %s/%s records', $countDataProcessed, $countData) . PHP_EOL;
} else {
echo 'Info: No record processed' . PHP_EOL;
}
$resultTime = microtime(true) - $startTime;
echo '-- End profile "'.$profile->getName().'" with batchId ' . $batchModel->getId() . ' was executed successfully in ' . gmdate('H:i:s', $resultTime) . PHP_EOL;
$batchModel->delete();
$registryKey = '_singleton/dataflow/batch';
if (Mage::registry($registryKey)) {
Mage::unregister($registryKey);
}
} catch (Mage_Core_Exception $e) {
echo $e->getMessage() . PHP_EOL;
} catch (Exception $e) {
echo 'Error "' . $e->getMessage() . '" executing profile Id: '.$profileId . PHP_EOL;
}
}
} catch (Exception $e) {
echo $e->getMessage() . PHP_EOL;
}
} else {
echo $this->usageHelp();
}
}
protected function _processDataflow()
{
}
/**
* Retrieve Usage Help Message
*
*/
public function usageHelp()
{
return <<<USAGE
Usage: php -f datafow.php -- [options]
--exec <ids> Show Indexer(s) Status
list Show all dataflow
help This help
<ids> Comma separated dataflow id
USAGE;
}
}
$shell = new Mage_Shell_Dataflow();
$shell->run();
@sirtordo
Copy link

sirtordo commented Oct 4, 2017

Great!

@alexmorco
Copy link

While configuring php shell script, We need to take care of these commands, Abstract.php, Compiler.php, Indexer.php, Log.php, You can check more details about these commands at, https://www.cloudways.com/blog/php-shell-scripts-magento/. Hope it will help your readers as well as I got help from your and this post.

@antiseptikk
Copy link

$batchModel->delete();
$registryKey = '_singleton/dataflow/batch';
if (Mage::registry($registryKey)) {
      Mage::unregister($registryKey);
}

saved my life/time ! Many thanks !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment