From f2adbeb2a41430f28a3427ddb4365c4075e9296a Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 6 Apr 2013 21:15:42 -0400 Subject: [PATCH] Add support for HTTP caching with Etag and LastModified --- miniflux/common.php | 2 +- miniflux/model.php | 112 ++++++++++++++++++++++++++++---------------- miniflux/schema.php | 11 ++++- 3 files changed, 82 insertions(+), 43 deletions(-) diff --git a/miniflux/common.php b/miniflux/common.php index 6f1f988..806aab6 100644 --- a/miniflux/common.php +++ b/miniflux/common.php @@ -9,7 +9,7 @@ require 'schema.php'; require 'model.php'; -const DB_VERSION = 1; +const DB_VERSION = 2; const APP_VERSION = 'master'; const APP_USERAGENT = 'Miniflux - http://miniflux.net'; const HTTP_TIMEOUT = 5; diff --git a/miniflux/model.php b/miniflux/model.php index 5f83a77..8f4cecb 100644 --- a/miniflux/model.php +++ b/miniflux/model.php @@ -64,7 +64,7 @@ function import_feeds($content) function import_feed($url) { $reader = new Reader; - $reader->download($url, HTTP_TIMEOUT, APP_USERAGENT); + $resource = $reader->download($url, '', '', HTTP_TIMEOUT, APP_USERAGENT); $parser = $reader->getParser(); @@ -72,15 +72,14 @@ function import_feed($url) $feed = $parser->execute(); - if (! $feed->title || ! $feed->url) { - - return false; - } + if ($feed === false) return false; + if (! $feed->title || ! $feed->url) return false; $db = \PicoTools\singleton('db'); if (! $db->table('feeds')->eq('feed_url', $reader->getUrl())->count()) { + // Etag and LastModified are added the next update $rs = $db->table('feeds')->save(array( 'title' => $feed->title, 'site_url' => $feed->url, @@ -101,6 +100,62 @@ function import_feed($url) } +function update_feeds() +{ + foreach (get_feeds_id() as $feed_id) { + + update_feed($feed_id); + } +} + + +function update_feed($feed_id) +{ + $feed = get_feed($feed_id); + + $reader = new Reader; + + $resource = $reader->download( + $feed['feed_url'], + $feed['last_modified'], + $feed['etag'], + HTTP_TIMEOUT, + APP_USERAGENT + ); + + if (! $resource->isModified()) { + + return true; + } + + $parser = $reader->getParser(); + + if ($parser !== false) { + + $feed = $parser->execute(); + + if ($feed !== false) { + + update_feed_cache_infos($feed_id, $resource->getLastModified(), $resource->getEtag()); + update_items($feed_id, $feed->items); + + return true; + } + } + + return false; +} + + +function get_feeds_id() +{ + return \PicoTools\singleton('db') + ->table('feeds') + ->asc('updated') + ->listing('id', 'id'); +} + + function get_feeds() { return \PicoTools\singleton('db') @@ -119,10 +174,21 @@ function get_feed($feed_id) } +function update_feed_cache_infos($feed_id, $last_modified, $etag) +{ + \PicoTools\singleton('db') + ->table('feeds') + ->eq('id', $feed_id) + ->save(array( + 'last_modified' => $last_modified, + 'etag' => $etag + )); +} + + function remove_feed($feed_id) { $db = \PicoTools\singleton('db'); - $db->table('items')->eq('feed_id', $feed_id)->remove(); return $db->table('feeds')->eq('id', $feed_id)->remove(); @@ -263,40 +329,6 @@ function flush_read() } -function update_feeds() -{ - foreach (get_feeds() as $feed) { - - $reader = new Reader; - $reader->download($feed['feed_url'], HTTP_TIMEOUT, APP_USERAGENT); - $parser = $reader->getParser(); - - if ($parser !== false) { - - update_items($feed['id'], $parser->execute()->items); - } - } -} - - -function update_feed($feed_id) -{ - $feed = get_feed($feed_id); - - $reader = new Reader; - $reader->download($feed['feed_url'], HTTP_TIMEOUT, APP_USERAGENT); - $parser = $reader->getParser(); - - if ($parser !== false) { - - update_items($feed['id'], $parser->execute()->items); - return true; - } - - return false; -} - - function update_items($feed_id, array $items) { $db = \PicoTools\singleton('db'); diff --git a/miniflux/schema.php b/miniflux/schema.php index c47f16d..e446af3 100644 --- a/miniflux/schema.php +++ b/miniflux/schema.php @@ -2,13 +2,20 @@ namespace Schema; + +function version_2($pdo) +{ + $pdo->exec('ALTER TABLE feeds ADD COLUMN last_modified TEXT'); + $pdo->exec('ALTER TABLE feeds ADD COLUMN etag TEXT'); +} + + function version_1($pdo) { $pdo->exec(" CREATE TABLE config ( username TEXT DEFAULT 'admin', - password TEXT, - history INTEGER DEFAULT '15' + password TEXT ) ");