Sauvegarde mieux et active la suppression

This commit is contained in:
Shikiryu 2017-11-07 21:11:26 +01:00
parent a9f48effb5
commit 16af2429da

View File

@ -15,13 +15,25 @@ foreach (new DirectoryIterator(EXISTING_DEALS_DIR) as $existing_deal) {
} }
} }
function copy_folder($from, $to)
{
$directory = new RecursiveDirectoryIterator($from);
foreach (new RecursiveIteratorIterator($directory) as $filename => $current) {
$source = $current->getPathName();
$destination = $to . $current->getFileName();
copy($source, $destination);
}
}
echo sprintf('%u annonces locales, normalement en ligne.%s', count($existing_deals), "\n"); echo sprintf('%u annonces locales, normalement en ligne.%s', count($existing_deals), "\n");
$script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true); $script_params = parse_ini_file(sprintf('%s/lbcreposter.ini', DEALS_DIR), true);
$config = new \Shikiryu\LBCReposter\Config($script_params); $config = new \Shikiryu\LBCReposter\Config($script_params);
$account = new \Shikiryu\LBCReposter\Account($config); $account = new \Shikiryu\LBCReposter\Account($config);
$actions = new \Shikiryu\LBCReposter\Actions($account); $actions = new \Shikiryu\LBCReposter\Actions($account);
$actions->setDebug(LOGS_DIR); $actions->setDebug(LOGS_DIR);
$deals_to_backup = $existing_deals;
if ($actions->connect()) { if ($actions->connect()) {
// existing deals // existing deals
@ -35,15 +47,18 @@ if ($actions->connect()) {
echo sprintf('L\'annonce %s existe déjà, ', $deal->getId()); echo sprintf('L\'annonce %s existe déjà, ', $deal->getId());
// si elle est vieille, on supprime et on recréé // si elle est vieille, on supprime et on recréé
if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) <= (new DateTime())) { if ($existing_deals[$deal->getId()]->getDateCreation()->add(new DateInterval('P1W')) <= (new DateTime())) {
// Suppression
echo sprintf('elle est vieille (%s), on la supprime. %s', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i'), "\n"); echo sprintf('elle est vieille (%s), on la supprime. %s', $existing_deals[$deal->getId()]->getDateCreation()->format('d/m/Y H:i'), "\n");
// $actions->delete($deal); $actions->delete($deal);
// Backup
mkdir(BACKUP_DEALS_DIR.'/'.$deal->getId());
copy_folder(EXISTING_DEALS_DIR.'/'.$deal->getId(), BACKUP_DEALS_DIR.'/'.$deal->getId());
// Placement en "à créer"
rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId()); rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), NEW_DEALS_DIR.'/'.$deal->getId());
// rename(EXISTING_DEALS_DIR.'/'.$deal->getId(), BACKUP_DEALS_DIR.'/'.$deal->getId());
} else { } else {
// sinon on la laisse // sinon on la laisse
echo sprintf('et tout va bien. %s', "\n"); echo sprintf('et tout va bien. %s', "\n");
} }
unset($deals_to_backup[$deal->getId()]);
} else { } else {
echo sprintf('L\'annonce %s n\'existait pas et est sauvegardée. %s', $deal->getSubject(), "\n"); echo sprintf('L\'annonce %s n\'existait pas et est sauvegardée. %s', $deal->getSubject(), "\n");
// Une annonce non sauvegardée ? on la sauve // Une annonce non sauvegardée ? on la sauve
@ -51,11 +66,6 @@ if ($actions->connect()) {
} }
} }
// Backup
foreach ($deals_to_backup as $deal_to_backup) {
rename(EXISTING_DEALS_DIR.'/'.$deal_to_backup->getId(), BACKUP_DEALS_DIR.'/'.$deal_to_backup->getId());
}
// Creation // Creation
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) { foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
if ($new_deal->isDir() && !$new_deal->isDot()) { if ($new_deal->isDir() && !$new_deal->isDot()) {