<?php
/**
 * 网站地图生成
 */

header('Content-Type: application/xml; charset=utf-8');

$db_config = [
    'host' => '43.226.47.181',
    'port' => 3306,
    'user' => 'root2',
    'pass' => '123456mmmm',
    'name' => 'pay_mqsq_cn'
];

$conn = new mysqli($db_config['host'], $db_config['user'], $db_config['pass'], $db_config['name'], $db_config['port']);
$conn->set_charset("utf8mb4");

$base_url = 'https://pay.mqsq.cn';

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

// 静态页面
$static_pages = [
    ['loc' => $base_url . '/', 'priority' => '1.0', 'changefreq' => 'daily'],
    ['loc' => $base_url . '/?page=home#features', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => $base_url . '/?page=home#channels', 'priority' => '0.8', 'changefreq' => 'weekly'],
    ['loc' => $base_url . '/?page=home#articles', 'priority' => '0.9', 'changefreq' => 'daily'],
];

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

// 文章列表
$result = $conn->query("SELECT id, created_at FROM articles WHERE status = 'published' ORDER BY created_at DESC LIMIT 100");
while ($row = $result->fetch_assoc()) {
    echo '<url>
        <loc>' . $base_url . '/?page=article&id=' . $row['id'] . '</loc>
        <lastmod>' . date('Y-m-d', strtotime($row['created_at'])) . '</lastmod>
        <changefreq>weekly</changefreq>
        <priority>0.7</priority>
    </url>
    ';
}

echo '</urlset>';

$conn->close();
