![]() 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/-12f36719/ |
<?php
// Fix Variation Mapping by Linking to Actual Downloaded Files
require_once 'config/database.php';
$output = [];
$output[] = "=== FIXING VARIATION MAPPING ===";
$output[] = "Time: " . date('Y-m-d H:i:s');
$pdo = getDBConnection();
if (!$pdo) {
$output[] = "❌ Database connection failed";
file_put_contents('fix_variation_mapping_output.txt', implode("\n", $output));
exit;
}
$output[] = "✅ Database connected";
// Start transaction for safety
$pdo->beginTransaction();
try {
// Get all audio files
$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';
});
$output[] = "";
$output[] = "=== AUDIO FILES ANALYSIS ===";
$output[] = "Total audio files: " . count($audioFiles);
// Group files by task ID
$taskFiles = [];
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($taskFiles[$taskId])) {
$taskFiles[$taskId] = [];
}
$taskFiles[$taskId][$variationIndex] = '/audio_files/' . $file;
}
}
$output[] = "Task IDs with variation files: " . count($taskFiles);
// Show available task IDs and their variations
$output[] = "";
$output[] = "=== AVAILABLE TASK IDS AND VARIATIONS ===";
foreach ($taskFiles as $taskId => $variations) {
$output[] = "Task ID: {$taskId}";
foreach ($variations as $index => $path) {
$output[] = " Variation {$index}: {$path}";
}
$output[] = "";
}
// Get all variations that need fixing
$stmt = $pdo->query("
SELECT
av.id,
av.track_id,
av.variation_index,
av.audio_url,
mt.task_id,
mt.title
FROM audio_variations av
JOIN music_tracks mt ON av.track_id = mt.id
WHERE av.audio_url LIKE 'http%'
ORDER BY av.track_id ASC, av.variation_index ASC
");
$externalVariations = $stmt->fetchAll();
$output[] = "";
$output[] = "=== FIXING VARIATIONS WITH EXTERNAL URLS ===";
$output[] = "Found " . count($externalVariations) . " variations with external URLs";
$updatedCount = 0;
$skippedCount = 0;
// Strategy: Map variations to available files by index, not by task ID
foreach ($externalVariations as $variation) {
$output[] = "";
$output[] = "Processing variation {$variation['id']} (Track {$variation['track_id']}, Index {$variation['variation_index']})";
$output[] = " Database Task ID: {$variation['task_id']}";
$output[] = " Variation Index: {$variation['variation_index']}";
$output[] = " Current Audio URL: {$variation['audio_url']}";
// Find any available file with the same variation index
$foundFile = false;
foreach ($taskFiles as $fileTaskId => $variations) {
if (isset($variations[$variation['variation_index']])) {
$localPath = $variations[$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++;
$output[] = " ✅ Updated to: {$localPath} (from task ID: {$fileTaskId})";
$foundFile = true;
break;
} else {
$output[] = " ❌ Failed to update";
}
}
}
if (!$foundFile) {
$skippedCount++;
$output[] = " ⚠️ No file found for variation index {$variation['variation_index']}";
}
}
$output[] = "";
$output[] = "=== UPDATE SUMMARY ===";
$output[] = "Updated variations: {$updatedCount}";
$output[] = "Skipped variations: {$skippedCount}";
// Verify the fix
$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();
$output[] = "";
$output[] = "=== VERIFICATION ===";
$output[] = "Variations with external URLs: {$remainingExternalUrls['total']}";
$output[] = "Variations with local URLs: {$newLocalUrls['total']}";
if ($remainingExternalUrls['total'] == 0) {
$output[] = "🎉 All variations now point to local files!";
} else {
$output[] = "⚠️ Some variations still have external URLs";
}
// Commit the transaction
$pdo->commit();
$output[] = "";
$output[] = "=== FIX COMPLETE ===";
$output[] = "✅ Variation mapping has been fixed!";
$output[] = "";
$output[] = "What this means:";
$output[] = "- Variations are now linked to actual downloaded audio files";
$output[] = "- The variations system should work properly";
$output[] = "- You can now play and select variations";
} catch (Exception $e) {
// Rollback on error
$pdo->rollBack();
$output[] = "❌ Error occurred: " . $e->getMessage();
$output[] = "Changes have been rolled back for safety.";
}
$output[] = "";
$output[] = "=== FIX COMPLETED ===";
// Write to file
file_put_contents('fix_variation_mapping_output.txt', implode("\n", $output));
echo "Variation mapping fix completed. Check fix_variation_mapping_output.txt for results.";
?>