<?php
// Set the content type to XML and specify charset for UTF-8 encoded content
header('Content-Type: application/xml; charset=utf-8');

// Include your data model or class file; adjust the path as needed
require_once("../models/class.php");

// Initialize your data object; ensure that Mallper class has a method for fetching articles
$objData = new Mallper();

// Define a function to filter specific characters; adjust as needed
function filterZwnj($input) {
    $charactersToFilter = array('‌', 'zwnj', '&', ';');
    $filteredInput = str_replace($charactersToFilter, '', $input);
    return $filteredInput;
}

// Begin the XML output for the sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>';
echo "\n<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd\">";

// Static URL example
echo "\n    <url>";
echo "\n        <loc>https://www.kurdmedia.net/kurdi/</loc>";
echo "\n        <changefreq>hourly</changefreq>";
echo "\n    </url>";

// Fetch article data; ensure 'getData' correctly fetches the desired articles
$articles = $objData->getData('so_article', 'id', 'ignore');
if (is_array($articles) && !empty($articles)) {
    $uniqueWords = []; // Initialize an array to keep track of unique words

    foreach ($articles as $article) {
        // Assuming you want to process article titles instead of content
        $titleWords = explode(' ', filterZwnj($article['authorname'])); // Adjusted to 'title'

        // Create a URL entry for each unique word
        foreach ($titleWords as $word) {
            $word = trim($word); // Trim spaces from each word
            if (!empty($word)) {
                $word = filter_var(strip_tags($word), FILTER_SANITIZE_STRING); // Sanitize
                
                if (!isset($uniqueWords[$word])) { // Ensure uniqueness
                    $uniqueWords[$word] = true; // Mark as added

                    echo "\n    <url>";
                    echo "\n        <loc>https://kurdmedia.net/search.php?keyword=" . htmlspecialchars($word) . "</loc>";
                    echo "\n        <changefreq>hourly</changefreq>";
                    echo "\n    </url>";
                }
            }
        }
    }
}

echo "\n</urlset>"; // Close the urlset element
?>

