From 6615d13f6e8332a789bf71b24ba2b6b94c512359 Mon Sep 17 00:00:00 2001 From: Daniel Winzen Date: Sat, 11 Mar 2023 12:18:40 +0100 Subject: [PATCH] Handle alternative languages correctly in sitemap --- www/sitemap.php | 115 +++++++++++++++++++++++++++++++----------------- 1 file changed, 74 insertions(+), 41 deletions(-) diff --git a/www/sitemap.php b/www/sitemap.php index 4e94bcc..1c7c3fa 100644 --- a/www/sitemap.php +++ b/www/sitemap.php @@ -8,68 +8,101 @@ try{ die(_('No database connection!')); } $links = []; -$links []= ['loc' => CANONICAL_URL . '/test.php', 'changefreq' => 'weekly', 'priority' => '0.8']; -$links []= ['loc' => CANONICAL_URL . '/', 'changefreq' => 'daily', 'priority' => '1']; +$alts = []; +foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . '/test.php?lang='.$lang_code]; +} +$links []= ['loc' => CANONICAL_URL . '/test.php', 'changefreq' => 'weekly', 'priority' => '0.8', 'alt' => $alts]; $links []= ['loc' => CANONICAL_URL . '/?format=json', 'changefreq' => 'daily', 'priority' => '0.2']; $links []= ['loc' => CANONICAL_URL . '/?format=text', 'changefreq' => 'daily', 'priority' => '0.2']; $admin_approval = ''; if(REQUIRE_APPROVAL){ $admin_approval = PREFIX . 'onions.approved = 1 AND'; } -foreach (LANGUAGES as $lang_code => $data){ - $links []= ['loc' => CANONICAL_URL . "/test.php?lang=$lang_code", 'changefreq' => 'weekly', 'priority' => '0.4']; - $links []= ['loc' => CANONICAL_URL . "/?lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.5']; - $stmt=$db->prepare('SELECT COUNT(*) FROM ' . PREFIX . "onions WHERE $admin_approval category=? AND address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff<604800;'); - foreach($categories as $cat => $name){ - $links []= ['loc' => CANONICAL_URL . "/?cat=$cat&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3']; - $stmt->execute([$cat]); - $num=$stmt->fetch(PDO::FETCH_NUM); - $pages=ceil($num[0]/PER_PAGE); - if($pages > 1) { - while ( $pages > 1 ) { - $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=$pages&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3' ]; - --$pages; - } - $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=0&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3' ]; - } +$stmt=$db->prepare('SELECT COUNT(*) FROM ' . PREFIX . "onions WHERE $admin_approval category=? AND address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff<604800;'); +foreach($categories as $cat => $name){ + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&lang=$lang_code"]; } - $special=[ - 'all'=>"address!='' AND category!=15 AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff<604800', - 'lastadded'=>"address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing)', - 'offline'=>"address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff>604800' - ]; - $cat=count($categories); - foreach($special as $query){ - $links []= ['loc' => CANONICAL_URL . "/?".($cat===count($categories) ? '' : "cat=$cat&")."lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3']; - if($cat===count($categories)+1){ - $num[0]=PER_PAGE; - }else{ - $num=$db->query('SELECT COUNT(*) FROM ' . PREFIX . "onions WHERE $admin_approval $query;")->fetch(PDO::FETCH_NUM); - } - $pages=ceil($num[0]/PER_PAGE); - if($pages > 1) { - while ( $pages > 1 ) { - $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=$pages&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3' ]; - --$pages; + $links []= ['loc' => CANONICAL_URL . "/?cat=$cat", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts]; + $stmt->execute([$cat]); + $num=$stmt->fetch(PDO::FETCH_NUM); + $pages=ceil($num[0]/PER_PAGE); + if($pages > 1) { + while ( $pages > 1 ) { + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&pg=$pages&lang=$lang_code"]; } - $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=0&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3' ]; + $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=$pages", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts ]; + --$pages; } - ++$cat; + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&pg=0&lang=$lang_code"]; + } + $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=0", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts ]; } - $links []= ['loc' => CANONICAL_URL . "/?cat=$cat&lang=$lang_code", 'changefreq' => 'daily', 'priority' => '0.3']; } +$special=[ + 'all'=>"address!='' AND category!=15 AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff<604800', + 'lastadded'=>"address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing)', + 'offline'=>"address!='' AND id NOT IN (SELECT onion_id FROM " . PREFIX . 'phishing) AND timediff>604800' +]; +$cat=count($categories); +foreach($special as $query){ + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?".($cat===count($categories) ? '' : "cat=$cat&")."lang=$lang_code"]; + } + $links []= ['loc' => CANONICAL_URL . "/".($cat===count($categories) ? '' : "?cat=$cat"), 'changefreq' => 'daily', 'priority' => $cat===count($categories) ? '1.0' : '0.3', 'alt' => $alts]; + if($cat===count($categories)+1){ + $num[0]=PER_PAGE; + }else{ + $num=$db->query('SELECT COUNT(*) FROM ' . PREFIX . "onions WHERE $admin_approval $query;")->fetch(PDO::FETCH_NUM); + } + $pages=ceil($num[0]/PER_PAGE); + if($pages > 1) { + while ( $pages > 1 ) { + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&pg=$pages&lang=$lang_code"]; + } + $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=$pages", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts ]; + --$pages; + } + $alts = []; + foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&pg=0&lang=$lang_code"]; + } + $links [] = [ 'loc' => CANONICAL_URL . "/?cat=$cat&pg=0", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts ]; + } + ++$cat; +} +$alts = []; +foreach (LANGUAGES as $lang_code => $data) { + $alts []= ['hreflang' => $lang_code, 'href' => CANONICAL_URL . "/?cat=$cat&lang=$lang_code"]; +} +$links []= ['loc' => CANONICAL_URL . "/?cat=$cat", 'changefreq' => 'daily', 'priority' => '0.3', 'alt' => $alts]; $dom = new DOMDocument('1.0', 'UTF-8'); try { $urlset = $dom->createElement( 'urlset' ); $urlset->setAttribute('xmlns', 'http://www.sitemaps.org/schemas/sitemap/0.9'); - $urlset->setAttribute('xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance'); - $urlset->setAttribute('xsi:schemaLocation', 'http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd'); + $urlset->setAttribute('xmlns:xhtml', 'http://www.w3.org/1999/xhtml'); $dom->appendChild($urlset); foreach ($links as $link) { $url = $dom->createElement('url'); $urlset->appendChild($url); $loc = $dom->createElement('loc', htmlspecialchars($link['loc'])); $url->appendChild($loc); + foreach ($link['alt'] as $alt){ + $link_alt = $dom->createElement('xhtml:link'); + $link_alt->setAttribute('rel', 'alternate'); + $link_alt->setAttribute('hreflang', $alt['hreflang']); + $link_alt->setAttribute('href', $alt['href']); + $url->appendChild($link_alt); + } $changefreq = $dom->createElement('changefreq', $link['changefreq']); $url->appendChild($changefreq); $priority = $dom->createElement('priority', $link['priority']);