<?php
header('Content-Type: text/xml; charset=UTF-8');
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo '<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">';

$staticUrls = [
    ['loc' => 'https://yxgoa.cn/', 'lastmod' => date('Y-m-d'), 'changefreq' => 'daily', 'priority' => '1.0'],
    ['loc' => 'https://yxgoa.cn/yxbk.html', 'lastmod' => '2025-11-21', 'changefreq' => 'weekly', 'priority' => '0.7'],
    ['loc' => 'https://yxgoa.cn/yxbqq.html', 'lastmod' => '2025-11-21', 'changefreq' => 'weekly', 'priority' => '0.6'],
    ['loc' => 'https://yxgoa.cn/yx.html', 'lastmod' => '2025-11-21', 'changefreq' => 'monthly', 'priority' => '0.6'],
];

foreach ($staticUrls as $page) {
    echo '<url><loc>' . htmlspecialchars($page['loc']) . '</loc>';
    echo '<lastmod>' . $page['lastmod'] . '</lastmod>';
    echo '<changefreq>' . $page['changefreq'] . '</changefreq>';
    echo '<priority>' . $page['priority'] . '</priority></url>';
}

$dsn = 'mysql:host=localhost;dbname=your_database_name;charset=utf8mb4';
$username = 'your_db_username';
$password = 'your_db_password';

try {
    $pdo = new PDO($dsn, $username, $password);
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    $stmt = $pdo->query("SELECT id, updated_at FROM posts WHERE status = 'published' ORDER BY updated_at DESC");

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $url = 'https://yxgoa.cn/index.php/archives/' . $row['id'] . '/';
        $lastmod = date('Y-m-d', strtotime($row['updated_at']));
        echo '<url>';
        echo '<loc>' . htmlspecialchars($url) . '</loc>';
        echo '<lastmod>' . $lastmod . '</lastmod>';
        echo '<changefreq>monthly</changefreq>';
        echo '<priority>0.8</priority>';
        echo '</url>';
    }
} catch (PDOException $e) {
    error_log('Sitemap DB Error: ' . $e->getMessage());
}

echo '</urlset>';
?>