256 lines
7.4 KiB
PHP
256 lines
7.4 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
/**
|
|
* Importiert hyg_v42.csv in die Tabelle star_hipparcos
|
|
*
|
|
* Erwartet:
|
|
* - config/database.php
|
|
* - CSV-Datei im selben Verzeichnis wie dieses Skript
|
|
*/
|
|
|
|
ini_set('display_errors', '1');
|
|
error_reporting(E_ALL);
|
|
date_default_timezone_set('UTC');
|
|
|
|
$config = require __DIR__ . '/../../config/database.php';
|
|
|
|
$csvFile = __DIR__ . '/hyg_v42.csv';
|
|
|
|
$dsn = sprintf(
|
|
'mysql:host=%s;dbname=%s;charset=%s',
|
|
$config['host'],
|
|
$config['dbname'],
|
|
$config['charset'] ?? 'utf8mb4'
|
|
);
|
|
|
|
$pdo = new PDO(
|
|
$dsn,
|
|
$config['user'],
|
|
$config['pass'],
|
|
[
|
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
]
|
|
);
|
|
|
|
function nullIfEmpty(mixed $value): mixed
|
|
{
|
|
if ($value === null) {
|
|
return null;
|
|
}
|
|
|
|
if (is_string($value)) {
|
|
$value = trim($value);
|
|
return $value === '' ? null : $value;
|
|
}
|
|
|
|
return $value;
|
|
}
|
|
|
|
function toNullableInt(mixed $value): ?int
|
|
{
|
|
$value = nullIfEmpty($value);
|
|
if ($value === null || !is_numeric((string)$value)) {
|
|
return null;
|
|
}
|
|
return (int)$value;
|
|
}
|
|
|
|
function toNullableFloat(mixed $value): ?float
|
|
{
|
|
$value = nullIfEmpty($value);
|
|
if ($value === null || !is_numeric((string)$value)) {
|
|
return null;
|
|
}
|
|
return (float)$value;
|
|
}
|
|
|
|
function toNullableString(mixed $value): ?string
|
|
{
|
|
$value = nullIfEmpty($value);
|
|
return $value === null ? null : (string)$value;
|
|
}
|
|
|
|
if (!is_file($csvFile)) {
|
|
exit("CSV-Datei nicht gefunden: {$csvFile}\n");
|
|
}
|
|
|
|
$handle = fopen($csvFile, 'r');
|
|
if ($handle === false) {
|
|
exit("CSV-Datei konnte nicht geöffnet werden: {$csvFile}\n");
|
|
}
|
|
|
|
/*
|
|
* escape explizit setzen, damit keine PHP-8.4+-Deprecation erscheint
|
|
*/
|
|
$header = fgetcsv($handle, 0, ',', '"', '');
|
|
if ($header === false) {
|
|
fclose($handle);
|
|
exit("CSV-Datei ist leer oder ungültig.\n");
|
|
}
|
|
|
|
$header = array_map(static fn($v) => trim((string)$v), $header);
|
|
|
|
$requiredColumns = ['hip', 'ra', 'dec', 'mag', 'con'];
|
|
foreach ($requiredColumns as $col) {
|
|
if (!in_array($col, $header, true)) {
|
|
fclose($handle);
|
|
exit("Pflichtspalte fehlt in CSV: {$col}\n");
|
|
}
|
|
}
|
|
|
|
$sql = "
|
|
INSERT INTO `star_hipparcos` (
|
|
`hip`, `hd`, `hr`, `gl`, `bf`, `proper`,
|
|
`ra`, `dec`, `dist`, `pmra`, `pmdec`, `rv`,
|
|
`mag`, `absmag`, `spect`, `ci`,
|
|
`x`, `y`, `z`, `vx`, `vy`, `vz`,
|
|
`rarad`, `decrad`, `pmrarad`, `pmdecrad`,
|
|
`bayer`, `flam`, `con`, `comp`, `comp_primary`,
|
|
`base`, `lum`, `var`, `var_min`, `var_max`,
|
|
`created_at`, `updated_at`
|
|
) VALUES (
|
|
:hip, :hd, :hr, :gl, :bf, :proper,
|
|
:ra, :dec, :dist, :pmra, :pmdec, :rv,
|
|
:mag, :absmag, :spect, :ci,
|
|
:x, :y, :z, :vx, :vy, :vz,
|
|
:rarad, :decrad, :pmrarad, :pmdecrad,
|
|
:bayer, :flam, :con, :comp, :comp_primary,
|
|
:base, :lum, :var, :var_min, :var_max,
|
|
NOW(), NOW()
|
|
)
|
|
ON DUPLICATE KEY UPDATE
|
|
`hd` = VALUES(`hd`),
|
|
`hr` = VALUES(`hr`),
|
|
`gl` = VALUES(`gl`),
|
|
`bf` = VALUES(`bf`),
|
|
`proper` = VALUES(`proper`),
|
|
`ra` = VALUES(`ra`),
|
|
`dec` = VALUES(`dec`),
|
|
`dist` = VALUES(`dist`),
|
|
`pmra` = VALUES(`pmra`),
|
|
`pmdec` = VALUES(`pmdec`),
|
|
`rv` = VALUES(`rv`),
|
|
`mag` = VALUES(`mag`),
|
|
`absmag` = VALUES(`absmag`),
|
|
`spect` = VALUES(`spect`),
|
|
`ci` = VALUES(`ci`),
|
|
`x` = VALUES(`x`),
|
|
`y` = VALUES(`y`),
|
|
`z` = VALUES(`z`),
|
|
`vx` = VALUES(`vx`),
|
|
`vy` = VALUES(`vy`),
|
|
`vz` = VALUES(`vz`),
|
|
`rarad` = VALUES(`rarad`),
|
|
`decrad` = VALUES(`decrad`),
|
|
`pmrarad` = VALUES(`pmrarad`),
|
|
`pmdecrad` = VALUES(`pmdecrad`),
|
|
`bayer` = VALUES(`bayer`),
|
|
`flam` = VALUES(`flam`),
|
|
`con` = VALUES(`con`),
|
|
`comp` = VALUES(`comp`),
|
|
`comp_primary` = VALUES(`comp_primary`),
|
|
`base` = VALUES(`base`),
|
|
`lum` = VALUES(`lum`),
|
|
`var` = VALUES(`var`),
|
|
`var_min` = VALUES(`var_min`),
|
|
`var_max` = VALUES(`var_max`),
|
|
`updated_at` = NOW()
|
|
";
|
|
|
|
$stmt = $pdo->prepare($sql);
|
|
|
|
$rowCount = 0;
|
|
$insertedOrUpdated = 0;
|
|
$skipped = 0;
|
|
|
|
$pdo->beginTransaction();
|
|
|
|
try {
|
|
while (($row = fgetcsv($handle, 0, ',', '"', '')) !== false) {
|
|
$rowCount++;
|
|
|
|
if ($row === [null] || $row === false) {
|
|
continue;
|
|
}
|
|
|
|
if (count($row) !== count($header)) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
$data = array_combine($header, $row);
|
|
if ($data === false) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
$hip = toNullableInt($data['hip'] ?? null);
|
|
|
|
if ($hip === null) {
|
|
$skipped++;
|
|
continue;
|
|
}
|
|
|
|
$stmt->execute([
|
|
':hip' => $hip,
|
|
':hd' => toNullableInt($data['hd'] ?? null),
|
|
':hr' => toNullableInt($data['hr'] ?? null),
|
|
':gl' => toNullableString($data['gl'] ?? null),
|
|
':bf' => toNullableString($data['bf'] ?? null),
|
|
':proper' => toNullableString($data['proper'] ?? null),
|
|
|
|
':ra' => toNullableFloat($data['ra'] ?? null),
|
|
':dec' => toNullableFloat($data['dec'] ?? null),
|
|
':dist' => toNullableFloat($data['dist'] ?? null),
|
|
':pmra' => toNullableFloat($data['pmra'] ?? null),
|
|
':pmdec' => toNullableFloat($data['pmdec'] ?? null),
|
|
':rv' => toNullableFloat($data['rv'] ?? null),
|
|
|
|
':mag' => toNullableFloat($data['mag'] ?? null),
|
|
':absmag' => toNullableFloat($data['absmag'] ?? null),
|
|
':spect' => toNullableString($data['spect'] ?? null),
|
|
':ci' => toNullableFloat($data['ci'] ?? null),
|
|
|
|
':x' => toNullableFloat($data['x'] ?? null),
|
|
':y' => toNullableFloat($data['y'] ?? null),
|
|
':z' => toNullableFloat($data['z'] ?? null),
|
|
':vx' => toNullableFloat($data['vx'] ?? null),
|
|
':vy' => toNullableFloat($data['vy'] ?? null),
|
|
':vz' => toNullableFloat($data['vz'] ?? null),
|
|
|
|
':rarad' => toNullableFloat($data['rarad'] ?? null),
|
|
':decrad' => toNullableFloat($data['decrad'] ?? null),
|
|
':pmrarad' => toNullableFloat($data['pmrarad'] ?? null),
|
|
':pmdecrad' => toNullableFloat($data['pmdecrad'] ?? null),
|
|
|
|
':bayer' => toNullableString($data['bayer'] ?? null),
|
|
':flam' => toNullableString($data['flam'] ?? null),
|
|
':con' => toNullableString($data['con'] ?? null),
|
|
':comp' => toNullableInt($data['comp'] ?? null),
|
|
':comp_primary' => toNullableInt($data['comp_primary'] ?? null),
|
|
|
|
':base' => toNullableString($data['base'] ?? null),
|
|
':lum' => toNullableFloat($data['lum'] ?? null),
|
|
':var' => toNullableString($data['var'] ?? null),
|
|
':var_min' => toNullableFloat($data['var_min'] ?? null),
|
|
':var_max' => toNullableFloat($data['var_max'] ?? null),
|
|
]);
|
|
|
|
$insertedOrUpdated++;
|
|
}
|
|
|
|
$pdo->commit();
|
|
} catch (Throwable $e) {
|
|
$pdo->rollBack();
|
|
fclose($handle);
|
|
exit("Fehler beim Import: " . $e->getMessage() . "\n");
|
|
}
|
|
|
|
fclose($handle);
|
|
|
|
echo "HYG-Import abgeschlossen\n";
|
|
echo "Verarbeitete Zeilen: {$rowCount}\n";
|
|
echo "Importiert/Aktualisiert: {$insertedOrUpdated}\n";
|
|
echo "Übersprungen: {$skipped}\n"; |