1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
| <?php
namespace Gg\SiteBundle\Command;
use Gg\SiteBundle\Entity\Photo;
use Gg\SiteBundle\Form\PhotoType;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Finder\Finder;
use \DateTime;
Class MajCommand extends ContainerAwareCommand{
protected function configure()
{
$this
->setName('photo:maj')
->setDescription('Mise à jour de la base de données de photos');
}
protected function execute(InputInterface $input, OutputInterface $output)
{
// Variables
$chemin=dirname(dirname(dirname(dirname(__DIR__)))).'/web/img/galerie/';
$logger=$this->getContainer()->get('logger');
$log=dirname($chemin).'/logs/LOG_'.date("Y_m_d_G_i_s").'.txt';
// Fonctions
function log_iptc($array){
if (array_key_exists('iptc', $array)){
$log_iptc='IPTC :'."\n";
foreach ($array['iptc'] as $cle=>$index) {
if (is_array($index)){
$log_iptc.="\t\t".$cle.' =>'."\n";
foreach ($index as $key=>$valeur) {
$log_iptc.="\t\t\tIndex: ".$key.'='.$valeur."\n";
}
}
}
return $log_iptc;
}
}
function log($log, $element){
$log_iptc = log_iptc($element);
file_put_contents($log,
'=> '.$element['nom']."\n".
"\t".'dernière modification: '.$element['date']->format('Y-m-d H:i:s')."\n".
"\t".'taille (octets) :'.$element['taille']."\n".
"\t".'chemin :'.$element['chemin']."\n".
"\t".$log_iptc."\n"
, FILE_APPEND);
}
// Log (app/logs)
$logger->info('début de l execution de la commande');
// LOG (web/img/logs)
file_put_contents($log,
'Mise à jour du '.date("j F Y G:i:s ")."\n\n"
, FILE_APPEND);
// Liste des photos en BDD
$array_photos=array();
$repository = $this ->getContainer()->get('doctrine')
->getManager()
->getRepository('GgSiteBundle:Photo');
$liste_photos = $repository->findAll();
foreach ($liste_photos as $element){
$array_photos[]=array(
'nom'=>$element->getNom(),
'taille'=>$element->getTaille(),
'date'=>$element->getDate(),
'chemin'=>$element->getChemin(),
'iptc'=>$element->getIptc()
);
}
// LOG photos BDD (web/img/logs)
file_put_contents($log,
count($array_photos).' photos en BDD : '."\n"
, FILE_APPEND);
foreach ($array_photos as $element){
log($log, $element);
}
// Liste des photos dans le répertoire
$finder = new Finder();
$finder ->files()
->in($chemin)
->name('*.JPG')
->name('*.JPEG')
->name('*.jpg')
->name('*.jpeg');
foreach ($finder as $element){
$photos[]=get_object_vars($element);
$file=$element->getRealpath();
$date=$element->getCTime();
$date=DateTime::__set_state(array(
'date' => date('Y-m-d H:i:s',$date),
'timezone_type' => 3,
'timezone' => 'Europe/Paris'));
GetImageSize ($file,$info);
$iptc = array(
'mots_cles'=> 'vide',
'date_creation'=> 'vide',
'heure_creation'=> 'vide',
'copyright'=> 'vide'
);
$iptc_index = array(
'mots_cles'=> '2#025',
'date_creation'=> '2#055',
'heure_creation'=> '2#060',
'copyright'=> '2#116'
);
if (isset($info["APP13"])){
$iptc_origine = iptcparse ($info["APP13"]);
foreach($iptc_index as $index=>$code){
if (is_array($iptc_origine)){
if (array_key_exists($code, $iptc_origine)) {
$iptc[$index] = $iptc_origine[$code];
}
}
};
}
$array_finder[]=array(
'nom'=>$element->getfilename(),
'taille'=>$element->getSize(),
'date'=>$date,
'chemin'=>$file,
'iptc'=>$iptc
);
}
// LOG photos Répertoire (web/img/logs)
file_put_contents($log,
"\n".count($array_finder).' fichiers dans le répertoire ('.$chemin.'):'."\n"
, FILE_APPEND);
foreach ($array_finder as $element) {
log($log, $element);
}
// nouvelle liste de photos à enregistrer en BDD
$nvlle_liste=array();
$modif_liste=array();
foreach ($array_finder as $photo){
// Modif Liste
foreach ($array_photos as $element){
if (in_array($photo['nom'], $element)){
if ($photo != $element){
$modif_liste[]=$photo;
}
}
}
// Nvlle Liste
if (!in_array($photo, $array_photos) && !in_array($photo, $modif_liste)){
$nvlle_liste[]=$photo;
}
}
// LOG photos à enregistrer (BDD)
// LOG Modifs
file_put_contents($log,
"\n".count($modif_liste).' photos modifiées à mettre à jour en BDD: '."\n"
, FILE_APPEND);
foreach ($modif_liste as $element){
log($log, $element);
}
// LOG Nvlles
file_put_contents($log,
"\n".count($nvlle_liste).' nouvelles photos à enregistrer en BDD: '."\n"
, FILE_APPEND);
foreach ($nvlle_liste as $element){
log($log, $element);
}
// Enregistrement en BDD
// Les Modifs
foreach ($modif_liste as $element){
$nom=$element['nom'];
$em = $this ->getContainer()->get('doctrine')
->getManager();
$photo= $em->getRepository('GgSiteBundle:Photo')
->getModif($nom);
$photo ->setTaille($element['taille'])
->setChemin($element['chemin'])
->setIptc($element['iptc'])
->setDate($element['date']);
$em-> flush();
}
// Les Nouveautés
foreach ($nvlle_liste as $element){
$photo = new Photo();
$photo ->setNom($element['nom'])
->setTaille($element['taille'])
->setChemin($element['chemin'])
->setIptc($element['iptc'])
->setDate($element['date']);
$repository = $this ->getContainer()->get('doctrine')
->getManager();
$repository ->persist($photo);
$repository ->flush();
}
// LOG Fin et fermeture (web/img/logs)
file_put_contents($log, 'FIN', FILE_APPEND);
// Log Fin (app/logs)
sleep(10);
$logger->info($log);
$logger->info('fin de l execution de la commande');
}
} |
Partager