/**
     * @param SQLIImportHandlerOptions $options
     */
    public function __construct( SQLIImportHandlerOptions $options = null )
    {
        parent::__construct( $options );

        $ftpFolder       = 'oscar1';
        $modeUAT         = false;
        $this->startDate = microtime( true );

        if ( $this->importINI->hasVariable( 'XMLImportSettings', 'ModeUAT' )
             && $this->importINI->variable( 'XMLImportSettings', 'ModeUAT' ) == 'enabled' )
        {
            $modeUAT = true;
        }

        if ( is_object($this->options) )
        {
            if ( $this->options->hasAttribute( 'reportLevel' ) && is_numeric($reportLevel = $this->options->attribute( 'reportLevel' )) )
                XMLImportMonitor::setReportLevel( $reportLevel );

            if ( $this->options->hasAttribute( 'ftpFolder' ) && in_array($this->options->attribute( 'ftpFolder' ),array('oscar1','oscar2') ) )
                $ftpFolder = $this->options->attribute( 'ftpFolder' );
        }

        $this->rootImportFolder = $ftpFolder;

        // Initialize sqliImportItem Statistics and logs
        XMLImportDB::init( $this->importINI );
        XMLImportMapping::init( $this->importINI );
        eZLog::setMaxLogSize( 2000000 );
        eZLog::setLogrotateFiles( 20 );
        XMLImportMonitor::setOrigin($ftpFolder);
        XMLImportMonitor::setSQLiImportItem( $this->getCurrentSQLiImportItem() );
        XMLImportMonitor::pushStatistic( 'start_date', time() );

        $this->fileHandler = XMLImportFileHandler::instance($this->importINI, $ftpFolder, $modeUAT);
        XMLImportDataHandler::setIni ( $this->importINI );
        XMLImportDataHandler::setFileHandler ( $this->fileHandler );
    }
if ( !isset($cli) )
    die('This is a eZexec script, please use php bin/php/ezexec.php -s site_admin ' . preg_replace('#^.*/extension/#', 'extension/', __FILE__) . "\n" );

$helpMessage = <<<EOD

Usage: php bin/php/ezexec -s site_admin extension/ezoscar/bin/php/temp_and_archive/align_staging_from_prod_step2.php

/!\ Stop the imports before running this script!!!!

Run this script AFTER align_staging_from_prod_step1.php

EOD;

/* @var $db eZDBInterface */
$db = eZDB::instance();
XMLImportDB::init(eZINI::instance('sqliimport.ini'));

$siteINI = eZINI::instance('site.ini');
$dbName = $siteINI->variable( 'DatabaseSettings', 'Database' );

$tableListPF = array( 'mm_publisher_folder_translation',
                      'mm_application_has_publisher_folder',
                      'mm_newsletter_has_publisherfolders' );
$tableListTX = array( 'mm_taxonomy_translation',
                      'mm_cluster_has_taxonomy' );
$tableListTXSpe = array( 'mm_speciality_mapping' );

// ------------------------- FINALIZE UPDATE -------------------

$queries = array();
$contentQueries = array();
    if ( isset($options['mode']) && in_array($options['mode'], array('full','csv')) )
    {
        $mode = $options['mode'];
    }

    $toFile = isset($options['toFile']);
}

$reportWarning  = ( $reportLevel == 'all' || $reportLevel == 'warning');
$reportError    = ( $reportLevel == 'all' || $reportLevel == 'error');


$importINI = eZINI::instance('sqliimport.ini');
$db = MMDB::instance();

XMLImportDB::init($importINI);

if ( $publisherID == 'all' )
{
    $pQuery = 'SELECT id,path from mm_publisher_folder';
}
else
{
    $pQuery = "SELECT id,path from mm_publisher_folder where id in ($publisherID)";
}

$results = $db->arrayQuery($pQuery);

if ( !$results )
{
    $script->shutdown( 1 , 'not a valid publisher ID' );
    public static function run()
    {
        global $cli, $script;
        
        XMLImportDB::init(eZINI::instance('sqliimport.ini'));
        self::$dbEz = eZDB::instance();
        
        self::$taxonomyCategoryIdentifiers = array();
        self::$taxonomyCategoryMapping     = array();
        self::$taxonomyById                = array();
        self::$publisherFolders            = array();
        foreach( self::$dbEz->arrayQuery("SELECT
                                            t.id,
                                            t.code,
                                            ct.attribute_identifier
                                        FROM
                                            mm_taxonomy t,
                                            mm_taxonomy_category ct
                                        WHERE
                                            t.taxonomy_category_id = ct.id AND
                                            ct.attribute_identifier != 'conditions'") as $row
        ){
            $taxonomyCategoryIdentifier = $row['attribute_identifier'];
            $taxonomyCode               = $row['code'];
            self::$taxonomyById[$row['id'].''] = $taxonomyCode;
        
            $mapping = array_flip( self::$taxonomyCategoryIdentifiers );
        
            $taxonomyCategoryIdentifierKey = null;
            if( !isset($mapping[$taxonomyCategoryIdentifier]) )
            {
                self::$taxonomyCategoryIdentifiers[] = $taxonomyCategoryIdentifier;
                $taxonomyCategoryIdentifierKey = count(self::$taxonomyCategoryIdentifiers) - 1;
            }
            else
            {
                $taxonomyCategoryIdentifierKey = $mapping[$taxonomyCategoryIdentifier];
            }
            self::$taxonomyCategoryMapping[$taxonomyCode] = $taxonomyCategoryIdentifierKey;
        }
        
        foreach( self::$dbEz->arrayQuery("SELECT     id, path
                                    FROM       mm_publisher_folder") as $row )
            self::$publisherFolders[$row['id']] = $row['path'];
        
        
        $offset = 0;
        $step   = 500;
        
        $continue = true;
        
        $count = 0;
        foreach( XMLImportDB::arrayQuery( "SELECT count(*) AS c FROM content" ) as $row )
            $count = $row['c'];
        
        $cli->output( 'Updating taxonomies for content_ids - '.$count.' objects found' );

        do
        {
            $sql    = sprintf( "SELECT   c.id,
                                         GROUP_CONCAT(a.language SEPARATOR '|') as lang,
                                         c.publisher_folder_id
                                FROM     content c,
                                         article a
                                WHERE    c.id = a.content_id
                                AND      c.id BETWEEN %d AND %d
                                GROUP BY c.id
                                ", $offset, ( $offset + $step - 1 ) );
            self::$publishersAndLanguages = array();
            foreach( XMLImportDB::arrayQuery($sql) as $row )
                self::$publishersAndLanguages[$row['id']] = array( 'lang' => explode('|', $row['lang']), 'publisher' => $row['publisher_folder_id'] );
            
            $sql    = sprintf( "SELECT   ct.content_id,
                                         ct.taxonomy_id
                                FROM     content_has_taxonomy ct
                                WHERE    ct.content_id BETWEEN %d AND %d
                                ORDER BY ct.content_id
                                ", $offset, ( $offset + $step - 1 ) );
            $cli->output( str_pad( number_format( ($offset + $step ) / $count * 100, 2 ), 6, ' ', STR_PAD_LEFT )."%  ".str_pad( $offset, 10, ' ', STR_PAD_LEFT).' > '.str_pad( $offset + $step - 1, 10, ' ', STR_PAD_LEFT ) );
            self::$dbEz->begin();
        
            $rows = XMLImportDB::arrayQuery( $sql );
            $continue = count($rows);
            
            $objectId = null;
            $taxonomies = array();
            foreach( $rows as $row )
            {
                if( !is_null($objectId) && $objectId != $row['content_id'] )
                {
                    self::updateObject( $objectId, $taxonomies );
                    
                    $objectId = $row['content_id'];
                    $taxonomies = array( $row['taxonomy_id'] );
                }
                else
                {
                    $objectId = $row['content_id'];
                    $taxonomies[] = $row['taxonomy_id'];
                }
            }
            if( $objectId )
                self::updateObject( $objectId, $taxonomies );
            $offset += $step;
        
            self::$dbEz->commit();
        }
        while($continue);
    }