aboutsummaryrefslogtreecommitdiff
path: root/app/Models/FeedDAO.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-06-13 18:51:52 +0200
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-06-13 18:51:52 +0200
commitf58fdfe93dbf969338fe9cecbd728e0f7add4617 (patch)
treee1629da3361235d1c4ef28944624326491628045 /app/Models/FeedDAO.php
parentc053825ff8f9792e692c101585481129b006937b (diff)
parent61f4d5457818204eb28ed394d4f1b97160542baa (diff)
Merge branch 'dev' into beta
Diffstat (limited to 'app/Models/FeedDAO.php')
-rw-r--r--app/Models/FeedDAO.php87
1 files changed, 71 insertions, 16 deletions
diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php
index e102da4ec..b65ff4af0 100644
--- a/app/Models/FeedDAO.php
+++ b/app/Models/FeedDAO.php
@@ -24,6 +24,36 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
}
}
+ public function addFeedObject($feed) {
+ // TODO: not sure if we should write this method in DAO since DAO
+ // should not be aware about feed class
+
+ // Add feed only if we don't find it in DB
+ $feed_search = $this->searchByUrl($feed->url());
+ if (!$feed_search) {
+ $values = array(
+ 'id' => $feed->id(),
+ 'url' => $feed->url(),
+ 'category' => $feed->category(),
+ 'name' => $feed->name(),
+ 'website' => $feed->website(),
+ 'description' => $feed->description(),
+ 'lastUpdate' => 0,
+ 'httpAuth' => $feed->httpAuth()
+ );
+
+ $id = $this->addFeed($values);
+ if ($id) {
+ $feed->_id($id);
+ $feed->faviconPrepare();
+ }
+
+ return $id;
+ }
+
+ return $feed_search->id();
+ }
+
public function updateFeed ($id, $valuesTmp) {
$set = '';
foreach ($valuesTmp as $key => $v) {
@@ -52,21 +82,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 {
@@ -164,7 +200,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
if (isset ($feed[$id])) {
return $feed[$id];
} else {
- return false;
+ return null;
}
}
public function searchByUrl ($url) {
@@ -180,7 +216,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
if (isset ($feed)) {
return $feed;
} else {
- return false;
+ return null;
}
}
@@ -192,8 +228,27 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
return self::daoToFeed ($stm->fetchAll (PDO::FETCH_ASSOC));
}
- public function listFeedsOrderUpdate () {
- $sql = 'SELECT id, name, url, pathEntries, httpAuth, keep_history FROM `' . $this->prefix . 'feed` ORDER BY lastUpdate';
+ public function arrayFeedCategoryNames() { //For API
+ $sql = 'SELECT f.id, f.name, c.name as c_name FROM `' . $this->prefix . 'feed` f '
+ . 'INNER JOIN `' . $this->prefix . 'category` c ON c.id = f.category';
+ $stm = $this->bd->prepare ($sql);
+ $stm->execute ();
+ $res = $stm->fetchAll(PDO::FETCH_ASSOC);
+ $feedCategoryNames = array();
+ foreach ($res as $line) {
+ $feedCategoryNames[$line['id']] = array(
+ 'name' => $line['name'],
+ 'c_name' => $line['c_name'],
+ );
+ }
+ return $feedCategoryNames;
+ }
+
+ public function listFeedsOrderUpdate ($cacheDuration = 1500) {
+ $sql = 'SELECT id, name, url, lastUpdate, pathEntries, httpAuth, keep_history '
+ . 'FROM `' . $this->prefix . 'feed` '
+ . 'WHERE lastUpdate < ' . (time() - intval($cacheDuration))
+ . ' ORDER BY lastUpdate';
$stm = $this->bd->prepare ($sql);
$stm->execute ();
@@ -220,6 +275,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
return $res[0]['count'];
}
+
public function countNotRead ($id) {
$sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND is_read=0';
$stm = $this->bd->prepare ($sql);
@@ -229,6 +285,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
return $res[0]['count'];
}
+
public function updateCachedValues () { //For one single feed, call updateLastUpdate($id)
$sql = 'UPDATE `' . $this->prefix . 'feed` f '
. 'INNER JOIN ('
@@ -241,9 +298,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo {
. 'SET f.cache_nbEntries=x.nbEntries, f.cache_nbUnreads=x.nbUnreads';
$stm = $this->bd->prepare ($sql);
- $values = array ($feed_id);
-
- if ($stm && $stm->execute ($values)) {
+ if ($stm && $stm->execute()) {
return $stm->rowCount();
} else {
$info = $stm->errorInfo();