From b68c2eb5687191fc9a354fc1a6d5d1dc7700e3d1 Mon Sep 17 00:00:00 2001 From: Timothy Allen Date: Sat, 26 Sep 2026 14:28:32 +0200 Subject: [PATCH] Give the feed of an empty site the current date. The feed passed NULL to strtotime() when there were no stories. --- feed.php | 6 ++++-- tests/php/Pages/FeedAndImageTest.php | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/feed.php b/feed.php index 564c60c..c15cafb 100644 --- a/feed.php +++ b/feed.php @@ -18,6 +18,8 @@ $query = "SELECT MAX(`date`) FROM $table;"; $result = $db->query($query) or db_fail(); $most_recent = $result->fetch_row(); $now = date("D, d M Y H:i:s O"); +// The date of the newest story, or now if there is none +$last_date = $most_recent[0] ? date("D, d M Y H:i:s O", strtotime($most_recent[0])) : $now; echo ''."\n"; echo ''."\n"; @@ -27,14 +29,14 @@ echo 'Most recent articles from '.xml_text(site_host()).''."\n"; echo 'en-za'."\n"; echo ''.$now.''."\n"; - echo ''.date("D, d M Y H:i:s O", strtotime($most_recent[0])).''."\n"; + echo ''.$last_date.''."\n"; echo ''.hidden_email(site_email('editor')).' (Editors)'."\n"; echo ''.hidden_email(site_email('webmaster')).' (Webmasters)'."\n"; echo ''."\n"; echo ''."\n"; echo 'Front Page'."\n"; echo ''.$site.'/'."\n"; - echo ''.date("D, d M Y H:i:s O", strtotime($most_recent[0])).''."\n"; + echo ''.$last_date.''."\n"; echo ''.$site.'/'."\n"; echo ''."\n"; diff --git a/tests/php/Pages/FeedAndImageTest.php b/tests/php/Pages/FeedAndImageTest.php index 381fca6..530b67a 100644 --- a/tests/php/Pages/FeedAndImageTest.php +++ b/tests/php/Pages/FeedAndImageTest.php @@ -42,6 +42,17 @@ class FeedAndImageTest extends PageTestCase { $this->assertSame(date('D, d M Y H:i:s O', strtotime('2021-01-03 10:00:00')), (string)$xml->channel->lastBuildDate); } + // Without stories, the feed dates are the time of the request + public function testTheFeedOfAnEmptySite(): void { + $this->setTables(array()); + $xml = simplexml_load_string($this->get('/feed')['body']); + $this->assertNotFalse($xml); + $now = (string)$xml->channel->pubDate; + $this->assertSame($now, (string)$xml->channel->lastBuildDate); + $this->assertSame($now, (string)$xml->channel->item[0]->pubDate); + $this->assertCount(1, $xml->channel->item); + } + public function testAnImageIsServedWithItsRealType(): void { $image = $this->get('/display?id=2'); $this->assertSame(200, $image['status']);