![]() Server : Apache/2 System : Linux server-15-235-50-60 5.15.0-164-generic #174-Ubuntu SMP Fri Nov 14 20:25:16 UTC 2025 x86_64 User : gositeme ( 1004) PHP Version : 8.2.29 Disable Function : exec,system,passthru,shell_exec,proc_close,proc_open,dl,popen,show_source,posix_kill,posix_mkfifo,posix_getpwuid,posix_setpgid,posix_setsid,posix_setuid,posix_setgid,posix_seteuid,posix_setegid,posix_uname Directory : /home/gositeme/.cursor-server/data/User/History/2352035c/ |
<?php
// Fix Audio URLs to point to local files
require_once 'config/database.php';
echo "<h1>🔧 Fixing Audio URLs to Local Files</h1>";
$pdo = getDBConnection();
if (!$pdo) {
echo "<p style='color: red;'>❌ Database connection failed</p>";
exit;
}
echo "<p style='color: green;'>✅ Database connected</p>";
// Start transaction for safety
$pdo->beginTransaction();
try {
echo "<h2>📊 Current Status</h2>";
// Count variations with external URLs
$stmt = $pdo->query("SELECT COUNT(*) as total FROM audio_variations WHERE audio_url LIKE 'http%'");
$externalUrls = $stmt->fetch();
$stmt = $pdo->query("SELECT COUNT(*) as total FROM audio_variations WHERE audio_url NOT LIKE 'http%'");
$localUrls = $stmt->fetch();
echo "<p><strong>Before fix:</strong></p>";
echo "<ul>";
echo "<li>Variations with external URLs: {$externalUrls['total']}</li>";
echo "<li>Variations with local URLs: {$localUrls['total']}</li>";
echo "</ul>";
if ($externalUrls['total'] == 0) {
echo "<p style='color: green;'>✅ No external URLs found! All variations already point to local files.</p>";
$pdo->rollBack();
exit;
}
echo "<h2>🔍 Finding Local Files</h2>";
// Get audio files directory
$audioDir = 'audio_files/';
if (!is_dir($audioDir)) {
throw new Exception("Audio files directory not found");
}
$audioFiles = scandir($audioDir);
$audioFiles = array_filter($audioFiles, function($file) {
return $file !== '.' && $file !== '..' && pathinfo($file, PATHINFO_EXTENSION) === 'mp3';
});
echo "<p><strong>Audio files found:</strong> " . count($audioFiles) . "</p>";
// Create mapping of task IDs to local files
$taskToLocalFiles = [];
foreach ($audioFiles as $file) {
if (strpos($file, '_variation_') !== false) {
// This is a variation file
$parts = explode('_variation_', $file);
$taskId = $parts[0];
$variationIndex = intval(explode('.', $parts[1])[0]);
if (!isset($taskToLocalFiles[$taskId])) {
$taskToLocalFiles[$taskId] = [];
}
$taskToLocalFiles[$taskId][$variationIndex] = '/audio_files/' . $file;
}
}
echo "<p><strong>Task ID to local files mapping:</strong></p>";
foreach (array_slice($taskToLocalFiles, 0, 5) as $taskId => $variations) {
echo "<p><strong>Task {$taskId}:</strong></p>";
foreach ($variations as $index => $path) {
echo "<p> Variation {$index}: {$path}</p>";
}
}
echo "<h2>🔧 Updating Database URLs</h2>";
// Get all variations with external URLs
$stmt = $pdo->query("SELECT id, track_id, variation_index, audio_url FROM audio_variations WHERE audio_url LIKE 'http%'");
$externalVariations = $stmt->fetchAll();
$updatedCount = 0;
$skippedCount = 0;
foreach ($externalVariations as $variation) {
// Get the task_id for this track
$stmt = $pdo->prepare("SELECT task_id FROM music_tracks WHERE id = ?");
$stmt->execute([$variation['track_id']]);
$track = $stmt->fetch();
if ($track && !empty($track['task_id'])) {
$taskId = $track['task_id'];
// Check if we have local files for this task
if (isset($taskToLocalFiles[$taskId]) && isset($taskToLocalFiles[$taskId][$variation['variation_index']])) {
$localPath = $taskToLocalFiles[$taskId][$variation['variation_index']];
// Update the database
$stmt = $pdo->prepare("UPDATE audio_variations SET audio_url = ? WHERE id = ?");
$result = $stmt->execute([$localPath, $variation['id']]);
if ($result) {
$updatedCount++;
echo "<p style='color: green;'>✅ Updated variation {$variation['id']} (Track {$variation['track_id']}, Index {$variation['variation_index']}) to {$localPath}</p>";
} else {
echo "<p style='color: red;'>❌ Failed to update variation {$variation['id']}</p>";
}
} else {
$skippedCount++;
echo "<p style='color: orange;'>⚠️ No local file found for track {$variation['track_id']}, variation {$variation['variation_index']} (Task ID: {$taskId})</p>";
}
} else {
$skippedCount++;
echo "<p style='color: orange;'>⚠️ No task_id found for track {$variation['track_id']}</p>";
}
}
echo "<h2>✅ Update Complete</h2>";
echo "<p><strong>Results:</strong></p>";
echo "<ul>";
echo "<li>Updated variations: {$updatedCount}</li>";
echo "<li>Skipped variations: {$skippedCount}</li>";
echo "</ul>";
// Verify the fix
echo "<h2>🔍 Verification</h2>";
$stmt = $pdo->query("SELECT COUNT(*) as total FROM audio_variations WHERE audio_url LIKE 'http%'");
$remainingExternalUrls = $stmt->fetch();
$stmt = $pdo->query("SELECT COUNT(*) as total FROM audio_variations WHERE audio_url NOT LIKE 'http%'");
$newLocalUrls = $stmt->fetch();
echo "<p><strong>After fix:</strong></p>";
echo "<ul>";
echo "<li>Variations with external URLs: {$remainingExternalUrls['total']}</li>";
echo "<li>Variations with local URLs: {$newLocalUrls['total']}</li>";
echo "</ul>";
if ($remainingExternalUrls['total'] == 0) {
echo "<p style='color: green;'>🎉 All variations now point to local files!</p>";
} else {
echo "<p style='color: orange;'>⚠️ Some variations still have external URLs</p>";
}
// Commit the transaction
$pdo->commit();
echo "<h2>🎉 Fix Complete!</h2>";
echo "<p style='color: green;'>✅ Audio URLs have been updated to point to local files!</p>";
echo "<p><strong>What this means:</strong></p>";
echo "<ul>";
echo "<li>Variations buttons will now load local audio files</li>";
echo "<li>No more external URL dependency</li>";
echo "<li>Faster loading and better reliability</li>";
echo "<li>Your variations should now work properly</li>";
echo "</ul>";
} catch (Exception $e) {
// Rollback on error
$pdo->rollBack();
echo "<p style='color: red;'>❌ Error occurred: " . $e->getMessage() . "</p>";
echo "<p>Changes have been rolled back for safety.</p>";
}
echo "<hr>";
echo "<p><em>Fix completed at: " . date('Y-m-d H:i:s') . "</em></p>";
?>