exec( 'CREATE TABLE IF NOT EXISTS effectifs ( uai VARCHAR(12) NOT NULL, annee SMALLINT NOT NULL, eleves INT NOT NULL DEFAULT 0, classes INT NOT NULL DEFAULT 0, PRIMARY KEY (uai, annee), INDEX idx_uai (uai) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4' ); // Télécharge le CSV dans un fichier temporaire si absent localement. $csv = __DIR__ . '/data/insee/effectifs.csv'; if (!is_file($csv)) { @mkdir(dirname($csv), 0777, true); say('… Téléchargement de l’export CSV …'); $fp = fopen($csv, 'w'); $ch = curl_init(EFFECTIFS_CSV); curl_setopt_array($ch, [ CURLOPT_FILE => $fp, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 600, CURLOPT_USERAGENT => 'ecoles-import/1.0', ]); if (!curl_exec($ch)) { throw new RuntimeException('Téléchargement échoué : ' . curl_error($ch)); } curl_close($ch); fclose($fp); } $fh = fopen($csv, 'r'); $header = fgetcsv($fh, 0, ';'); $header[0] = preg_replace('/^\xEF\xBB\xBF/', '', (string) $header[0]); // retire un éventuel BOM $iY = array_search('rentree_scolaire', $header, true); $iU = array_search('numero_ecole', $header, true); $iE = array_search('nombre_total_eleves', $header, true); $iC = array_search('nombre_total_classes', $header, true); if (in_array(false, [$iY, $iU, $iE, $iC], true)) { throw new RuntimeException('Colonnes attendues introuvables dans le CSV.'); } $ins = $pdo->prepare('REPLACE INTO effectifs (uai, annee, eleves, classes) VALUES (?, ?, ?, ?)'); $pdo->beginTransaction(); $n = 0; $num = static fn($v) => (int) round((float) str_replace(',', '.', (string) $v)); while (($row = fgetcsv($fh, 0, ';')) !== false) { $uai = $row[$iU] ?? ''; $annee = (int) ($row[$iY] ?? 0); if ($uai === '' || $annee === 0) { continue; } $ins->execute([$uai, $annee, $num($row[$iE] ?? 0), $num($row[$iC] ?? 0)]); if (++$n % 5000 === 0) { $pdo->commit(); $pdo->beginTransaction(); if ($n % 100000 === 0) say(" … $n lignes"); } } $pdo->commit(); fclose($fh); $ecoles = (int) $pdo->query('SELECT COUNT(DISTINCT uai) FROM effectifs')->fetchColumn(); say("✔ effectifs : $n lignes importées ($ecoles écoles)."); say('🎉 Terminé.'); } catch (Throwable $e) { if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack(); } http_response_code(500); say('!! ERREUR : ' . $e->getMessage()); exit(1); }