![]() 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/domains/soundstudiopro.com/private_html/ |
<?php
session_start();
require_once 'config/database.php';
// Check if user is admin
if (!isset($_SESSION['is_admin']) || !$_SESSION['is_admin']) {
header('Location: /auth/login.php');
exit;
}
$pdo = getDBConnection();
// Handle form submissions
$message = '';
$action = $_POST['action'] ?? $_GET['action'] ?? '';
if ($action === 'resync_metadata') {
try {
// Get all tracks with API callback data in metadata
$stmt = $pdo->query("
SELECT id, title, metadata, task_id
FROM music_tracks
WHERE status = 'complete'
AND metadata IS NOT NULL
AND metadata != ''
");
$tracks = $stmt->fetchAll();
$updated_count = 0;
$processed_count = 0;
foreach ($tracks as $track) {
$processed_count++;
$metadata = json_decode($track['metadata'], true);
// Initialize new clean metadata structure
$clean_metadata = [];
// Extract from nested API response structure
if (isset($metadata['data']['data']) && is_array($metadata['data']['data'])) {
$audio_data = $metadata['data']['data'][0] ?? [];
// Extract metadata fields
$clean_metadata['genre'] = $audio_data['gerne'] ?? $audio_data['genre'] ?? 'Electronic';
$clean_metadata['style'] = $audio_data['style'] ?? '';
$clean_metadata['tags'] = $audio_data['tags'] ?? '';
$clean_metadata['duration'] = $audio_data['duration'] ?? null;
$clean_metadata['title'] = $audio_data['title'] ?? '';
$clean_metadata['image_url'] = $audio_data['image_url'] ?? '';
$clean_metadata['lyrics'] = $audio_data['lyric'] ?? $audio_data['lyrics'] ?? '';
$clean_metadata['prompt'] = $audio_data['gpt_description_prompt'] ?? '';
// Additional metadata
if (isset($audio_data['meta_tags'])) {
$meta_tags = json_decode($audio_data['meta_tags'], true);
if (is_array($meta_tags)) {
$clean_metadata['bpm'] = $meta_tags['bpm'] ?? null;
$clean_metadata['key'] = $meta_tags['key'] ?? null;
$clean_metadata['mood'] = $meta_tags['mood'] ?? null;
$clean_metadata['energy'] = $meta_tags['energy'] ?? null;
}
}
} else {
// Fallback for tracks with different metadata structure
$clean_metadata['genre'] = $metadata['genre'] ?? 'Electronic';
$clean_metadata['style'] = $metadata['style'] ?? '';
$clean_metadata['tags'] = $metadata['tags'] ?? '';
}
// Update title if empty or generic
$new_title = null;
if (empty($track['title']) || $track['title'] === 'Untitled Track' || $track['title'] === 'Generated Track') {
if (!empty($clean_metadata['title'])) {
$new_title = $clean_metadata['title'];
} elseif (!empty($clean_metadata['prompt'])) {
// Create title from prompt (first 50 chars)
$new_title = substr($clean_metadata['prompt'], 0, 50) . '...';
}
}
// Prepare update query
if ($new_title) {
$update_stmt = $pdo->prepare("
UPDATE music_tracks
SET metadata = ?, title = ?, updated_at = NOW()
WHERE id = ?
");
$update_stmt->execute([json_encode($clean_metadata), $new_title, $track['id']]);
} else {
$update_stmt = $pdo->prepare("
UPDATE music_tracks
SET metadata = ?, updated_at = NOW()
WHERE id = ?
");
$update_stmt->execute([json_encode($clean_metadata), $track['id']]);
}
$updated_count++;
}
$message = "✅ Successfully processed $processed_count tracks and updated $updated_count tracks with clean metadata!";
} catch (Exception $e) {
$message = "❌ Error: " . $e->getMessage();
}
}
if ($action === 'fix_genres') {
try {
// Fix tracks that defaulted to 'Electronic' - try to extract real genre from tags or style
$stmt = $pdo->query("
SELECT id, metadata, prompt
FROM music_tracks
WHERE status = 'complete'
AND JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.genre')) = 'Electronic'
");
$tracks = $stmt->fetchAll();
$updated_count = 0;
foreach ($tracks as $track) {
$metadata = json_decode($track['metadata'], true);
$prompt = strtolower($track['prompt']);
// Try to detect genre from prompt keywords
$detected_genre = 'Electronic'; // default
if (strpos($prompt, 'jazz') !== false) $detected_genre = 'Jazz';
elseif (strpos($prompt, 'rock') !== false) $detected_genre = 'Rock';
elseif (strpos($prompt, 'pop') !== false) $detected_genre = 'Pop';
elseif (strpos($prompt, 'hip hop') !== false || strpos($prompt, 'rap') !== false) $detected_genre = 'Hip Hop';
elseif (strpos($prompt, 'classical') !== false || strpos($prompt, 'orchestra') !== false) $detected_genre = 'Classical';
elseif (strpos($prompt, 'country') !== false) $detected_genre = 'Country';
elseif (strpos($prompt, 'reggae') !== false) $detected_genre = 'Reggae';
elseif (strpos($prompt, 'blues') !== false) $detected_genre = 'Blues';
elseif (strpos($prompt, 'folk') !== false) $detected_genre = 'Folk';
elseif (strpos($prompt, 'ambient') !== false || strpos($prompt, 'chill') !== false) $detected_genre = 'Ambient';
elseif (strpos($prompt, 'house') !== false) $detected_genre = 'House';
elseif (strpos($prompt, 'techno') !== false) $detected_genre = 'Techno';
elseif (strpos($prompt, 'trance') !== false) $detected_genre = 'Trance';
elseif (strpos($prompt, 'dubstep') !== false) $detected_genre = 'Dubstep';
elseif (strpos($prompt, 'drum') !== false && strpos($prompt, 'bass') !== false) $detected_genre = 'Drum & Bass';
if ($detected_genre !== 'Electronic') {
$metadata['genre'] = $detected_genre;
$update_stmt = $pdo->prepare("
UPDATE music_tracks
SET metadata = ?, updated_at = NOW()
WHERE id = ?
");
$update_stmt->execute([json_encode($metadata), $track['id']]);
$updated_count++;
}
}
$message = "✅ Updated $updated_count tracks with better genre detection based on prompts!";
} catch (Exception $e) {
$message = "❌ Error: " . $e->getMessage();
}
}
// Get current stats
$stats = $pdo->query("
SELECT
COUNT(*) as total_tracks,
COUNT(CASE WHEN metadata IS NOT NULL AND metadata != '' THEN 1 END) as has_metadata,
COUNT(CASE WHEN title IS NULL OR title = '' OR title = 'Untitled Track' THEN 1 END) as missing_titles,
COUNT(CASE WHEN JSON_EXTRACT(metadata, '$.genre') IS NOT NULL THEN 1 END) as has_genre
FROM music_tracks
WHERE status = 'complete'
")->fetch();
$genre_stats = $pdo->query("
SELECT JSON_UNQUOTE(JSON_EXTRACT(metadata, '$.genre')) as genre, COUNT(*) as count
FROM music_tracks
WHERE status = 'complete'
AND JSON_EXTRACT(metadata, '$.genre') IS NOT NULL
GROUP BY genre
ORDER BY count DESC
")->fetchAll();
?>
<!DOCTYPE html>
<html>
<head>
<title>Admin - Metadata Resync Tool</title>
<style>
body { font-family: Arial, sans-serif; margin: 2rem; background: #f8f9fa; }
.container { max-width: 1200px; margin: 0 auto; }
.card { background: white; padding: 2rem; margin: 1rem 0; border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); }
.btn { background: #007bff; color: white; padding: 1rem 2rem; border: none; border-radius: 6px; cursor: pointer; margin: 0.5rem; text-decoration: none; display: inline-block; }
.btn:hover { background: #0056b3; }
.btn-danger { background: #dc3545; }
.btn-success { background: #28a745; }
.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 1rem; margin: 1rem 0; }
.stat-card { background: #e9ecef; padding: 1rem; border-radius: 6px; text-align: center; }
.message { padding: 1rem; margin: 1rem 0; border-radius: 6px; background: #d4edda; color: #155724; border: 1px solid #c3e6cb; }
.error { background: #f8d7da; color: #721c24; border: 1px solid #f5c6cb; }
</style>
</head>
<body>
<div class="container">
<h1>🔧 Admin - Track Metadata Resync Tool</h1>
<?php if ($message): ?>
<div class="message <?= strpos($message, '❌') !== false ? 'error' : '' ?>">
<?= $message ?>
</div>
<?php endif; ?>
<div class="card">
<h2>📊 Current Database Stats</h2>
<div class="stats">
<div class="stat-card">
<h3><?= $stats['total_tracks'] ?></h3>
<p>Total Tracks</p>
</div>
<div class="stat-card">
<h3><?= $stats['has_metadata'] ?></h3>
<p>With Metadata</p>
</div>
<div class="stat-card">
<h3><?= $stats['missing_titles'] ?></h3>
<p>Missing Titles</p>
</div>
<div class="stat-card">
<h3><?= $stats['has_genre'] ?></h3>
<p>Have Genres</p>
</div>
</div>
</div>
<div class="card">
<h2>🎵 Current Genres Distribution</h2>
<div style="max-height: 300px; overflow-y: auto;">
<?php foreach ($genre_stats as $genre): ?>
<div style="display: flex; justify-content: space-between; padding: 0.5rem; border-bottom: 1px solid #eee;">
<span><strong><?= htmlspecialchars($genre['genre'] ?: 'NULL') ?></strong></span>
<span><?= $genre['count'] ?> tracks</span>
</div>
<?php endforeach; ?>
</div>
</div>
<div class="card">
<h2>🔄 Resync Actions</h2>
<p>Use these tools to fix metadata issues and improve track information.</p>
<form method="post" style="margin: 1rem 0;">
<input type="hidden" name="action" value="resync_metadata">
<button type="submit" class="btn btn-success">
🔄 Full Metadata Resync
</button>
<p><small>Extracts proper metadata from API callback data, fixes titles, genres, and other fields.</small></p>
</form>
<form method="post" style="margin: 1rem 0;">
<input type="hidden" name="action" value="fix_genres">
<button type="submit" class="btn">
🎵 Smart Genre Detection
</button>
<p><small>Analyzes track prompts to detect better genres for tracks currently marked as 'Electronic'.</small></p>
</form>
<a href="debug_metadata.php" class="btn" target="_blank">
🔍 Debug Metadata
</a>
<p><small>View detailed metadata diagnostic information.</small></p>
</div>
<div class="card">
<h2>⚠️ Notes</h2>
<ul>
<li>Full resync will reprocess all track metadata from API callback data</li>
<li>This will fix missing titles, incorrect genres, and extract all available metadata</li>
<li>Smart genre detection uses prompt analysis to improve genre classification</li>
<li>Always backup your database before running bulk operations</li>
</ul>
</div>
</div>
</body>
</html>