Fixed sitemap to find stylesheet, relative urls

This commit is contained in:
2025-05-16 13:48:37 -06:00
parent f6c4505ea4
commit 31ace4a40f
7 changed files with 19 additions and 241 deletions
+19 -4
View File
@@ -14,13 +14,27 @@
priority: optional, relative to other pages, from 0.0 to 1.0
*/
header('Content-Type: text/xml; charset=utf-8');
$stylesheet_url = $config['SITEMAP_STYLESHEET'] ?? '/sitemap/sitemap.xsl';
$fileglob = glob('*.php');
$base = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['SERVER_NAME']}";
# Server Settings. Will select defaults for CLI usage
$document_root = $_SERVER['DOCUMENT_ROOT'] ?: realpath(__DIR__.'/..');
$scheme = (!empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] !== 'off') ? 'https' : 'http';
$host = $_SERVER['SERVER_NAME'] ?? 'localhost';
$base_url = $scheme . '://' . $host;
# Check for stylesheet before emitting xsl record
if (file_exists($document_root.$stylesheet_url)) {
$stylesheet = "<?xml-stylesheet type=\"text/xsl\" href=\"$stylesheet_url\"?" . ">\n";
} else {
error_log(__FILE__ . "$name cannot be found in $document_root");
}
# Find relative folder and scan for docs
$article_path = str_replace($document_root, '', dirname(__FILE__));
$fileglob = glob('*.{php,md}',GLOB_BRACE);
?>
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="/sitemap.xsl"?>
<?= $stylesheet ?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:image="http://www.google.com/schemas/sitemap-image/1.1"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd http://www.google.com/schemas/sitemap-image/1.1 http://www.google.com/schemas/sitemap-image/1.1/sitemap-image.xsd"
@@ -30,6 +44,7 @@ $base = "{$_SERVER['REQUEST_SCHEME']}://{$_SERVER['SERVER_NAME']}";
foreach($fileglob as $file) {
if ($file === 'index.php') continue;
if ($file === 'sitemap.php') continue;
if ($file === 'README.md') continue;
# $page = file_get_contents($file);
# $update = stat($file)->modified_date;
@@ -37,7 +52,7 @@ foreach($fileglob as $file) {
$update = '2025-05-07';
?>
<url>
<loc><?= $base ?>/articles/<?= $file ?></loc>
<loc><?= $base_url . $article_path . '/' . $file ?></loc>
<lastmod><?= $update ?></lastmod>
</url>
<?php