/**
  * Returns the idArchive if the archive is available in the database.
  * Returns false if the archive needs to be computed.
  * 
  * An archive is available if
  * - for today, the archive was computed less than maxTimestampArchive seconds ago
  * - for any other day, if the archive was computed once this day was finished
  * - for other periods, if the archive was computed once the period was finished
  *
  * @return int|false
  */
 protected function isArchived()
 {
     $bindSQL = array($this->idsite, $this->strDateStart, $this->strDateEnd, $this->periodId);
     $timeStampWhere = " AND UNIX_TIMESTAMP(ts_archived) >= ? ";
     $bindSQL[] = $this->maxTimestampArchive;
     $sqlQuery = "\tSELECT idarchive, value, name, UNIX_TIMESTAMP(date1) as timestamp\n\t\t\t\t\t\tFROM " . $this->tableArchiveNumeric->getTableName() . "\n\t\t\t\t\t\tWHERE idsite = ?\n\t\t\t\t\t\t\tAND date1 = ?\n\t\t\t\t\t\t\tAND date2 = ?\n\t\t\t\t\t\t\tAND period = ?\n\t\t\t\t\t\t\tAND ( (name = 'done' AND value = " . Piwik_ArchiveProcessing::DONE_OK . ")\n\t\t\t\t\t\t\t\t\tOR name = 'nb_visits')\n\t\t\t\t\t\t\t{$timeStampWhere}\n\t\t\t\t\t\tORDER BY ts_archived DESC";
     $results = Piwik_FetchAll($sqlQuery, $bindSQL);
     if (empty($results)) {
         return false;
     }
     $idarchive = false;
     // we look for the more recent idarchive
     foreach ($results as $result) {
         if ($result['name'] == 'done') {
             $idarchive = $result['idarchive'];
             $this->timestampDateStart = $result['timestamp'];
             break;
         }
     }
     // case when we have a nb_visits entry in the archive, but the process is not finished yet or failed to finish
     // therefore we don't have the done=OK
     if ($idarchive === false) {
         return false;
     }
     // we look for the nb_visits result for this more recent archive
     foreach ($results as $result) {
         if ($result['name'] == 'nb_visits' && $result['idarchive'] == $idarchive) {
             $this->isThereSomeVisits = $result['value'] != 0;
             break;
         }
     }
     return $idarchive;
 }
 /**
  * Returns the idArchive if the archive is available in the database.
  * Returns false if the archive needs to be computed.
  * 
  * An archive is available if
  * - for today, the archive was computed less than minDatetimeArchiveProcessedUTC seconds ago
  * - for any other day, if the archive was computed once this day was finished
  * - for other periods, if the archive was computed once the period was finished
  *
  * @return int|false
  */
 protected function isArchived()
 {
     $bindSQL = array($this->idsite, $this->period->getDateStart()->toString('Y-m-d'), $this->period->getDateEnd()->toString('Y-m-d'), $this->periodId);
     $timeStampWhere = '';
     if ($this->minDatetimeArchiveProcessedUTC) {
         $timeStampWhere = " AND ts_archived >= ? ";
         $bindSQL[] = Piwik_Date::factory($this->minDatetimeArchiveProcessedUTC)->getDatetime();
     }
     // When a Segment is specified, we try and only process the requested report in the archive
     // As a limitation, we don't know all the time which plugin should process which report
     // There is a catch all flag 'all' appended to archives containing all reports already
     // We look for this 'done.ABCDEFG.all', or for an archive that contains only our plugin data 'done.ABDCDEFG.Referers'
     $done = $this->getDoneStringFlag();
     $doneAllPluginsProcessed = $this->getDoneStringFlag($flagArchiveAsAllPlugins = true);
     $sqlSegmentsFindArchiveAllPlugins = '';
     if ($done != $doneAllPluginsProcessed) {
         $sqlSegmentsFindArchiveAllPlugins = "OR (name = '" . $doneAllPluginsProcessed . "' AND value = " . Piwik_ArchiveProcessing::DONE_OK . ")\n\t\t\t\t\tOR (name = '" . $doneAllPluginsProcessed . "' AND value = " . Piwik_ArchiveProcessing::DONE_OK_TEMPORARY . ")";
     }
     $sqlQuery = "\tSELECT idarchive, value, name, date1 as startDate\n\t\t\t\t\t\tFROM " . $this->tableArchiveNumeric->getTableName() . "\n\t\t\t\t\t\tWHERE idsite = ?\n\t\t\t\t\t\t\tAND date1 = ?\n\t\t\t\t\t\t\tAND date2 = ?\n\t\t\t\t\t\t\tAND period = ?\n\t\t\t\t\t\t\tAND ( (name = '" . $done . "' AND value = " . Piwik_ArchiveProcessing::DONE_OK . ")\n\t\t\t\t\t\t\t\t\tOR (name = '" . $done . "' AND value = " . Piwik_ArchiveProcessing::DONE_OK_TEMPORARY . ")\n\t\t\t\t\t\t\t\t\t{$sqlSegmentsFindArchiveAllPlugins}\n\t\t\t\t\t\t\t\t\tOR name = 'nb_visits')\n\t\t\t\t\t\t\t{$timeStampWhere}\n\t\t\t\t\t\tORDER BY ts_archived DESC";
     $results = Piwik_FetchAll($sqlQuery, $bindSQL);
     if (empty($results)) {
         return false;
     }
     $idarchive = false;
     // we look for the more recent idarchive
     foreach ($results as $result) {
         if ($result['name'] == $done || $result['name'] == $doneAllPluginsProcessed) {
             $idarchive = $result['idarchive'];
             $this->timestampDateStart = Piwik_Date::factory($result['startDate'])->getTimestamp();
             break;
         }
     }
     // case when we have a nb_visits entry in the archive, but the process is not finished yet or failed to finish
     // therefore we don't have the done=OK
     if ($idarchive === false) {
         return false;
     }
     if ($this->getPluginBeingProcessed($this->getRequestedReport()) == 'VisitsSummary') {
         $this->isThereSomeVisits = false;
     }
     // we look for the nb_visits result for this most recent archive
     foreach ($results as $result) {
         if ($result['name'] == 'nb_visits' && $result['idarchive'] == $idarchive) {
             $this->isThereSomeVisits = $result['value'] > 0;
             $this->setNumberOfVisits($result['value']);
             break;
         }
     }
     return $idarchive;
 }