<?php
// 翊推广 - 多站点 Sitemap 生成器
header('Content-Type:application/xml;charset=utf-8');

$db = new mysqli("localhost","xiaoyi","Yzdtdq88","xiaoyi_promotion");
$db->set_charset("utf8mb4");

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

// 所有站点首页
$rs = $db->query("SELECT id, domain, name, updated_at FROM yt_sites WHERE status=1");
while($site = $rs->fetch_assoc()) {
  $domain = $site["domain"];
  echo '<url><loc>http://'.$domain.'/</loc><priority>1.0</priority><changefreq>daily</changefreq></url>';
  
  // 各站点的文章
  $sid = $site["id"];
  $arts = $db->query("SELECT id, updated_at FROM yt_articles WHERE site_id=$sid AND status=1 ORDER BY id DESC LIMIT 200");
  while($a = $arts->fetch_assoc()) {
    $date = substr($a["updated_at"]??"",0,10);
    echo '<url><loc>http://'.$domain.'/?id='.$sid.'&article_id='.$a["id"].'</loc><lastmod>'.$date.'</lastmod><priority>0.8</priority></url>';
  }
}

echo '</urlset>';
