Actualiser api.php

This commit is contained in:
2026-06-24 10:25:04 +02:00
parent b50daabd4f
commit 4bf47c8bda
+58 -168
View File
@@ -452,17 +452,18 @@ switch ($action) {
else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); } else { http_response_code(400); echo json_encode(["success" => false, "error" => "Aucun élément sélectionné."]); }
break; break;
// ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ── // ── IMPORT PAR LOTS CSV (CROISEMENT UPC + TMDB) ──
case 'import_batch': case 'import_batch':
checkAuth($pdo); checkAuth($pdo);
set_time_limit(0); // 🔥 EMPÊCHE PHP DE TUER LE SCRIPT APRÈS 30 SECONDES set_time_limit(0); // Empêche le timeout PHP
$items = $data['items'] ?? []; $items = $data['items'] ?? [];
$type = $data['type'] ?? 'videotheque'; $type = $data['type'] ?? 'videotheque';
$tmdbApiKey = getTmdbApiKey($pdo); $tmdbApiKey = getTmdbApiKey($pdo);
$imported = 0; $imported = 0;
try {
$pdo->beginTransaction(); // 🔥 UNE SEULE transaction
// ── PHASE 1 : Enrichissement HTTP (HORS transaction pour éviter les timeouts) ──
$enriched = [];
foreach ($items as $rowData) { foreach ($items as $rowData) {
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre'; $title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? ''; $publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
@@ -470,32 +471,29 @@ switch ($action) {
$id = makeStableId($type, $title, $year); $id = makeStableId($type, $title, $year);
if ($type === 'videotheque') { if ($type === 'videotheque') {
// Normalisation EAN // Normalisation des données
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? ''; $ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
if (!empty($ean)) { if (!empty($ean)) {
$eanFloat = floatval($ean); $eanFloat = floatval($ean);
if ($eanFloat > 0) $ean = (string) round($eanFloat); if ($eanFloat > 0) $ean = (string) round($eanFloat);
$ean = preg_replace('/[^0-9]/', '', $ean); $ean = preg_replace('/[^0-9]/', '', $ean);
} }
// Normalisation length
$lengthRaw = $rowData['length'] ?? ''; $lengthRaw = $rowData['length'] ?? '';
$length = ''; $length = '';
if ($lengthRaw !== '' && $lengthRaw !== null) { if ($lengthRaw !== '' && $lengthRaw !== null) {
$lengthVal = floatval($lengthRaw); $lengthVal = floatval($lengthRaw);
if ($lengthVal > 0) $length = (string) round($lengthVal); if ($lengthVal > 0) $length = (string) round($lengthVal);
} }
// Normalisation number_of_discs
$discsRaw = $rowData['number_of_discs'] ?? ''; $discsRaw = $rowData['number_of_discs'] ?? '';
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1; $discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
$description = $rowData['description'] ?? $rowData['Description'] ?? ''; $description = $rowData['description'] ?? $rowData['Description'] ?? '';
$publisher = $rowData['publisher'] ?? ''; $publisher = $rowData['publisher'] ?? '';
$aspect = $rowData['aspect_ratio'] ?? ''; $aspect = $rowData['aspect_ratio'] ?? '';
$format = $rowData['format'] ?? detectFormat($title, $description); $format = $rowData['format'] ?? detectFormat($title, $description);
$poster = $rowData['poster'] ?? ''; $poster = $rowData['poster'] ?? '';
$director = ''; $director = '';
// Acteurs depuis CSV
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? ''; $csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = ''; $actors = '';
if (!empty($csvActors)) { if (!empty($csvActors)) {
@@ -512,7 +510,7 @@ switch ($action) {
if (empty($publisher)) $publisher = $upcData['publisher']; if (empty($publisher)) $publisher = $upcData['publisher'];
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format']; if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
} }
// 1.5 DVDFr : affiche FR + métadonnées (priorité sur UPCitemdb) // 1.5 DVDFr
$dvdfrData = fetchDVDFr($ean, $pdo); $dvdfrData = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrData)) { if (!empty($dvdfrData)) {
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster']; if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
@@ -528,8 +526,7 @@ switch ($action) {
if ($tmdbApiKey && !empty($title)) { if ($tmdbApiKey && !empty($title)) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo); $tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
if (!$tmdbData || empty($tmdbData['overview'])) { if (!$tmdbData || empty($tmdbData['overview'])) {
$cleanTitle = $title; $cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $title);
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0]; $cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
$cleanTitle = explode(' - ', $cleanTitle)[0]; $cleanTitle = explode(' - ', $cleanTitle)[0];
$cleanTitle = trim($cleanTitle); $cleanTitle = trim($cleanTitle);
@@ -541,7 +538,6 @@ switch ($action) {
if ($tmdbData) { if ($tmdbData) {
if (!empty($tmdbData['title'])) $title = $tmdbData['title']; if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
if (empty($director)) $director = $tmdbData['director'] ?? ''; if (empty($director)) $director = $tmdbData['director'] ?? '';
elseif (!empty($tmdbData['director']) && strpos($director, ',') === false && strpos($tmdbData['director'], ',') !== false) $director = $tmdbData['director'];
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year']; if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length']; if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview']; if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
@@ -549,10 +545,25 @@ switch ($action) {
} }
} }
$enriched[] = compact('id','title','year','director','poster','format','length','publisher','ean','discs','aspect','description','actors'); // INSERT SQL
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year),
director=IF(VALUES(director)!='',VALUES(director),director),
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
format=IF(VALUES(format)!='',VALUES(format),format),
length=IF(VALUES(length)!='',VALUES(length),length),
publisher=IF(VALUES(publisher)!='',VALUES(publisher),publisher),
ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13),
number_of_discs=IF(VALUES(number_of_discs)!=1,VALUES(number_of_discs),number_of_discs),
aspect_ratio=IF(VALUES(aspect_ratio)!='',VALUES(aspect_ratio),aspect_ratio),
description=IF(VALUES(description)!='',VALUES(description),description),
actors=IF(VALUES(actors)!='',VALUES(actors),actors)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
} else { } else {
// Critiques // Pour les critiques
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? ''; $ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null; $rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? ''; $review = $rowData['Review'] ?? $rowData['review'] ?? '';
@@ -563,155 +574,34 @@ switch ($action) {
$director = $tmdbData['director']; $director = $tmdbData['director'];
$poster = $tmdbData['poster']; $poster = $tmdbData['poster'];
$streaming = $tmdbData['streaming']; $streaming = $tmdbData['streaming'];
if (empty($year)) $year = $tmdbData['year']; if(empty($year)) $year = $tmdbData['year'];
} }
} }
if (empty($streaming)) $streaming = 'Support physique / Cinéma'; if (empty($streaming)) $streaming = 'Support physique / Cinéma';
$enriched[] = compact('id','title','year','director','poster','rating','review','streaming');
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year),
rating=VALUES(rating),
review=IF(VALUES(review)!='',VALUES(review),review),
director=IF(VALUES(director)!='',VALUES(director),director),
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
streaming=IF(VALUES(streaming)!='',VALUES(streaming),streaming)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
} }
$imported++;
} }
$pdo->commit();
$pdo->beginTransaction();
$pdo->beginTransaction();
try {
foreach ($items as $rowData) {
$title = $rowData['title'] ?? $rowData['Name'] ?? $rowData['Title'] ?? 'Sans titre';
$publishDate = $rowData['publish_date'] ?? $rowData['Year'] ?? $rowData['year'] ?? $rowData['Date'] ?? '';
$year = extractYear($publishDate);
$id = makeStableId($type, $title, $year);
if ($type === 'videotheque') {
$director = '';
$ean = $rowData['ean_isbn13'] ?? $rowData['EAN'] ?? '';
if (!empty($ean)) {
$eanFloat = floatval($ean);
if ($eanFloat > 0) $ean = (string) round($eanFloat);
$ean = preg_replace('/[^0-9]/', '', $ean);
}
$description = $rowData['description'] ?? $rowData['Description'] ?? '';
$publisher = $rowData['publisher'] ?? '';
$lengthRaw = $rowData['length'] ?? '';
$length = '';
if ($lengthRaw !== '' && $lengthRaw !== null) {
$lengthVal = floatval($lengthRaw);
if ($lengthVal > 0) $length = (string) round($lengthVal);
}
$discsRaw = $rowData['number_of_discs'] ?? '';
$discs = (is_numeric($discsRaw) && floatval($discsRaw) > 0) ? (int) round(floatval($discsRaw)) : 1;
$aspect = $rowData['aspect_ratio'] ?? '';
$format = $rowData['format'] ?? detectFormat($title, $description);
$poster = $rowData['poster'] ?? '';
$csvActors = $rowData['ensemble'] ?? $rowData['creators'] ?? '';
$actors = '';
if (!empty($csvActors)) {
$actorsArray = array_map('trim', explode(',', $csvActors));
$actors = implode(', ', array_slice($actorsArray, 0, 4));
}
if (!empty($ean)) {
$upcData = fetchUPCitemdb($ean, $pdo);
if ($upcData) {
if (empty($poster) && !empty($upcData['poster'])) $poster = $upcData['poster'];
if ($title === 'Sans titre') $title = $upcData['title'];
if (empty($publisher)) $publisher = $upcData['publisher'];
if (empty($format) || $format === 'Blu-ray') $format = $upcData['format'];
}
$dvdfrData = fetchDVDFr($ean, $pdo);
if (!empty($dvdfrData)) {
if (!empty($dvdfrData['poster'])) $poster = $dvdfrData['poster'];
if (!empty($dvdfrData['publisher'])) $publisher = $dvdfrData['publisher'];
if (!empty($dvdfrData['format']) && (empty($format) || $format === 'Blu-ray')) $format = $dvdfrData['format'];
if (!empty($dvdfrData['length']) && empty($length)) $length = $dvdfrData['length'];
if (!empty($dvdfrData['aspect']) && empty($aspect)) $aspect = $dvdfrData['aspect'];
if (!empty($dvdfrData['discs']) && $discs === 1) $discs = (int)$dvdfrData['discs'];
}
}
if ($tmdbApiKey && !empty($title)) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
if (!$tmdbData || empty($tmdbData['overview'])) {
$cleanTitle = $title;
// 🔥 CORRECTION BUG FATAL : preg_ireplace n'existe pas, on utilise str_ireplace
$cleanTitle = str_ireplace(['coffret ', 'l\'intégrale ', 'intégrale ', 'trilogie ', 'quadrilogie ', 'collection '], '', $cleanTitle);
$cleanTitle = preg_split('/(\/|\+)/', $cleanTitle)[0];
$cleanTitle = explode(' - ', $cleanTitle)[0];
$cleanTitle = trim($cleanTitle);
if (!empty($cleanTitle) && $cleanTitle !== $title) {
$tmdbFallback = fetchTMDBFull($cleanTitle, '', $tmdbApiKey, $pdo);
if ($tmdbFallback && !empty($tmdbFallback['overview'])) $tmdbData = $tmdbFallback;
}
}
if ($tmdbData) {
if (!empty($tmdbData['title'])) $title = $tmdbData['title'];
if (empty($director)) $director = $tmdbData['director'] ?? '';
if (empty($year) && !empty($tmdbData['year'])) $year = $tmdbData['year'];
if (empty($length) && !empty($tmdbData['length'])) $length = $tmdbData['length'];
if (!empty($tmdbData['overview'])) $description = $tmdbData['overview'];
if (!empty($tmdbData['cast'])) $actors = implode(', ', $tmdbData['cast']);
}
}
// 🔥 SQL MIS À JOUR (Ajout de title et year dans le UPDATE)
$sql = "INSERT INTO videotheque (id, title, year, director, poster, format, length, publisher, ean_isbn13, number_of_discs, aspect_ratio, description, actors)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year),
director=IF(VALUES(director)!='',VALUES(director),director),
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
format=IF(VALUES(format)!='',VALUES(format),format),
length=IF(VALUES(length)!='',VALUES(length),length),
publisher=IF(VALUES(publisher)!='',VALUES(publisher),publisher),
ean_isbn13=IF(VALUES(ean_isbn13)!='',VALUES(ean_isbn13),ean_isbn13),
number_of_discs=IF(VALUES(number_of_discs)!=1,VALUES(number_of_discs),number_of_discs),
aspect_ratio=IF(VALUES(aspect_ratio)!='',VALUES(aspect_ratio),aspect_ratio),
description=IF(VALUES(description)!='',VALUES(description),description),
actors=IF(VALUES(actors)!='',VALUES(actors),actors)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $format, $length, $publisher, $ean, $discs, $aspect, $description, $actors]);
} else {
// Pour les critiques
$ratingRaw = $rowData['Rating'] ?? $rowData['rating'] ?? '';
$rating = ($ratingRaw !== '' && $ratingRaw !== null) ? (float)$ratingRaw : null;
$review = $rowData['Review'] ?? $rowData['review'] ?? '';
$director = ''; $poster = ''; $streaming = '';
if ($tmdbApiKey && !empty($title)) {
$tmdbData = fetchTMDBFull($title, $year, $tmdbApiKey, $pdo);
if ($tmdbData) {
$director = $tmdbData['director'];
$poster = $tmdbData['poster'];
$streaming = $tmdbData['streaming'];
if(empty($year)) $year = $tmdbData['year'];
}
}
if (empty($streaming)) $streaming = 'Support physique / Cinéma';
$sql = "INSERT INTO critiques (id, title, year, director, poster, rating, review, streaming)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
ON DUPLICATE KEY UPDATE
title=VALUES(title), year=VALUES(year),
rating=VALUES(rating),
review=IF(VALUES(review)!='',VALUES(review),review),
director=IF(VALUES(director)!='',VALUES(director),director),
poster=IF(VALUES(poster)!='',VALUES(poster),poster),
streaming=IF(VALUES(streaming)!='',VALUES(streaming),streaming)";
$stmt = $pdo->prepare($sql);
$stmt->execute([$id, $title, $year, $director, $poster, $rating, $review, $streaming]);
}
$imported++;
}
$pdo->commit();
echo json_encode(["success" => true, "imported" => $imported]);
} catch (\Throwable $e) {
// 🔥 FILET DE SÉCURITÉ : Si une erreur fatale PHP survient, on annule tout proprement
$pdo->rollBack();
http_response_code(500);
// On renvoie l'erreur en JSON pour que le JavaScript puisse l'afficher proprement
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]);
}
echo json_encode(["success" => true, "imported" => $imported]); echo json_encode(["success" => true, "imported" => $imported]);
break; } catch (\Throwable $e) {
// 🔥 FILET DE SÉCURITÉ : Annule tout et renvoie l'erreur en JSON (plus de HTML)
if ($pdo->inTransaction()) {
$pdo->rollBack();
}
http_response_code(500);
echo json_encode(["success" => false, "error" => "Erreur serveur : " . $e->getMessage()]);
}
break;
} }