A free, self-hostable news aggregator…
Rafraîchissement des flux en cache super rapide
Alexandre Alapetitecf8ee6bd4822
diff --git a/app/Controllers/feedController.php b/app/Controllers/feedController.phpindex c40b3c400..cf9c993d5 100755--- a/app/Controllers/feedController.php+++ b/app/Controllers/feedController.php@@ -191,36 +191,45 @@ class FreshRSS_feed_Controller extends Minz_ActionController {foreach ($feeds as $feed) {try {$url = $feed->url();+ $feedHistory = $feed->keepHistory();+$feed->load(false);$entries = array_reverse($feed->entries()); //We want chronological order and SimplePie uses reverse order+ $hasTransaction = false;- //For this feed, check last n entry GUIDs already in database- $existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);- $useDeclaredDate = empty($existingGuids);+ if (count($entries) > 0) {+ //For this feed, check last n entry GUIDs already in database+ $existingGuids = array_fill_keys ($entryDAO->listLastGuidsByFeed ($feed->id (), count($entries) + 10), 1);+ $useDeclaredDate = empty($existingGuids);- $feedHistory = $feed->keepHistory();- if ($feedHistory == -2) { //default- $feedHistory = $this->view->conf->keep_history_default;- }+ if ($feedHistory == -2) { //default+ $feedHistory = $this->view->conf->keep_history_default;+ }++ $hasTransaction = true;+ $feedDAO->beginTransaction();- // On ne vérifie pas strictement que l'article n'est pas déjà en BDD- // La BDD refusera l'ajout car (id_feed, guid) doit être unique- $feedDAO->beginTransaction ();- foreach ($entries as $entry) {- $eDate = $entry->date (true);- if ((!isset ($existingGuids[$entry->guid ()])) &&- (($feedHistory != 0) || ($eDate >= $date_min))) {- $values = $entry->toArray ();- //Use declared date at first import, otherwise use discovery date- $values['id'] = ($useDeclaredDate || $eDate < $date_min) ?- min(time(), $eDate) . uSecString() :- uTimeString();- $values['is_read'] = $is_read;- $entryDAO->addEntry ($values);+ // On ne vérifie pas strictement que l'article n'est pas déjà en BDD+ // La BDD refusera l'ajout car (id_feed, guid) doit être unique+ foreach ($entries as $entry) {+ $eDate = $entry->date (true);+ if ((!isset ($existingGuids[$entry->guid ()])) &&+ (($feedHistory != 0) || ($eDate >= $date_min))) {+ $values = $entry->toArray ();+ //Use declared date at first import, otherwise use discovery date+ $values['id'] = ($useDeclaredDate || $eDate < $date_min) ?+ min(time(), $eDate) . uSecString() :+ uTimeString();+ $values['is_read'] = $is_read;+ $entryDAO->addEntry ($values);+ }}}if (($feedHistory >= 0) && (rand(0, 30) === 1)) {+ if (!$hasTransaction) {+ $feedDAO->beginTransaction();+ }$nb = $feedDAO->cleanOldEntries ($feed->id (), $date_min, max($feedHistory, count($entries) + 10));if ($nb > 0) {Minz_Log::record ($nb . ' old entries cleaned in feed [' . $feed->url() . ']', Minz_Log::DEBUG);@@ -228,8 +237,10 @@ class FreshRSS_feed_Controller extends Minz_ActionController {}// on indique que le flux vient d'être mis à jour en BDD- $feedDAO->updateLastUpdate ($feed->id ());- $feedDAO->commit ();+ $feedDAO->updateLastUpdate ($feed->id (), 0, $hasTransaction);+ if ($hasTransaction) {+ $feedDAO->commit();+ }$flux_update++;if ($feed->url() !== $url) { //URL has changed (auto-discovery)$feedDAO->updateFeed($feed->id(), array('url' => $feed->url()));diff --git a/app/Models/Feed.php b/app/Models/Feed.phpindex 22c019080..662476b7e 100644--- a/app/Models/Feed.php+++ b/app/Models/Feed.php@@ -193,10 +193,10 @@ class FreshRSS_Feed extends Minz_Model {}$feed = customSimplePie();$feed->set_feed_url ($url);- $feed->init ();+ $initResult = $feed->init ();- if ($feed->error ()) {- throw new FreshRSS_Feed_Exception ($feed->error . ' [' . $url . ']');+ if ((!$initResult) || $feed->error()) {+ throw new FreshRSS_Feed_Exception ($feed->error() . ' [' . $url . ']');}// si on a utilisé l'auto-discover, notre url va avoir changé@@ -217,11 +217,15 @@ class FreshRSS_Feed extends Minz_Model {$this->_description(html_only_entity_decode($feed->get_description()));}- // et on charge les articles du flux- $this->loadEntries ($feed);+ if (($initResult == SIMPLEPIE_INIT_SUCCESS) || $loadDetails) {+ $this->loadEntries($feed); // et on charge les articles du flux+ } else {+ $this->entries = array();+ }}}}+private function loadEntries ($feed) {$entries = array ();diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.phpindex e102da4ec..a17ff0718 100644--- a/app/Models/FeedDAO.php+++ b/app/Models/FeedDAO.php@@ -52,21 +52,27 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {}}- public function updateLastUpdate ($id, $inError = 0) {- $sql = 'UPDATE `' . $this->prefix . 'feed` f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE- . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'- . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'- . 'lastUpdate=?, error=? '- . 'WHERE f.id=?';-- $stm = $this->bd->prepare ($sql);+ public function updateLastUpdate ($id, $inError = 0, $updateCache = true) {+ if ($updateCache) {+ $sql = 'UPDATE `' . $this->prefix . 'feed` f ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE+ . 'SET f.cache_nbEntries=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=f.id),'+ . 'f.cache_nbUnreads=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=f.id AND e2.is_read=0),'+ . 'lastUpdate=?, error=? '+ . 'WHERE f.id=?';+ } else {+ $sql = 'UPDATE `' . $this->prefix . 'feed` f '+ . 'SET lastUpdate=?, error=? '+ . 'WHERE f.id=?';+ }$values = array (- time (),+ time(),$inError,$id,);+ $stm = $this->bd->prepare ($sql);+if ($stm && $stm->execute ($values)) {return $stm->rowCount();} else {diff --git a/lib/SimplePie/SimplePie.php b/lib/SimplePie/SimplePie.phpindex f02037c10..97b9310db 100644--- a/lib/SimplePie/SimplePie.php+++ b/lib/SimplePie/SimplePie.php@@ -402,6 +402,9 @@ define('SIMPLEPIE_FILE_SOURCE_CURL', 8);*/define('SIMPLEPIE_FILE_SOURCE_FILE_GET_CONTENTS', 16);+define('SIMPLEPIE_INIT_FAIL', 0); //FreshRSS+define('SIMPLEPIE_INIT_SUCCESS', 1); //FreshRSS+define('SIMPLEPIE_INIT_CACHE', 2); //FreshRSS/**@@ -1226,7 +1229,7 @@ class SimplePie// Check absolute bare minimum requirements.if (!extension_loaded('xml') || !extension_loaded('pcre')){- return false;+ return SIMPLEPIE_INIT_FAIL;}// Then check the xml extension is sane (i.e., libxml 2.7.x issue on PHP < 5.2.9 and libxml 2.7.0 to 2.7.2 on any version) if we don't have xmlreader.elseif (!extension_loaded('xmlreader'))@@ -1241,7 +1244,7 @@ class SimplePie}if (!$xml_is_sane){- return false;+ return SIMPLEPIE_INIT_FAIL;}}@@ -1273,11 +1276,11 @@ class SimplePie}$i++;}- return (bool) $success;+ return inval($success);}elseif ($this->feed_url === null && $this->raw_data === null){- return false;+ return SIMPLEPIE_INIT_FAIL;}$this->error = null;@@ -1298,10 +1301,10 @@ class SimplePie// Fetch the data via SimplePie_File into $this->raw_dataif (($fetched = $this->fetch_data($cache)) === true){- return true;+ return SIMPLEPIE_INIT_CACHE;}elseif ($fetched === false) {- return false;+ return SIMPLEPIE_INIT_FAIL;}list($headers, $sniffed) = $fetched;@@ -1378,7 +1381,7 @@ class SimplePie{$this->error = "A feed could not be found at $this->feed_url. This does not appear to be a valid RSS or Atom feed.";$this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));- return false;+ return SIMPLEPIE_INIT_FAIL;}if (isset($headers))@@ -1392,7 +1395,7 @@ class SimplePie{trigger_error("$this->cache_location is not writeable. Make sure you've set the correct relative or absolute path, and that the location is server-writable.", E_USER_WARNING);}- return true;+ return SIMPLEPIE_INIT_SUCCESS;}}}@@ -1409,7 +1412,7 @@ class SimplePie$this->registry->call('Misc', 'error', array($this->error, E_USER_NOTICE, __FILE__, __LINE__));- return false;+ return SIMPLEPIE_INIT_FAIL;}/**