public function getInvalidatedArchiveIdsSafeToDelete($archiveTable, array $idSites)
    {
        try {
            Db::get()->query('SET SESSION group_concat_max_len=' . (128 * 1024));
        } catch (\Exception $ex) {
            $this->logger->info("Could not set group_concat_max_len MySQL session variable.");
        }

        $idSites = array_map(function ($v) { return (int)$v; }, $idSites);

        $sql = "SELECT idsite, date1, date2, period, name,
                       GROUP_CONCAT(idarchive, '.', value ORDER BY ts_archived DESC) as archives
                  FROM `$archiveTable`
                 WHERE name LIKE 'done%'
                   AND value IN (" . ArchiveWriter::DONE_INVALIDATED . ','
                                   . ArchiveWriter::DONE_OK . ','
                                   . ArchiveWriter::DONE_OK_TEMPORARY . ")
                   AND idsite IN (" . implode(',', $idSites) . ")
                 GROUP BY idsite, date1, date2, period, name";

        $archiveIds = array();

        $rows = Db::fetchAll($sql);
        foreach ($rows as $row) {
            $duplicateArchives = explode(',', $row['archives']);

            $firstArchive = array_shift($duplicateArchives);
            list($firstArchiveId, $firstArchiveValue) = explode('.', $firstArchive);

            // if the first archive (ie, the newest) is an 'ok' or 'ok temporary' archive, then
            // all invalidated archives after it can be deleted
            if ($firstArchiveValue == ArchiveWriter::DONE_OK
                || $firstArchiveValue == ArchiveWriter::DONE_OK_TEMPORARY
            ) {
                foreach ($duplicateArchives as $pair) {
                    if (strpos($pair, '.') === false) {
                        $this->logger->info("GROUP_CONCAT cut off the query result, you may have to purge archives again.");
                        break;
                    }

                    list($idarchive, $value) = explode('.', $pair);
                    if ($value == ArchiveWriter::DONE_INVALIDATED) {
                        $archiveIds[] = $idarchive;
                    }
                }
            }
        }

        return $archiveIds;
    }