if ( $results )
        {
            echo $index . ' : ' . count($results)."\n";
        }
        else
        {
            echo "No results\n";
        }
    }
}
else
{
    foreach ( $deletes as $index => $delete )
    {
        $resSelect = XMLImportDB::arrayQuery($selects[$index]);

        if ( $resSelect )
        {
            $temp = array();

            foreach ( $resSelect as $result )
            {
                $temp[] = $result['result'];
            }

            $concat  = '(' . implode(',', $temp) . ');';
            $deleteQ = $delete . $concat;

            echo "$deleteQ\n";
        }
$messageReport = "";
$csvMessages = array();

foreach ($results as $publisherInfo)
{
    if ( !isset($since) )
    {
        $query = "SELECT date,version,xml_file,error_messages,warning_messages from import_status where publisher_folder_id={$publisherInfo['id']} order by xml_file ASC, version DESC";
    }
    else
    {
        $sinceTimestamp = time() - 60 * 60 * 24;
        $query = "SELECT date,version,xml_file,error_messages,warning_messages from import_status where publisher_folder_id={$publisherInfo['id']} and date >= $sinceTimestamp order by xml_file ASC, version DESC";
    }

    $results = XMLImportDB::arrayQuery($query);

    if ( !$results )
    {
        continue;
    }

    $currentXML      = "";
    $warningMessages = array();
    $errorMessages   = array();
    $allMessages     = array();
    $publisherName   = $publisherInfo['path'];
    $publisherCount  = 0;
    $warningCount    = 0;
    $errorCount      = 0;
    /**
     * @param string $type
     * @param array$fieldArray
     * @return bool|array
     */
    private static function existsDB( $type, $fieldArray )
    {

        $type = self::getAdjustedType( $fieldArray, $type );

        if ( empty( $type ) || empty( $fieldArray ) )
            return false;

        switch ( $type )
        {
            case 'article':
                $language          = $fieldArray['language']->calculatedValue;
                $providerID        = $fieldArray['publisher_internal_id']->calculatedValue;
                $publisherFolderID = $fieldArray['publisher']->calculatedValue;

                if ( !empty( $language ) && !empty( $providerID ) && !empty( $publisherFolderID ) )
                {
                    $sql = sprintf( "SELECT c.id as content_id, a.id as id, a.language as language
                            FROM content as c, article as a
                            WHERE a.content_id = c.id
                                AND a.language = '%s'
                                AND a.publisher_internal_id = '%s'
                                AND c.publisher_folder_id = %s",
                        $language,
                        $providerID,
                        $publisherFolderID
                    );
                    $altSql = sprintf( "SELECT c.id as content_id
                            FROM content as c, article as a
                            WHERE a.content_id = c.id
                                AND a.publisher_internal_id = '%s'
                                AND c.publisher_folder_id = %s",
                        $providerID,
                        $publisherFolderID
                    );
                }
            break;
            case 'audio':
            case 'image':
            case 'file':
            case 'pdf':
                $tablename         = $type;
                $filename          = preg_replace('#//+#', '/', $fieldArray['file']->calculatedValue);
                $wrongFilename     = preg_replace('#^(.*)?/(images|pdf)/(.*)$#', '$1/$2//$3', $filename); // <- dirty hot fix to match on wrong file paths set by ptools
                $publisherFolderID = $fieldArray['publisher']->calculatedValue;

                if ( !empty($filename) && !empty($publisherFolderID) )
                {
                    $sql = sprintf( "SELECT c.id as content_id, t.id as id
                            FROM content as c, %s as t
                            WHERE t.content_id = c.id
                                AND ( t.file = '%s' OR t.file = '%s' )
                                AND c.publisher_folder_id = %s",
                        $tablename,
                        $filename,
                        $wrongFilename,
                        $publisherFolderID
                    );
                }
            break;
            case 'video':
                $tablename         = $type;
                $filename          = isset( $fieldArray['file'] ) ? $fieldArray['file']->calculatedValue : null;
                $publisherFolderID = isset( $fieldArray['publisher']) ? $fieldArray['publisher']->calculatedValue : null;
                $brightcoveId      = isset( $fieldArray['brightcoveid'] ) ? $fieldArray['brightcoveid']->calculatedValue : null;

                if ( ( !empty($filename) || !empty($brightcoveId) ) && !empty($publisherFolderID) )
                {
                    $sql = sprintf( "SELECT c.id as content_id, t.id as id
                            FROM content as c, %s as t
                            WHERE t.content_id = c.id
                                AND t.file = '%s'
                                AND t.file <> ''
                                AND t.file IS NOT NULL
                                AND c.publisher_folder_id = %s",
                        $tablename,
                        $filename,
                        $publisherFolderID
                    );
                    $altSql = sprintf( "SELECT c.id as content_id, t.id as id
                            FROM content as c, %s as t
                            WHERE t.content_id = c.id
                                AND (t.file = '' OR t.file IS NULL)
                                AND t.brightcoveid = '%s'
                                AND t.brightcoveid <> ''
                                AND t.brightcoveid IS NOT NULL
                                AND c.publisher_folder_id = %s",
                        $tablename,
                        $brightcoveId,
                        $publisherFolderID
                    );
                }
            break;
            case 'link':
                $tablename         = $type;
                $url               = $fieldArray['url']->calculatedValue;
                $publisherFolderID = $fieldArray['publisher']->calculatedValue;

                if ( !empty( $url ) && !empty( $publisherFolderID ) )
                {
                    $sql = sprintf( "SELECT c.id as content_id, t.id as id
                            FROM content as c, %s as t
                            WHERE t.content_id = c.id
                                AND t.url = '%s'
                                AND c.publisher_folder_id = %s",
                        $tablename,
                        $url,
                        $publisherFolderID
                    );
                }
            break;
            case 'html_media':
                $tablename         = $type;
                $headline          = $fieldArray['headline']->calculatedValue;
                $publisherFolderID = $fieldArray['publisher']->calculatedValue;

                if ( !empty( $headline ) && !empty( $publisherFolderID ) )
                {
                    $sql = sprintf( "SELECT c.id as content_id, t.id as id
                            FROM content as c, %s as t
                            WHERE t.content_id = c.id
                                AND t.headline = '%s'
                                AND c.publisher_folder_id = %s",
                        $tablename,
                        $headline,
                        $publisherFolderID
                    );
                }
                break;
            case 'contact':
                // Not in MM 1.6 scope
            default:
            break;
        }

        if ( $sql )
        {
            $result = XMLImportDB::arrayQuery( $sql );

            if ( empty( $result ) && isset( $altSql ) )
                $result = XMLImportDB::arrayQuery( $altSql );

            if ( !empty($result) )
                return $result[0];
        }

        return false;
    }
{
    $filePart = explode('/', __FILE__);
    $i = 0;
    while ($filePart[$i] != 'extension' )
    {
        unset ($filePart[$i]);
        $i++;
    }
    die('This is a eZexec script, please use php bin/php/ezexec.php -s site_admin ' . implode('/',$filePart) . "\n" );
}

XMLImportDB::init(eZINI::instance('sqliimport.ini'));
$dbEz         = eZDB::instance();
$queryModel   = "select id, path from publisher_folder";
$queryEz      = "select id, path from mm_publisher_folder";
$resultsModel = XMLImportDB::arrayQuery($queryModel);
$resultsEz    = $dbEz->arrayQuery($queryEz);
$mapping      = array();
$stop         = false;

foreach ( $resultsModel as $resultModel )
{
    $mapping[$resultModel['path']] = array( 'modelId' => $resultModel['id'] );
}

foreach ( $resultsEz as $resultEz )
{
    if ( !isset( $mapping[$resultEz['path']] ))
    {
        $mapping[$resultEz['path']] = array();
    }
    /**
     * Filters files toward retries
     * 
     * @param array $list
     * @return array
     */
    public function filterRetries ( $list )
    {
        foreach (array_keys($list) as $key)
        {
            $file              = $list[$key];
            $fileSplit         = explode('_', $file['file']);
            $fileSQL           = $fileSplit[1] . '_' . $fileSplit[2];
            $file              = $list[$key];
            $filePath          = $this->rootImportFolder . '/' . $file['publisher'] . '/' . $file['file'];
            $publisherFolderId = $this->getPublisherIdFromFolderName($file['publisher']);
            $query             = "SELECT file_md5 FROM %s WHERE `xml_file` = '%s' AND `publisher_folder_id` = '%s' ORDER BY version DESC";
            $results           = XMLImportDB::arrayQuery(sprintf($query, XMLImportMonitor::MONITORING_TABLE, $fileSQL, $publisherFolderId ));

            if ( $results )
            {
                $fileMd5 = md5_file($filePath);

                if ( $fileMd5 && $fileMd5 == $results[0]['file_md5'] )
                    unset( $list[$key] );
            }
        }

        return $list;
    }
function fetchArticleImportStatusWithMaximumVersion($articleId)
{
    $query = "SELECT id FROM import_status WHERE article_id = '%s' ORDER BY version LIMIT 0,1";
    $query = sprintf($query, $articleId);

    $result = XMLImportDB::arrayQuery($query);
    if (count($result) == 0)
    {
        throw new Exception("No import status present for article id {$articleId}");
    }

    return $result[0]['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);
    }
// add content field
foreach( XMLImportDB::arrayQuery("SELECT * FROM content LIMIT 1") as $row )
{
    if( !array_key_exists('new_publisher_folder_id', $row) )
        XMLImportDB::query( "ALTER TABLE content ADD COLUMN new_publisher_folder_id int(11) AFTER publisher_folder_id" );
    XMLImportDB::query( "UPDATE content SET new_publisher_folder_id = publisher_folder_id" );
}
// add import_status field
foreach( XMLImportDB::arrayQuery("SELECT * FROM import_status LIMIT 1") as $row )
{
    if( !array_key_exists('new_publisher_folder_id', $row) )
        XMLImportDB::query( "ALTER TABLE import_status ADD COLUMN new_publisher_folder_id int(11) AFTER publisher_folder_id" );
    XMLImportDB::query( "UPDATE import_status SET new_publisher_folder_id = publisher_folder_id" );
}
// add content_has_taxonomy fields
foreach( XMLImportDB::arrayQuery("SELECT * FROM content_has_taxonomy LIMIT 1") as $row )
{
    if( array_key_exists('old_taxonomy_id', $row) )
        XMLImportDB::query( "ALTER TABLE content_has_taxonomy DROP COLUMN old_taxonomy_id" );
    if( !array_key_exists('new_taxonomy_id', $row) )
        XMLImportDB::query( "ALTER TABLE content_has_taxonomy ADD COLUMN new_taxonomy_id int(11) AFTER taxonomy_id" );
    XMLImportDB::query( "UPDATE content_has_taxonomy SET new_taxonomy_id = content_has_taxonomy_id" );
}

$sql = "SELECT count(*) FROM INFORMATION_SCHEMA.COLUMNS
                           WHERE TABLE_SCHEMA = '$dbName'
                           AND TABLE_NAME = 'mm_publisher_folder'
                           AND COLUMN_NAME = 'new_publisher_folder_id'";
foreach( $db->arrayQuery( $sql ) as $row )
{
    if ( $row[0] == 0 )
$i = 0;
$mappingOldIDWithKey = array();
$sql = 'SELECT ct.taxonomy_id, t.source_key FROM content_has_taxonomy as ct INNER JOIN taxonomy as t ON ct.taxonomy_id = t.id';
$sql .= $tmpColumnExist ? ' WHERE ct.new_taxonomy_id = "" OR ct.new_taxonomy_id = 0' : '';
$sql .= ' GROUP BY taxonomy_id LIMIT %s, 500;';

$rows = XMLImportDB::arrayQuery(sprintf($sql, 0));
while ( is_array($rows) && count($rows) > 0 )
{
    while ( list(, $row) = each($rows) )
    {
        $mappingOldIDWithKey[$row['taxonomy_id']] = $row['source_key'];
        $i++;
    }

    $rows = XMLImportDB::arrayQuery(sprintf($sql, $i));
}
$cli->notice(sprintf('%s old taxonomy id to update', count($mappingOldIDWithKey)));




// Create mapping with new taxonomy id and key
$i = 0;
$mappingKeyWithNewID = array();
$sql = 'SELECT id, code FROM mm_taxonomy GROUP BY id LIMIT %s, 500;';

$rows = $dbEz->arrayQuery(sprintf($sql, 0));
while ( is_array($rows) && count($rows) > 0 )
{
    while ( list(, $row) = each($rows) )