Add support for HTTP caching with Etag and LastModified
This commit is contained in:
parent
3a1050b749
commit
f2adbeb2a4
@ -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;
|
||||
|
@ -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');
|
||||
|
@ -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
|
||||
)
|
||||
");
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user