<?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('‌', ' Rzwnj ', '&', ';');
    $filteredInput = str_replace($charactersToFilter, '', $input);
    return $filteredInput;
}

// Begin the XML output for the sitemap
echo '<?xml version="1.0" encoding="UTF-8"?>';
?>
<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 -->
    <url>
        <loc>https://www.kurdmedia.net/kurdi/</loc>
        <changefreq>hourly</changefreq>
    </url>
    <?php
    // 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) {
            // Split the title into words
            $titleWords = explode(' ', filterZwnj($article['title'])); // Apply the filter if necessary

            // Create a URL entry for each word, ensuring uniqueness
            foreach ($titleWords as $word) {
                $word = trim($word); // Trim spaces from each word
                if (!empty($word) && !isset($uniqueWords[$word])) { // Check if the word is not empty and unique
                    $uniqueWords[$word] = true; // Mark the word as added to the array

                    echo '<url>';
                    echo '<loc>https://kurdmedia.net/search.php?keyword=' . htmlspecialchars($word) . '</loc>';
                    echo '<changefreq>hourly</changefreq>';
                    echo '</url>';
                }
            }
        }
    }
    ?>



</urlset>
