original_string: public static function getArchiveIds($siteIds, $periods, $segment, $plugins)
      {
          if (empty($siteIds)) {
              throw new \Exception("Website IDs could not be read from the request, ie. idSite=");
          }
  
          foreach ($siteIds as $index => $siteId) {
              $siteIds[$index] = (int) $siteId;
          }
  
          $getArchiveIdsSql = "SELECT idsite, name, date1, date2, MAX(idarchive) as idarchive
                                 FROM %s
                                WHERE idsite IN (" . implode(',', $siteIds) . ")
                                  AND " . self::getNameCondition($plugins, $segment) . "
                                  AND %s
                             GROUP BY idsite, date1, date2, name";
  
          $monthToPeriods = array();
          foreach ($periods as $period) {
              /** @var Period $period */
              if ($period->getDateStart()->isLater(Date::now()->addDay(2))) {
                  continue; // avoid creating any archive tables in the future
              }
              $table = ArchiveTableCreator::getNumericTable($period->getDateStart());
              $monthToPeriods[$table][] = $period;
          }
  
          // for every month within the archive query, select from numeric table
          $result = array();
          foreach ($monthToPeriods as $table => $periods) {
              $firstPeriod = reset($periods);
  
              $bind = array();
  
              if ($firstPeriod instanceof Range) {
                  $dateCondition = "date1 = ? AND date2 = ?";
                  $bind[] = $firstPeriod->getDateStart()->toString('Y-m-d');
                  $bind[] = $firstPeriod->getDateEnd()->toString('Y-m-d');
              } else {
                  // we assume there is no range date in $periods
                  $dateCondition = '(';
  
                  foreach ($periods as $period) {
                      if (strlen($dateCondition) > 1) {
                          $dateCondition .= ' OR ';
                      }
  
                      $dateCondition .= "(period = ? AND date1 = ? AND date2 = ?)";
                      $bind[] = $period->getId();
                      $bind[] = $period->getDateStart()->toString('Y-m-d');
                      $bind[] = $period->getDateEnd()->toString('Y-m-d');
                  }
  
                  $dateCondition .= ')';
              }
  
              $sql = sprintf($getArchiveIdsSql, $table, $dateCondition);
  
              $archiveIds = Db::fetchAll($sql, $bind);
  
              // get the archive IDs
              foreach ($archiveIds as $row) {
                  //FIXMEA duplicate with Archive.php
                  $dateStr = $row['date1'] . ',' . $row['date2'];
  
                  $result[$row['name']][$dateStr][] = $row['idarchive'];
              }
          }
  
          return $result;
      }
code: public static function getArchiveIds($siteIds, $periods, $segment, $plugins)
      {
          if (empty($siteIds)) {
              throw new \Exception("Website IDs could not be read from the request, ie. idSite=");
          }
  
          foreach ($siteIds as $index => $siteId) {
              $siteIds[$index] = (int) $siteId;
          }
  
          $getArchiveIdsSql = "SELECT idsite, name, date1, date2, MAX(idarchive) as idarchive
                                 FROM %s
                                WHERE idsite IN (" . implode(',', $siteIds) . ")
                                  AND " . self::getNameCondition($plugins, $segment) . "
                                  AND %s
                             GROUP BY idsite, date1, date2, name";
  
          $monthToPeriods = array();
          foreach ($periods as $period) {
              /** @var Period $period */
              if ($period->getDateStart()->isLater(Date::now()->addDay(2))) {
                  continue; // avoid creating any archive tables in the future
              }
              $table = ArchiveTableCreator::getNumericTable($period->getDateStart());
              $monthToPeriods[$table][] = $period;
          }
  
          // for every month within the archive query, select from numeric table
          $result = array();
          foreach ($monthToPeriods as $table => $periods) {
              $firstPeriod = reset($periods);
  
              $bind = array();
  
              if ($firstPeriod instanceof Range) {
                  $dateCondition = "date1 = ? AND date2 = ?";
                  $bind[] = $firstPeriod->getDateStart()->toString('Y-m-d');
                  $bind[] = $firstPeriod->getDateEnd()->toString('Y-m-d');
              } else {
                  // we assume there is no range date in $periods
                  $dateCondition = '(';
  
                  foreach ($periods as $period) {
                      if (strlen($dateCondition) > 1) {
                          $dateCondition .= ' OR ';
                      }
  
                      $dateCondition .= "(period = ? AND date1 = ? AND date2 = ?)";
                      $bind[] = $period->getId();
                      $bind[] = $period->getDateStart()->toString('Y-m-d');
                      $bind[] = $period->getDateEnd()->toString('Y-m-d');
                  }
  
                  $dateCondition .= ')';
              }
  
              $sql = sprintf($getArchiveIdsSql, $table, $dateCondition);
  
              $archiveIds = Db::fetchAll($sql, $bind);
  
              // get the archive IDs
              foreach ($archiveIds as $row) {
                  //FIXMEA duplicate with Archive.php
                  $dateStr = $row['date1'] . ',' . $row['date2'];
  
                  $result[$row['name']][$dateStr][] = $row['idarchive'];
              }
          }
  
          return $result;
      }
code_tokens: public static function getArchiveIds ( $ siteIds , $ periods , $ segment , $ plugins ) { if ( empty ( $ siteIds ) ) { throw new \ Exception ( "Website IDs could not be read from the request, ie. idSite=" ) ; } foreach ( $ siteIds as $ index => $ siteId ) { $ siteIds [ $ index ] = ( int ) $ siteId ; } $ getArchiveIdsSql = "SELECT idsite, name, date1, date2, MAX(idarchive) as idarchive
                                 FROM %s
                                WHERE idsite IN (" . implode ( ',' , $ siteIds ) . ")
                                  AND " . self :: getNameCondition ( $ plugins , $ segment ) . "
                                  AND %s
                             GROUP BY idsite, date1, date2, name" ; $ monthToPeriods = array ( ) ; foreach ( $ periods as $ period ) { /** @var Period $period */ if ( $ period -> getDateStart ( ) -> isLater ( Date :: now ( ) -> addDay ( 2 ) ) ) { continue ; // avoid creating any archive tables in the future } $ table = ArchiveTableCreator :: getNumericTable ( $ period -> getDateStart ( ) ) ; $ monthToPeriods [ $ table ] [ ] = $ period ; } // for every month within the archive query, select from numeric table $ result = array ( ) ; foreach ( $ monthToPeriods as $ table => $ periods ) { $ firstPeriod = reset ( $ periods ) ; $ bind = array ( ) ; if ( $ firstPeriod instanceof Range ) { $ dateCondition = "date1 = ? AND date2 = ?" ; $ bind [ ] = $ firstPeriod -> getDateStart ( ) -> toString ( 'Y-m-d' ) ; $ bind [ ] = $ firstPeriod -> getDateEnd ( ) -> toString ( 'Y-m-d' ) ; } else { // we assume there is no range date in $periods $ dateCondition = '(' ; foreach ( $ periods as $ period ) { if ( strlen ( $ dateCondition ) > 1 ) { $ dateCondition .= ' OR ' ; } $ dateCondition .= "(period = ? AND date1 = ? AND date2 = ?)" ; $ bind [ ] = $ period -> getId ( ) ; $ bind [ ] = $ period -> getDateStart ( ) -> toString ( 'Y-m-d' ) ; $ bind [ ] = $ period -> getDateEnd ( ) -> toString ( 'Y-m-d' ) ; } $ dateCondition .= ')' ; } $ sql = sprintf ( $ getArchiveIdsSql , $ table , $ dateCondition ) ; $ archiveIds = Db :: fetchAll ( $ sql , $ bind ) ; // get the archive IDs foreach ( $ archiveIds as $ row ) { //FIXMEA duplicate with Archive.php $ dateStr = $ row [ 'date1' ] . ',' . $ row [ 'date2' ] ; $ result [ $ row [ 'name' ] ] [ $ dateStr ] [ ] = $ row [ 'idarchive' ] ; } } return $ result ; }
docstring: Queries and returns archive IDs for a set of sites, periods, and a segment.
  
  @param array $siteIds
  @param array $periods
  @param Segment $segment
  @param array $plugins List of plugin names for which data is being requested.
  @return array Archive IDs are grouped by archive name and period range, ie,
  array(
  'VisitsSummary.done' => array(
  '2010-01-01' => array(1,2,3)
  )
  )
  @throws
docstring_tokens: Queries and returns archive IDs for a set of sites periods and a segment .