private function debug( $string, $label, $level = eZDebug::LEVEL_DEBUG )
 {
     if ( empty( $this->cli ) )
     {
         $debug = eZDebug::instance();
         $debug->write( $string, $level, $label );
     }
     else
     {
         $label = date( DATE_ATOM ) . ' -- ' . $label;
         $this->cli->output( $label . " :" );
         if ( is_object( $string ) || is_array( $string ) )
         {
             $string = eZDebug::dumpVariable( $string );
         }
         $this->cli->output( $string );
     }
 }
    /**
     * @param string $xmlFileName
     * @param string $type
     */
    protected function uploadXMLtoFTP( $xmlFileName, $type )
    {
        if( !extension_loaded( 'ftp' ) )
        {
            $this->cli->output( 'PHP FTP extension is not loaded' );
        }
        else
        {
            $localFile = $this->articlesLocalPath . '/' . $xmlFileName . '_' . $this->articlesLocale . '.xml';
            $localTmpFile = $this->articlesLocalPath . '/' . $xmlFileName . '_' . time() . '_' . $this->articlesLocale . '.xml';
            if( !file_exists( $localFile ) )
            {
                $this->script->shutdown( 0, "$localFile doesn't exist" );
                return;
            }

            try
            {
                $resource = ftp_connect( $this->host[$this->env], $this->port );
            }
            catch( \Exception $e )
            {
                $this->script->shutdown( 0, 'Failed to connect on FTP server' );
                return;
            }

            if( !ftp_login( $resource, $this->username, $this->password ) )
            {
                $this->script->shutdown( 0, 'Failed to authenticate on FTP server' );
                return;
            }
            else
            {
                if( !is_resource( $resource ) )
                {
                    $this->script->shutdown( 0, 'Not connected to FTP server' );
                    return;
                }
                else
                {
                    try
                    {
                        ftp_pasv($resource, true);

                        $dom = new DOMDocument();
                        $dom->load( $localFile );
                        $xpath = new DomXpath( $dom );

                        $dateCreation = $xpath->query( '//field[@name="date_creation"]' );
                        $dateCreation->item( 0 )->nodeValue = '';
                        $dateCreation->item( 0 )->appendChild($dom->createTextNode( date('Y-m-d H:i', time() - 7200 ) ) );

                        $datePublish = $xpath->query( '//field[@name="date_publish"]' );
                        $datePublish->item( 0 )->nodeValue = '';
                        $datePublish->item( 0 )->appendChild($dom->createTextNode( date('Y-m-d H:i', time() - 7200) ) );

                        $title = $xpath->query( '//field[@name="title"]' );
                        $title->item( 0 )->nodeValue = '';
                        $title->item( 0 )->appendChild( $dom->createCDATASection( 'Article_' . time() . " $type article" ) );

                        file_put_contents( $localTmpFile, $dom->saveXML() );
                        $stream = fopen( $localTmpFile, 'r' );

                        if( !ftp_fput( $resource, $this->articleRemotePath[$type] . '/' . $xmlFileName . '_' . time() . '_' . $this->articlesLocale . '.xml', $stream, FTP_ASCII ) )
                        {
                            unlink( $localTmpFile );
                            $this->script->shutdown( 0, 'Failed to put a file on FTP server' );
                        }
                        unlink( $localTmpFile );
                        fclose( $stream );

                        $importMonitor = new NewRelicImportMonitoring();
                        $importMonitor->setAttribute( 'article_title', 'Article_' . time() . " $type article" );
                        $importMonitor->setAttribute( 'date_insert', time() );
                        $importMonitor->setAttribute( 'new_relic_report', 0 );
                        $importMonitor->store();
                    }
                    catch( \Exception $e )
                    {
                        $this->script->shutdown( 0, 'An error occured while uploading the XML to the FTP' );
                        return;
                    }
                }
            }
            ftp_pasv( $resource, true );
            ftp_close( $resource );
        }
    }
    'description' => ( 'Publisher folder synchronization' ),
    'use-modules' => true,
    'use-extensions' => true,
    'debug-output' => false,
) );

$script->startup();
$options = $script->getOptions( '[clusterIdentifier:][auto]', "", array(
    'clusterIdentifier' => "(optional) Cluster Identifier to filter Interactive List",
    'auto' => "(optional) Cluster Identifier to filter Interactive List",
    )
);
$script->initialize();

$cli             = new QuestionInteractiveCli();
MMSynchLog::$cli = eZCli::instance();

SQLIContentPublisher::getInstance()->setOptions(new SQLIContentPublishOptions(array(
    'modification_check' => false,
    'update_null_field' => true
) ) );

// Use admin
$user   = eZUser::fetchByName( 'admin' );
$userID = $user->attribute( 'contentobject_id' );
eZUser::setCurrentlyLoggedInUser( $user, $userID );

$whereClause = "";

$query = "select f.id, f.path as path
        from mm_application_has_publisher_folder as h