[sitemap] add tests, and big rewrite to pass tests, fixes #1016

This commit is contained in:
Shish 2024-01-18 19:47:48 +00:00
parent cc9cd396ad
commit 2a561392d0
2 changed files with 94 additions and 137 deletions

View file

@ -4,173 +4,131 @@ declare(strict_types=1);
namespace Shimmie2; namespace Shimmie2;
class XMLSitemapURL
{
public function __construct(
public string $url,
public string $changefreq,
public string $priority,
public string $date
) {
}
}
class XMLSitemap extends Extension class XMLSitemap extends Extension
{ {
private string $sitemap_queue = "";
private string $sitemap_filepath = ""; // set onPageRequest
public function onPageRequest(PageRequestEvent $event): void public function onPageRequest(PageRequestEvent $event): void
{ {
if ($event->page_matches("sitemap.xml")) { if ($event->page_matches("sitemap.xml")) {
global $config; global $config, $page;
$this->sitemap_filepath = data_path("cache/sitemap.xml"); $cache_path = data_path("cache/sitemap.xml");
// determine if new sitemap needs to be generated
if ($this->new_sitemap_needed()) { if ($this->new_sitemap_needed($cache_path)) {
// determine which type of sitemap to generate $xml = $this->handle_full_sitemap();
if ($config->get_bool("sitemap_generatefull", false)) { file_put_contents($cache_path, $xml);
$this->handle_full_sitemap(); // default false until cache fixed
} else {
$this->handle_smaller_sitemap();
}
} else {
$this->display_existing_sitemap();
} }
$xml = file_get_contents($cache_path);
$page->set_mode(PageMode::DATA);
$page->set_mime(MimeType::XML_APPLICATION);
$page->set_data($xml);
} }
} }
public function onSetupBuilding(SetupBuildingEvent $event): void
{
$sb = $event->panel->create_new_block("Sitemap");
$sb->add_bool_option("sitemap_generatefull", "Generate full sitemap");
$sb->add_label("<br>(Enabled: every image and tag in sitemap, generation takes longer)");
$sb->add_label("<br>(Disabled: only display the last 50 uploads in the sitemap)");
}
// sitemap with only the latest 50 images
private function handle_smaller_sitemap()
{
/* --- Add latest images to sitemap with higher priority --- */
$latestimages = Search::find_images(limit: 50);
if (empty($latestimages)) {
return;
}
$latestimages_urllist = [];
$last_image = null;
foreach ($latestimages as $arrayid => $image) {
// create url from image id's
$latestimages_urllist[$arrayid] = "post/view/$image->id";
$last_image = $image;
}
$this->add_sitemap_queue(
$latestimages_urllist,
"monthly",
"0.8",
date("Y-m-d", strtotime($last_image->posted))
);
/* --- Display page --- */
// when sitemap is ok, display it from the file
$this->generate_display_sitemap();
}
// Full sitemap // Full sitemap
private function handle_full_sitemap() private function handle_full_sitemap()
{ {
global $database, $config; global $database, $config;
$urls = [];
// add index // add index
$index = []; $urls[] = new XMLSitemapURL(
$index[0] = $config->get_string(SetupConfig::FRONT_PAGE); $config->get_string(SetupConfig::FRONT_PAGE),
$this->add_sitemap_queue($index, "weekly", "1"); "weekly",
"1",
date("Y-m-d")
);
/* --- Add 20 most used tags --- */ /* --- Add 20 most used tags --- */
$popular_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 0,20"); foreach ($database->get_col("SELECT tag FROM tags ORDER BY count DESC LIMIT 20") as $tag) {
foreach ($popular_tags as $arrayid => $tag) { $urls[] = new XMLSitemapURL(
$tag = $tag['tag']; "post/list/$tag/1",
$popular_tags[$arrayid] = "post/list/$tag/"; "weekly",
"0.9",
date("Y-m-d")
);
} }
$this->add_sitemap_queue($popular_tags, "monthly", "0.9" /* not sure how to deal with date here */);
/* --- Add latest images to sitemap with higher priority --- */ /* --- Add latest images to sitemap with higher priority --- */
$latestimages = Search::find_images(limit: 50); foreach(Search::find_images(limit: 50) as $image) {
$latestimages_urllist = []; $urls[] = new XMLSitemapURL(
$latest_image = null; "post/view/$image->id",
foreach ($latestimages as $arrayid => $image) { "weekly",
// create url from image id's "0.8",
$latestimages_urllist[$arrayid] = "post/view/$image->id"; date("Y-m-d", strtotime($image->posted))
$latest_image = $image; );
} }
$this->add_sitemap_queue($latestimages_urllist, "monthly", "0.8", date("Y-m-d", strtotime($latest_image->posted)));
/* --- Add other tags --- */ /* --- Add other tags --- */
$other_tags = $database->get_all("SELECT tag, count FROM tags ORDER BY `count` DESC LIMIT 21,10000000"); foreach ($database->get_col("SELECT tag FROM tags ORDER BY count DESC LIMIT 10000 OFFSET 21") as $tag) {
foreach ($other_tags as $arrayid => $tag) { $urls[] = new XMLSitemapURL(
$tag = $tag['tag']; "post/list/$tag/1",
// create url from tags (tagme ignored) "weekly",
if ($tag != "tagme") { "0.7",
$other_tags[$arrayid] = "post/list/$tag/"; date("Y-m-d")
} );
} }
$this->add_sitemap_queue($other_tags, "monthly", "0.7" /* not sure how to deal with date here */);
/* --- Add all other images to sitemap with lower priority --- */ /* --- Add all other images to sitemap with lower priority --- */
$otherimages = Search::find_images(offset: 51, limit: 10000000); foreach(Search::find_images(offset: 51, limit: 10000) as $image) {
$image = null; $urls[] = new XMLSitemapURL(
foreach ($otherimages as $arrayid => $image) { "post/view/$image->id",
// create url from image id's "monthly",
$otherimages[$arrayid] = "post/view/$image->id"; "0.6",
date("Y-m-d", strtotime($image->posted))
);
} }
assert(!is_null($image));
$this->add_sitemap_queue($otherimages, "monthly", "0.6", date("Y-m-d", strtotime($image->posted)));
/* --- Display page --- */ /* --- Display page --- */
// when sitemap is ok, display it from the file return $this->generate_sitemap($urls);
$this->generate_display_sitemap();
} }
/** /**
* Adds an array of urls to the sitemap with the given information. * @param XMLSitemapURL[] $urls
*/ */
private function add_sitemap_queue( private function generate_sitemap(array $urls): string
array $urls,
string $changefreq = "monthly",
string $priority = "0.5",
string $date = "2013-02-01"
) {
foreach ($urls as $url) {
$link = make_http(make_link("$url"));
$this->sitemap_queue .= "
<url>
<loc>$link</loc>
<lastmod>$date</lastmod>
<changefreq>$changefreq</changefreq>
<priority>$priority</priority>
</url>";
}
}
// sets sitemap with entries in sitemap_queue
private function generate_display_sitemap()
{ {
global $page; $xml = "<" . "?xml version=\"1.0\" encoding=\"utf-8\"?" . ">\n" .
"<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">\n";
foreach($urls as $url) {
$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>
";
}
$xml .= "</urlset>\n";
$xml = "<" . "?xml version=\"1.0\" encoding=\"utf-8\"?" . "> return $xml;
<urlset xmlns=\"http://www.sitemaps.org/schemas/sitemap/0.9\">
$this->sitemap_queue
</urlset>";
// Generate new sitemap
file_put_contents($this->sitemap_filepath, $xml);
$page->set_mode(PageMode::DATA);
$page->set_mime(MimeType::XML_APPLICATION);
$page->set_data($xml);
} }
/** /**
* Returns true if a new sitemap is needed. * Returns true if a new sitemap is needed.
*/ */
private function new_sitemap_needed(): bool private function new_sitemap_needed($cache_path): bool
{ {
if (!file_exists($this->sitemap_filepath)) { if (!file_exists($cache_path)) {
return true; return true;
} }
$sitemap_generation_interval = 86400; // allow new site map every day $sitemap_generation_interval = 86400; // allow new site map every day
$last_generated_time = filemtime($this->sitemap_filepath); $last_generated_time = filemtime($cache_path);
// if file doesn't exist, return true // if file doesn't exist, return true
if ($last_generated_time == false) { if ($last_generated_time == false) {
@ -178,21 +136,6 @@ class XMLSitemap extends Extension
} }
// if it's been a day since last sitemap creation, return true // if it's been a day since last sitemap creation, return true
if ($last_generated_time + $sitemap_generation_interval < time()) { return ($last_generated_time + $sitemap_generation_interval < time());
return true;
} else {
return false;
}
}
private function display_existing_sitemap()
{
global $page;
$xml = file_get_contents($this->sitemap_filepath);
$page->set_mode(PageMode::DATA);
$page->set_mime(MimeType::XML_APPLICATION);
$page->set_data($xml);
} }
} }

View file

@ -8,6 +8,20 @@ class XMLSitemapTest extends ShimmiePHPUnitTestCase
{ {
public function testBasic(): void public function testBasic(): void
{ {
// check empty DB
@unlink(data_path("cache/sitemap.xml"));
$page = $this->get_page('sitemap.xml');
$this->assertEquals(200, $page->code);
$this->log_in_as_user();
$this->post_image("tests/pbx_screenshot.jpg", "pbx computer screenshot");
// check DB with one image
@unlink(data_path("cache/sitemap.xml"));
$page = $this->get_page('sitemap.xml');
$this->assertEquals(200, $page->code);
// check caching
$page = $this->get_page('sitemap.xml'); $page = $this->get_page('sitemap.xml');
$this->assertEquals(200, $page->code); $this->assertEquals(200, $page->code);
} }