×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: bejoy balan
Added: Sep 24, 2014 7:49 AM
Modified: Sep 24, 2014 7:51 AM
Views: 2119
  1. The code consists of two files. mag_login.php and mag_import.php. Both are needed. Place them in /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/
  2.  
  3. mag_import.php:
  4.  
  5. <?php
  6.  
  7. /**
  8.  * Path to the root of your magento installation
  9.  */
  10. $root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
  11.  
  12. /**
  13.  * Url to your magento installation.
  14.  */
  15. $url = 'http://www.yourmagentostore.com/';
  16.  
  17. /**
  18.  * relative path from the magento root to the login file.
  19.  */
  20. $login = 'shell/mag_login.php';
  21.  
  22. /**
  23.  * name of the logfile, will be places in magentoroot/var/log/
  24.  */
  25. $logFileName= 'import.log';
  26.  
  27. /**
  28.  * how many products will be parsed at each post. Usually 10-50.
  29.  */
  30. $atOnce = 50;
  31.  
  32.  
  33.  
  34. /**
  35.  * DO NOT EDIT BELOW THIS LINE
  36.  */
  37. function convert ($size)
  38. {
  39.         $unit=array('b','kb','mb','gb','tb','pb');
  40.         return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
  41. }
  42.  
  43. ini_set('memory_limit', '128M');
  44.  
  45. $profileId = $argv[1];
  46. if (! isset($profileId)) {
  47.         exit ("\nPlease specify a profile id. You can find it in the admin panel->Import/Export->Profiles.\nUsage: \n\t\t  php -f $argv[0] PROFILE_ID\n\t example: php -f $argv[0] 7\n");
  48. }
  49. $recordCount = 0;
  50.  
  51.  
  52. //getting Magento
  53. require_once $root.'app/Mage.php';
  54. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  55.  
  56. //starting the import
  57. Mage::log("\n\n", null, $logFileName);
  58. Mage::log(convert(memory_get_usage()) . " - " . "STARTING IMPORT", null, $logFileName);
  59.  
  60. $profile = Mage::getModel('dataflow/profile');
  61. $userModel = Mage::getModel('admin/user');
  62. $userModel->setUserId(0);
  63. Mage::getSingleton('admin/session')->setUser($userModel);
  64. if ($profileId) {
  65.    $profile->load($profileId);
  66.    if (!$profile->getId()) {
  67.       Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
  68.    }
  69. }
  70.  
  71. /**
  72.  * get het login information.
  73.  */
  74. exec("php -f {$root}{$login}", $result);
  75.  
  76. $loginInformation = json_decode($result[0]);
  77.  
  78. $sessionId = $loginInformation->sessionId;
  79. $formKey =  $loginInformation->formKey;
  80.  
  81. //clean dataflow_batch_import table so it doesn't get amazingly big.
  82. $db = Mage::getSingleton('core/resource')->getConnection('core_write');
  83. $db->query("TRUNCATE TABLE `dataflow_batch_import`");
  84. Mage::log(convert(memory_get_usage()) . " - " . "Table dataflow_batch_import cleaned", null, $logFileName);
  85.  
  86. //load profile
  87. if ($profileId) {
  88.         $profile->load($profileId);
  89.         if (!$profile->getId()) {
  90.                 Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
  91.         }
  92. }
  93. Mage::register('current_convert_profile', $profile);
  94.  
  95. //run the profile
  96. Mage::log(convert(memory_get_usage()) . " - " . "Preparing profile...", null, $logFileName);
  97. $profile->run();
  98. Mage::log(convert(memory_get_usage()) . " - " . "...Done", null, $logFileName);
  99.  
  100. //get to work
  101. $batchModel = Mage::getSingleton('dataflow/batch');
  102. if ($batchModel->getId()) {
  103.         #echo "getId ok\n";
  104.         if ($batchModel->getAdapter()) {
  105.                 #echo "getAdapter ok\n";
  106.                 $batchId = $batchModel->getId();
  107.                 Mage::log(convert(memory_get_usage()) . " - " . "Loaded batch id $batchId", null, $logFileName);
  108.                
  109.                 $batchImportModel = $batchModel->getBatchImportModel();
  110.                 $importIds = $batchImportModel->getIdCollection();
  111.                 $batchModel = Mage::getModel('dataflow/batch')->load($batchId);
  112.                 $adapter = Mage::getModel($batchModel->getAdapter());
  113.                 $postdata = array();
  114.                 $postnum = 0;
  115.                 $totalproducts = count($importIds);
  116.                
  117.                 Mage::log(convert(memory_get_usage()) . " - 0/{$totalproducts}", null, $logFileName);
  118.  
  119.                 foreach ($importIds as $importId) {
  120.                         #echo "importing $importId\n";
  121.                         $recordCount++;
  122.                         $postdata[] = "rows[]=$importId";
  123.                        
  124.                         //echo "$importId ";
  125.                        
  126.                         if ($recordCount%$atOnce == 0 || $recordCount == $totalproducts) {
  127.                                 $postnum++;
  128.                                 $postdata[] = "batch_id=$batchId";
  129.                                 $postdata[] = "form_key=$formKey";
  130.                                 $postdatastring = implode('&', $postdata);
  131.                                 $postdata = array();
  132.                                
  133.                                 //print_r($postdatastring);
  134.                                 //Mage::log(convert(memory_get_usage()) . " - Start cURL request #$postnum", null, $logFileName);
  135.                                
  136.                                 $ch = curl_init();
  137.                                
  138.                                 curl_setopt($ch, CURLOPT_URL,$url."index.php/admin/system_convert_profile/batchRun/?isAjax=true");
  139.                                 curl_setopt($ch, CURLOPT_TIMEOUT, 100 );
  140.                                 curl_setopt($ch, CURLOPT_COOKIE, "adminhtml=$sessionId" );
  141.                                 curl_setopt($ch, CURLOPT_POST, 1);
  142.                                 curl_setopt($ch, CURLOPT_POSTFIELDS, $postdatastring);
  143.                                 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  144.                                
  145.                                 $buffer = curl_exec($ch);
  146.  
  147.                                 if (empty($buffer))
  148.                                 {
  149.                                     Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} - Response is empty - ERROR" . curl_error($ch), null, $logFileName);
  150.                                 }
  151.                                 else
  152.                                 {                                              
  153.                                         $result = json_decode($buffer);
  154.                                        
  155.                                         Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} [$buffer]" , null, $logFileName);
  156.                                         if (count($result->errors))
  157.                                         {
  158.                                                 foreach ($result->errors as $error)
  159.                                                 {
  160.                                                         Mage::log(convert(memory_get_usage()) . " - ERROR: $error", null, $logFileName);
  161.                                                 }
  162.                                         }
  163.                                 }
  164.                                
  165.                                 curl_close ($ch);
  166.                         }
  167.  
  168.                 }
  169.                 foreach ($profile->getExceptions() as $e) {
  170.                         Mage::log(convert(memory_get_usage()) . " - " . $e->getMessage(), null, $logFileName);
  171.                 }
  172.         }
  173. }
  174. Mage::log(convert(memory_get_usage()) . " - " . "Completed!", null, $logFileName);
  175. mag_login.php:
  176.  
  177. <?php
  178.  
  179. /**
  180.  * Path to the root of your magento installation.
  181.  * include traing slash.
  182.  */
  183. $root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
  184.  
  185. /**
  186.  * Username that has the rights to import products.
  187.  */
  188. $username = 'USERNAME';
  189.  
  190. /**
  191.  * Password
  192.  */
  193. $password = 'PASSWORD';
  194.  
  195.  
  196.  
  197. /**
  198.  * DO NOT EDIT BELOW THIS LINE
  199.  */
  200. //getting Magento
  201. require_once $root.'app/Mage.php';
  202. Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
  203.  
  204. //starting the import
  205.  
  206. Mage::getSingleton("admin/session", array("name"=>"adminhtml"));
  207. $session = Mage::getSingleton("admin/session");
  208.  
  209. try
  210. {
  211.         $session->login($username, $password);
  212. } catch(Exception $e) {
  213.         echo 'Message: ' .$e->getMessage();
  214. }
  215.  
  216. if(! $session->isLoggedIn())
  217. {
  218.                 Mage::log(convert(memory_get_usage()) . " - " . "Could not log in with '$username'", null, $logFileName);
  219.                 exit;
  220. }
  221.        
  222. $sessionId = $session->getEncryptedSessionId();
  223. $formKey =  Mage::getSingleton('core/session')->getFormKey();
  224.  
  225. echo json_encode(array('sessionId' => $sessionId, 'formKey' => $formKey));
  226.  
  227. Copy the above code to your local Magento installation, put them in a secure folder, that isn't accesible by a browser and login to the shell. Fill in the correct information into the files. After that is done, start up your shell, browse to the folder. Run the file with:
  228.  
  229. 1
  230. php -f mag_import.php 7
  231. In the line above, 7 should be replaced by your own profile Id, which can be found the first column of the profiles.
  232.  
  233. Making the Magento product import fully automatic.
  234.  
  235. If we got the above working, we need to get the working fully automatic. We want to import our products and do a complete reindex of magento every night. First we create a file that can be executed by the shell that imports the products and reindexes it. Call it something like import.sh (sh from shell):
  236.  
  237. 1
  238. 2
  239. 3
  240. 4
  241. 5
  242. echo mag_import.php 7
  243. php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/mag_product_import.php 7
  244.  
  245. echo indexer.php reindexall
  246. php /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/indexer.php reindexall
  247. Try and run the file with the following:
  248.  
  249. 1
  250. bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/shell_dayly.sh
  251. If that all works properly, we can setup our cronjob to do it automatically every night. Enter the following into you shell:
  252.  
  253. 1
  254. crontab -e
  255. If you already have setup your Magento cron, you should see something like:
  256.  
  257. 1
  258. */5 * * * * php -f /PATH/TO/YOUR/MAGENTOINSTALLATION/cron.php
  259. Now, if something happens to our import, somethings goes wrong, etc, we wan't to get notified. Add the following line at the top of the crontab file.
  260.  
  261. 1
  262. MAILTO=info@youremail.com
  263. Below the Magento line we add our import cron:
  264.  
  265. 1
  266. 0 0 * * * bash /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/import.sh
  267. Now save the crontab (depends on the editor how) and your all set.
  268.  
  269. Note: if you want to see error regarding the import, lines that could not be processed open /PATH/TO/YOUR/MAGENTOINSTALLATION/var/log/import.log or browse to the file in the shell and type:
  270.  
  271. 1
  272. tail -f import.log

1 comment

bejoy balan 8 years ago
The code consists of two files. mag_login.php and mag_import.php. Both are needed. Place them in /PATH/TO/YOUR/MAGENTOINSTALLATION/shell/

mag_import.php:

<?php
 
/**
* Path to the root of your magento installation
*/
$root = '/PATH/TO/YOUR/MAGENTOINSTALLATION/';
 
/**
* Url to your magento installation.
*/
$url = 'www.yourmagentostore.com/';
 
/**
* relative path from the magento root to the login file.
*/
$login = 'shell/mag_login.php';
 
/**
* name of the logfile, will be places in magentoroot/var/log/
*/
$logFileName= 'import.log';
 
/**
* how many products will be parsed at each post. Usually 10-50.
*/
$atOnce = 50;
 
 
 
/**
* DO NOT EDIT BELOW THIS LINE
*/
function convert ($size)
{
$unit=array('b','kb','mb','gb','tb','pb');
return @round($size/pow(1024,($i=floor(log($size,1024)))),2).' '.$unit[$i];
}
 
set_time_limit(0);
ini_set('memory_limit', '128M');
 
$profileId = $argv[1];
if (! isset($profileId)) {
exit ("\nPlease specify a profile id. You can find it in the admin panel->Import/Export->Profiles.\nUsage: \n\t\t php -f $argv[0] PROFILE_ID\n\t example: php -f $argv[0] 7\n");
}
$recordCount = 0;
 
 
//getting Magento
require_once $root.'app/Mage.php';
ob_implicit_flush();
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
 
//starting the import
Mage::log("\n\n", null, $logFileName);
Mage::log(convert(memory_get_usage()) . " - " . "STARTING IMPORT", null, $logFileName);
 
$profile = Mage::getModel('dataflow/profile');
$userModel = Mage::getModel('admin/user');
$userModel->setUserId(0);
Mage::getSingleton('admin/session')->setUser($userModel);
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
 
/**
* get het login information.
*/
exec("php -f {$root}{$login}", $result);
 
$loginInformation = json_decode($result[0]);
 
$sessionId = $loginInformation->sessionId;
$formKey = $loginInformation->formKey;
 
//clean dataflow_batch_import table so it doesn't get amazingly big.
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$db->query("TRUNCATE TABLE `dataflow_batch_import`");
Mage::log(convert(memory_get_usage()) . " - " . "Table dataflow_batch_import cleaned", null, $logFileName);
 
//load profile
if ($profileId) {
$profile->load($profileId);
if (!$profile->getId()) {
Mage::getSingleton('adminhtml/session')->addError('ERROR: Could not load profile');
}
}
Mage::register('current_convert_profile', $profile);
 
//run the profile
Mage::log(convert(memory_get_usage()) . " - " . "Preparing profile...", null, $logFileName);
$profile->run();
Mage::log(convert(memory_get_usage()) . " - " . "...Done", null, $logFileName);
 
//get to work
$batchModel = Mage::getSingleton('dataflow/batch');
if ($batchModel->getId()) {
#echo "getId ok\n";
if ($batchModel->getAdapter()) {
#echo "getAdapter ok\n";
$batchId = $batchModel->getId();
Mage::log(convert(memory_get_usage()) . " - " . "Loaded batch id $batchId", null, $logFileName);

$batchImportModel = $batchModel->getBatchImportModel();
$importIds = $batchImportModel->getIdCollection();
$batchModel = Mage::getModel('dataflow/batch')->load($batchId);
$adapter = Mage::getModel($batchModel->getAdapter());
$postdata = array();
$postnum = 0;
$totalproducts = count($importIds);

Mage::log(convert(memory_get_usage()) . " - 0/{$totalproducts}", null, $logFileName);
 
foreach ($importIds as $importId) {
#echo "importing $importId\n";
$recordCount++;
$postdata[] = "rows[]=$importId";

//echo "$importId ";

if ($recordCount%$atOnce == 0 || $recordCount == $totalproducts) {
$postnum++;
$postdata[] = "batch_id=$batchId";
$postdata[] = "form_key=$formKey";
$postdatastring = implode('&', $postdata);
$postdata = array();

//print_r($postdatastring);
//Mage::log(convert(memory_get_usage()) . " - Start cURL request #$postnum", null, $logFileName);

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL,$url."index.php/admin/system_convert_profile/batchRun/?isAjax=true");
curl_setopt($ch, CURLOPT_TIMEOUT, 100 );
curl_setopt($ch, CURLOPT_COOKIE, "adminhtml=$sessionId" );
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdatastring);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$buffer = curl_exec($ch);
 
if (empty($buffer))
{
Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} - Response is empty - ERROR" . curl_error($ch), null, $logFileName);
}
else
{
$result = json_decode($buffer);

Mage::log(convert(memory_get_usage()) . " - {$recordCount}/{$totalproducts} [$buffer]" , null, $logFileName);
if (count($result->errors))
{
foreach ($result->errors as $error)
{
Mage::log(convert(memory_get_usage()) . " - ERROR: $error", null, $logFileName);
}
}
}

curl_close ($ch);
}
 
}
foreach ($profile->getExceptions() as $e) {
Mage::log(convert(memory_get_usage()) . " - " . $e->getMessage(), null, $logFileName);
}
}
}
Mage::log(convert(memory_get_usage()) . " - " . "Completed!", null, $logFileName);

Write a comment