update-sitemap.php 1.31 KB
Newer Older
dmorley's avatar
dmorley committed
1 2 3 4 5 6 7 8 9 10 11
<?php

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

declare(strict_types=1);

use RedBeanPHP\R;

require_once __DIR__ . '/../boot.php';
dmorley's avatar
dmorley committed
12

dmorley's avatar
dmorley committed
13 14
try {
    $pods = R::getAll('
dmorley's avatar
dmorley committed
15
        SELECT softwarename, domain, date_updated::TIMESTAMP::DATE
dmorley's avatar
dmorley committed
16 17 18 19 20 21 22 23 24 25
        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";
dmorley's avatar
dmorley committed
26 27 28 29 30 31
        if ($pod['softwarename']) {
            $softwareclean = preg_replace("/[^a-zA-Z]/", "", $pod['softwarename']);
            $xml .= "\t\t<loc>https://{$softwareclean}.podupti.me/{$pod['domain']}</loc>\n";
        } else {
            $xml .= "\t\t<loc>https://podupti.me/{$pod['domain']}</loc>\n";
        }
dmorley's avatar
dmorley committed
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
        $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');