HEX
Server: Apache
System: Linux 136-243-153-58.cprapid.com 4.18.0-553.81.1.el8_10.x86_64 #1 SMP Mon Oct 27 11:29:19 EDT 2025 x86_64
User: mytest (1001)
PHP: 8.2.30
Disabled: exec,passthru,shell_exec,system
Upload Files
File: /home/mytest/.trash/cron-refresh-static-data.php.24
#!/usr/bin/env php
<?php
/**
 * Cron Job: Refresh Static Yacht Data Cache
 * 
 * Run this WEEKLY (e.g., every Sunday at 3 AM):
 * 0 3 * * 0 /usr/bin/php /path/to/cron-refresh-static-data.php >> /path/to/logs/static-data-cron.log 2>&1
 * 
 * Refreshes static yacht data from Booking Manager API:
 * - Equipment/Amenities
 * - Extras
 * - Technical Specifications
 * - Base Location
 * - Company Information
 * - Yacht Images
 * 
 * This data changes infrequently, so weekly refresh is sufficient per BM API guide.
 */

require_once __DIR__ . '/config.php';
require_once __DIR__ . '/database/Database.php';
require_once __DIR__ . '/api/services/BookingManagerService.php';

echo "[" . date('Y-m-d H:i:s') . "] Starting static yacht data refresh...\n";

try {
    $db = Database::getInstance();
    $bmService = new BookingManagerService();
    
    $stmt = $db->prepare("SELECT id, booking_manager_id, name FROM yachts WHERE booking_manager_id IS NOT NULL AND booking_manager_id != '' ORDER BY name ASC");
    $result = $stmt->execute();
    
    $yachts = [];
    while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
        $yachts[] = $row;
    }
    
    echo "Found " . count($yachts) . " yachts to refresh\n\n";
    
    $successCount = 0;
    $errorCount = 0;
    
    foreach ($yachts as $yacht) {
        echo "🔄 Refreshing: {$yacht['name']} (BM ID: {$yacht['booking_manager_id']})\n";
        
        try {
            $yachtData = $bmService->getYacht($yacht['booking_manager_id'], false);
            
            if (isset($yachtData['error'])) {
                echo "  ✗ Error: {$yachtData['error']}\n";
                $errorCount++;
                continue;
            }
            
            $staticData = [
                'equipment' => $yachtData['equipment'] ?? [],
                'equipmentRaw' => $yachtData['equipmentRaw'] ?? [],
                'extras' => $yachtData['extras'] ?? [],
                'baseId' => $yachtData['baseId'] ?? null,
                'baseName' => $yachtData['baseName'] ?? null,
                'homeBase' => $yachtData['homeBase'] ?? null,
                'companyId' => $yachtData['companyId'] ?? null,
                'companyName' => $yachtData['companyName'] ?? null,
                'description' => $yachtData['description'] ?? null,
                'descriptions' => $yachtData['descriptions'] ?? [],
                'certificate' => $yachtData['certificate'] ?? null,
                'minimumCharterDuration' => $yachtData['minimumCharterDuration'] ?? null,
                'defaultCheckInDay' => $yachtData['defaultCheckInDay'] ?? null,
                'defaultCheckInTime' => $yachtData['defaultCheckInTime'] ?? null,
                'defaultCheckOutTime' => $yachtData['defaultCheckOutTime'] ?? null,
                'validDateFrom' => $yachtData['validDateFrom'] ?? null,
                'validDateTo' => $yachtData['validDateTo'] ?? null,
                'technicalSpecs' => $yachtData['technicalSpecs'] ?? []
            ];
            
            $cacheKey = 'yacht_static_' . $yacht['booking_manager_id'];
            $cacheValue = json_encode($staticData);
            $expiresAt = date('Y-m-d H:i:s', strtotime('+7 days'));
            
            $checkStmt = $db->prepare("SELECT id FROM yacht_static_cache WHERE yacht_id = ?");
            $checkStmt->bindValue(1, $yacht['booking_manager_id'], SQLITE3_TEXT);
            $checkResult = $checkStmt->execute();
            $exists = $checkResult->fetchArray(SQLITE3_ASSOC);
            
            if ($exists) {
                $updateStmt = $db->prepare("
                    UPDATE yacht_static_cache 
                    SET cache_data = ?, expires_at = ?, updated_at = CURRENT_TIMESTAMP
                    WHERE yacht_id = ?
                ");
                $updateStmt->bindValue(1, $cacheValue, SQLITE3_TEXT);
                $updateStmt->bindValue(2, $expiresAt, SQLITE3_TEXT);
                $updateStmt->bindValue(3, $yacht['booking_manager_id'], SQLITE3_TEXT);
                $updateStmt->execute();
            } else {
                $insertStmt = $db->prepare("
                    INSERT INTO yacht_static_cache (yacht_id, cache_data, expires_at, updated_at)
                    VALUES (?, ?, ?, CURRENT_TIMESTAMP)
                ");
                $insertStmt->bindValue(1, $yacht['booking_manager_id'], SQLITE3_TEXT);
                $insertStmt->bindValue(2, $cacheValue, SQLITE3_TEXT);
                $insertStmt->bindValue(3, $expiresAt, SQLITE3_TEXT);
                $insertStmt->execute();
            }
            
            $equipmentCount = count($staticData['equipment']);
            $extrasCount = count($staticData['extras']);
            $hasTechSpecs = !empty($staticData['technicalSpecs']) ? 'Yes' : 'No';
            
            echo "  ✓ Cached: {$equipmentCount} equipment, {$extrasCount} extras, Tech Specs: {$hasTechSpecs}\n";
            echo "  ✓ Cache expires: {$expiresAt}\n";
            $successCount++;
            
            sleep(1);
            
        } catch (Exception $e) {
            echo "  ✗ Error: " . $e->getMessage() . "\n";
            $errorCount++;
        }
    }
    
    echo "\n" . str_repeat("=", 60) . "\n";
    echo "📊 Summary:\n";
    echo "   Total yachts: " . count($yachts) . "\n";
    echo "   Successfully refreshed: {$successCount}\n";
    echo "   Errors: {$errorCount}\n";
    echo str_repeat("=", 60) . "\n";
    
    $cleanupStmt = $db->prepare("DELETE FROM yacht_static_cache WHERE expires_at < datetime('now')");
    $cleanupStmt->execute();
    
    echo "\n[" . date('Y-m-d H:i:s') . "] Static data refresh completed!\n";
    
} catch (Exception $e) {
    echo "\n[" . date('Y-m-d H:i:s') . "] ERROR: " . $e->getMessage() . "\n";
    exit(1);
}

exit(0);