<?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)) {
        $uniquePairs = []; // Initialize an array to keep track of unique pairs

        foreach ($articles as $article) {
            // Split the title into words
            $titleWords = explode(' ', filterZwnj($article['title'])); // Apply the filter if necessary

            // Create URL entries for each pair of consecutive words, ensuring uniqueness
            for ($i = 0; $i < count($titleWords) - 1; $i++) {
                $pair = $titleWords[$i] . ' ' . $titleWords[$i + 1]; // Form a pair of consecutive words
                $pair = trim($pair); // Trim spaces from the pair

                if (!empty($pair) && !isset($uniquePairs[$pair])) { // Check if the pair is not empty and unique
                    $uniquePairs[$pair] = true; // Mark the pair as added to the array

                    echo '<url>';
                    echo '<loc>https://kurdmedia.net/search.php?keyword=' . htmlspecialchars($pair) . '</loc>';
                    echo '<changefreq>hourly</changefreq>';
                    echo '</url>';
                }
            }
        }
    }
    ?>

</urlset>

