exec( 'CREATE TABLE IF NOT EXISTS pop_commune ( comc VARCHAR(5) NOT NULL, annee SMALLINT NOT NULL, pop INT NOT NULL DEFAULT 0, PRIMARY KEY (comc, annee), INDEX idx_comc (comc) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4' ); $csv = __DIR__ . '/data/insee/pop_hist_communes.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(POP_HIST_CSV); curl_setopt_array($ch, [ CURLOPT_FILE => $fp, CURLOPT_FOLLOWLOCATION => true, CURLOPT_TIMEOUT => 600, CURLOPT_USERAGENT => 'ecoles-import/1.0', ]); $ok = curl_exec($ch); $err = curl_error($ch); curl_close($ch); fclose($fp); if (!$ok) { @unlink($csv); throw new RuntimeException('Téléchargement échoué : ' . $err); } } $fh = fopen($csv, 'r'); $header = fgetcsv($fh, 0, ';'); $header[0] = preg_replace('/^\xEF\xBB\xBF/', '', (string) $header[0]); // retire un éventuel BOM $iC = array_search('code_insee', $header, true); $iY = array_search('annee', $header, true); $iP = array_search('population_municipale', $header, true); if ($iC === false || $iY === false || $iP === false) { throw new RuntimeException('Colonnes code_insee / annee / population_municipale introuvables.'); } $ins = $pdo->prepare('REPLACE INTO pop_commune (comc, annee, pop) VALUES (?, ?, ?)'); $pdo->beginTransaction(); $n = 0; while (($row = fgetcsv($fh, 0, ';')) !== false) { $comc = trim((string) ($row[$iC] ?? '')); $annee = (int) ($row[$iY] ?? 0); $pop = (int) round((float) str_replace([' ', ','], ['', '.'], (string) ($row[$iP] ?? '0'))); if ($comc === '' || $annee === 0) { continue; } $ins->execute([$comc, $annee, $pop]); if (++$n % 10000 === 0) { $pdo->commit(); $pdo->beginTransaction(); if ($n % 100000 === 0) say(" … $n lignes"); } } $pdo->commit(); fclose($fh); $communes = (int) $pdo->query('SELECT COUNT(DISTINCT comc) FROM pop_commune')->fetchColumn(); say("✔ pop_commune : $n lignes importées ($communes communes)."); say('🎉 Terminé.'); } catch (Throwable $e) { if (isset($pdo) && $pdo->inTransaction()) { $pdo->rollBack(); } http_response_code(500); say('!! ERREUR : ' . $e->getMessage()); exit(1); }