2021-12-14 18:32:47 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
2014-04-28 06:24:19 +00:00
|
|
|
|
2023-01-10 22:44:09 +00:00
|
|
|
namespace Shimmie2;
|
2013-02-11 07:43:06 +00:00
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
class XMLSitemapURL
|
2014-04-28 06:24:19 +00:00
|
|
|
{
|
2024-01-18 19:47:48 +00:00
|
|
|
public function __construct(
|
|
|
|
public string $url,
|
|
|
|
public string $changefreq,
|
|
|
|
public string $priority,
|
|
|
|
public string $date
|
|
|
|
) {
|
|
|
|
}
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
class XMLSitemap extends Extension
|
|
|
|
{
|
2024-01-15 11:52:35 +00:00
|
|
|
public function onPageRequest(PageRequestEvent $event): void
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
if ($event->page_matches("sitemap.xml")) {
|
2024-01-18 19:47:48 +00:00
|
|
|
global $config, $page;
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
$cache_path = data_path("cache/sitemap.xml");
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
if ($this->new_sitemap_needed($cache_path)) {
|
|
|
|
$xml = $this->handle_full_sitemap();
|
|
|
|
file_put_contents($cache_path, $xml);
|
|
|
|
}
|
2019-05-28 16:59:38 +00:00
|
|
|
|
2024-02-20 00:22:25 +00:00
|
|
|
$xml = \Safe\file_get_contents($cache_path);
|
2024-01-18 19:47:48 +00:00
|
|
|
$page->set_mode(PageMode::DATA);
|
|
|
|
$page->set_mime(MimeType::XML_APPLICATION);
|
|
|
|
$page->set_data($xml);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Full sitemap
|
2024-01-20 14:10:59 +00:00
|
|
|
private function handle_full_sitemap(): string
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
|
|
|
global $database, $config;
|
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
$urls = [];
|
|
|
|
|
2019-05-28 16:59:38 +00:00
|
|
|
// add index
|
2024-01-18 19:47:48 +00:00
|
|
|
$urls[] = new XMLSitemapURL(
|
|
|
|
$config->get_string(SetupConfig::FRONT_PAGE),
|
|
|
|
"weekly",
|
|
|
|
"1",
|
|
|
|
date("Y-m-d")
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
/* --- Add 20 most used tags --- */
|
2024-01-18 19:47:48 +00:00
|
|
|
foreach ($database->get_col("SELECT tag FROM tags ORDER BY count DESC LIMIT 20") as $tag) {
|
|
|
|
$urls[] = new XMLSitemapURL(
|
|
|
|
"post/list/$tag/1",
|
|
|
|
"weekly",
|
|
|
|
"0.9",
|
|
|
|
date("Y-m-d")
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --- Add latest images to sitemap with higher priority --- */
|
2024-08-31 16:05:18 +00:00
|
|
|
foreach (Search::find_images(limit: 50) as $image) {
|
2024-01-18 19:47:48 +00:00
|
|
|
$urls[] = new XMLSitemapURL(
|
|
|
|
"post/view/$image->id",
|
|
|
|
"weekly",
|
|
|
|
"0.8",
|
2024-02-20 00:22:25 +00:00
|
|
|
date("Y-m-d", \Safe\strtotime($image->posted))
|
2024-01-18 19:47:48 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --- Add other tags --- */
|
2024-01-18 19:47:48 +00:00
|
|
|
foreach ($database->get_col("SELECT tag FROM tags ORDER BY count DESC LIMIT 10000 OFFSET 21") as $tag) {
|
|
|
|
$urls[] = new XMLSitemapURL(
|
|
|
|
"post/list/$tag/1",
|
|
|
|
"weekly",
|
|
|
|
"0.7",
|
|
|
|
date("Y-m-d")
|
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --- Add all other images to sitemap with lower priority --- */
|
2024-08-31 16:05:18 +00:00
|
|
|
foreach (Search::find_images(offset: 51, limit: 10000) as $image) {
|
2024-01-18 19:47:48 +00:00
|
|
|
$urls[] = new XMLSitemapURL(
|
|
|
|
"post/view/$image->id",
|
|
|
|
"monthly",
|
|
|
|
"0.6",
|
2024-02-20 00:22:25 +00:00
|
|
|
date("Y-m-d", \Safe\strtotime($image->posted))
|
2024-01-18 19:47:48 +00:00
|
|
|
);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --- Display page --- */
|
2024-01-18 19:47:48 +00:00
|
|
|
return $this->generate_sitemap($urls);
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-01-18 19:47:48 +00:00
|
|
|
* @param XMLSitemapURL[] $urls
|
2019-05-28 16:59:38 +00:00
|
|
|
*/
|
2024-01-18 19:47:48 +00:00
|
|
|
private function generate_sitemap(array $urls): string
|
|
|
|
{
|
|
|
|
$xml = "<" . "?xml version=\"1.0\" encoding=\"utf-8\"?" . ">\n" .
|
|
|
|
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
|
2024-08-31 16:05:18 +00:00
|
|
|
foreach ($urls as $url) {
|
2024-01-18 19:47:48 +00:00
|
|
|
$link = make_http(make_link($url->url));
|
|
|
|
$xml .= "
|
|
|
|
<url>
|
|
|
|
<loc>$link</loc>
|
|
|
|
<lastmod>$url->date</lastmod>
|
|
|
|
<changefreq>$url->changefreq</changefreq>
|
|
|
|
<priority>$url->priority</priority>
|
|
|
|
</url>
|
|
|
|
";
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2024-01-18 19:47:48 +00:00
|
|
|
$xml .= "</urlset>\n";
|
2014-04-28 06:24:19 +00:00
|
|
|
|
2024-01-18 19:47:48 +00:00
|
|
|
return $xml;
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns true if a new sitemap is needed.
|
|
|
|
*/
|
2024-01-20 14:10:59 +00:00
|
|
|
private function new_sitemap_needed(string $cache_path): bool
|
2019-05-28 16:59:38 +00:00
|
|
|
{
|
2024-01-18 19:47:48 +00:00
|
|
|
if (!file_exists($cache_path)) {
|
2019-05-28 16:59:38 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sitemap_generation_interval = 86400; // allow new site map every day
|
2024-01-18 19:47:48 +00:00
|
|
|
$last_generated_time = filemtime($cache_path);
|
2019-05-28 16:59:38 +00:00
|
|
|
|
|
|
|
// if file doesn't exist, return true
|
|
|
|
if ($last_generated_time == false) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if it's been a day since last sitemap creation, return true
|
2024-01-18 19:47:48 +00:00
|
|
|
return ($last_generated_time + $sitemap_generation_interval < time());
|
2019-05-28 16:59:38 +00:00
|
|
|
}
|
2013-02-11 07:43:06 +00:00
|
|
|
}
|