Import from cli/cronjob

Back in the days Vinai Kopp gave a talk about ImportExport module.

In the talk is a code snippet which helps to run importExport from CLI or as a cronjob. Unfortunately it is a screenshot and I typed the snippet already a couple of times. Time to make sure it is copy pastable:

<?php

require 'html/app/Mage.php';

Mage::app();

/** @var $import Mage_ImportExport_Model_Import */
$import = Mage::getModel('importexport/import');
$import->setEntity(Mage_Catalog_Model_Product::ENTITY);
$file = './import.csv';

if (!$file || !file_exists($file)) {
    echo 'File does not exist.';
    die();
}

$validationResult = $import->validateSource($file);

if ($import->getProcessedRowsCount() <= 0 || !$validationResult) {
    printf(
        'File %s contains %s corrupt records (from a total of %s)',
        $file,
        $import->getInvalidRowsCount(),
        $import->getProcessedRowsCount(),
        );
    foreach ($import->getErrors() as $type => $lines) {
        $lines = implode(', ', $lines);
        printf("\n:::: $type :::: \nIn Line(s) $lines \n");
    }
    die();
}

$import->importSource();
$import->invalidateIndex();