🔨 Sauvegarde les suppressions et supprime les créations
This commit is contained in:
parent
852e9ba1c5
commit
0353fe68cc
24
cron.php
24
cron.php
@ -28,12 +28,21 @@ function copy_folder($from, $to)
|
||||
}
|
||||
}
|
||||
|
||||
function delete_folder($folder) {
|
||||
$files = array_diff(scandir($folder), ['.','..']);
|
||||
foreach ($files as $file) {
|
||||
(is_dir(sprintf('%s/%s', $folder, $file))) ? delete_folder(sprintf('%s/%s', $folder, $file)) : unlink(sprintf('%s/%s', $folder, $file));
|
||||
}
|
||||
return rmdir($folder);
|
||||
}
|
||||
|
||||
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);
|
||||
$config = new \Shikiryu\LBCReposter\Config($script_params);
|
||||
$account = new \Shikiryu\LBCReposter\Account($config);
|
||||
$actions = new \Shikiryu\LBCReposter\Actions($account);
|
||||
$actions->setDebug(LOGS_DIR);
|
||||
$deals_to_backup = $existing_deals;
|
||||
|
||||
if ($actions->connect()) {
|
||||
// existing deals
|
||||
@ -61,6 +70,7 @@ if ($actions->connect()) {
|
||||
// sinon on la laisse
|
||||
echo sprintf('et tout va bien. %s', "\n");
|
||||
}
|
||||
unset($deals_to_backup[$deal->getId()]);
|
||||
} else {
|
||||
echo sprintf('L\'annonce %s n\'existait pas et est sauvegardée. %s', $deal->getSubject(), "\n");
|
||||
// Une annonce non sauvegardée ? on la sauve
|
||||
@ -68,15 +78,21 @@ 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
|
||||
foreach (new DirectoryIterator(NEW_DEALS_DIR) as $new_deal) {
|
||||
if ($new_deal->isDir() && !$new_deal->isDot()) {
|
||||
$new_deal = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
|
||||
if ($actions->create($new_deal)) {
|
||||
echo sprintf('L\'annonce %s a bien été créée. %s', $new_deal->getSubject(), "\n");
|
||||
$deal_to_create = \Shikiryu\LBCReposter\Deal::fromJSON($new_deal->getRealPath() . '/data.json');
|
||||
if ($actions->create($deal_to_create)) {
|
||||
delete_folder($new_deal->getRealPath());
|
||||
echo sprintf('L\'annonce %s a bien été créée. %s', $deal_to_create->getSubject(), "\n");
|
||||
break;
|
||||
} else {
|
||||
echo sprintf('L\'annonce %s n\'a pas bien été créée. %s', $new_deal->getSubject(), "\n");
|
||||
echo sprintf('L\'annonce %s n\'a pas bien été créée. %s', $deal_to_create->getSubject(), "\n");
|
||||
}
|
||||
sleep(30);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user