require 'autoload.php';

$script = eZScript::instance( array(
                'description'    => ( "Clear all varnish for the given url pattern" ),
                'use-modules'    => true,
                'use-extensions' => true,
                'debug-output'   => false,
) );

$script->startup();

$options = $script->getOptions( "[uri:][clusterIdentifier:]", "", array(
    'uri'                   => 'Uri pattern to clear',
    'clusterIdentifier'     => '(optional) clusterIdentifier'
) );

$script->initialize();

$uri = isset($options['uri']) ? $options['uri'] : null;
$clusterIdentifier = isset($options['clusterIdentifier']) ? $options['clusterIdentifier'] : null;

if ( is_null($uri) )
    $script->shutdown(1, "Missing mandatory parameter uri.");

$varnishControl = new VarnishControl();
$varnishControl->isVerbose = $script->verboseOutputLevel() + 1;
$varnishControl->banUri( '/url/'.$uri, $clusterIdentifier );

$script->shutdown();

for ($i=1; $i<=$iterationsLimit; $i++)
{
    $results = $db->arrayQuery($sql);
    if (count($results) <= 0)
    {
        break;
    }

    // construct ids table with all ezpending id and $nodeList with all node to clear from varnish side
    $ids      = array();
    $nodeList = array();
    foreach ($results as $result)
    {
        $ids[]                            = $result["id"];
        $nodeList[$result['param_int']][] = $result['param'];
    }

    foreach ($nodeList as $key => $item)
    {
        $nodeList[$key] = array_unique($nodeList[$key]);
    }

    // clear varnish for all node id
    $varnishControl = new VarnishControl();
    $varnishControl->banNodes($nodeList);

    $sqlDelete = sprintf("DELETE FROM ezpending_actions WHERE id IN (%s)", implode(',', $ids));
    $db->query($sqlDelete);
    $cli->output("Removed $clearLimit items, $i/$iterationsLimit");
    sleep(1);
}
    // we let the search plugin deal with the indexation and flag the nodes not to be reindexed
    eZContentOperationCollection::registerSearchObject($object_id, false);
    eZContentObject::clearCache();
}

$i = 0;

//solrObjects
foreach($solrJsonObjects as $solrJsonObject)
{
    if (!($i%200) )
    {
        echo "Traitement Solr : $i\n";
    }

    $i++;

    $solrIndexationJob = new SolrIndexationJob(null);
    $solrIndexationJob->setAttribute('data', $solrJsonObject);
    $solrIndexationJob->store();
}

// Purge varnish cache of listing sort by most views :
echo "Purge varnish cache of listing sort by most views (".count($varnish_node_ids)." objects)\n";
$varnishControl = new VarnishControl();
$varnishControl->banMostViews( $varnish_node_ids );
// Purge varnish cache for homepage
echo "Purge varnish cache for homepage";
$varnishControl->banUri('/esibuild/main_view/app_content//');
                'explainOther'  => '',
                'hl.fl'         => '',
            );

            $solrBase = new eZSolrBase();
            $result = $solrBase->rawSolrRequest( '/select', $params, 'php' );
            $result['response']['docs'][0]['attr_content_rating_'.$row['cluster_identifier'].'_f'] = $row['total'];

            $solrIndexationJob = new SolrIndexationJob(null);
            $solrIndexationJob->setAttribute('data', json_encode($result['response']['docs'][0]));
            $solrIndexationJob->store();
        }

        $mdb->query(
            sprintf(
                'UPDATE mm_rating_remote SET to_reindex = 0 WHERE remote_id = "%s"',
                $remoteId
            )
        );
    }

    eZContentObject::clearCache();
    $offset += 200;
}

// Purge varnish cache of listing sort by most rated :
$varnishControl = new VarnishControl();
$varnishControl->banMostPopular( $varnishNodeIds );

echo $counter." objects treated\n";
    catch (Exception $e)
    {
        $cli->output($e->getMessage());
    }
}

//get XML
$xml = getXML();

if(!empty($xml))
{
    //fetch objects parent node
    /* @type $parentObject eZContentObject */
    /* @type $parentNode eZContentObjectTreeNode */
    $parentObject = eZContentObject::fetchByRemoteID( 'congress_report_rss_folder' );
    $parentNode = $parentObject->mainNode();

    //delete parent node childrens
    deleteNodes($parentNode);

    //convert XML string into eZ Publish objects
    XMLToHTMLObjects($parentNode, $xml);

    //clear homepage varnish cache
    $varnishControl = new VarnishControl();
    $varnishControl->banNodes($parentNode->attribute('parent_node_id'));

    //clear parent view cache
    eZContentCacheManager::clearObjectViewCache( $parentObject->attribute('id'), true );
}