Files
ecoles/import_comc.php
T
Arnaud 5633969d98 Version initiale — carte des établissements scolaires
Carte Leaflet (annuaire Éducation nationale) + API PHP/MySQL :
fiche établissement (effectifs, IPS, population INSEE, DPE ADEME),
GED documents/photos, commentaires par groupe, gestion utilisateurs
et politique de mots de passe. Imports opendata (effectifs, IPS,
population, DPE, IRIS).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
2026-06-03 21:35:41 +02:00

79 lines
2.7 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
/**
* Ajoute / met à jour le code INSEE commune (comc) des établissements,
* depuis l'opendata annuaire. Nécessaire pour relier les écoles à la
* population INSEE (indexée par code commune) — cf. taux de scolarisation.
*
* Usage : php import_comc.php
*/
declare(strict_types=1);
require __DIR__ . '/api/db.php';
const ANNUAIRE_COMC =
'https://data.education.gouv.fr/api/explore/v2.1/catalog/datasets/fr-en-annuaire-education/'
. 'exports/csv?select=identifiant_de_l_etablissement,code_commune&delimiter=%3B';
function say(string $m): void { echo $m . "\n"; flush(); }
try {
$pdo = db();
try {
$pdo->exec('ALTER TABLE etablissements ADD COLUMN comc VARCHAR(5) NULL');
$pdo->exec('ALTER TABLE etablissements ADD INDEX idx_comc (comc)');
say('✔ Colonne comc ajoutée.');
} catch (Throwable $e) {
say(' Colonne comc déjà présente.');
}
$tmp = __DIR__ . '/data/insee/annuaire_comc.csv';
@mkdir(dirname($tmp), 0777, true);
say('… Téléchargement (identifiant + code_commune) …');
$fp = fopen($tmp, 'w');
$ch = curl_init(ANNUAIRE_COMC);
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($tmp, 'r');
$header = fgetcsv($fh, 0, ';');
$header[0] = preg_replace('/^\xEF\xBB\xBF/', '', (string) $header[0]);
$iId = array_search('identifiant_de_l_etablissement', $header, true);
$iCom = array_search('code_commune', $header, true);
if ($iId === false || $iCom === false) {
throw new RuntimeException('Colonnes attendues introuvables.');
}
$upd = $pdo->prepare('UPDATE etablissements SET comc = ? WHERE id = ?');
$pdo->beginTransaction();
$n = 0;
while (($r = fgetcsv($fh, 0, ';')) !== false) {
$id = $r[$iId] ?? '';
$com = $r[$iCom] ?? '';
if ($id === '' || $com === '') {
continue;
}
$upd->execute([$com, $id]);
if (++$n % 5000 === 0) { $pdo->commit(); $pdo->beginTransaction(); }
}
$pdo->commit();
fclose($fh);
@unlink($tmp);
$filled = (int) $pdo->query("SELECT COUNT(*) FROM etablissements WHERE comc IS NOT NULL AND comc <> ''")->fetchColumn();
say("✔ comc renseigné pour $filled établissements (sur $n lignes).");
say('🎉 Terminé.');
} catch (Throwable $e) {
if (isset($pdo) && $pdo->inTransaction()) {
$pdo->rollBack();
}
http_response_code(500);
say('!! ERREUR : ' . $e->getMessage());
exit(1);
}