PDO::ERRMODE_EXCEPTION, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, ] ); $recordsReceived = 0; $recordsImported = 0; $recordsSkipped = 0; try { rebuildCometsTable($pdo); [$lines, $resolvedSource] = resolveSourceLines($sourceUrl, $localSourcePath); $statement = $pdo->prepare( 'INSERT INTO `comets_mpc` ( `designation_packed`, `orbit_type`, `year_of_perihelion`, `month_of_perihelion`, `day_of_perihelion`, `perihelion_dist_au`, `eccentricity`, `arg_perihelion_deg`, `ascending_node_deg`, `inclination_deg`, `epoch_compact`, `epoch_date`, `absolute_magnitude_h`, `slope_parameter_g`, `designation_and_name`, `reference_text`, `raw_record`, `source_url`, `imported_at`, `created_at`, `updated_at` ) VALUES ( :designation_packed, :orbit_type, :year_of_perihelion, :month_of_perihelion, :day_of_perihelion, :perihelion_dist_au, :eccentricity, :arg_perihelion_deg, :ascending_node_deg, :inclination_deg, :epoch_compact, :epoch_date, :absolute_magnitude_h, :slope_parameter_g, :designation_and_name, :reference_text, :raw_record, :source_url, NOW(), NOW(), NOW() )' ); $pdo->beginTransaction(); foreach ($lines as $lineNumber => $line) { $trimmedLine = rtrim($line, "\r\n"); if (trim($trimmedLine) === '') { continue; } $recordsReceived++; try { $record = parseSoft00CmtLine($trimmedLine); $record[':source_url'] = $resolvedSource; $statement->execute($record); $recordsImported++; } catch (Throwable $rowError) { $recordsSkipped++; echo 'Uebersprungen Zeile ' . ($lineNumber + 1) . ': ' . $rowError->getMessage() . "\n"; } } $pdo->commit(); echo "Import erfolgreich\n"; echo "Quelle: {$resolvedSource}\n"; echo "Empfangen: {$recordsReceived}\n"; echo "Importiert: {$recordsImported}\n"; echo "Uebersprungen: {$recordsSkipped}\n"; } catch (Throwable $e) { if ($pdo->inTransaction()) { $pdo->rollBack(); } writeError('Fehler: ' . $e->getMessage()); exit(1); } function writeError(string $message): void { if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') { fwrite(STDERR, $message . PHP_EOL); return; } echo htmlspecialchars($message, ENT_QUOTES, 'UTF-8') . "
\n"; } function fetchRemoteLines(string $url): array { if (function_exists('curl_init')) { return fetchRemoteLinesViaCurl($url); } try { return fetchRemoteLinesViaStream($url); } catch (Throwable $streamError) { return fetchRemoteLinesViaSocket($url, $streamError->getMessage()); } } function resolveSourceLines(string $remoteUrl, ?string $localSourcePath = null): array { if ($localSourcePath !== null && trim($localSourcePath) !== '') { $lines = fetchLocalLines($localSourcePath); return [$lines, realpath($localSourcePath) ?: $localSourcePath]; } try { $lines = fetchRemoteLines($remoteUrl); return [$lines, $remoteUrl]; } catch (Throwable $remoteError) { $defaultLocalPath = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Soft00Cmt.txt'; if (is_file($defaultLocalPath)) { $lines = fetchLocalLines($defaultLocalPath); return [$lines, realpath($defaultLocalPath) ?: $defaultLocalPath]; } throw new RuntimeException( $remoteError->getMessage() . ' | Lege alternativ eine lokale Datei unter scripts/import/Soft00Cmt.txt ab ' . 'oder uebergib ihren Pfad als Argument bzw. ?source=' ); } } function fetchLocalLines(string $path): array { if (!is_file($path)) { throw new RuntimeException('Lokale Datei nicht gefunden: ' . $path); } $contents = @file_get_contents($path); if ($contents === false) { $error = error_get_last(); throw new RuntimeException('Lokale Datei konnte nicht gelesen werden: ' . ($error['message'] ?? $path)); } return preg_split("/\r\n|\n|\r/", $contents) ?: []; } function fetchRemoteLinesViaCurl(string $url): array { $ch = curl_init($url); curl_setopt_array($ch, [ CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => true, CURLOPT_CONNECTTIMEOUT => 20, CURLOPT_TIMEOUT => 300, CURLOPT_USERAGENT => 'AstroMINT MPC Comet Importer/2.0', CURLOPT_HTTPHEADER => [ 'Accept: text/plain', ], ]); $response = curl_exec($ch); if ($response === false) { throw new RuntimeException('Download fehlgeschlagen: ' . curl_error($ch)); } $httpCode = (int) curl_getinfo($ch, CURLINFO_HTTP_CODE); if ($httpCode !== 200) { throw new RuntimeException('HTTP-Fehler beim Abruf: ' . $httpCode); } return preg_split("/\r\n|\n|\r/", $response) ?: []; } function fetchRemoteLinesViaStream(string $url): array { $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'timeout' => 300, 'ignore_errors' => true, 'header' => implode("\r\n", [ 'User-Agent: AstroMINT MPC Comet Importer/2.0', 'Accept: text/plain', ]), ], 'ssl' => [ 'verify_peer' => true, 'verify_peer_name' => true, ], ]); $response = @file_get_contents($url, false, $context); if ($response === false) { $error = error_get_last(); throw new RuntimeException('Download fehlgeschlagen: ' . ($error['message'] ?? 'unbekannter Fehler')); } $statusLine = null; foreach (($http_response_header ?? []) as $headerLine) { if (stripos($headerLine, 'HTTP/') === 0) { $statusLine = $headerLine; break; } } if ($statusLine !== null && preg_match('/\s(\d{3})\s/', $statusLine, $matches) === 1) { $httpCode = (int) $matches[1]; if ($httpCode !== 200) { throw new RuntimeException('HTTP-Fehler beim Abruf: ' . $httpCode); } } return preg_split("/\r\n|\n|\r/", $response) ?: []; } function fetchRemoteLinesViaSocket(string $url, ?string $previousError = null): array { $parts = parse_url($url); if (!is_array($parts) || ($parts['scheme'] ?? '') !== 'https' || !isset($parts['host'])) { throw new RuntimeException('Socket-Fallback unterstuetzt nur HTTPS-URLs.'); } $host = (string) $parts['host']; $path = (string) ($parts['path'] ?? '/'); $query = isset($parts['query']) ? '?' . $parts['query'] : ''; $target = 'ssl://' . $host . ':443'; $context = stream_context_create([ 'ssl' => [ 'verify_peer' => true, 'verify_peer_name' => true, 'SNI_enabled' => true, 'peer_name' => $host, ], ]); $errno = 0; $errstr = ''; $socket = @stream_socket_client($target, $errno, $errstr, 30, STREAM_CLIENT_CONNECT, $context); if ($socket === false) { $detail = $errstr !== '' ? $errstr : 'Verbindung konnte nicht aufgebaut werden'; if ($previousError !== null && $previousError !== '') { $detail .= ' | file_get_contents: ' . $previousError; } throw new RuntimeException('Download fehlgeschlagen: ' . $detail); } stream_set_timeout($socket, 300); $request = implode("\r\n", [ 'GET ' . $path . $query . ' HTTP/1.1', 'Host: ' . $host, 'User-Agent: AstroMINT MPC Comet Importer/2.0', 'Accept: text/plain', 'Connection: close', '', '', ]); fwrite($socket, $request); $response = stream_get_contents($socket); fclose($socket); if ($response === false || $response === '') { throw new RuntimeException('Download fehlgeschlagen: leere Antwort vom Server'); } $separatorPos = strpos($response, "\r\n\r\n"); if ($separatorPos === false) { throw new RuntimeException('Download fehlgeschlagen: ungueltige HTTP-Antwort'); } $headerText = substr($response, 0, $separatorPos); $body = substr($response, $separatorPos + 4); $headerLines = explode("\r\n", $headerText); $statusLine = $headerLines[0] ?? ''; if (preg_match('/\s(\d{3})\s/', $statusLine, $matches) !== 1) { throw new RuntimeException('Download fehlgeschlagen: HTTP-Status unbekannt'); } $httpCode = (int) $matches[1]; if ($httpCode !== 200) { throw new RuntimeException('HTTP-Fehler beim Abruf: ' . $httpCode); } $headers = []; foreach (array_slice($headerLines, 1) as $headerLine) { $parts = explode(':', $headerLine, 2); if (count($parts) === 2) { $headers[strtolower(trim($parts[0]))] = trim($parts[1]); } } if (($headers['transfer-encoding'] ?? '') === 'chunked') { $body = decodeChunkedHttpBody($body); } return preg_split("/\r\n|\n|\r/", $body) ?: []; } function decodeChunkedHttpBody(string $body): string { $decoded = ''; $offset = 0; $length = strlen($body); while ($offset < $length) { $lineEnd = strpos($body, "\r\n", $offset); if ($lineEnd === false) { break; } $hexLength = trim(substr($body, $offset, $lineEnd - $offset)); if ($hexLength === '') { break; } $chunkLength = hexdec($hexLength); $offset = $lineEnd + 2; if ($chunkLength === 0) { break; } $decoded .= substr($body, $offset, $chunkLength); $offset += $chunkLength + 2; } return $decoded; } function parseSoft00CmtLine(string $line): array { $padded = str_pad($line, 172, ' '); $designationPacked = requireStringField(readFixedField($padded, 1, 12), 'designation_packed'); $designationAndName = requireStringField(readFixedField($padded, 103, 158), 'designation_and_name'); $epochCompact = toNullableCompactDate(readFixedField($padded, 82, 89)); return [ ':designation_packed' => $designationPacked, ':orbit_type' => deriveOrbitType($designationAndName, $designationPacked), ':year_of_perihelion' => requireIntField(readFixedField($padded, 15, 18), 'year_of_perihelion'), ':month_of_perihelion' => requireIntField(readFixedField($padded, 20, 21), 'month_of_perihelion'), ':day_of_perihelion' => requireFloatField(readFixedField($padded, 23, 29), 'day_of_perihelion'), ':perihelion_dist_au' => requireFloatField(readFixedField($padded, 32, 39), 'perihelion_dist_au'), ':eccentricity' => requireFloatField(readFixedField($padded, 42, 49), 'eccentricity'), ':arg_perihelion_deg' => requireFloatField(readFixedField($padded, 52, 59), 'arg_perihelion_deg'), ':ascending_node_deg' => requireFloatField(readFixedField($padded, 62, 69), 'ascending_node_deg'), ':inclination_deg' => requireFloatField(readFixedField($padded, 72, 79), 'inclination_deg'), ':epoch_compact' => $epochCompact, ':epoch_date' => $epochCompact !== null ? compactDateToSql($epochCompact) : null, ':absolute_magnitude_h' => toNullableFloat(readFixedField($padded, 91, 95)), ':slope_parameter_g' => toNullableFloat(readFixedField($padded, 97, 101)), ':designation_and_name' => $designationAndName, ':reference_text' => toNullableString(readFixedField($padded, 160, 200)), ':raw_record' => $line, ]; } function deriveOrbitType(string $designationAndName, string $designationPacked): string { if (preg_match('/^\d+([A-Z])\//', $designationAndName, $matches) === 1) { return $matches[1]; } if (preg_match('/^([A-Z])\//', $designationAndName, $matches) === 1) { return $matches[1]; } if (preg_match('/([A-Z])/', ltrim($designationPacked), $matches) === 1) { return $matches[1]; } throw new RuntimeException('orbit_type konnte nicht abgeleitet werden'); } function readFixedField(string $line, int $startColumn, int $endColumn): ?string { $offset = $startColumn - 1; $length = $endColumn - $startColumn + 1; $value = substr($line, $offset, $length); if ($value === false) { return null; } $value = trim($value); return $value === '' ? null : $value; } function requireStringField(?string $value, string $fieldName): string { if ($value === null || $value === '') { throw new RuntimeException("Pflichtfeld fehlt oder ist ungueltig: {$fieldName}"); } return $value; } function requireIntField(?string $value, string $fieldName): int { $value = trim((string) $value); if ($value === '' || preg_match('/^-?\d+$/', $value) !== 1) { throw new RuntimeException("Pflichtfeld fehlt oder ist ungueltig: {$fieldName}"); } return (int) $value; } function requireFloatField(?string $value, string $fieldName): float { $value = trim((string) $value); if ($value === '' || !is_numeric($value)) { throw new RuntimeException("Pflichtfeld fehlt oder ist ungueltig: {$fieldName}"); } return (float) $value; } function toNullableCompactDate(?string $value): ?string { $value = trim((string) $value); if ($value === '') { return null; } if (preg_match('/^\d{8}$/', $value) !== 1) { throw new RuntimeException('epoch_compact ist ungueltig'); } return $value; } function toNullableString(?string $value): ?string { if ($value === null) { return null; } $value = trim($value); return $value === '' ? null : $value; } function toNullableFloat(?string $value): ?float { if ($value === null) { return null; } $value = trim($value); if ($value === '' || !is_numeric($value)) { return null; } return (float) $value; } function compactDateToSql(string $value): ?string { if (preg_match('/^\d{8}$/', $value) !== 1) { return null; } $year = (int) substr($value, 0, 4); $month = (int) substr($value, 4, 2); $day = (int) substr($value, 6, 2); if (!checkdate($month, $day, $year)) { return null; } return sprintf('%04d-%02d-%02d', $year, $month, $day); } function normalizeDbConfig(array $config): array { return [ 'host' => (string) ($config['host'] ?? '127.0.0.1'), 'port' => (int) ($config['port'] ?? 3306), 'dbname' => (string) ($config['dbname'] ?? ''), 'user' => (string) ($config['user'] ?? ''), 'pass' => (string) ($config['pass'] ?? ''), 'charset' => (string) ($config['charset'] ?? 'utf8mb4'), ]; } function rebuildCometsTable(PDO $pdo): void { $pdo->exec('DROP TABLE IF EXISTS `comets_mpc`'); $pdo->exec(<<<'SQL' CREATE TABLE `comets_mpc` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, `designation_packed` varchar(12) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL COMMENT 'Fixed-width MPC-Packbezeichnung aus Soft00Cmt.txt, inkl. Fragmentkennung', `orbit_type` char(1) NOT NULL COMMENT 'Abgeleiteter Kometentyp, z. B. C, P oder I', `year_of_perihelion` smallint(6) NOT NULL COMMENT 'Jahr des Periheldurchgangs', `month_of_perihelion` tinyint(3) unsigned NOT NULL COMMENT 'Monat des Periheldurchgangs', `day_of_perihelion` decimal(8,4) NOT NULL COMMENT 'Tag des Periheldurchgangs inklusive Tagesbruchteil', `perihelion_dist_au` decimal(12,6) NOT NULL COMMENT 'Periheldistanz q in AE', `eccentricity` decimal(12,6) NOT NULL COMMENT 'Exzentrizitaet e', `arg_perihelion_deg` decimal(9,4) NOT NULL COMMENT 'Argument des Perihels in Grad', `ascending_node_deg` decimal(9,4) NOT NULL COMMENT 'Laenge des aufsteigenden Knotens in Grad', `inclination_deg` decimal(9,4) NOT NULL COMMENT 'Bahnneigung in Grad', `epoch_compact` char(8) DEFAULT NULL COMMENT 'Epoche des Elementsatzes als YYYYMMDD; kann bei Fragmenten fehlen', `epoch_date` date DEFAULT NULL COMMENT 'Epoche des Elementsatzes als SQL-Datum', `absolute_magnitude_h` decimal(5,1) DEFAULT NULL COMMENT 'Absolute Helligkeit H laut MPC', `slope_parameter_g` decimal(4,1) DEFAULT NULL COMMENT 'Steigungsparameter G laut MPC', `designation_and_name` varchar(255) NOT NULL COMMENT 'Lesbare Bezeichnung, z. B. C/1995 O1 (Hale-Bopp)', `reference_text` varchar(64) DEFAULT NULL COMMENT 'Referenz aus der MPC-Datei, z. B. MPEC 2026-FC3', `raw_record` varchar(255) DEFAULT NULL COMMENT 'Originale Fixed-width-Zeile aus Soft00Cmt.txt', `source_url` varchar(255) DEFAULT NULL COMMENT 'Quelle des letzten Imports', `imported_at` datetime NOT NULL DEFAULT current_timestamp() COMMENT 'Zeitpunkt des letzten Imports', `created_at` datetime NOT NULL DEFAULT current_timestamp(), `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(), PRIMARY KEY (`id`), UNIQUE KEY `uq_mpc_comets_designation_packed` (`designation_packed`), KEY `idx_mpc_comets_orbit_type` (`orbit_type`), KEY `idx_mpc_comets_perihelion_year_month` (`year_of_perihelion`,`month_of_perihelion`), KEY `idx_mpc_comets_epoch_compact` (`epoch_compact`), KEY `idx_mpc_comets_designation_and_name` (`designation_and_name`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='Kometenelemente aus Soft00Cmt.txt des Minor Planet Center' SQL ); }