<?php
header('Content-Type: application/xml; charset=utf-8');
require_once('connect.php');

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">' . "\n";

// 主要静态页面
$static_pages = [
    ['url' => 'https://www.jyut6.com/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['url' => 'https://www.jyut6.com/search.php', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['url' => 'https://www.jyut6.com/recent.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['url' => 'https://www.jyut6.com/hotlrcs.php', 'priority' => '0.8', 'changefreq' => 'daily'],
    ['url' => 'https://www.jyut6.com/alllrcs.php', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['url' => 'https://www.jyut6.com/apps.php', 'priority' => '0.6', 'changefreq' => 'monthly'],
    ['url' => 'https://www.jyut6.com/shici.php', 'priority' => '0.7', 'changefreq' => 'weekly'],
    ['url' => 'https://www.jyut6.com/donate.php', 'priority' => '0.4', 'changefreq' => 'monthly'],
    ['url' => 'https://www.jyut6.com/privacy_policy.php', 'priority' => '0.3', 'changefreq' => 'yearly'],
    ['url' => 'https://www.jyut6.com/terms_of_use.php', 'priority' => '0.3', 'changefreq' => 'yearly']
];

foreach ($static_pages as $page) {
    echo '  <url>' . "\n";
    echo '    <loc>' . $page['url'] . '</loc>' . "\n";
    echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
    echo '    <changefreq>' . $page['changefreq'] . '</changefreq>' . "\n";
    echo '    <priority>' . $page['priority'] . '</priority>' . "\n";
    echo '  </url>' . "\n";
}

// 获取所有已审核的歌词页面
$sql_lyrics = "SELECT id, editDate FROM jyutLrc WHERE checkinfo='1' ORDER BY id DESC";
$result_lyrics = $conn->query($sql_lyrics);
if ($result_lyrics && $result_lyrics->num_rows > 0) {
    while ($row = $result_lyrics->fetch_assoc()) {
        $lastmod = !empty($row['editDate']) ? date('Y-m-d', strtotime($row['editDate'])) : date('Y-m-d');
        echo '  <url>' . "\n";
        echo '    <loc>https://www.jyut6.com/lrc.show.php?id=' . $row['id'] . '</loc>' . "\n";
        echo '    <lastmod>' . $lastmod . '</lastmod>' . "\n";
        echo '    <changefreq>monthly</changefreq>' . "\n";
        echo '    <priority>0.6</priority>' . "\n";
        echo '  </url>' . "\n";
    }
}

// 获取所有诗词页面
$sql_poems = "SELECT id FROM jyutShiCi ORDER BY id DESC";
$result_poems = $conn->query($sql_poems);
if ($result_poems && $result_poems->num_rows > 0) {
    while ($row = $result_poems->fetch_assoc()) {
        echo '  <url>' . "\n";
        echo '    <loc>https://www.jyut6.com/shici.show.php?id=' . $row['id'] . '</loc>' . "\n";
        echo '    <lastmod>' . date('Y-m-d') . '</lastmod>' . "\n";
        echo '    <changefreq>monthly</changefreq>' . "\n";
        echo '    <priority>0.5</priority>' . "\n";
        echo '  </url>' . "\n";
    }
}

echo '</urlset>' . "\n";
$conn->close();
?>