<?php

require 'autoload.php';
$cli = eZCLI::instance();
$script = eZScript::instance(array('description' => "Clean sqliimporttoken", 'use-session' => false, 'use-modules' => true, 'use-extensions' => true));
$script->startup();
$options = $script->getOptions('[start][stop]', '', array('start' => 'Start rsync status', 'stop' => 'Stop rssync status'));
$script->initialize();
$script->setUseDebugAccumulators(true);
$rsyncStatusName = 'rsync_status';
$statusRunning = 1;
$statusHalted = 0;
$rsyncStatus = eZSiteData::fetchByName($rsyncStatusName);
if (!$rsyncStatus) {
    $row = array('name' => 'rsync_status', 'value' => $statusHalted);
    $rsyncStatus = new eZSiteData($row);
}
if ($options['stop']) {
    $rsyncStatus->setAttribute('value', $statusHalted);
} elseif ($options['start']) {
    $rsyncStatus->setAttribute('value', $statusRunning);
}
$rsyncStatus->store();
$cli->output("Rsync status: " . $rsyncStatus->attribute('value'));
$script->shutdown();
 /**
  * return void
  */
 protected function logImport()
 {
     $log = new eZSiteData(null);
     $log->setAttribute('name', $this->application->name . '_last_import_date');
     $log->store();
 }
    public static function importTripDatabase($month, $batch, $area)
    {
        echo "--------------------------------\r\n Importing 'Trip Database' data\r\n--------------------------------\r\n";
        $minMonth = ((self::$autoMode) ? self::$month    : (is_null($month) ? 1 : $month));
        $minBatch = ((self::$autoMode) ? self::$batch    : (is_null($batch) ? 1 : $batch));
        $maxMonth = ((self::$autoMode) ? self::$month+1  : (is_null($month) ? 13: $month+1));
        $maxBatch = ((self::$autoMode) ? self::$batch+1  : (is_null($batch) ? 6 : $batch+1));
        $areas    = is_null($area)    ? self::$areas    : array($area);

        $jsonArticles = array();
        foreach($areas as $area)
        {
            for($month = $minMonth; $month<$maxMonth ; $month++)
            {
                self::$month = $month;
                for($batch=$minBatch; $batch<$maxBatch ; $batch++)
                {
                    self::$batch = $batch;
                    $xmlResponse = self::curlCallTripAPI($area);
                    $jsonArticles[] = self::processXML($xmlResponse);
                }
            }
        }

        foreach($jsonArticles as $container)
        {
            foreach($container as $article)
            {
                $solrIndexationJob = new SolrIndexationJob(null);
                $solrIndexationJob->setAttribute('data', $article);
                $solrIndexationJob->store();
            }
        }

        $log = new eZSiteData(null);
        $log->setAttribute('name', 'trip_last_import_date');
        $log->setAttribute('value', self::$year.'-'.self::$month.'-'.self::$batch);
        $log->store();

        echo "\r\n".self::$totalProcessed." articles were found and imported.\r\nRun process_solr_indexation_jobs to import them into SolR.\r\n";
        echo "\r\nEnd of script. Bye!\r\n\r\n";
    }
 /**
  * return void
  */
 protected function logImport()
 {
     $log = new eZSiteData(null);
     $log->setAttribute('name', 'congress_report_rss_last_import_date');
     $log->store();
 }