update-sitemap.php 1.05 KB
Newer Older
dmorley's avatar
dmorley committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
<?php

/**
 * Create a basic sitemap.xml file to help site get indexed
 */

declare(strict_types=1);

use RedBeanPHP\R;

require_once __DIR__ . '/../boot.php';

try {
    $pods = R::getAll('
        SELECT domain, date_updated::TIMESTAMP::DATE
        FROM pods
        WHERE date_updated IS NOT NULL
        ORDER BY date_updated DESC
    ');

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

    foreach ($pods as $pod) {
        $xml .= "\t<url>\n";
        $xml .= "\t\t<loc>https://podupti.me/{$pod['domain']}</loc>\n";
        $xml .= "\t\t<lastmod>{$pod['date_updated']}</lastmod>\n";
        $xml .= "\t\t<changefreq>hourly</changefreq>\n";
        $xml .= "\t</url>\n";
    }

    $xml .= '</urlset>';

    file_put_contents(c('sitemap'), $xml);
} catch (\RedBeanPHP\RedException $e) {
    debug('Error in SQL query: ' . sprintf($e->getMessage()));
} catch (\Exception $e) {
    debug('Error updating sitemap: ' . sprintf($e->getMessage()));
}

updateMeta('sitemap_updated');