Last active
November 7, 2016 06:19
-
-
Save unicoder88/6b6d3bede68330948eb493f96ddca936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* @author Convert Team | |
* @copyright Copyright (c) 2016 Convert (http://www.convert.no/) | |
*/ | |
/** | |
* Runs Dataflow profile, works for export and import | |
* | |
* Usage: php run_dataflow_profile.php [profile_id] [import_filename] | |
* | |
* profile_id defaults to 1 - export all products profile | |
* import_filename required only when importing, relative to var/import/ | |
*/ | |
require __DIR__ . '/app/Mage.php'; | |
$profileId = $argc > 1 ? $argv[1] : 1; | |
$importFilename = $argc > 2 ? $argv[2] : null; | |
// disable flat products collection | |
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); | |
Mage::app()->getLayout()->setArea('adminhtml'); | |
// mark entity_id attribute not system to include in report | |
$attributes = Mage::getConfig()->getNode('admin/fieldsets/catalog_product_dataflow'); | |
$attributes->entity_id->system = 0; | |
// fix code that wants getAction()->getFullActionName(). Yes Mageworx_CustomOptions, I'm looking at you | |
Mage::app()->getFrontController()->setAction(new Varien_Object()); | |
/** @var Mage_Dataflow_Model_Profile $profile */ | |
$profile = Mage::getModel('dataflow/profile'); | |
$profile->load($profileId); | |
if (!$profile->getId()) { | |
print "Profile $profileId not found.\n"; | |
return 1; | |
} | |
Mage::register('current_convert_profile', $profile); | |
/** @var Mage_Adminhtml_Block_System_Convert_Profile_Run $block */ | |
$block = Mage::app()->getLayout()->createBlock('adminhtml/system_convert_profile_run'); | |
if ($importFilename) { | |
Mage::app()->getRequest()->setParam('files', $importFilename); | |
} | |
print "Running profile \"{$profile->getName()}\"...\n"; | |
$profile->run(); | |
$batchModel = $block->getBatchModel(); | |
$hasAdapter = $block->getBatchModelHasAdapter(); | |
// import - no chunks, all at once, pray for memory limit :) | |
if ($batchModel->getId() && $hasAdapter) { | |
$batchImportModel = $batchModel->getBatchImportModel(); | |
$adapter = Mage::getModel($batchModel->getAdapter()); | |
$adapter->setBatchParams($batchModel->getParams()); | |
$rowIds = $batchImportModel->getIdCollection(); | |
print "Found " . count($rowIds) . " rows. Importing...\n"; | |
$saved = 0; | |
$errors = 0; | |
foreach ($rowIds as $importId) { | |
$batchImportModel->load($importId); | |
try { | |
$importData = $batchImportModel->getBatchData(); | |
$adapter->saveRow($importData); | |
print '.'; | |
} catch (Exception $e) { | |
fwrite(STDERR, "\nsaveRow error, import #$importId: {$e->getMessage()}\n"); | |
$errors++; | |
continue; | |
} | |
$saved++; | |
} | |
if (method_exists($adapter, 'getEventPrefix')) { | |
print "dispatch before finish event\n"; | |
Mage::dispatchEvent($adapter->getEventPrefix() . '_finish_before', array('adapter' => $adapter)); | |
$adapter->clearAffectedEntityIds(); | |
} | |
print "before finish\n"; | |
try { | |
$batchModel->beforeFinish(); | |
} catch (Exception $e) { | |
fwrite(STDERR, "beforeFinish error: {$e->getMessage()}\n"); | |
} | |
print "deleting batch\n"; | |
$batchModel->delete(); | |
print "Saved $saved, errors $errors.\n"; | |
} | |
print 'Done, memory ' . number_format(memory_get_peak_usage(true)) . " bytes.\n"; | |
return 0; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment