<?php
/**
 * Динамический sitemap для статей
 * Генерируется автоматически из базы данных
 */

header('Content-Type: application/xml; charset=utf-8');
require_once 'db.php';

echo '<?xml version="1.0" encoding="UTF-8"?>' . "\n";
?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
        xmlns:image="http://www.google.com/schemas/sitemap-image/1.1">

<?php
try {
    $stmt = $pdo->prepare("
        SELECT slug, title, picture, updated_at, created_at 
        FROM articles 
        WHERE status = 'published' 
        ORDER BY updated_at DESC
    ");
    $stmt->execute();
    $articles = $stmt->fetchAll();
    
    foreach ($articles as $article) {
        $lastmod = date('Y-m-d', strtotime($article['updated_at'] ?? $article['created_at']));
        $url = "https://nikolayfilatov.ru/article.php?slug=" . urlencode($article['slug']);
        
        echo "  <url>\n";
        echo "    <loc>" . htmlspecialchars($url) . "</loc>\n";
        echo "    <lastmod>" . $lastmod . "</lastmod>\n";
        echo "    <changefreq>monthly</changefreq>\n";
        echo "    <priority>0.8</priority>\n";
        
        if (!empty($article['picture'])) {
            echo "    <image:image>\n";
            echo "      <image:loc>https://nikolayfilatov.ru" . htmlspecialchars($article['picture']) . "</image:loc>\n";
            echo "      <image:title>" . htmlspecialchars($article['title']) . "</image:title>\n";
            echo "    </image:image>\n";
        }
        
        echo "  </url>\n";
    }
    
} catch (PDOException $e) {
    error_log("Sitemap generation error: " . $e->getMessage());
}
?>

</urlset> 