![]() 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
/**
* Update all tracks with keywords extracted from API.box metadata
*
* This script:
* 1. Fetches all tracks from the database
* 2. Extracts keywords from metadata JSON (specifically from metadata.tags)
* 3. Updates the tags column with these keywords
*/
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once 'config/database.php';
// Function to extract keywords from metadata (same logic as community_fixed.php)
function extractKeywordsFromMetadata($tags, $metadata) {
$keywords = [];
// Extract from tags column (if already exists)
if (!empty($tags)) {
if (is_string($tags)) {
// API.box tags come as strings like "organic acoustic; deep drums; primal energy; tribal; epic brass; dramatic violin; native flute"
// Split by comma OR semicolon
$tagArray = preg_split('/[,;]/', $tags);
foreach ($tagArray as $tag) {
$tag = trim($tag);
// Filter out generic/default values
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
} elseif (is_array($tags)) {
foreach ($tags as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
}
}
// Extract from metadata JSON - ONLY the tags field, nothing else
if (!empty($metadata)) {
$meta = is_string($metadata) ? json_decode($metadata, true) : $metadata;
if (is_array($meta) && isset($meta['tags']) && !empty($meta['tags'])) {
// ONLY extract from the tags field in metadata
if (is_array($meta['tags'])) {
foreach ($meta['tags'] as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
} elseif (is_string($meta['tags'])) {
// Parse the tags string (comma or semicolon separated)
// Handle formats like: "organic acoustic; deep drums primal energy tribal epic brass dramatic violin native flute"
$tagArray = preg_split('/[,;]/', $meta['tags']);
foreach ($tagArray as $tag) {
$tag = trim($tag);
// Filter out generic/default values
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
}
}
// Also check api_box_raw_data as fallback (in case tags are in the raw API response)
if (empty($keywords) && isset($meta['api_box_raw_data'])) {
$rawData = is_string($meta['api_box_raw_data']) ? json_decode($meta['api_box_raw_data'], true) : $meta['api_box_raw_data'];
if (is_array($rawData)) {
// Check in data.data.data[0].tags (common API.box format)
if (isset($rawData['data']['data']) && is_array($rawData['data']['data']) && !empty($rawData['data']['data'])) {
$trackData = $rawData['data']['data'][0];
if (isset($trackData['tags']) && !empty($trackData['tags'])) {
$rawTags = $trackData['tags'];
if (is_string($rawTags)) {
$tagArray = preg_split('/[,;]/', $rawTags);
foreach ($tagArray as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
} elseif (is_array($rawTags)) {
foreach ($rawTags as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
}
}
}
// Fallback to top-level tags
if (empty($keywords) && isset($rawData['tags']) && !empty($rawData['tags'])) {
$rawTags = $rawData['tags'];
if (is_string($rawTags)) {
$tagArray = preg_split('/[,;]/', $rawTags);
foreach ($tagArray as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
} elseif (is_array($rawTags)) {
foreach ($rawTags as $tag) {
$tag = trim($tag);
if (!empty($tag) && strlen($tag) > 1 && !in_array(strtolower($tag), ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4'])) {
$keywords[] = $tag;
}
}
}
}
}
}
}
// Normalize: remove duplicates, trim, filter empty, limit length
$keywords = array_map('trim', $keywords);
$keywords = array_filter($keywords, function($k) {
// Filter out empty, too short, too long, and generic values
$kLower = strtolower($k);
$genericValues = ['synthesizer', 'neutral', 'medium', 'electronic', 'c major', '4/4', 'null', 'none', ''];
return !empty($k) && strlen($k) > 1 && strlen($k) < 50 && !in_array($kLower, $genericValues);
});
$keywords = array_unique($keywords);
$keywords = array_values($keywords); // Re-index
// Return as string (semicolon-separated for storage in tags column)
return implode('; ', $keywords);
}
// Get database connection
$pdo = getDBConnection();
if (!$pdo) {
die("â Database connection failed\n");
}
echo "<!DOCTYPE html>
<html>
<head>
<title>Update Tracks Keywords</title>
<style>
body { font-family: Arial, sans-serif; padding: 20px; background: #1a1a1a; color: #fff; }
.success { color: #48bb78; }
.error { color: #f56565; }
.info { color: #4299e1; }
.warning { color: #ed8936; }
.track-item { padding: 10px; margin: 5px 0; background: #2d2d2d; border-radius: 5px; }
.stats { background: #2d2d2d; padding: 15px; border-radius: 5px; margin: 20px 0; }
</style>
</head>
<body>
<h1>đ Updating Tracks with Keywords from Metadata</h1>
";
// Fetch all tracks
$stmt = $pdo->prepare("
SELECT
id,
title,
task_id,
tags,
metadata
FROM music_tracks
WHERE status = 'complete'
ORDER BY id ASC
");
$stmt->execute();
$tracks = $stmt->fetchAll(PDO::FETCH_ASSOC);
$totalTracks = count($tracks);
$updatedCount = 0;
$skippedCount = 0;
$errorCount = 0;
echo "<div class='stats'>";
echo "<p><strong>Total tracks found:</strong> {$totalTracks}</p>";
echo "<p>Processing tracks...</p>";
echo "</div>";
// Process each track
foreach ($tracks as $track) {
$trackId = $track['id'];
$trackTitle = htmlspecialchars($track['title'] ?: 'Untitled');
$currentTags = $track['tags'];
$metadata = $track['metadata'];
// Extract keywords from metadata
$extractedKeywords = extractKeywordsFromMetadata($currentTags, $metadata);
// Skip if no keywords found
if (empty($extractedKeywords)) {
$skippedCount++;
echo "<div class='track-item'>";
echo "<span class='warning'>â ī¸ Track #{$trackId}: \"{$trackTitle}\" - No keywords found in metadata</span>";
echo "</div>";
continue;
}
// Check if tags are already the same (avoid unnecessary updates)
if ($currentTags === $extractedKeywords) {
$skippedCount++;
echo "<div class='track-item'>";
echo "<span class='info'>âšī¸ Track #{$trackId}: \"{$trackTitle}\" - Tags already up to date</span>";
echo "</div>";
continue;
}
// Update the track
try {
$updateStmt = $pdo->prepare("
UPDATE music_tracks
SET tags = ?,
updated_at = CURRENT_TIMESTAMP
WHERE id = ?
");
$result = $updateStmt->execute([$extractedKeywords, $trackId]);
if ($result) {
$updatedCount++;
echo "<div class='track-item'>";
echo "<span class='success'>â
Track #{$trackId}: \"{$trackTitle}\"</span><br>";
echo "<small>Old tags: " . htmlspecialchars($currentTags ?: '(empty)') . "</small><br>";
echo "<small>New tags: " . htmlspecialchars($extractedKeywords) . "</small>";
echo "</div>";
} else {
$errorCount++;
echo "<div class='track-item'>";
echo "<span class='error'>â Track #{$trackId}: \"{$trackTitle}\" - Update failed</span>";
echo "</div>";
}
} catch (Exception $e) {
$errorCount++;
echo "<div class='track-item'>";
echo "<span class='error'>â Track #{$trackId}: \"{$trackTitle}\" - Error: " . htmlspecialchars($e->getMessage()) . "</span>";
echo "</div>";
}
// Flush output for real-time progress
if (ob_get_level() > 0) {
ob_flush();
}
flush();
}
// Summary
echo "<div class='stats'>";
echo "<h2>đ Summary</h2>";
echo "<p class='success'><strong>â
Updated:</strong> {$updatedCount} tracks</p>";
echo "<p class='info'><strong>âšī¸ Skipped:</strong> {$skippedCount} tracks</p>";
echo "<p class='error'><strong>â Errors:</strong> {$errorCount} tracks</p>";
echo "<p><strong>đ Total processed:</strong> {$totalTracks} tracks</p>";
echo "</div>";
echo "<p class='success'><strong>⨠Update complete!</strong></p>";
echo "</body></html>";
?>