diff --git a/lib/Feed.php b/lib/Feed.php index a81b2f3c..33a3b34c 100644 --- a/lib/Feed.php +++ b/lib/Feed.php @@ -287,11 +287,15 @@ class Feed { $offset = $this->normalizeDateDiff($diff); $now->modify("+".$offset); } else { + // the algorithm for updated feeds (returning 200 rather than 304) uses the same parameters as for 304, + // save that the last three intervals between item dates are computed, and if any two fall within + // the same interval range, that interval is used (e.g. if the intervals are 23m, 12m, and 4h, the used + // interval is "less than 30m"). If there is no commonality, the feed is checked in 1 hour. $offsets = []; $dates = $this->gatherDates(); if(sizeof($dates) > 3) { for($a = 0; $a < 3; $a++) { - $diff = $dates[$a+1] - $dates[$a]; + $diff = $dates[$a] - $dates[$a+1]; $offsets[] = $this->normalizeDateDiff($diff); } if($offsets[0]==$offsets[1] || $offsets[0]==$offsets[2]) { diff --git a/tests/Feed/TestFeed.php b/tests/Feed/TestFeed.php index fabcce56..0fb1851d 100644 --- a/tests/Feed/TestFeed.php +++ b/tests/Feed/TestFeed.php @@ -251,4 +251,31 @@ class TestFeed extends \PHPUnit\Framework\TestCase { $exp = strtotime("now + 3 hours"); $this->assertTime($exp, $f->nextFetch); } + + function testComputeNextFetchFrom200() { + // if less than half an hour, check in 15 minutes + $f = new Feed(null, $this->base."NextFetch/30m"); + $exp = strtotime("now + 15 minutes"); + $this->assertTime($exp, $f->nextFetch); + // if less than an hour, check in 30 minutes + $f = new Feed(null, $this->base."NextFetch/1h"); + $exp = strtotime("now + 30 minutes"); + $this->assertTime($exp, $f->nextFetch); + // if less than three hours, check in an hour + $f = new Feed(null, $this->base."NextFetch/3h"); + $exp = strtotime("now + 1 hour"); + $this->assertTime($exp, $f->nextFetch); + // if more than 36 hours, check in 24 hours + $f = new Feed(null, $this->base."NextFetch/36h"); + $exp = strtotime("now + 24 hours"); + $this->assertTime($exp, $f->nextFetch); + // otherwise check in three hours + $f = new Feed(null, $this->base."NextFetch/3-36h"); + $exp = strtotime("now + 3 hours"); + $this->assertTime($exp, $f->nextFetch); + // and if there is no common interval, check in an hour + $f = new Feed(null, $this->base."NextFetch/Fallback"); + $exp = strtotime("now + 1 hour"); + $this->assertTime($exp, $f->nextFetch); + } } \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/1h.php b/tests/docroot/Feed/NextFetch/1h.php new file mode 100644 index 00000000..ad8bfe7e --- /dev/null +++ b/tests/docroot/Feed/NextFetch/1h.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Sat, 27 May 2017 22:02:00 GMT + http://example.com/1 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/2 + + + Sat, 27 May 2017 22:00:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 20:21:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/3-36h.php b/tests/docroot/Feed/NextFetch/3-36h.php new file mode 100644 index 00000000..9ae4bb9f --- /dev/null +++ b/tests/docroot/Feed/NextFetch/3-36h.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Sat, 27 May 2017 07:00:00 GMT + http://example.com/1 + + + Sat, 27 May 2017 12:00:00 GMT + http://example.com/2 + + + Sat, 27 May 2017 16:00:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/30m.php b/tests/docroot/Feed/NextFetch/30m.php new file mode 100644 index 00000000..35b42d76 --- /dev/null +++ b/tests/docroot/Feed/NextFetch/30m.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Sat, 27 May 2017 11:00:00 GMT + http://example.com/1 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/2 + + + Sat, 27 May 2017 22:00:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 21:31:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/36h.php b/tests/docroot/Feed/NextFetch/36h.php new file mode 100644 index 00000000..85e505d8 --- /dev/null +++ b/tests/docroot/Feed/NextFetch/36h.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Mon, 22 May 2017 07:00:00 GMT + http://example.com/1 + + + Wed, 24 May 2017 12:00:00 GMT + http://example.com/2 + + + Fri, 26 May 2017 16:00:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/3h.php b/tests/docroot/Feed/NextFetch/3h.php new file mode 100644 index 00000000..9ed43588 --- /dev/null +++ b/tests/docroot/Feed/NextFetch/3h.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Sat, 27 May 2017 07:00:00 GMT + http://example.com/1 + + + Sat, 27 May 2017 09:12:00 GMT + http://example.com/2 + + + Sat, 27 May 2017 12:00:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file diff --git a/tests/docroot/Feed/NextFetch/Fallback.php b/tests/docroot/Feed/NextFetch/Fallback.php new file mode 100644 index 00000000..11cc30bf --- /dev/null +++ b/tests/docroot/Feed/NextFetch/Fallback.php @@ -0,0 +1,29 @@ + "application/rss+xml", + 'content' => << + + Example title + http://example.com + Example description + + + Sat, 27 May 2017 11:00:00 GMT + http://example.com/1 + + + Sat, 27 May 2017 21:12:00 GMT + http://example.com/2 + + + Sat, 27 May 2017 21:20:00 GMT + http://example.com/3 + + + Sat, 27 May 2017 22:10:00 GMT + http://example.com/4 + + + +MESSAGE_BODY +]; \ No newline at end of file