aboutsummaryrefslogtreecommitdiff
path: root/app/Models
diff options
context:
space:
mode:
authorGravatar Alexandre Alapetite <alexandre@alapetite.fr> 2019-10-31 18:15:47 +0100
committerGravatar GitHub <noreply@github.com> 2019-10-31 18:15:47 +0100
commit3aa66f317b496ccd9a2df914bbc747c52081a7ad (patch)
tree6a3f3f74899801abdca00546e213dfdc141c53cf /app/Models
parent82611c9622ed23b0e9fcf5f9f651ddffa1fd7706 (diff)
parentfcae48f313d399050cb15f37a8a73ae52fc67796 (diff)
Merge pull request #2599 from FreshRSS/dev1.15.0
FreshRSS 1.15
Diffstat (limited to 'app/Models')
-rw-r--r--app/Models/Auth.php7
-rw-r--r--app/Models/Category.php31
-rw-r--r--app/Models/CategoryDAO.php251
-rw-r--r--app/Models/CategoryDAOSQLite.php17
-rw-r--r--app/Models/ConfigurationSetter.php17
-rw-r--r--app/Models/Context.php18
-rw-r--r--app/Models/DatabaseDAO.php250
-rw-r--r--app/Models/DatabaseDAOPGSQL.php49
-rw-r--r--app/Models/DatabaseDAOSQLite.php36
-rw-r--r--app/Models/Entry.php12
-rw-r--r--app/Models/EntryDAO.php467
-rw-r--r--app/Models/EntryDAOPGSQL.php28
-rw-r--r--app/Models/EntryDAOSQLite.php92
-rw-r--r--app/Models/Factory.php12
-rw-r--r--app/Models/Feed.php55
-rw-r--r--app/Models/FeedDAO.php208
-rw-r--r--app/Models/FeedDAOSQLite.php4
-rw-r--r--app/Models/StatsDAO.php47
-rw-r--r--app/Models/StatsDAOPGSQL.php5
-rw-r--r--app/Models/StatsDAOSQLite.php5
-rw-r--r--app/Models/Tag.php2
-rw-r--r--app/Models/TagDAO.php139
-rw-r--r--app/Models/TagDAOSQLite.php2
-rw-r--r--app/Models/UserDAO.php89
24 files changed, 1080 insertions, 763 deletions
diff --git a/app/Models/Auth.php b/app/Models/Auth.php
index 6d079a01f..b7fb0e6d6 100644
--- a/app/Models/Auth.php
+++ b/app/Models/Auth.php
@@ -219,10 +219,6 @@ class FreshRSS_FormAuth {
return false;
}
- if (!function_exists('password_verify')) {
- include_once(LIB_PATH . '/password_compat.php');
- }
-
return password_verify($nonce . $hash, $challenge);
}
@@ -283,8 +279,7 @@ class FreshRSS_FormAuth {
$cookie_duration = empty($limits['cookie_duration']) ? 2592000 : $limits['cookie_duration'];
$oldest = time() - $cookie_duration;
foreach (new DirectoryIterator(DATA_PATH . '/tokens/') as $file_info) {
- // $extension = $file_info->getExtension(); doesn't work in PHP < 5.3.7
- $extension = pathinfo($file_info->getFilename(), PATHINFO_EXTENSION);
+ $extension = $file_info->getExtension();
if ($extension === 'txt' && $file_info->getMTime() < $oldest) {
@unlink($file_info->getPathname());
}
diff --git a/app/Models/Category.php b/app/Models/Category.php
index fa711aa66..a195c88b3 100644
--- a/app/Models/Category.php
+++ b/app/Models/Category.php
@@ -8,6 +8,7 @@ class FreshRSS_Category extends Minz_Model {
private $feeds = null;
private $hasFeedsWithError = false;
private $isDefault = false;
+ private $attributes = [];
public function __construct($name = '', $feeds = null) {
$this->_name($name);
@@ -68,8 +69,19 @@ class FreshRSS_Category extends Minz_Model {
return $this->hasFeedsWithError;
}
- public function _id($value) {
- $this->id = $value;
+ public function attributes($key = '') {
+ if ($key == '') {
+ return $this->attributes;
+ } else {
+ return isset($this->attributes[$key]) ? $this->attributes[$key] : null;
+ }
+ }
+
+ public function _id($id) {
+ $this->id = $id;
+ if ($id == FreshRSS_CategoryDAO::DEFAULTCATEGORYID) {
+ $this->_name(_t('gen.short.default_category'));
+ }
}
public function _name($value) {
$this->name = trim($value);
@@ -84,4 +96,19 @@ class FreshRSS_Category extends Minz_Model {
$this->feeds = $values;
}
+
+ public function _attributes($key, $value) {
+ if ('' == $key) {
+ if (is_string($value)) {
+ $value = json_decode($value, true);
+ }
+ if (is_array($value)) {
+ $this->attributes = $value;
+ }
+ } elseif (null === $value) {
+ unset($this->attributes[$key]);
+ } else {
+ $this->attributes[$key] = $value;
+ }
+ }
}
diff --git a/app/Models/CategoryDAO.php b/app/Models/CategoryDAO.php
index 6535adae7..c1277751c 100644
--- a/app/Models/CategoryDAO.php
+++ b/app/Models/CategoryDAO.php
@@ -4,23 +4,92 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
const DEFAULTCATEGORYID = 1;
+ protected function addColumn($name) {
+ Minz_Log::warning(__method__ . ': ' . $name);
+ try {
+ if ('attributes' === $name) { //v1.15.0
+ $ok = $this->pdo->exec('ALTER TABLE `_category` ADD COLUMN attributes TEXT') !== false;
+
+ $stm = $this->pdo->query('SELECT * FROM `_feed`');
+ $feeds = $stm->fetchAll(PDO::FETCH_ASSOC);
+
+ $stm = $this->pdo->prepare('UPDATE `_feed` SET attributes = :attributes WHERE id = :id');
+ foreach ($feeds as $feed) {
+ if (empty($feed['keep_history']) || empty($feed['id'])) {
+ continue;
+ }
+ $keepHistory = $feed['keep_history'];
+ $attributes = empty($feed['attributes']) ? [] : json_decode($feed['attributes'], true);
+ if (is_string($attributes)) { //Legacy risk of double-encoding
+ $attributes = json_decode($attributes, true);
+ }
+ if (!is_array($attributes)) {
+ $attributes = [];
+ }
+ if ($keepHistory > 0) {
+ $attributes['archiving']['keep_min'] = intval($keepHistory);
+ } elseif ($keepHistory == -1) { //Infinite
+ $attributes['archiving']['keep_period'] = false;
+ $attributes['archiving']['keep_max'] = false;
+ $attributes['archiving']['keep_min'] = false;
+ } else {
+ continue;
+ }
+ $stm->bindValue(':id', $feed['id'], PDO::PARAM_INT);
+ $stm->bindValue(':attributes', json_encode($attributes, JSON_UNESCAPED_SLASHES));
+ $stm->execute();
+ }
+
+ if ($this->pdo->dbType() !== 'sqlite') { //SQLite does not support DROP COLUMN
+ $this->pdo->exec('ALTER TABLE `_feed` DROP COLUMN keep_history');
+ } else {
+ $this->pdo->exec('DROP INDEX IF EXISTS feed_keep_history_index'); //SQLite at least drop index
+ }
+ return $ok;
+ }
+ } catch (Exception $e) {
+ Minz_Log::error(__method__ . ': ' . $e->getMessage());
+ }
+ return false;
+ }
+
+ protected function autoUpdateDb($errorInfo) {
+ if (isset($errorInfo[0])) {
+ if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
+ foreach (['attributes'] as $column) {
+ if (stripos($errorInfo[2], $column) !== false) {
+ return $this->addColumn($column);
+ }
+ }
+ }
+ }
+ return false;
+ }
+
public function addCategory($valuesTmp) {
- $sql = 'INSERT INTO `' . $this->prefix . 'category`(name) '
- . 'SELECT * FROM (SELECT TRIM(?)) c2 ' //TRIM() to provide a type hint as text for PostgreSQL
- . 'WHERE NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'tag` WHERE name = TRIM(?))'; //No tag of the same name
- $stm = $this->bd->prepare($sql);
+ $sql = 'INSERT INTO `_category`(name, attributes) '
+ . 'SELECT * FROM (SELECT TRIM(?), ?) c2 ' //TRIM() to provide a type hint as text for PostgreSQL
+ . 'WHERE NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = TRIM(?))'; //No tag of the same name
+ $stm = $this->pdo->prepare($sql);
$valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
+ if (!isset($valuesTmp['attributes'])) {
+ $valuesTmp['attributes'] = [];
+ }
$values = array(
$valuesTmp['name'],
+ is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
$valuesTmp['name'],
);
if ($stm && $stm->execute($values)) {
- return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
+ return $this->pdo->lastInsertId('`_category_id_seq`');
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error addCategory: ' . $info[2]);
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ if ($this->autoUpdateDb($info)) {
+ return $this->addCategory($valuesTmp);
+ }
+ Minz_Log::error('SQL error addCategory: ' . json_encode($info));
return false;
}
}
@@ -39,13 +108,17 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
}
public function updateCategory($id, $valuesTmp) {
- $sql = 'UPDATE `' . $this->prefix . 'category` SET name=? WHERE id=? '
- . 'AND NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'tag` WHERE name = ?)'; //No tag of the same name
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_category` SET name=?, attributes=? WHERE id=? '
+ . 'AND NOT EXISTS (SELECT 1 FROM `_tag` WHERE name = ?)'; //No tag of the same name
+ $stm = $this->pdo->prepare($sql);
$valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, FreshRSS_DatabaseDAO::LENGTH_INDEX_UNICODE, 'UTF-8');
+ if (!isset($valuesTmp['attributes'])) {
+ $valuesTmp['attributes'] = [];
+ }
$values = array(
$valuesTmp['name'],
+ is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
$id,
$valuesTmp['name'],
);
@@ -53,8 +126,11 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error updateCategory: ' . $info[2]);
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ if ($this->autoUpdateDb($info)) {
+ return $this->updateCategory($valuesTmp);
+ }
+ Minz_Log::error('SQL error updateCategory: ' . json_encode($info));
return false;
}
}
@@ -63,27 +139,42 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
if ($id <= self::DEFAULTCATEGORYID) {
return false;
}
- $sql = 'DELETE FROM `' . $this->prefix . 'category` WHERE id=?';
- $stm = $this->bd->prepare($sql);
-
- $values = array($id);
-
- if ($stm && $stm->execute($values)) {
+ $sql = 'DELETE FROM `_category` WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ if ($stm && $stm->execute()) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error deleteCategory: ' . $info[2]);
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ Minz_Log::error('SQL error deleteCategory: ' . json_encode($info));
return false;
}
}
- public function searchById($id) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=?';
- $stm = $this->bd->prepare($sql);
-
- $values = array($id);
+ public function selectAll() {
+ $sql = 'SELECT id, name, attributes FROM `_category`';
+ $stm = $this->pdo->query($sql);
+ if ($stm != false) {
+ while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
+ yield $row;
+ }
+ } else {
+ $info = $this->pdo->errorInfo();
+ if ($this->autoUpdateDb($info)) {
+ foreach ($this->selectAll() as $category) { // `yield from` requires PHP 7+
+ yield $category;
+ }
+ }
+ Minz_Log::error(__method__ . ' error: ' . json_encode($info));
+ yield false;
+ }
+ }
- $stm->execute($values);
+ public function searchById($id) {
+ $sql = 'SELECT * FROM `_category` WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
@@ -94,15 +185,15 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
}
}
public function searchByName($name) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE name=?';
- $stm = $this->bd->prepare($sql);
-
- $values = array($name);
-
- $stm->execute($values);
+ $sql = 'SELECT * FROM `_category` WHERE name=:name';
+ $stm = $this->pdo->prepare($sql);
+ if ($stm == false) {
+ return false;
+ }
+ $stm->bindParam(':name', $name);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
-
if (isset($cat[0])) {
return $cat[0];
} else {
@@ -110,30 +201,61 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
}
}
+ public function listSortedCategories($prePopulateFeeds = true, $details = false) {
+ $categories = $this->listCategories($prePopulateFeeds, $details);
+
+ if (!is_array($categories)) {
+ return $categories;
+ }
+
+ uasort($categories, function ($a, $b) {
+ $aPosition = $a->attributes('position');
+ $bPosition = $b->attributes('position');
+ if ($aPosition === $bPosition) {
+ return ($a->name() < $b->name()) ? -1 : 1;
+ } elseif (null === $aPosition) {
+ return 1;
+ } elseif (null === $bPosition) {
+ return -1;
+ }
+ return ($aPosition < $bPosition) ? -1 : 1;
+ });
+
+ return $categories;
+ }
+
public function listCategories($prePopulateFeeds = true, $details = false) {
if ($prePopulateFeeds) {
- $sql = 'SELECT c.id AS c_id, c.name AS c_name, '
+ $sql = 'SELECT c.id AS c_id, c.name AS c_name, c.attributes AS c_attributes, '
. ($details ? 'f.* ' : 'f.id, f.name, f.url, f.website, f.priority, f.error, f.`cache_nbEntries`, f.`cache_nbUnreads`, f.ttl ')
- . 'FROM `' . $this->prefix . 'category` c '
- . 'LEFT OUTER JOIN `' . $this->prefix . 'feed` f ON f.category=c.id '
+ . 'FROM `_category` c '
+ . 'LEFT OUTER JOIN `_feed` f ON f.category=c.id '
. 'WHERE f.priority >= :priority_normal '
. 'GROUP BY f.id, c_id '
. 'ORDER BY c.name, f.name';
- $stm = $this->bd->prepare($sql);
- $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
- return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
+ $stm = $this->pdo->prepare($sql);
+ $values = [ ':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL ];
+ if ($stm && $stm->execute($values)) {
+ return self::daoToCategoryPrepopulated($stm->fetchAll(PDO::FETCH_ASSOC));
+ } else {
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ if ($this->autoUpdateDb($info)) {
+ return $this->listCategories($prePopulateFeeds, $details);
+ }
+ Minz_Log::error('SQL error listCategories: ' . json_encode($info));
+ return false;
+ }
} else {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` ORDER BY name';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $sql = 'SELECT * FROM `_category` ORDER BY name';
+ $stm = $this->pdo->query($sql);
return self::daoToCategory($stm->fetchAll(PDO::FETCH_ASSOC));
}
}
public function getDefault() {
- $sql = 'SELECT * FROM `' . $this->prefix . 'category` WHERE id=' . self::DEFAULTCATEGORYID;
- $stm = $this->bd->prepare($sql);
-
+ $sql = 'SELECT * FROM `_category` WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindValue(':id', self::DEFAULTCATEGORYID, PDO::PARAM_INT);
$stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$cat = self::daoToCategory($res);
@@ -155,12 +277,12 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
$cat = new FreshRSS_Category(_t('gen.short.default_category'));
$cat->_id(self::DEFAULTCATEGORYID);
- $sql = 'INSERT INTO `' . $this->prefix . 'category`(id, name) VALUES(?, ?)';
- if (parent::$sharedDbType === 'pgsql') {
+ $sql = 'INSERT INTO `_category`(id, name) VALUES(?, ?)';
+ if ($this->pdo->dbType() === 'pgsql') {
//Force call to nextval()
- $sql .= ' RETURNING nextval(\'"' . $this->prefix . 'category_id_seq"\');';
+ $sql .= " RETURNING nextval('`_category_id_seq`');";
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$values = array(
$cat->id(),
@@ -168,9 +290,9 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
);
if ($stm && $stm->execute($values)) {
- return $this->bd->lastInsertId('"' . $this->prefix . 'category_id_seq"');
+ return $this->pdo->lastInsertId('`_category_id_seq`');
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error check default category: ' . json_encode($info));
return false;
}
@@ -179,31 +301,27 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
}
public function count() {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'category`';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $sql = 'SELECT COUNT(*) AS count FROM `_category`';
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
-
return $res[0]['count'];
}
public function countFeed($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'feed` WHERE category=?';
- $stm = $this->bd->prepare($sql);
- $values = array($id);
- $stm->execute($values);
+ $sql = 'SELECT COUNT(*) AS count FROM `_feed` WHERE category=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
-
return $res[0]['count'];
}
public function countNotRead($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE category=? AND e.is_read=0';
- $stm = $this->bd->prepare($sql);
- $values = array($id);
- $stm->execute($values);
+ $sql = 'SELECT COUNT(*) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE category=:id AND e.is_read=0';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
-
return $res[0]['count'];
}
@@ -248,6 +366,7 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
$feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
);
$cat->_id($previousLine['c_id']);
+ $cat->_attributes('', $previousLine['c_attributes']);
$list[$previousLine['c_id']] = $cat;
$feedsDao = array(); //Prepare for next category
@@ -264,6 +383,7 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
$feedDao->daoToFeed($feedsDao, $previousLine['c_id'])
);
$cat->_id($previousLine['c_id']);
+ $cat->_attributes('', $previousLine['c_attributes']);
$list[$previousLine['c_id']] = $cat;
}
@@ -282,6 +402,7 @@ class FreshRSS_CategoryDAO extends Minz_ModelPdo implements FreshRSS_Searchable
$dao['name']
);
$cat->_id($dao['id']);
+ $cat->_attributes('', isset($dao['attributes']) ? $dao['attributes'] : '');
$cat->_isDefault(static::DEFAULTCATEGORYID === intval($dao['id']));
$list[$key] = $cat;
}
diff --git a/app/Models/CategoryDAOSQLite.php b/app/Models/CategoryDAOSQLite.php
new file mode 100644
index 000000000..e32545c90
--- /dev/null
+++ b/app/Models/CategoryDAOSQLite.php
@@ -0,0 +1,17 @@
+<?php
+
+class FreshRSS_CategoryDAOSQLite extends FreshRSS_CategoryDAO {
+
+ protected function autoUpdateDb($errorInfo) {
+ if ($tableInfo = $this->pdo->query("PRAGMA table_info('category')")) {
+ $columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1);
+ foreach (['attributes'] as $column) {
+ if (!in_array($column, $columns)) {
+ return $this->addColumn($column);
+ }
+ }
+ }
+ return false;
+ }
+
+}
diff --git a/app/Models/ConfigurationSetter.php b/app/Models/ConfigurationSetter.php
index ec6380df4..b1d271f41 100644
--- a/app/Models/ConfigurationSetter.php
+++ b/app/Models/ConfigurationSetter.php
@@ -79,11 +79,6 @@ class FreshRSS_ConfigurationSetter {
$data['html5_notif_timeout'] = $value >= 0 ? $value : 0;
}
- private function _keep_history_default(&$data, $value) {
- $value = intval($value);
- $data['keep_history_default'] = $value >= FreshRSS_Feed::KEEP_HISTORY_INFINITE ? $value : 0;
- }
-
// It works for system config too!
private function _language(&$data, $value) {
$value = strtolower($value);
@@ -94,11 +89,6 @@ class FreshRSS_ConfigurationSetter {
$data['language'] = $value;
}
- private function _old_entries(&$data, $value) {
- $value = intval($value);
- $data['old_entries'] = $value > 0 ? $value : 3;
- }
-
private function _passwordHash(&$data, $value) {
$data['passwordHash'] = ctype_graph($value) && (strlen($value) >= 60) ? $value : '';
}
@@ -257,6 +247,9 @@ class FreshRSS_ConfigurationSetter {
private function _topline_read(&$data, $value) {
$data['topline_read'] = $this->handleBool($value);
}
+ private function _topline_display_authors(&$data, $value) {
+ $data['topline_display_authors'] = $this->handleBool($value);
+ }
/**
* The (not so long) list of setters for system configuration.
@@ -386,4 +379,8 @@ class FreshRSS_ConfigurationSetter {
$data['auto_update_url'] = $value;
}
+
+ private function _force_email_validation(&$data, $value) {
+ $data['force_email_validation'] = $this->handleBool($value);
+ }
}
diff --git a/app/Models/Context.php b/app/Models/Context.php
index 95dc47c8c..e27330665 100644
--- a/app/Models/Context.php
+++ b/app/Models/Context.php
@@ -51,6 +51,24 @@ class FreshRSS_Context {
// Init configuration.
self::$system_conf = Minz_Configuration::get('system');
self::$user_conf = Minz_Configuration::get('user');
+
+ //Legacy
+ $oldEntries = (int)FreshRSS_Context::$user_conf->param('old_entries', 0);
+ $keepMin = (int)FreshRSS_Context::$user_conf->param('keep_history_default', -5);
+ if ($oldEntries > 0 || $keepMin > -5) { //Freshrss < 1.15
+ $archiving = FreshRSS_Context::$user_conf->archiving;
+ $archiving['keep_max'] = false;
+ if ($oldEntries > 0) {
+ $archiving['keep_period'] = 'P' . $oldEntries . 'M';
+ }
+ if ($keepMin > 0) {
+ $archiving['keep_min'] = $keepMin;
+ } elseif ($keepMin == -1) { //Infinite
+ $archiving['keep_period'] = false;
+ $archiving['keep_min'] = false;
+ }
+ FreshRSS_Context::$user_conf->archiving = $archiving;
+ }
}
/**
diff --git a/app/Models/DatabaseDAO.php b/app/Models/DatabaseDAO.php
index b331eccc3..a36b469b1 100644
--- a/app/Models/DatabaseDAO.php
+++ b/app/Models/DatabaseDAO.php
@@ -8,25 +8,50 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
//MySQL error codes
const ER_BAD_FIELD_ERROR = '42S22';
const ER_BAD_TABLE_ERROR = '42S02';
- const ER_TRUNCATED_WRONG_VALUE_FOR_FIELD = '1366';
+ const ER_DATA_TOO_LONG = '1406';
//MySQL InnoDB maximum index length for UTF8MB4
//https://dev.mysql.com/doc/refman/8.0/en/innodb-restrictions.html
const LENGTH_INDEX_UNICODE = 191;
+ public function create() {
+ require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
+ $db = FreshRSS_Context::$system_conf->db;
+
+ try {
+ $sql = sprintf($SQL_CREATE_DB, empty($db['base']) ? '' : $db['base']);
+ return $this->pdo->exec($sql) !== false;
+ } catch (PDOException $e) {
+ $_SESSION['bd_error'] = $e->getMessage();
+ syslog(LOG_DEBUG, __method__ . ' warning: ' . $e->getMessage());
+ return false;
+ }
+ }
+
+ public function testConnection() {
+ try {
+ $sql = 'SELECT 1';
+ $stm = $this->pdo->query($sql);
+ $res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
+ return $res != false;
+ } catch (PDOException $e) {
+ $_SESSION['bd_error'] = $e->getMessage();
+ syslog(LOG_DEBUG, __method__ . ' warning: ' . $e->getMessage());
+ return false;
+ }
+ }
+
public function tablesAreCorrect() {
- $sql = 'SHOW TABLES';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query('SHOW TABLES');
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$tables = array(
- $this->prefix . 'category' => false,
- $this->prefix . 'feed' => false,
- $this->prefix . 'entry' => false,
- $this->prefix . 'entrytmp' => false,
- $this->prefix . 'tag' => false,
- $this->prefix . 'entrytag' => false,
+ $this->pdo->prefix() . 'category' => false,
+ $this->pdo->prefix() . 'feed' => false,
+ $this->pdo->prefix() . 'entry' => false,
+ $this->pdo->prefix() . 'entrytmp' => false,
+ $this->pdo->prefix() . 'tag' => false,
+ $this->pdo->prefix() . 'entrytag' => false,
);
foreach ($res as $value) {
$tables[array_pop($value)] = true;
@@ -36,10 +61,8 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
}
public function getSchema($table) {
- $sql = 'DESC ' . $this->prefix . $table;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
-
+ $sql = 'DESC `_' . $table . '`';
+ $stm = $this->pdo->query($sql);
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
@@ -63,7 +86,7 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
public function feedIsCorrect() {
return $this->checkTable('feed', array(
'id', 'url', 'category', 'name', 'website', 'description', 'lastUpdate',
- 'priority', 'pathEntries', 'httpAuth', 'error', 'keep_history', 'ttl', 'attributes',
+ 'priority', 'pathEntries', 'httpAuth', 'error', 'ttl', 'attributes',
'cache_nbEntries', 'cache_nbUnreads',
));
}
@@ -119,9 +142,9 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
$values = array($db['base']);
if (!$all) {
$sql .= ' AND table_name LIKE ?';
- $values[] = $this->prefix . '%';
+ $values[] = $this->pdo->prefix() . '%';
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
@@ -132,30 +155,23 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
$tables = array('category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag');
foreach ($tables as $table) {
- $sql = 'OPTIMIZE TABLE `' . $this->prefix . $table . '`'; //MySQL
- $stm = $this->bd->prepare($sql);
- $ok &= $stm != false;
- if ($stm) {
- $ok &= $stm->execute();
- }
+ $sql = 'OPTIMIZE TABLE `_' . $table . '`'; //MySQL
+ $ok &= ($this->pdo->exec($sql) !== false);
}
return $ok;
}
public function ensureCaseInsensitiveGuids() {
$ok = true;
- $db = FreshRSS_Context::$system_conf->db;
- if ($db['type'] === 'mysql') {
- include_once(APP_PATH . '/SQL/install.sql.mysql.php');
- if (defined('SQL_UPDATE_GUID_LATIN1_BIN')) { //FreshRSS 1.12
- try {
- $sql = sprintf(SQL_UPDATE_GUID_LATIN1_BIN, $this->prefix);
- $stm = $this->bd->prepare($sql);
- $ok = $stm->execute();
- } catch (Exception $e) {
- $ok = false;
- Minz_Log::error('FreshRSS_DatabaseDAO::ensureCaseInsensitiveGuids error: ' . $e->getMessage());
- }
+ if ($this->pdo->dbType() === 'mysql') {
+ include(APP_PATH . '/SQL/install.sql.mysql.php');
+
+ $ok = false;
+ try {
+ $ok = $this->pdo->exec($SQL_UPDATE_GUID_LATIN1_BIN) !== false; //FreshRSS 1.12
+ } catch (Exception $e) {
+ $ok = false;
+ Minz_Log::error(__METHOD__ . ' error: ' . $e->getMessage());
}
}
return $ok;
@@ -164,4 +180,168 @@ class FreshRSS_DatabaseDAO extends Minz_ModelPdo {
public function minorDbMaintenance() {
$this->ensureCaseInsensitiveGuids();
}
+
+ private static function stdError($error) {
+ if (defined('STDERR')) {
+ fwrite(STDERR, $error . "\n");
+ }
+ Minz_Log::error($error);
+ return false;
+ }
+
+ const SQLITE_EXPORT = 1;
+ const SQLITE_IMPORT = 2;
+
+ public function dbCopy($filename, $mode, $clearFirst = false) {
+ $error = '';
+
+ $userDAO = FreshRSS_Factory::createUserDao();
+ $catDAO = FreshRSS_Factory::createCategoryDao();
+ $feedDAO = FreshRSS_Factory::createFeedDao();
+ $entryDAO = FreshRSS_Factory::createEntryDao();
+ $tagDAO = FreshRSS_Factory::createTagDao();
+
+ switch ($mode) {
+ case self::SQLITE_EXPORT:
+ if (@filesize($filename) > 0) {
+ $error = 'Error: SQLite export file already exists: ' . $filename;
+ }
+ break;
+ case self::SQLITE_IMPORT:
+ if (!is_readable($filename)) {
+ $error = 'Error: SQLite import file is not readable: ' . $filename;
+ } elseif ($clearFirst) {
+ $userDAO->deleteUser();
+ if ($this->pdo->dbType() === 'sqlite') {
+ //We cannot just delete the .sqlite file otherwise PDO gets buggy.
+ //SQLite is the only one with database-level optimization, instead of at table level.
+ $this->optimize();
+ }
+ } else {
+ $nbEntries = $entryDAO->countUnreadRead();
+ if (!empty($nbEntries['all'])) {
+ $error = 'Error: Destination database already contains some entries!';
+ }
+ }
+ break;
+ default:
+ $error = 'Invalid copy mode!';
+ break;
+ }
+ if ($error != '') {
+ return self::stdError($error);
+ }
+
+ $sqlite = null;
+
+ try {
+ $sqlite = new MinzPDOSQLite('sqlite:' . $filename);
+ } catch (Exception $e) {
+ $error = 'Error while initialising SQLite copy: ' . $e->getMessage();
+ return self::stdError($error);
+ }
+
+ Minz_ModelPdo::clean();
+ $userDAOSQLite = new FreshRSS_UserDAO('', $sqlite);
+ $categoryDAOSQLite = new FreshRSS_CategoryDAOSQLite('', $sqlite);
+ $feedDAOSQLite = new FreshRSS_FeedDAOSQLite('', $sqlite);
+ $entryDAOSQLite = new FreshRSS_EntryDAOSQLite('', $sqlite);
+ $tagDAOSQLite = new FreshRSS_TagDAOSQLite('', $sqlite);
+
+ switch ($mode) {
+ case self::SQLITE_EXPORT:
+ $userFrom = $userDAO; $userTo = $userDAOSQLite;
+ $catFrom = $catDAO; $catTo = $categoryDAOSQLite;
+ $feedFrom = $feedDAO; $feedTo = $feedDAOSQLite;
+ $entryFrom = $entryDAO; $entryTo = $entryDAOSQLite;
+ $tagFrom = $tagDAO; $tagTo = $tagDAOSQLite;
+ break;
+ case self::SQLITE_IMPORT:
+ $userFrom = $userDAOSQLite; $userTo = $userDAO;
+ $catFrom = $categoryDAOSQLite; $catTo = $catDAO;
+ $feedFrom = $feedDAOSQLite; $feedTo = $feedDAO;
+ $entryFrom = $entryDAOSQLite; $entryTo = $entryDAO;
+ $tagFrom = $tagDAOSQLite; $tagTo = $tagDAO;
+ break;
+ }
+
+ $idMaps = [];
+
+ if (defined('STDERR')) {
+ fwrite(STDERR, "Start SQL copy…\n");
+ }
+
+ $userTo->createUser();
+
+ $catTo->beginTransaction();
+ foreach ($catFrom->selectAll() as $category) {
+ $cat = $catTo->searchByName($category['name']); //Useful for the default category
+ if ($cat != null) {
+ $catId = $cat->id();
+ } else {
+ $catId = $catTo->addCategory($category);
+ if ($catId == false) {
+ $error = 'Error during SQLite copy of categories!';
+ return self::stdError($error);
+ }
+ }
+ $idMaps['c' . $category['id']] = $catId;
+ }
+ foreach ($feedFrom->selectAll() as $feed) {
+ $feed['category'] = empty($idMaps['c' . $feed['category']]) ? FreshRSS_CategoryDAO::DEFAULTCATEGORYID : $idMaps['c' . $feed['category']];
+ $feedId = $feedTo->addFeed($feed);
+ if ($feedId == false) {
+ $error = 'Error during SQLite copy of feeds!';
+ return self::stdError($error);
+ }
+ $idMaps['f' . $feed['id']] = $feedId;
+ }
+ $catTo->commit();
+
+ $nbEntries = $entryFrom->count();
+ $n = 0;
+ $entryTo->beginTransaction();
+ foreach ($entryFrom->selectAll() as $entry) {
+ $n++;
+ if (!empty($idMaps['f' . $entry['id_feed']])) {
+ $entry['id_feed'] = $idMaps['f' . $entry['id_feed']];
+ if (!$entryTo->addEntry($entry, false)) {
+ $error = 'Error during SQLite copy of entries!';
+ return self::stdError($error);
+ }
+ }
+ if ($n % 100 === 1 && defined('STDERR')) { //Display progression
+ fwrite(STDERR, "\033[0G" . $n . '/' . $nbEntries);
+ }
+ }
+ if (defined('STDERR')) {
+ fwrite(STDERR, "\033[0G" . $n . '/' . $nbEntries . "\n");
+ }
+ $entryTo->commit();
+ $feedTo->updateCachedValues();
+
+ $idMaps = [];
+
+ $tagTo->beginTransaction();
+ foreach ($tagFrom->selectAll() as $tag) {
+ $tagId = $tagTo->addTag($tag);
+ if ($tagId == false) {
+ $error = 'Error during SQLite copy of tags!';
+ return self::stdError($error);
+ }
+ $idMaps['t' . $tag['id']] = $tagId;
+ }
+ foreach ($tagFrom->selectEntryTag() as $entryTag) {
+ if (!empty($idMaps['t' . $entryTag['id_tag']])) {
+ $entryTag['id_tag'] = $idMaps['t' . $entryTag['id_tag']];
+ if (!$tagTo->tagEntry($entryTag['id_tag'], $entryTag['id_entry'])) {
+ $error = 'Error during SQLite copy of entry-tags!';
+ return self::stdError($error);
+ }
+ }
+ }
+ $tagTo->commit();
+
+ return true;
+ }
}
diff --git a/app/Models/DatabaseDAOPGSQL.php b/app/Models/DatabaseDAOPGSQL.php
index 8582b5719..1a6b3599e 100644
--- a/app/Models/DatabaseDAOPGSQL.php
+++ b/app/Models/DatabaseDAOPGSQL.php
@@ -13,18 +13,18 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
$db = FreshRSS_Context::$system_conf->db;
$dbowner = $db['user'];
$sql = 'SELECT * FROM pg_catalog.pg_tables where tableowner=?';
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$values = array($dbowner);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$tables = array(
- $this->prefix . 'category' => false,
- $this->prefix . 'feed' => false,
- $this->prefix . 'entry' => false,
- $this->prefix . 'entrytmp' => false,
- $this->prefix . 'tag' => false,
- $this->prefix . 'entrytag' => false,
+ $this->pdo->prefix() . 'category' => false,
+ $this->pdo->prefix() . 'feed' => false,
+ $this->pdo->prefix() . 'entry' => false,
+ $this->pdo->prefix() . 'entrytmp' => false,
+ $this->pdo->prefix() . 'tag' => false,
+ $this->pdo->prefix() . 'entrytag' => false,
);
foreach ($res as $value) {
$tables[array_pop($value)] = true;
@@ -35,8 +35,8 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
public function getSchema($table) {
$sql = 'select column_name as field, data_type as type, column_default as default, is_nullable as null from INFORMATION_SCHEMA.COLUMNS where table_name = ?';
- $stm = $this->bd->prepare($sql);
- $stm->execute(array($this->prefix . $table));
+ $stm = $this->pdo->prepare($sql);
+ $stm->execute(array($this->pdo->prefix() . $table));
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
@@ -49,12 +49,23 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
);
}
- public function size($all = true) {
- $db = FreshRSS_Context::$system_conf->db;
- $sql = 'SELECT pg_size_pretty(pg_database_size(?))';
- $values = array($db['base']);
- $stm = $this->bd->prepare($sql);
- $stm->execute($values);
+ public function size($all = false) {
+ if ($all) {
+ $db = FreshRSS_Context::$system_conf->db;
+ $sql = 'SELECT pg_database_size(:base)';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':base', $db['base']);
+ $stm->execute();
+ } else {
+ $sql = "SELECT "
+ . "pg_total_relation_size('{$this->pdo->prefix()}category') + "
+ . "pg_total_relation_size('{$this->pdo->prefix()}feed') + "
+ . "pg_total_relation_size('{$this->pdo->prefix()}entry') + "
+ . "pg_total_relation_size('{$this->pdo->prefix()}entrytmp') + "
+ . "pg_total_relation_size('{$this->pdo->prefix()}tag') + "
+ . "pg_total_relation_size('{$this->pdo->prefix()}entrytag')";
+ $stm = $this->pdo->query($sql);
+ }
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
}
@@ -64,12 +75,8 @@ class FreshRSS_DatabaseDAOPGSQL extends FreshRSS_DatabaseDAOSQLite {
$tables = array('category', 'feed', 'entry', 'entrytmp', 'tag', 'entrytag');
foreach ($tables as $table) {
- $sql = 'VACUUM `' . $this->prefix . $table . '`';
- $stm = $this->bd->prepare($sql);
- $ok &= $stm != false;
- if ($stm) {
- $ok &= $stm->execute();
- }
+ $sql = 'VACUUM `_' . $table . '`';
+ $ok &= ($this->pdo->exec($sql) !== false);
}
return $ok;
}
diff --git a/app/Models/DatabaseDAOSQLite.php b/app/Models/DatabaseDAOSQLite.php
index a93a209b2..413e7ee09 100644
--- a/app/Models/DatabaseDAOSQLite.php
+++ b/app/Models/DatabaseDAOSQLite.php
@@ -6,17 +6,16 @@
class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
public function tablesAreCorrect() {
$sql = 'SELECT name FROM sqlite_master WHERE type="table"';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$tables = array(
- 'category' => false,
- 'feed' => false,
- 'entry' => false,
- 'entrytmp' => false,
- 'tag' => false,
- 'entrytag' => false,
+ $this->pdo->prefix() . 'category' => false,
+ $this->pdo->prefix() . 'feed' => false,
+ $this->pdo->prefix() . 'entry' => false,
+ $this->pdo->prefix() . 'entrytmp' => false,
+ $this->pdo->prefix() . 'tag' => false,
+ $this->pdo->prefix() . 'entrytag' => false,
);
foreach ($res as $value) {
$tables[$value['name']] = true;
@@ -27,9 +26,7 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
public function getSchema($table) {
$sql = 'PRAGMA table_info(' . $table . ')';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
-
+ $stm = $this->pdo->query($sql);
return $this->listDaoToSchema($stm->fetchAll(PDO::FETCH_ASSOC));
}
@@ -57,15 +54,18 @@ class FreshRSS_DatabaseDAOSQLite extends FreshRSS_DatabaseDAO {
}
public function size($all = false) {
- return @filesize(join_path(DATA_PATH, 'users', $this->current_user, 'db.sqlite'));
+ $sum = 0;
+ if ($all) {
+ foreach (glob(DATA_PATH . '/users/*/db.sqlite') as $filename) {
+ $sum += @filesize($filename);
+ }
+ } else {
+ $sum = @filesize(DATA_PATH . '/users/' . $this->current_user . '/db.sqlite');
+ }
+ return $sum;
}
public function optimize() {
- $sql = 'VACUUM';
- $stm = $this->bd->prepare($sql);
- if ($stm) {
- return $stm->execute();
- }
- return false;
+ return $this->pdo->exec('VACUUM') !== false;
}
}
diff --git a/app/Models/Entry.php b/app/Models/Entry.php
index 3bb977283..d90f828bc 100644
--- a/app/Models/Entry.php
+++ b/app/Models/Entry.php
@@ -327,7 +327,7 @@ class FreshRSS_Entry extends Minz_Model {
}
$ch = curl_init();
- curl_setopt_array($ch, array(
+ curl_setopt_array($ch, [
CURLOPT_URL => $url,
CURLOPT_REFERER => SimplePie_Misc::url_remove_credentials($url),
CURLOPT_HTTPHEADER => array('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
@@ -337,13 +337,9 @@ class FreshRSS_Entry extends Minz_Model {
//CURLOPT_FAILONERROR => true;
CURLOPT_MAXREDIRS => 4,
CURLOPT_RETURNTRANSFER => true,
- ));
- if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
- }
- if (defined('CURLOPT_ENCODING')) {
- curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
- }
+ CURLOPT_FOLLOWLOCATION => true,
+ CURLOPT_ENCODING => '', //Enable all encodings
+ ]);
curl_setopt_array($ch, $system_conf->curl_options);
if (isset($attributes['ssl_verify'])) {
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, $attributes['ssl_verify'] ? 2 : 0);
diff --git a/app/Models/EntryDAO.php b/app/Models/EntryDAO.php
index b47cd55ad..99e99f463 100644
--- a/app/Models/EntryDAO.php
+++ b/app/Models/EntryDAO.php
@@ -3,11 +3,11 @@
class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function isCompressed() {
- return parent::$sharedDbType === 'mysql';
+ return true;
}
public function hasNativeHex() {
- return parent::$sharedDbType !== 'sqlite';
+ return true;
}
public function sqlHexDecode($x) {
@@ -19,106 +19,40 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
//TODO: Move the database auto-updates to DatabaseDAO
- protected function addColumn($name) {
- Minz_Log::warning('FreshRSS_EntryDAO::addColumn: ' . $name);
- $hasTransaction = false;
+ protected function createEntryTempTable() {
+ $ok = false;
+ $hadTransaction = $this->pdo->inTransaction();
+ if ($hadTransaction) {
+ $this->pdo->commit();
+ }
try {
- $stm = null;
- if ($name === 'lastSeen') { //v1.1.1
- if (!$this->bd->inTransaction()) {
- $this->bd->beginTransaction();
- $hasTransaction = true;
- }
- $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN `lastSeen` INT(11) DEFAULT 0');
- if ($stm && $stm->execute()) {
- $stm = $this->bd->prepare('CREATE INDEX entry_lastSeen_index ON `' . $this->prefix . 'entry`(`lastSeen`);'); //"IF NOT EXISTS" does not exist in MySQL 5.7
- if ($stm && $stm->execute()) {
- if ($hasTransaction) {
- $this->bd->commit();
- }
- return true;
- }
- }
- if ($hasTransaction) {
- $this->bd->rollBack();
- }
- } elseif ($name === 'hash') { //v1.1.1
- $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'entry` ADD COLUMN hash BINARY(16)');
- return $stm && $stm->execute();
- }
- } catch (Exception $e) {
- Minz_Log::error('FreshRSS_EntryDAO::addColumn error: ' . $e->getMessage());
- if ($hasTransaction) {
- $this->bd->rollBack();
- }
+ require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
+ Minz_Log::warning('SQL CREATE TABLE entrytmp...');
+ $ok = $this->pdo->exec($SQL_CREATE_TABLE_ENTRYTMP . $SQL_CREATE_INDEX_ENTRY_1) !== false;
+ } catch (Exception $ex) {
+ Minz_Log::error(__method__ . ' error: ' . $ex->getMessage());
}
- return false;
+ if ($hadTransaction) {
+ $this->pdo->beginTransaction();
+ }
+ return $ok;
}
- private $triedUpdateToUtf8mb4 = false;
-
- //TODO: Move the database auto-updates to DatabaseDAO
- protected function updateToUtf8mb4() {
- if ($this->triedUpdateToUtf8mb4) {
+ private function updateToMediumBlob() {
+ if ($this->pdo->dbType() !== 'mysql') {
return false;
}
- $this->triedUpdateToUtf8mb4 = true;
- $db = FreshRSS_Context::$system_conf->db;
- if ($db['type'] === 'mysql') {
- include_once(APP_PATH . '/SQL/install.sql.mysql.php');
- if (defined('SQL_UPDATE_UTF8MB4')) {
- Minz_Log::warning('Updating MySQL to UTF8MB4...'); //v1.5.0
- $hadTransaction = $this->bd->inTransaction();
- if ($hadTransaction) {
- $this->bd->commit();
- }
- $ok = false;
- try {
- $sql = sprintf(SQL_UPDATE_UTF8MB4, $this->prefix, $db['base']);
- $stm = $this->bd->prepare($sql);
- $ok = $stm->execute();
- } catch (Exception $e) {
- Minz_Log::error('FreshRSS_EntryDAO::updateToUtf8mb4 error: ' . $e->getMessage());
- }
- if ($hadTransaction) {
- $this->bd->beginTransaction();
- //NB: Transaction not starting. Why? (tested on PHP 7.0.8-0ubuntu and MySQL 5.7.13-0ubuntu)
- }
- return $ok;
- }
- }
- return false;
- }
+ Minz_Log::warning('Update MySQL table to use MEDIUMBLOB...');
- //TODO: Move the database auto-updates to DatabaseDAO
- protected function createEntryTempTable() {
- $ok = false;
- $hadTransaction = $this->bd->inTransaction();
- if ($hadTransaction) {
- $this->bd->commit();
- }
+ $sql = <<<'SQL'
+ALTER TABLE `_entry` MODIFY `content_bin` MEDIUMBLOB;
+ALTER TABLE `_entrytmp` MODIFY `content_bin` MEDIUMBLOB;
+SQL;
try {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
- Minz_Log::warning('SQL CREATE TABLE entrytmp...');
- if (defined('SQL_CREATE_TABLE_ENTRYTMP')) {
- $sql = sprintf(SQL_CREATE_TABLE_ENTRYTMP, $this->prefix);
- $stm = $this->bd->prepare($sql);
- $ok = $stm && $stm->execute();
- } else {
- global $SQL_CREATE_TABLE_ENTRYTMP;
- $ok = !empty($SQL_CREATE_TABLE_ENTRYTMP);
- foreach ($SQL_CREATE_TABLE_ENTRYTMP as $instruction) {
- $sql = sprintf($instruction, $this->prefix);
- $stm = $this->bd->prepare($sql);
- $ok &= $stm && $stm->execute();
- }
- }
+ $ok = $this->pdo->exec($sql) !== false;
} catch (Exception $e) {
- Minz_Log::error('FreshRSS_EntryDAO::createEntryTempTable error: ' . $e->getMessage());
- }
- if ($hadTransaction) {
- $this->bd->beginTransaction();
+ $ok = false;
+ Minz_Log::error(__method__ . ' error: ' . $e->getMessage());
}
return $ok;
}
@@ -126,14 +60,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
//TODO: Move the database auto-updates to DatabaseDAO
protected function autoUpdateDb($errorInfo) {
if (isset($errorInfo[0])) {
- if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR) {
- //autoAddColumn
- foreach (array('lastSeen', 'hash') as $column) {
- if (stripos($errorInfo[2], $column) !== false) {
- return $this->addColumn($column);
- }
- }
- } elseif ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR) {
+ if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_TABLE_ERROR) {
if (stripos($errorInfo[2], 'tag') !== false) {
$tagDAO = FreshRSS_Factory::createTagDao();
return $tagDAO->createTagTable(); //v1.12.0
@@ -143,8 +70,10 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
}
if (isset($errorInfo[1])) {
- if ($errorInfo[1] == FreshRSS_DatabaseDAO::ER_TRUNCATED_WRONG_VALUE_FOR_FIELD) {
- return $this->updateToUtf8mb4(); //v1.5.0
+ if ($errorInfo[1] == FreshRSS_DatabaseDAO::ER_DATA_TOO_LONG) {
+ if (stripos($errorInfo[2], 'content_bin') !== false) {
+ return $this->updateToMediumBlob(); //v1.15.0
+ }
}
}
return false;
@@ -152,9 +81,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
private $addEntryPrepared = null;
- public function addEntry($valuesTmp) {
+ public function addEntry($valuesTmp, $useTmpTable = true) {
if ($this->addEntryPrepared == null) {
- $sql = 'INSERT INTO `' . $this->prefix . 'entrytmp` (id, guid, title, author, '
+ $sql = 'INSERT INTO `_' . ($useTmpTable ? 'entrytmp' : 'entry') . '` (id, guid, title, author, '
. ($this->isCompressed() ? 'content_bin' : 'content')
. ', link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags) '
. 'VALUES(:id, :guid, :title, :author, '
@@ -162,7 +91,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
. ', :link, :date, :last_seen, '
. $this->sqlHexDecode(':hash')
. ', :is_read, :is_favorite, :id_feed, :tags)';
- $this->addEntryPrepared = $this->bd->prepare($sql);
+ $this->addEntryPrepared = $this->pdo->prepare($sql);
}
if ($this->addEntryPrepared) {
$this->addEntryPrepared->bindParam(':id', $valuesTmp['id']);
@@ -178,7 +107,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$valuesTmp['link'] = safe_ascii($valuesTmp['link']);
$this->addEntryPrepared->bindParam(':link', $valuesTmp['link']);
$this->addEntryPrepared->bindParam(':date', $valuesTmp['date'], PDO::PARAM_INT);
- $valuesTmp['lastSeen'] = time();
+ if (empty($valuesTmp['lastSeen'])) {
+ $valuesTmp['lastSeen'] = time();
+ }
$this->addEntryPrepared->bindParam(':last_seen', $valuesTmp['lastSeen'], PDO::PARAM_INT);
$valuesTmp['is_read'] = $valuesTmp['is_read'] ? 1 : 0;
$this->addEntryPrepared->bindParam(':is_read', $valuesTmp['is_read'], PDO::PARAM_INT);
@@ -191,14 +122,14 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($this->hasNativeHex()) {
$this->addEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
} else {
- $valuesTmp['hashBin'] = pack('H*', $valuesTmp['hash']); //hex2bin() is PHP5.4+
+ $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
$this->addEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
}
}
if ($this->addEntryPrepared && $this->addEntryPrepared->execute()) {
return true;
} else {
- $info = $this->addEntryPrepared == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $this->addEntryPrepared->errorInfo();
+ $info = $this->addEntryPrepared == null ? $this->pdo->errorInfo() : $this->addEntryPrepared->errorInfo();
if ($this->autoUpdateDb($info)) {
$this->addEntryPrepared = null;
return $this->addEntry($valuesTmp);
@@ -211,22 +142,26 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function commitNewEntries() {
- $sql = 'SET @rank=(SELECT MAX(id) - COUNT(*) FROM `' . $this->prefix . 'entrytmp`); ' . //MySQL-specific
- 'INSERT IGNORE INTO `' . $this->prefix . 'entry`
- (
- id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
- ) ' .
- 'SELECT @rank:=@rank+1 AS id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
- FROM `' . $this->prefix . 'entrytmp`
- ORDER BY date; ' .
- 'DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= @rank;';
- $hadTransaction = $this->bd->inTransaction();
+ $sql = <<<'SQL'
+SET @rank=(SELECT MAX(id) - COUNT(*) FROM `_entrytmp`);
+
+INSERT IGNORE INTO `_entry` (
+ id, guid, title, author, content_bin, link, date, `lastSeen`,
+ hash, is_read, is_favorite, id_feed, tags
+)
+SELECT @rank:=@rank+1 AS id, guid, title, author, content_bin, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
+FROM `_entrytmp`
+ORDER BY date;
+
+DELETE FROM `_entrytmp` WHERE id <= @rank;';
+SQL;
+ $hadTransaction = $this->pdo->inTransaction();
if (!$hadTransaction) {
- $this->bd->beginTransaction();
+ $this->pdo->beginTransaction();
}
- $result = $this->bd->exec($sql) !== false;
+ $result = $this->pdo->exec($sql) !== false;
if (!$hadTransaction) {
- $this->bd->commit();
+ $this->pdo->commit();
}
return $result;
}
@@ -239,7 +174,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
if ($this->updateEntryPrepared === null) {
- $sql = 'UPDATE `' . $this->prefix . 'entry` '
+ $sql = 'UPDATE `_entry` '
. 'SET title=:title, author=:author, '
. ($this->isCompressed() ? 'content_bin=COMPRESS(:content)' : 'content=:content')
. ', link=:link, date=:date, `lastSeen`=:last_seen, '
@@ -247,7 +182,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
. ', ' . ($valuesTmp['is_read'] === null ? '' : 'is_read=:is_read, ')
. 'tags=:tags '
. 'WHERE id_feed=:id_feed AND guid=:guid';
- $this->updateEntryPrepared = $this->bd->prepare($sql);
+ $this->updateEntryPrepared = $this->pdo->prepare($sql);
}
$valuesTmp['guid'] = substr($valuesTmp['guid'], 0, 760);
@@ -273,14 +208,14 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($this->hasNativeHex()) {
$this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hash']);
} else {
- $valuesTmp['hashBin'] = pack('H*', $valuesTmp['hash']); //hex2bin() is PHP5.4+
+ $valuesTmp['hashBin'] = hex2bin($valuesTmp['hash']);
$this->updateEntryPrepared->bindParam(':hash', $valuesTmp['hashBin']);
}
if ($this->updateEntryPrepared && $this->updateEntryPrepared->execute()) {
return true;
} else {
- $info = $this->updateEntryPrepared == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $this->updateEntryPrepared->errorInfo();
+ $info = $this->updateEntryPrepared == null ? $this->pdo->errorInfo() : $this->updateEntryPrepared->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->updateEntry($valuesTmp);
}
@@ -308,16 +243,16 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return 0;
}
FreshRSS_UserDAO::touch();
- $sql = 'UPDATE `' . $this->prefix . 'entry` '
+ $sql = 'UPDATE `_entry` '
. 'SET is_favorite=? '
. 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
$values = array($is_favorite ? 1 : 0);
$values = array_merge($values, $ids);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markFavorite: ' . $info[2]);
return false;
}
@@ -335,11 +270,11 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
* @return boolean
*/
protected function updateCacheUnreads($catId = false, $feedId = false) {
- $sql = 'UPDATE `' . $this->prefix . 'feed` f '
+ $sql = 'UPDATE `_feed` f '
. 'LEFT OUTER JOIN ('
. 'SELECT e.id_feed, '
. 'COUNT(*) AS nbUnreads '
- . 'FROM `' . $this->prefix . 'entry` e '
+ . 'FROM `_entry` e '
. 'WHERE e.is_read=0 '
. 'GROUP BY e.id_feed'
. ') x ON x.id_feed=f.id '
@@ -358,11 +293,11 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$sql .= ' f.category=?';
$values[] = $catId;
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return true;
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
return false;
}
@@ -392,14 +327,14 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return $affected;
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` '
+ $sql = 'UPDATE `_entry` '
. 'SET is_read=? '
. 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?)';
$values = array($is_read ? 1 : 0);
$values = array_merge($values, $ids);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markRead: ' . $info[2]);
return false;
}
@@ -409,16 +344,16 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
return $affected;
} else {
- $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
+ $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
. 'SET e.is_read=?,'
. 'f.`cache_nbUnreads`=f.`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
. 'WHERE e.id=? AND e.is_read=?';
$values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markRead: ' . $info[2]);
return false;
}
@@ -453,7 +388,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
+ $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
. 'SET e.is_read=? '
. 'WHERE e.is_read <> ? AND e.id <= ?';
if ($onlyFavorites) {
@@ -465,9 +400,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
return false;
}
@@ -496,16 +431,16 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
Minz_Log::debug('Calling markReadCat(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id '
+ $sql = 'UPDATE `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id '
. 'SET e.is_read=? '
. 'WHERE f.category=? AND e.is_read <> ? AND e.id <= ?';
$values = array($is_read ? 1 : 0, $id, $is_read ? 1 : 0, $idMax);
list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadCat: ' . $info[2]);
return false;
}
@@ -533,39 +468,39 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$idMax = time() . '000000';
Minz_Log::debug('Calling markReadFeed(0) is deprecated!');
}
- $this->bd->beginTransaction();
+ $this->pdo->beginTransaction();
- $sql = 'UPDATE `' . $this->prefix . 'entry` '
+ $sql = 'UPDATE `_entry` '
. 'SET is_read=? '
. 'WHERE id_feed=? AND is_read <> ? AND id <= ?';
$values = array($is_read ? 1 : 0, $id_feed, $is_read ? 1 : 0, $idMax);
list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadFeed: ' . $info[2] . ' with SQL: ' . $sql . $search);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
$affected = $stm->rowCount();
if ($affected > 0) {
- $sql = 'UPDATE `' . $this->prefix . 'feed` '
+ $sql = 'UPDATE `_feed` '
. 'SET `cache_nbUnreads`=`cache_nbUnreads`-' . $affected
- . ' WHERE id=?';
- $values = array($id_feed);
- $stm = $this->bd->prepare($sql);
- if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ . ' WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id_feed, PDO::PARAM_INT);
+ if (!($stm && $stm->execute())) {
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadFeed cache: ' . $info[2]);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
}
- $this->bd->commit();
+ $this->pdo->commit();
return $affected;
}
@@ -582,7 +517,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
Minz_Log::debug('Calling markReadTag(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'entrytag` et ON et.id_entry = e.id '
+ $sql = 'UPDATE `_entry` e INNER JOIN `_entrytag` et ON et.id_entry = e.id '
. 'SET e.is_read = ? '
. 'WHERE '
. ($id == '' ? '' : 'et.id_tag = ? AND ')
@@ -596,9 +531,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadTag: ' . $info[2]);
return false;
}
@@ -609,48 +544,86 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return $affected;
}
- public function cleanOldEntries($id_feed, $date_min, $keep = 15) { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
- $sql = 'DELETE FROM `' . $this->prefix . 'entry` '
- . 'WHERE id_feed=:id_feed AND id<=:id_max '
- . 'AND is_favorite=0 ' //Do not remove favourites
- . 'AND `lastSeen` < (SELECT maxLastSeen FROM (SELECT (MAX(e3.`lastSeen`)-99) AS maxLastSeen FROM `' . $this->prefix . 'entry` e3 WHERE e3.id_feed=:id_feed) recent) ' //Do not remove the most newly seen articles, plus a few seconds of tolerance
- . 'AND id NOT IN (SELECT id_entry FROM `' . $this->prefix . 'entrytag`) ' //Do not purge tagged entries
- . 'AND id NOT IN (SELECT id FROM (SELECT e2.id FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=:id_feed ORDER BY id DESC LIMIT :keep) keep)'; //Double select: MySQL doesn't support 'LIMIT & IN/ALL/ANY/SOME subquery'
- $stm = $this->bd->prepare($sql);
+ public function cleanOldEntries($id_feed, $options = []) { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
+ $sql = 'DELETE FROM `_entry` WHERE id_feed = :id_feed1'; //No alias for MySQL / MariaDB
+ $params = [];
+ $params[':id_feed1'] = $id_feed;
- if ($stm) {
- $id_max = intval($date_min) . '000000';
- $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
- $stm->bindParam(':id_max', $id_max, PDO::PARAM_STR);
- $stm->bindParam(':keep', $keep, PDO::PARAM_INT);
+ //==Exclusions==
+ if (!empty($options['keep_favourites'])) {
+ $sql .= ' AND is_favorite = 0';
+ }
+ if (!empty($options['keep_unreads'])) {
+ $sql .= ' AND is_read = 1';
+ }
+ if (!empty($options['keep_labels'])) {
+ $sql .= ' AND NOT EXISTS (SELECT 1 FROM `_entrytag` WHERE id_entry = id)';
+ }
+ if (!empty($options['keep_min']) && $options['keep_min'] > 0) {
+ //Double SELECT for MySQL workaround ERROR 1093 (HY000)
+ $sql .= ' AND `lastSeen` < (SELECT `lastSeen`'
+ . ' FROM (SELECT e2.`lastSeen` FROM `_entry` e2 WHERE e2.id_feed = :id_feed2'
+ . ' ORDER BY e2.`lastSeen` DESC LIMIT 1 OFFSET :keep_min) last_seen2)';
+ $params[':id_feed2'] = $id_feed;
+ $params[':keep_min'] = (int)$options['keep_min'];
+ }
+ //Keep at least the articles seen at the last refresh
+ $sql .= ' AND `lastSeen` < (SELECT maxlastseen'
+ . ' FROM (SELECT MAX(e3.`lastSeen`) AS maxlastseen FROM `_entry` e3 WHERE e3.id_feed = :id_feed3) last_seen3)';
+ $params[':id_feed3'] = $id_feed;
+
+ //==Inclusions==
+ $sql .= ' AND (1=0';
+ if (!empty($options['keep_period'])) {
+ $sql .= ' OR `lastSeen` < :max_last_seen';
+ $now = new DateTime('now');
+ $now->sub(new DateInterval($options['keep_period']));
+ $params[':max_last_seen'] = $now->format('U');
}
+ if (!empty($options['keep_max']) && $options['keep_max'] > 0) {
+ $sql .= ' OR `lastSeen` <= (SELECT `lastSeen`'
+ . ' FROM (SELECT e4.`lastSeen` FROM `_entry` e4 WHERE e4.id_feed = :id_feed4'
+ . ' ORDER BY e4.`lastSeen` DESC LIMIT 1 OFFSET :keep_max) last_seen4)';
+ $params[':id_feed4'] = $id_feed;
+ $params[':keep_max'] = (int)$options['keep_max'];
+ }
+ $sql .= ')';
+
+ $stm = $this->pdo->prepare($sql);
- if ($stm && $stm->execute()) {
+ if ($stm && $stm->execute($params)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
- return $this->cleanOldEntries($id_feed, $date_min, $keep);
+ return $this->cleanOldEntries($id_feed, $options);
}
- Minz_Log::error('SQL error cleanOldEntries: ' . $info[2]);
+ Minz_Log::error(__method__ . ' error:' . json_encode($info));
return false;
}
}
+ public function selectAll() {
+ $sql = 'SELECT id, guid, title, author, '
+ . ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
+ . ', link, date, `lastSeen`, ' . $this->sqlHexEncode('hash') . ' AS hash, is_read, is_favorite, id_feed, tags '
+ . 'FROM `_entry`';
+ $stm = $this->pdo->query($sql);
+ while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
+ yield $row;
+ }
+ }
+
public function searchByGuid($id_feed, $guid) {
// un guid est unique pour un flux donné
$sql = 'SELECT id, guid, title, author, '
. ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ', link, date, is_read, is_favorite, id_feed, tags '
- . 'FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
- $stm = $this->bd->prepare($sql);
-
- $values = array(
- $id_feed,
- $guid,
- );
-
- $stm->execute($values);
+ . 'FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
+ $stm->bindParam(':guid', $guid);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$entries = self::daoToEntries($res);
return isset($entries[0]) ? $entries[0] : null;
@@ -660,22 +633,21 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$sql = 'SELECT id, guid, title, author, '
. ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ', link, date, is_read, is_favorite, id_feed, tags '
- . 'FROM `' . $this->prefix . 'entry` WHERE id=?';
- $stm = $this->bd->prepare($sql);
-
- $values = array($id);
-
- $stm->execute($values);
+ . 'FROM `_entry` WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$entries = self::daoToEntries($res);
return isset($entries[0]) ? $entries[0] : null;
}
public function searchIdByGuid($id_feed, $guid) {
- $sql = 'SELECT id FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid=?';
- $stm = $this->bd->prepare($sql);
- $values = array($id_feed, $guid);
- $stm->execute($values);
+ $sql = 'SELECT id FROM `_entry` WHERE id_feed=:id_feed AND guid=:guid';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id_feed', $id_feed, PDO::PARAM_INT);
+ $stm->bindParam(':guid', $guid);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return isset($res[0]) ? $res[0] : null;
}
@@ -859,7 +831,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$where .= '1=1 ';
break;
case 'ST': //Starred or tagged
- $where .= 'e.is_favorite=1 OR EXISTS (SELECT et2.id_tag FROM `' . $this->prefix . 'entrytag` et2 WHERE et2.id_entry = e.id) ';
+ $where .= 'e.is_favorite=1 OR EXISTS (SELECT et2.id_tag FROM `_entrytag` et2 WHERE et2.id_entry = e.id) ';
break;
default:
throw new FreshRSS_EntriesGetter_Exception('Bad type in Entry->listByType: [' . $type . ']!');
@@ -870,9 +842,9 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return array(array_merge($values, $searchValues),
'SELECT '
. ($type === 'T' ? 'DISTINCT ' : '')
- . 'e.id FROM `' . $this->prefix . 'entry` e '
- . 'INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed = f.id '
- . ($type === 't' || $type === 'T' ? 'INNER JOIN `' . $this->prefix . 'entrytag` et ON et.id_entry = e.id ' : '')
+ . 'e.id FROM `_entry` e '
+ . 'INNER JOIN `_feed` f ON e.id_feed = f.id '
+ . ($type === 't' || $type === 'T' ? 'INNER JOIN `_entrytag` et ON et.id_entry = e.id ' : '')
. 'WHERE ' . $where
. $search
. 'ORDER BY e.id ' . $order
@@ -885,17 +857,17 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$sql = 'SELECT e0.id, e0.guid, e0.title, e0.author, '
. ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ', e0.link, e0.date, e0.is_read, e0.is_favorite, e0.id_feed, e0.tags '
- . 'FROM `' . $this->prefix . 'entry` e0 '
+ . 'FROM `_entry` e0 '
. 'INNER JOIN ('
. $sql
. ') e2 ON e2.id=e0.id '
. 'ORDER BY e0.id ' . $order;
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return $stm;
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error listWhereRaw: ' . $info[2]);
return false;
}
@@ -918,11 +890,11 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$sql = 'SELECT id, guid, title, author, '
. ($this->isCompressed() ? 'UNCOMPRESS(content_bin) AS content' : 'content')
. ', link, date, is_read, is_favorite, id_feed, tags '
- . 'FROM `' . $this->prefix . 'entry` '
+ . 'FROM `_entry` '
. 'WHERE id IN (' . str_repeat('?,', count($ids) - 1). '?) '
. 'ORDER BY id ' . $order;
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$stm->execute($ids);
return self::daoToEntries($stm->fetchAll(PDO::FETCH_ASSOC));
}
@@ -930,7 +902,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function listIdsWhere($type = 'a', $id = '', $state = FreshRSS_Entry::STATE_ALL, $order = 'DESC', $limit = 1, $firstId = '', $filters = null) { //For API
list($values, $sql) = $this->sqlListWhere($type, $id, $state, $order, $limit, $firstId, $filters);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$stm->execute($values);
return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
@@ -941,8 +913,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return array();
}
$guids = array_unique($guids);
- $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `' . $this->prefix . 'entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT guid, ' . $this->sqlHexEncode('hash') . ' AS hex_hash FROM `_entry` WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
+ $stm = $this->pdo->prepare($sql);
$values = array($id_feed);
$values = array_merge($values, $guids);
if ($stm && $stm->execute($values)) {
@@ -953,7 +925,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
return $result;
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->listHashForFeedGuids($id_feed, $guids);
}
@@ -967,8 +939,8 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if (count($guids) < 1) {
return 0;
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_entry` SET `lastSeen`=? WHERE id_feed=? AND guid IN (' . str_repeat('?,', count($guids) - 1). '?)';
+ $stm = $this->pdo->prepare($sql);
if ($mtime <= 0) {
$mtime = time();
}
@@ -977,7 +949,7 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->updateLastSeen($id_feed, $guids);
}
@@ -988,65 +960,70 @@ class FreshRSS_EntryDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function countUnreadRead() {
- $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE f.priority > 0'
- . ' UNION SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id WHERE f.priority > 0 AND e.is_read=0';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0'
+ . ' UNION SELECT COUNT(e.id) AS count FROM `_entry` e INNER JOIN `_feed` f ON e.id_feed=f.id WHERE f.priority > 0 AND e.is_read=0';
+ $stm = $this->pdo->query($sql);
+ if ($stm === false) {
+ return false;
+ }
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
rsort($res);
$all = empty($res[0]) ? 0 : $res[0];
$unread = empty($res[1]) ? 0 : $res[1];
return array('all' => $all, 'unread' => $unread, 'read' => $all - $unread);
}
+
public function count($minPriority = null) {
- $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e';
+ $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
if ($minPriority !== null) {
- $sql .= ' INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
+ $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
$sql .= ' WHERE f.priority > ' . intval($minPriority);
}
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
+ if ($stm == false) {
+ return false;
+ }
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return isset($res[0]) ? $res[0] : 0;
}
+
public function countNotRead($minPriority = null) {
- $sql = 'SELECT COUNT(e.id) AS count FROM `' . $this->prefix . 'entry` e';
+ $sql = 'SELECT COUNT(e.id) AS count FROM `_entry` e';
if ($minPriority !== null) {
- $sql .= ' INNER JOIN `' . $this->prefix . 'feed` f ON e.id_feed=f.id';
+ $sql .= ' INNER JOIN `_feed` f ON e.id_feed=f.id';
}
$sql .= ' WHERE e.is_read=0';
if ($minPriority !== null) {
$sql .= ' AND f.priority > ' . intval($minPriority);
}
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
return $res[0];
}
public function countUnreadReadFavorites() {
- $sql = <<<SQL
- SELECT c
- FROM (
- SELECT COUNT(e1.id) AS c
- , 1 AS o
- FROM `{$this->prefix}entry` AS e1
- JOIN `{$this->prefix}feed` AS f1 ON e1.id_feed = f1.id
- WHERE e1.is_favorite = 1
- AND f1.priority >= :priority_normal
- UNION
- SELECT COUNT(e2.id) AS c
- , 2 AS o
- FROM `{$this->prefix}entry` AS e2
- JOIN `{$this->prefix}feed` AS f2 ON e2.id_feed = f2.id
- WHERE e2.is_favorite = 1
- AND e2.is_read = 0
- AND f2.priority >= :priority_normal
- ) u
+ $sql = <<<'SQL'
+SELECT c FROM (
+ SELECT COUNT(e1.id) AS c, 1 AS o
+ FROM `_entry` AS e1
+ JOIN `_feed` AS f1 ON e1.id_feed = f1.id
+ WHERE e1.is_favorite = 1
+ AND f1.priority >= :priority_normal1
+ UNION
+ SELECT COUNT(e2.id) AS c, 2 AS o
+ FROM `_entry` AS e2
+ JOIN `_feed` AS f2 ON e2.id_feed = f2.id
+ WHERE e2.is_favorite = 1
+ AND e2.is_read = 0
+ AND f2.priority >= :priority_normal2
+ ) u
ORDER BY o
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute(array(':priority_normal' => FreshRSS_Feed::PRIORITY_NORMAL));
+ $stm = $this->pdo->prepare($sql);
+ //Binding a value more than once is not standard and does not work with native prepared statements (e.g. MySQL) https://bugs.php.net/bug.php?id=40417
+ $stm->bindValue(':priority_normal1', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
+ $stm->bindValue(':priority_normal2', FreshRSS_Feed::PRIORITY_NORMAL, PDO::PARAM_INT);
+ $stm->execute();
$res = $stm->fetchAll(PDO::FETCH_COLUMN, 0);
rsort($res);
$all = empty($res[0]) ? 0 : $res[0];
diff --git a/app/Models/EntryDAOPGSQL.php b/app/Models/EntryDAOPGSQL.php
index e571e457f..9afea279f 100644
--- a/app/Models/EntryDAOPGSQL.php
+++ b/app/Models/EntryDAOPGSQL.php
@@ -2,6 +2,10 @@
class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
+ public function hasNativeHex() {
+ return true;
+ }
+
public function sqlHexDecode($x) {
return 'decode(' . $x . ", 'hex')";
}
@@ -31,25 +35,27 @@ class FreshRSS_EntryDAOPGSQL extends FreshRSS_EntryDAOSQLite {
public function commitNewEntries() {
$sql = 'DO $$
DECLARE
-maxrank bigint := (SELECT MAX(id) FROM `' . $this->prefix . 'entrytmp`);
-rank bigint := (SELECT maxrank - COUNT(*) FROM `' . $this->prefix . 'entrytmp`);
+maxrank bigint := (SELECT MAX(id) FROM `_entrytmp`);
+rank bigint := (SELECT maxrank - COUNT(*) FROM `_entrytmp`);
BEGIN
- INSERT INTO `' . $this->prefix . 'entry` (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
- (SELECT rank + row_number() OVER(ORDER BY date) AS id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
- FROM `' . $this->prefix . 'entrytmp` AS etmp
+ INSERT INTO `_entry`
+ (id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
+ (SELECT rank + row_number() OVER(ORDER BY date) AS id, guid, title, author, content,
+ link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
+ FROM `_entrytmp` AS etmp
WHERE NOT EXISTS (
- SELECT 1 FROM `' . $this->prefix . 'entry` AS ereal
+ SELECT 1 FROM `_entry` AS ereal
WHERE (etmp.id = ereal.id) OR (etmp.id_feed = ereal.id_feed AND etmp.guid = ereal.guid))
ORDER BY date);
- DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= maxrank;
+ DELETE FROM `_entrytmp` WHERE id <= maxrank;
END $$;';
- $hadTransaction = $this->bd->inTransaction();
+ $hadTransaction = $this->pdo->inTransaction();
if (!$hadTransaction) {
- $this->bd->beginTransaction();
+ $this->pdo->beginTransaction();
}
- $result = $this->bd->exec($sql) !== false;
+ $result = $this->pdo->exec($sql) !== false;
if (!$hadTransaction) {
- $this->bd->commit();
+ $this->pdo->commit();
}
return $result;
}
diff --git a/app/Models/EntryDAOSQLite.php b/app/Models/EntryDAOSQLite.php
index f8cd14fe6..12e8f27e1 100644
--- a/app/Models/EntryDAOSQLite.php
+++ b/app/Models/EntryDAOSQLite.php
@@ -2,32 +2,32 @@
class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
+ public function isCompressed() {
+ return false;
+ }
+
+ public function hasNativeHex() {
+ return false;
+ }
+
public function sqlHexDecode($x) {
return $x;
}
protected function autoUpdateDb($errorInfo) {
- if ($tableInfo = $this->bd->query("SELECT sql FROM sqlite_master where name='tag'")) {
+ if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) {
$showCreate = $tableInfo->fetchColumn();
if (stripos($showCreate, 'tag') === false) {
$tagDAO = FreshRSS_Factory::createTagDao();
return $tagDAO->createTagTable(); //v1.12.0
}
}
- if ($tableInfo = $this->bd->query("SELECT sql FROM sqlite_master where name='entrytmp'")) {
+ if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='entrytmp'")) {
$showCreate = $tableInfo->fetchColumn();
if (stripos($showCreate, 'entrytmp') === false) {
return $this->createEntryTempTable(); //v1.7.0
}
}
- if ($tableInfo = $this->bd->query("SELECT sql FROM sqlite_master where name='entry'")) {
- $showCreate = $tableInfo->fetchColumn();
- foreach (array('lastSeen', 'hash') as $column) {
- if (stripos($showCreate, $column) === false) {
- return $this->addColumn($column);
- }
- }
- }
return false;
}
@@ -36,27 +36,27 @@ class FreshRSS_EntryDAOSQLite extends FreshRSS_EntryDAO {
DROP TABLE IF EXISTS `tmp`;
CREATE TEMP TABLE `tmp` AS
SELECT id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
- FROM `' . $this->prefix . 'entrytmp`
+ FROM `_entrytmp`
ORDER BY date;
-INSERT OR IGNORE INTO `' . $this->prefix . 'entry`
+INSERT OR IGNORE INTO `_entry`
(id, guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags)
SELECT rowid + (SELECT MAX(id) - COUNT(*) FROM `tmp`) AS id,
guid, title, author, content, link, date, `lastSeen`, hash, is_read, is_favorite, id_feed, tags
FROM `tmp`
ORDER BY date;
-DELETE FROM `' . $this->prefix . 'entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
+DELETE FROM `_entrytmp` WHERE id <= (SELECT MAX(id) FROM `tmp`);
DROP TABLE IF EXISTS `tmp`;
';
- $hadTransaction = $this->bd->inTransaction();
+ $hadTransaction = $this->pdo->inTransaction();
if (!$hadTransaction) {
- $this->bd->beginTransaction();
+ $this->pdo->beginTransaction();
}
- $result = $this->bd->exec($sql) !== false;
+ $result = $this->pdo->exec($sql) !== false;
if (!$result) {
- Minz_Log::error('SQL error commitNewEntries: ' . json_encode($this->bd->errorInfo()));
+ Minz_Log::error('SQL error commitNewEntries: ' . json_encode($this->pdo->errorInfo()));
}
if (!$hadTransaction) {
- $this->bd->commit();
+ $this->pdo->commit();
}
return $result;
}
@@ -66,10 +66,10 @@ DROP TABLE IF EXISTS `tmp`;
}
protected function updateCacheUnreads($catId = false, $feedId = false) {
- $sql = 'UPDATE `' . $this->prefix . 'feed` '
+ $sql = 'UPDATE `_feed` '
. 'SET `cache_nbUnreads`=('
- . 'SELECT COUNT(*) AS nbUnreads FROM `' . $this->prefix . 'entry` e '
- . 'WHERE e.id_feed=`' . $this->prefix . 'feed`.id AND e.is_read=0)';
+ . 'SELECT COUNT(*) AS nbUnreads FROM `_entry` e '
+ . 'WHERE e.id_feed=`_feed`.id AND e.is_read=0)';
$hasWhere = false;
$values = array();
if ($feedId !== false) {
@@ -84,11 +84,11 @@ DROP TABLE IF EXISTS `tmp`;
$sql .= ' category=?';
$values[] = $catId;
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return true;
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error updateCacheUnreads: ' . $info[2]);
return false;
}
@@ -118,30 +118,30 @@ DROP TABLE IF EXISTS `tmp`;
return $affected;
}
} else {
- $this->bd->beginTransaction();
- $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read=? WHERE id=? AND is_read=?';
+ $this->pdo->beginTransaction();
+ $sql = 'UPDATE `_entry` SET is_read=? WHERE id=? AND is_read=?';
$values = array($is_read ? 1 : 0, $ids, $is_read ? 0 : 1);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markRead 1: ' . $info[2]);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
$affected = $stm->rowCount();
if ($affected > 0) {
- $sql = 'UPDATE `' . $this->prefix . 'feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
- . 'WHERE id=(SELECT e.id_feed FROM `' . $this->prefix . 'entry` e WHERE e.id=?)';
+ $sql = 'UPDATE `_feed` SET `cache_nbUnreads`=`cache_nbUnreads`' . ($is_read ? '-' : '+') . '1 '
+ . 'WHERE id=(SELECT e.id_feed FROM `_entry` e WHERE e.id=?)';
$values = array($ids);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markRead 2: ' . $info[2]);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
}
- $this->bd->commit();
+ $this->pdo->commit();
return $affected;
}
}
@@ -174,19 +174,19 @@ DROP TABLE IF EXISTS `tmp`;
Minz_Log::debug('Calling markReadEntries(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` SET is_read = ? WHERE is_read <> ? AND id <= ?';
+ $sql = 'UPDATE `_entry` SET is_read = ? WHERE is_read <> ? AND id <= ?';
if ($onlyFavorites) {
$sql .= ' AND is_favorite=1';
} elseif ($priorityMin >= 0) {
- $sql .= ' AND id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
+ $sql .= ' AND id_feed IN (SELECT f.id FROM `_feed` f WHERE f.priority > ' . intval($priorityMin) . ')';
}
$values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadEntries: ' . $info[2]);
return false;
}
@@ -215,17 +215,17 @@ DROP TABLE IF EXISTS `tmp`;
Minz_Log::debug('Calling markReadCat(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` '
+ $sql = 'UPDATE `_entry` '
. 'SET is_read = ? '
. 'WHERE is_read <> ? AND id <= ? AND '
- . 'id_feed IN (SELECT f.id FROM `' . $this->prefix . 'feed` f WHERE f.category=?)';
+ . 'id_feed IN (SELECT f.id FROM `_feed` f WHERE f.category=?)';
$values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax, $id);
list($searchValues, $search) = $this->sqlListEntriesWhere('', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadCat: ' . $info[2]);
return false;
}
@@ -249,10 +249,10 @@ DROP TABLE IF EXISTS `tmp`;
Minz_Log::debug('Calling markReadTag(0) is deprecated!');
}
- $sql = 'UPDATE `' . $this->prefix . 'entry` e '
+ $sql = 'UPDATE `_entry` e '
. 'SET e.is_read = ? '
. 'WHERE e.is_read <> ? AND e.id <= ? AND '
- . 'e.id IN (SELECT et.id_entry FROM `' . $this->prefix . 'entrytag` et '
+ . 'e.id IN (SELECT et.id_entry FROM `_entrytag` et '
. ($id == '' ? '' : 'WHERE et.id = ?')
. ')';
$values = array($is_read ? 1 : 0, $is_read ? 1 : 0, $idMax);
@@ -262,9 +262,9 @@ DROP TABLE IF EXISTS `tmp`;
list($searchValues, $search) = $this->sqlListEntriesWhere('e.', $filters, $state);
- $stm = $this->bd->prepare($sql . $search);
+ $stm = $this->pdo->prepare($sql . $search);
if (!($stm && $stm->execute(array_merge($values, $searchValues)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error markReadTag: ' . $info[2]);
return false;
}
diff --git a/app/Models/Factory.php b/app/Models/Factory.php
index 1accb491c..69885c205 100644
--- a/app/Models/Factory.php
+++ b/app/Models/Factory.php
@@ -2,8 +2,18 @@
class FreshRSS_Factory {
+ public static function createUserDao($username = null) {
+ return new FreshRSS_UserDAO($username);
+ }
+
public static function createCategoryDao($username = null) {
- return new FreshRSS_CategoryDAO($username);
+ $conf = Minz_Configuration::get('system');
+ switch ($conf->db['type']) {
+ case 'sqlite':
+ return new FreshRSS_CategoryDAOSQLite($username);
+ default:
+ return new FreshRSS_CategoryDAO($username);
+ }
}
public static function createFeedDao($username = null) {
diff --git a/app/Models/Feed.php b/app/Models/Feed.php
index 89989236c..0a45a1f4c 100644
--- a/app/Models/Feed.php
+++ b/app/Models/Feed.php
@@ -7,8 +7,8 @@ class FreshRSS_Feed extends Minz_Model {
const TTL_DEFAULT = 0;
- const KEEP_HISTORY_DEFAULT = -2;
- const KEEP_HISTORY_INFINITE = -1;
+ const ARCHIVING_RETENTION_COUNT_LIMIT = 10000;
+ const ARCHIVING_RETENTION_PERIOD = 'P3M';
private $id = 0;
private $url;
@@ -24,9 +24,8 @@ class FreshRSS_Feed extends Minz_Model {
private $pathEntries = '';
private $httpAuth = '';
private $error = false;
- private $keep_history = self::KEEP_HISTORY_DEFAULT;
private $ttl = self::TTL_DEFAULT;
- private $attributes = array();
+ private $attributes = [];
private $mute = false;
private $hash = null;
private $lockPath = '';
@@ -110,9 +109,6 @@ class FreshRSS_Feed extends Minz_Model {
public function inError() {
return $this->error;
}
- public function keepHistory() {
- return $this->keep_history;
- }
public function ttl() {
return $this->ttl;
}
@@ -153,18 +149,17 @@ class FreshRSS_Feed extends Minz_Model {
return $this->nbNotRead;
}
public function faviconPrepare() {
- global $favicons_dir;
require_once(LIB_PATH . '/favicons.php');
$url = $this->website;
if ($url == '') {
$url = $this->url;
}
- $txt = $favicons_dir . $this->hash() . '.txt';
+ $txt = FAVICONS_DIR . $this->hash() . '.txt';
if (!file_exists($txt)) {
file_put_contents($txt, $url);
}
if (FreshRSS_Context::$isCli) {
- $ico = $favicons_dir . $this->hash() . '.ico';
+ $ico = FAVICONS_DIR . $this->hash() . '.ico';
$ico_mtime = @filemtime($ico);
$txt_mtime = @filemtime($txt);
if ($txt_mtime != false &&
@@ -231,12 +226,6 @@ class FreshRSS_Feed extends Minz_Model {
public function _error($value) {
$this->error = (bool)$value;
}
- public function _keepHistory($value) {
- $value = intval($value);
- $value = min($value, 1000000);
- $value = max($value, self::KEEP_HISTORY_DEFAULT);
- $this->keep_history = $value;
- }
public function _ttl($value) {
$value = intval($value);
$value = min($value, 100000000);
@@ -470,6 +459,28 @@ class FreshRSS_Feed extends Minz_Model {
$this->entries = $entries;
}
+ public function cleanOldEntries() { //Remember to call updateCachedValue($id_feed) or updateCachedValues() just after
+ $archiving = $this->attributes('archiving');
+ if ($archiving == null) {
+ $catDAO = FreshRSS_Factory::createCategoryDao();
+ $category = $catDAO->searchById($this->category());
+ $archiving = $category == null ? null : $category->attributes('archiving');
+ if ($archiving == null) {
+ $archiving = FreshRSS_Context::$user_conf->archiving;
+ }
+ }
+ if (is_array($archiving)) {
+ $entryDAO = FreshRSS_Factory::createEntryDao();
+ $nb = $entryDAO->cleanOldEntries($this->id(), $archiving);
+ if ($nb > 0) {
+ $needFeedCacheRefresh = true;
+ Minz_Log::debug($nb . ' entries cleaned in feed [' . $this->url(false) . '] with: ' . json_encode($archiving));
+ }
+ return $nb;
+ }
+ return false;
+ }
+
protected function cacheFilename() {
return CACHE_PATH . '/' . md5($this->url) . '.spc';
}
@@ -701,7 +712,7 @@ class FreshRSS_Feed extends Minz_Model {
file_put_contents($hubFilename, json_encode($hubJson));
}
$ch = curl_init();
- curl_setopt_array($ch, array(
+ curl_setopt_array($ch, [
CURLOPT_URL => $hubJson['hub'],
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POSTFIELDS => http_build_query(array(
@@ -712,13 +723,9 @@ class FreshRSS_Feed extends Minz_Model {
)),
CURLOPT_USERAGENT => FRESHRSS_USERAGENT,
CURLOPT_MAXREDIRS => 10,
- ));
- if (version_compare(PHP_VERSION, '5.6.0') >= 0 || ini_get('open_basedir') == '') {
- curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //Keep option separated for open_basedir PHP bug 65646
- }
- if (defined('CURLOPT_ENCODING')) {
- curl_setopt($ch, CURLOPT_ENCODING, ''); //Enable all encodings
- }
+ CURLOPT_FOLLOWLOCATION => true,
+ CURLOPT_ENCODING => '', //Enable all encodings
+ ]);
$response = curl_exec($ch);
$info = curl_getinfo($ch);
diff --git a/app/Models/FeedDAO.php b/app/Models/FeedDAO.php
index c9c9f6301..fa0001df7 100644
--- a/app/Models/FeedDAO.php
+++ b/app/Models/FeedDAO.php
@@ -3,14 +3,13 @@
class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
protected function addColumn($name) {
- Minz_Log::warning('FreshRSS_FeedDAO::addColumn: ' . $name);
+ Minz_Log::warning(__method__ . ': ' . $name);
try {
if ($name === 'attributes') { //v1.11.0
- $stm = $this->bd->prepare('ALTER TABLE `' . $this->prefix . 'feed` ADD COLUMN attributes TEXT');
- return $stm && $stm->execute();
+ return $this->pdo->exec('ALTER TABLE `_feed` ADD COLUMN attributes TEXT') !== false;
}
} catch (Exception $e) {
- Minz_Log::error('FreshRSS_FeedDAO::addColumn error: ' . $e->getMessage());
+ Minz_Log::error(__method__ . ' error: ' . $e->getMessage());
}
return false;
}
@@ -18,7 +17,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
protected function autoUpdateDb($errorInfo) {
if (isset($errorInfo[0])) {
if ($errorInfo[0] === FreshRSS_DatabaseDAO::ER_BAD_FIELD_ERROR || $errorInfo[0] === FreshRSS_DatabaseDAOPGSQL::UNDEFINED_COLUMN) {
- foreach (array('attributes') as $column) {
+ foreach (['attributes'] as $column) {
if (stripos($errorInfo[2], $column) !== false) {
return $this->addColumn($column);
}
@@ -30,7 +29,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function addFeed($valuesTmp) {
$sql = '
- INSERT INTO `' . $this->prefix . 'feed`
+ INSERT INTO `_feed`
(
url,
category,
@@ -39,18 +38,24 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
description,
`lastUpdate`,
priority,
+ `pathEntries`,
`httpAuth`,
error,
- keep_history,
ttl,
attributes
)
VALUES
- (?, ?, ?, ?, ?, ?, 10, ?, 0, ?, ?, ?)';
- $stm = $this->bd->prepare($sql);
+ (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)';
+ $stm = $this->pdo->prepare($sql);
$valuesTmp['url'] = safe_ascii($valuesTmp['url']);
$valuesTmp['website'] = safe_ascii($valuesTmp['website']);
+ if (!isset($valuesTmp['pathEntries'])) {
+ $valuesTmp['pathEntries'] = '';
+ }
+ if (!isset($valuesTmp['attributes'])) {
+ $valuesTmp['attributes'] = [];
+ }
$values = array(
substr($valuesTmp['url'], 0, 511),
@@ -59,16 +64,18 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
substr($valuesTmp['website'], 0, 255),
mb_strcut($valuesTmp['description'], 0, 1023, 'UTF-8'),
$valuesTmp['lastUpdate'],
+ isset($valuesTmp['priority']) ? intval($valuesTmp['priority']) : FreshRSS_Feed::PRIORITY_MAIN_STREAM,
+ mb_strcut($valuesTmp['pathEntries'], 0, 511, 'UTF-8'),
base64_encode($valuesTmp['httpAuth']),
- FreshRSS_Feed::KEEP_HISTORY_DEFAULT,
+ isset($valuesTmp['error']) ? intval($valuesTmp['error']) : 0,
isset($valuesTmp['ttl']) ? intval($valuesTmp['ttl']) : FreshRSS_Feed::TTL_DEFAULT,
- isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
+ is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
);
if ($stm && $stm->execute($values)) {
- return $this->bd->lastInsertId('"' . $this->prefix . 'feed_id_seq"');
+ return $this->pdo->lastInsertId('`_feed_id_seq`');
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->addFeed($valuesTmp);
}
@@ -129,13 +136,13 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($key === 'httpAuth') {
$valuesTmp[$key] = base64_encode($v);
} elseif ($key === 'attributes') {
- $valuesTmp[$key] = json_encode($v);
+ $valuesTmp[$key] = is_string($valuesTmp[$key]) ? $valuesTmp[$key] : json_encode($valuesTmp[$key], JSON_UNESCAPED_SLASHES);
}
}
$set = substr($set, 0, -2);
- $sql = 'UPDATE `' . $this->prefix . 'feed` SET ' . $set . ' WHERE id=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_feed` SET ' . $set . ' WHERE id=?';
+ $stm = $this->pdo->prepare($sql);
foreach ($valuesTmp as $v) {
$values[] = $v;
@@ -145,7 +152,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->updateFeed($id, $valuesTmp);
}
@@ -166,7 +173,7 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function updateLastUpdate($id, $inError = false, $mtime = 0) { //See also updateCachedValue()
- $sql = 'UPDATE `' . $this->prefix . 'feed` '
+ $sql = 'UPDATE `_feed` '
. 'SET `lastUpdate`=?, error=? '
. 'WHERE id=?';
$values = array(
@@ -174,12 +181,12 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$inError ? 1 : 0,
$id,
);
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error updateLastUpdate: ' . $info[2]);
return false;
}
@@ -192,8 +199,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$newCat = $catDAO->getDefault();
}
- $sql = 'UPDATE `' . $this->prefix . 'feed` SET category=? WHERE category=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_feed` SET category=? WHERE category=?';
+ $stm = $this->pdo->prepare($sql);
$values = array(
$newCat->id(),
@@ -203,44 +210,54 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error changeCategory: ' . $info[2]);
return false;
}
}
public function deleteFeed($id) {
- $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE id=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'DELETE FROM `_feed` WHERE id=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error deleteFeed: ' . $info[2]);
return false;
}
}
public function deleteFeedByCategory($id) {
- $sql = 'DELETE FROM `' . $this->prefix . 'feed` WHERE category=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'DELETE FROM `_feed` WHERE category=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error deleteFeedByCategory: ' . $info[2]);
return false;
}
}
+ public function selectAll() {
+ $sql = 'SELECT id, url, category, name, website, description, `lastUpdate`, priority, '
+ . '`pathEntries`, `httpAuth`, error, ttl, attributes '
+ . 'FROM `_feed`';
+ $stm = $this->pdo->query($sql);
+ while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
+ yield $row;
+ }
+ }
+
public function searchById($id) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE id=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT * FROM `_feed` WHERE id=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
@@ -255,8 +272,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
}
public function searchByUrl($url) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE url=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT * FROM `_feed` WHERE url=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($url);
@@ -272,25 +289,21 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function listFeedsIds() {
- $sql = 'SELECT id FROM `' . $this->prefix . 'feed`';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $sql = 'SELECT id FROM `_feed`';
+ $stm = $this->pdo->query($sql);
return $stm->fetchAll(PDO::FETCH_COLUMN, 0);
}
public function listFeeds() {
- $sql = 'SELECT * FROM `' . $this->prefix . 'feed` ORDER BY name';
- $stm = $this->bd->prepare($sql);
- $stm->execute();
-
+ $sql = 'SELECT * FROM `_feed` ORDER BY name';
+ $stm = $this->pdo->query($sql);
return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
}
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();
+ $sql = 'SELECT f.id, f.name, c.name as c_name FROM `_feed` f '
+ . 'INNER JOIN `_category` c ON c.id = f.category';
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
$feedCategoryNames = array();
foreach ($res as $line) {
@@ -307,17 +320,18 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
*/
public function listFeedsOrderUpdate($defaultCacheDuration = 3600, $limit = 0) {
$this->updateTTL();
- $sql = 'SELECT id, url, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, keep_history, ttl, attributes '
- . 'FROM `' . $this->prefix . 'feed` '
+ $sql = 'SELECT id, url, name, website, `lastUpdate`, `pathEntries`, `httpAuth`, ttl, attributes '
+ . 'FROM `_feed` '
. ($defaultCacheDuration < 0 ? '' : 'WHERE ttl >= ' . FreshRSS_Feed::TTL_DEFAULT
- . ' AND `lastUpdate` < (' . (time() + 60) . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
+ . ' AND `lastUpdate` < (' . (time() + 60)
+ . '-(CASE WHEN ttl=' . FreshRSS_Feed::TTL_DEFAULT . ' THEN ' . intval($defaultCacheDuration) . ' ELSE ttl END)) ')
. 'ORDER BY `lastUpdate` '
. ($limit < 1 ? '' : 'LIMIT ' . intval($limit));
- $stm = $this->bd->prepare($sql);
- if ($stm && $stm->execute()) {
+ $stm = $this->pdo->query($sql);
+ if ($stm !== false) {
return self::daoToFeed($stm->fetchAll(PDO::FETCH_ASSOC));
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->listFeedsOrderUpdate($defaultCacheDuration);
}
@@ -327,8 +341,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function listByCategory($cat) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'feed` WHERE category=? ORDER BY name';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT * FROM `_feed` WHERE category=? ORDER BY name';
+ $stm = $this->pdo->prepare($sql);
$values = array($cat);
@@ -338,8 +352,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function countEntries($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -348,8 +362,8 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
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);
+ $sql = 'SELECT COUNT(*) AS count FROM `_entry` WHERE id_feed=? AND is_read=0';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -357,62 +371,51 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
return $res[0]['count'];
}
- public function updateCachedValue($id) { //For multiple feeds, call updateCachedValues()
- $sql = 'UPDATE `' . $this->prefix . 'feed` ' //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
- . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=`' . $this->prefix . 'feed`.id),'
- . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=`' . $this->prefix . 'feed`.id AND e2.is_read=0) '
- . 'WHERE id=?';
- $values = array($id);
- $stm = $this->bd->prepare($sql);
-
- if ($stm && $stm->execute($values)) {
- return $stm->rowCount();
- } else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
- return false;
+ public function updateCachedValues($id = null) {
+ //2 sub-requests with FOREIGN KEY(e.id_feed), INDEX(e.is_read) faster than 1 request with GROUP BY or CASE
+ $sql = 'UPDATE `_feed` '
+ . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `_entry` e1 WHERE e1.id_feed=`_feed`.id),'
+ . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `_entry` e2 WHERE e2.id_feed=`_feed`.id AND e2.is_read=0)'
+ . ($id != null ? ' WHERE id=:id' : '');
+ $stm = $this->pdo->prepare($sql);
+ if ($id != null) {
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
}
- }
- public function updateCachedValues() { //For one single feed, call updateCachedValue($id)
- $sql = 'UPDATE `' . $this->prefix . 'feed` '
- . 'SET `cache_nbEntries`=(SELECT COUNT(e1.id) FROM `' . $this->prefix . 'entry` e1 WHERE e1.id_feed=`' . $this->prefix . 'feed`.id),'
- . '`cache_nbUnreads`=(SELECT COUNT(e2.id) FROM `' . $this->prefix . 'entry` e2 WHERE e2.id_feed=`' . $this->prefix . 'feed`.id AND e2.is_read=0)';
- $stm = $this->bd->prepare($sql);
if ($stm && $stm->execute()) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error updateCachedValues: ' . $info[2]);
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ Minz_Log::error('SQL error updateCachedValue: ' . $info[2]);
return false;
}
}
public function truncate($id) {
- $sql = 'DELETE FROM `' . $this->prefix . 'entry` WHERE id_feed=?';
- $stm = $this->bd->prepare($sql);
- $values = array($id);
- $this->bd->beginTransaction();
- if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $sql = 'DELETE FROM `_entry` WHERE id_feed=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ $this->pdo->beginTransaction();
+ if (!($stm && $stm->execute())) {
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error truncate: ' . $info[2]);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
$affected = $stm->rowCount();
- $sql = 'UPDATE `' . $this->prefix . 'feed` '
- . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0 WHERE id=?';
- $values = array($id);
- $stm = $this->bd->prepare($sql);
- if (!($stm && $stm->execute($values))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $sql = 'UPDATE `_feed` '
+ . 'SET `cache_nbEntries`=0, `cache_nbUnreads`=0 WHERE id=:id';
+ $stm = $this->pdo->prepare($sql);
+ $stm->bindParam(':id', $id, PDO::PARAM_INT);
+ if (!($stm && $stm->execute())) {
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error truncate: ' . $info[2]);
- $this->bd->rollBack();
+ $this->pdo->rollBack();
return false;
}
- $this->bd->commit();
+ $this->pdo->commit();
return $affected;
}
@@ -446,7 +449,6 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
$myFeed->_pathEntries(isset($dao['pathEntries']) ? $dao['pathEntries'] : '');
$myFeed->_httpAuth(isset($dao['httpAuth']) ? base64_decode($dao['httpAuth']) : '');
$myFeed->_error(isset($dao['error']) ? $dao['error'] : 0);
- $myFeed->_keepHistory(isset($dao['keep_history']) ? $dao['keep_history'] : FreshRSS_Feed::KEEP_HISTORY_DEFAULT);
$myFeed->_ttl(isset($dao['ttl']) ? $dao['ttl'] : FreshRSS_Feed::TTL_DEFAULT);
$myFeed->_attributes('', isset($dao['attributes']) ? $dao['attributes'] : '');
$myFeed->_nbNotRead(isset($dao['cache_nbUnreads']) ? $dao['cache_nbUnreads'] : 0);
@@ -461,20 +463,16 @@ class FreshRSS_FeedDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function updateTTL() {
- $sql = <<<SQL
-UPDATE `{$this->prefix}feed`
- SET ttl = :new_value
- WHERE ttl = :old_value
-SQL;
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_feed` SET ttl=:new_value WHERE ttl=:old_value';
+ $stm = $this->pdo->prepare($sql);
if (!($stm && $stm->execute(array(':new_value' => FreshRSS_Feed::TTL_DEFAULT, ':old_value' => -2)))) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL warning updateTTL 1: ' . $info[2] . ' ' . $sql);
- $sql2 = 'ALTER TABLE `' . $this->prefix . 'feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
- $stm = $this->bd->prepare($sql2);
- if (!($stm && $stm->execute())) {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $sql2 = 'ALTER TABLE `_feed` ADD COLUMN ttl INT NOT NULL DEFAULT ' . FreshRSS_Feed::TTL_DEFAULT; //v0.7.3
+ $stm = $this->pdo->query($sql2);
+ if ($stm === false) {
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error updateTTL 2: ' . $info[2] . ' ' . $sql2);
}
} else {
diff --git a/app/Models/FeedDAOSQLite.php b/app/Models/FeedDAOSQLite.php
index 3c203b378..0f685867a 100644
--- a/app/Models/FeedDAOSQLite.php
+++ b/app/Models/FeedDAOSQLite.php
@@ -3,9 +3,9 @@
class FreshRSS_FeedDAOSQLite extends FreshRSS_FeedDAO {
protected function autoUpdateDb($errorInfo) {
- if ($tableInfo = $this->bd->query("PRAGMA table_info('feed')")) {
+ if ($tableInfo = $this->pdo->query("PRAGMA table_info('feed')")) {
$columns = $tableInfo->fetchAll(PDO::FETCH_COLUMN, 1);
- foreach (array('attributes') as $column) {
+ foreach (['attributes'] as $column) {
if (!in_array($column, $columns)) {
return $this->addColumn($column);
}
diff --git a/app/Models/StatsDAO.php b/app/Models/StatsDAO.php
index 67ada73f7..cbfa79c61 100644
--- a/app/Models/StatsDAO.php
+++ b/app/Models/StatsDAO.php
@@ -45,13 +45,11 @@ SELECT COUNT(1) AS total,
COUNT(1) - SUM(e.is_read) AS count_unreads,
SUM(e.is_read) AS count_reads,
SUM(e.is_favorite) AS count_favorites
-FROM `{$this->prefix}entry` AS e
-, `{$this->prefix}feed` AS f
+FROM `_entry` AS e, `_feed` AS f
WHERE e.id_feed = f.id
{$filter}
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
return $res[0];
@@ -73,13 +71,12 @@ SQL;
$sql = <<<SQL
SELECT {$sqlDay} AS day,
COUNT(*) as count
-FROM `{$this->prefix}entry`
+FROM `_entry`
WHERE date >= {$oldest} AND date < {$midnight}
GROUP BY day
ORDER BY day ASC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
foreach ($res as $value) {
@@ -143,14 +140,13 @@ SQL;
$sql = <<<SQL
SELECT DATE_FORMAT(FROM_UNIXTIME(e.date), '{$period}') AS period
, COUNT(1) AS count
-FROM `{$this->prefix}entry` AS e
+FROM `_entry` AS e
{$restrict}
GROUP BY period
ORDER BY period ASC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_NAMED);
$repartition = array();
@@ -207,11 +203,10 @@ SQL;
SELECT COUNT(1) AS count
, MIN(date) AS date_min
, MAX(date) AS date_max
-FROM `{$this->prefix}entry` AS e
+FROM `_entry` AS e
{$restrict}
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetch(PDO::FETCH_NAMED);
$date_min = new \DateTime();
$date_min->setTimestamp($res['date_min']);
@@ -251,14 +246,12 @@ SQL;
$sql = <<<SQL
SELECT c.name AS label
, COUNT(f.id) AS data
-FROM `{$this->prefix}category` AS c,
-`{$this->prefix}feed` AS f
+FROM `_category` AS c, `_feed` AS f
WHERE c.id = f.category
GROUP BY label
ORDER BY data DESC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
return $res;
@@ -274,16 +267,13 @@ SQL;
$sql = <<<SQL
SELECT c.name AS label
, COUNT(e.id) AS data
-FROM `{$this->prefix}category` AS c,
-`{$this->prefix}feed` AS f,
-`{$this->prefix}entry` AS e
+FROM `_category` AS c, `_feed` AS f, `_entry` AS e
WHERE c.id = f.category
AND f.id = e.id_feed
GROUP BY label
ORDER BY data DESC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
return $res;
@@ -300,17 +290,14 @@ SELECT f.id AS id
, MAX(f.name) AS name
, MAX(c.name) AS category
, COUNT(e.id) AS count
-FROM `{$this->prefix}category` AS c,
-`{$this->prefix}feed` AS f,
-`{$this->prefix}entry` AS e
+FROM `_category` AS c, `_feed` AS f, `_entry` AS e
WHERE c.id = f.category
AND f.id = e.id_feed
GROUP BY f.id
ORDER BY count DESC
LIMIT 10
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
return $stm->fetchAll(PDO::FETCH_ASSOC);
}
@@ -325,14 +312,12 @@ SELECT MAX(f.id) as id
, MAX(f.name) AS name
, MAX(date) AS last_date
, COUNT(*) AS nb_articles
-FROM `{$this->prefix}feed` AS f,
-`{$this->prefix}entry` AS e
+FROM `_feed` AS f, `_entry` AS e
WHERE f.id = e.id_feed
GROUP BY f.id
ORDER BY name
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
return $stm->fetchAll(PDO::FETCH_ASSOC);
}
diff --git a/app/Models/StatsDAOPGSQL.php b/app/Models/StatsDAOPGSQL.php
index 1effbb64b..4a66068cb 100644
--- a/app/Models/StatsDAOPGSQL.php
+++ b/app/Models/StatsDAOPGSQL.php
@@ -47,14 +47,13 @@ class FreshRSS_StatsDAOPGSQL extends FreshRSS_StatsDAO {
$sql = <<<SQL
SELECT extract( {$period} from to_timestamp(e.date)) AS period
, COUNT(1) AS count
-FROM "{$this->prefix}entry" AS e
+FROM `_entry` AS e
{$restrict}
GROUP BY period
ORDER BY period ASC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_NAMED);
foreach ($res as $value) {
diff --git a/app/Models/StatsDAOSQLite.php b/app/Models/StatsDAOSQLite.php
index 6cfc20463..f96f8f479 100644
--- a/app/Models/StatsDAOSQLite.php
+++ b/app/Models/StatsDAOSQLite.php
@@ -15,14 +15,13 @@ class FreshRSS_StatsDAOSQLite extends FreshRSS_StatsDAO {
$sql = <<<SQL
SELECT strftime('{$period}', e.date, 'unixepoch') AS period
, COUNT(1) AS count
-FROM `{$this->prefix}entry` AS e
+FROM `_entry` AS e
{$restrict}
GROUP BY period
ORDER BY period ASC
SQL;
- $stm = $this->bd->prepare($sql);
- $stm->execute();
+ $stm = $this->pdo->query($sql);
$res = $stm->fetchAll(PDO::FETCH_NAMED);
$repartition = array();
diff --git a/app/Models/Tag.php b/app/Models/Tag.php
index 3eb989cc1..0d50e356c 100644
--- a/app/Models/Tag.php
+++ b/app/Models/Tag.php
@@ -3,7 +3,7 @@
class FreshRSS_Tag extends Minz_Model {
private $id = 0;
private $name;
- private $attributes = array();
+ private $attributes = [];
private $nbEntries = -1;
private $nbUnread = -1;
diff --git a/app/Models/TagDAO.php b/app/Models/TagDAO.php
index 297d24c96..5882eee76 100644
--- a/app/Models/TagDAO.php
+++ b/app/Models/TagDAO.php
@@ -8,37 +8,24 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function createTagTable() {
$ok = false;
- $hadTransaction = $this->bd->inTransaction();
+ $hadTransaction = $this->pdo->inTransaction();
if ($hadTransaction) {
- $this->bd->commit();
+ $this->pdo->commit();
}
try {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
+ require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
Minz_Log::warning('SQL ALTER GUID case sensitivity...');
$databaseDAO = FreshRSS_Factory::createDatabaseDAO();
$databaseDAO->ensureCaseInsensitiveGuids();
Minz_Log::warning('SQL CREATE TABLE tag...');
- if (defined('SQL_CREATE_TABLE_TAGS')) {
- $sql = sprintf(SQL_CREATE_TABLE_TAGS, $this->prefix);
- $stm = $this->bd->prepare($sql);
- $ok = $stm && $stm->execute();
- } else {
- global $SQL_CREATE_TABLE_TAGS;
- $ok = !empty($SQL_CREATE_TABLE_TAGS);
- foreach ($SQL_CREATE_TABLE_TAGS as $instruction) {
- $sql = sprintf($instruction, $this->prefix);
- $stm = $this->bd->prepare($sql);
- $ok &= $stm && $stm->execute();
- }
- }
+ $ok = $this->pdo->exec($SQL_CREATE_TABLE_TAGS) !== false;
} catch (Exception $e) {
Minz_Log::error('FreshRSS_EntryDAO::createTagTable error: ' . $e->getMessage());
}
if ($hadTransaction) {
- $this->bd->beginTransaction();
+ $this->pdo->beginTransaction();
}
return $ok;
}
@@ -55,22 +42,25 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function addTag($valuesTmp) {
- $sql = 'INSERT INTO `' . $this->prefix . 'tag`(name, attributes) '
- . 'SELECT * FROM (SELECT TRIM(?), TRIM(?)) t2 ' //TRIM() to provide a type hint as text for PostgreSQL
- . 'WHERE NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'category` WHERE name = TRIM(?))'; //No category of the same name
- $stm = $this->bd->prepare($sql);
+ $sql = 'INSERT INTO `_tag`(name, attributes) '
+ . 'SELECT * FROM (SELECT TRIM(?) as name, TRIM(?) as attributes) t2 ' //TRIM() gives a text type hint to PostgreSQL
+ . 'WHERE NOT EXISTS (SELECT 1 FROM `_category` WHERE name = TRIM(?))'; //No category of the same name
+ $stm = $this->pdo->prepare($sql);
$valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
+ if (!isset($valuesTmp['attributes'])) {
+ $valuesTmp['attributes'] = [];
+ }
$values = array(
$valuesTmp['name'],
- isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
+ is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
$valuesTmp['name'],
);
if ($stm && $stm->execute($values)) {
- return $this->bd->lastInsertId('"' . $this->prefix . 'tag_id_seq"');
+ return $this->pdo->lastInsertId('`_tag_id_seq`');
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error addTag: ' . $info[2]);
return false;
}
@@ -89,14 +79,17 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function updateTag($id, $valuesTmp) {
- $sql = 'UPDATE `' . $this->prefix . 'tag` SET name=?, attributes=? WHERE id=? '
- . 'AND NOT EXISTS (SELECT 1 FROM `' . $this->prefix . 'category` WHERE name = ?)'; //No category of the same name
- $stm = $this->bd->prepare($sql);
+ $sql = 'UPDATE `_tag` SET name=?, attributes=? WHERE id=? '
+ . 'AND NOT EXISTS (SELECT 1 FROM `_category` WHERE name = ?)'; //No category of the same name
+ $stm = $this->pdo->prepare($sql);
$valuesTmp['name'] = mb_strcut(trim($valuesTmp['name']), 0, 63, 'UTF-8');
+ if (!isset($valuesTmp['attributes'])) {
+ $valuesTmp['attributes'] = [];
+ }
$values = array(
$valuesTmp['name'],
- isset($valuesTmp['attributes']) ? json_encode($valuesTmp['attributes']) : '',
+ is_string($valuesTmp['attributes']) ? $valuesTmp['attributes'] : json_encode($valuesTmp['attributes'], JSON_UNESCAPED_SLASHES),
$id,
$valuesTmp['name'],
);
@@ -104,7 +97,7 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error updateTag: ' . $info[2]);
return false;
}
@@ -125,23 +118,39 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
if ($id <= 0) {
return false;
}
- $sql = 'DELETE FROM `' . $this->prefix . 'tag` WHERE id=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'DELETE FROM `_tag` WHERE id=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
if ($stm && $stm->execute($values)) {
return $stm->rowCount();
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error deleteTag: ' . $info[2]);
return false;
}
}
+ public function selectAll() {
+ $sql = 'SELECT id, name, attributes FROM `_tag`';
+ $stm = $this->pdo->query($sql);
+ while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
+ yield $row;
+ }
+ }
+
+ public function selectEntryTag() {
+ $sql = 'SELECT id_tag, id_entry FROM `_entrytag`';
+ $stm = $this->pdo->query($sql);
+ while ($row = $stm->fetch(PDO::FETCH_ASSOC)) {
+ yield $row;
+ }
+ }
+
public function searchById($id) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'tag` WHERE id=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT * FROM `_tag` WHERE id=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -150,8 +159,8 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function searchByName($name) {
- $sql = 'SELECT * FROM `' . $this->prefix . 'tag` WHERE name=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT * FROM `_tag` WHERE name=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($name);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -162,20 +171,20 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function listTags($precounts = false) {
if ($precounts) {
$sql = 'SELECT t.id, t.name, count(e.id) AS unreads '
- . 'FROM `' . $this->prefix . 'tag` t '
- . 'LEFT OUTER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id '
- . 'LEFT OUTER JOIN `' . $this->prefix . 'entry` e ON et.id_entry = e.id AND e.is_read = 0 '
+ . 'FROM `_tag` t '
+ . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id '
+ . 'LEFT OUTER JOIN `_entry` e ON et.id_entry = e.id AND e.is_read = 0 '
. 'GROUP BY t.id '
. 'ORDER BY t.name';
} else {
- $sql = 'SELECT * FROM `' . $this->prefix . 'tag` ORDER BY name';
+ $sql = 'SELECT * FROM `_tag` ORDER BY name';
}
- $stm = $this->bd->prepare($sql);
- if ($stm && $stm->execute()) {
+ $stm = $this->pdo->query($sql);
+ if ($stm !== false) {
return self::daoToTag($stm->fetchAll(PDO::FETCH_ASSOC));
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->listTags($precounts);
}
@@ -185,13 +194,13 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function count() {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'tag`';
- $stm = $this->bd->prepare($sql);
- if ($stm && $stm->execute()) {
+ $sql = 'SELECT COUNT(*) AS count FROM `_tag`';
+ $stm = $this->pdo->query($sql);
+ if ($stm !== false) {
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
return $res[0]['count'];
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->count();
}
@@ -201,8 +210,8 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function countEntries($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entrytag` WHERE id_tag=?';
- $stm = $this->bd->prepare($sql);
+ $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` WHERE id_tag=?';
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -210,10 +219,10 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
public function countNotRead($id) {
- $sql = 'SELECT COUNT(*) AS count FROM `' . $this->prefix . 'entrytag` et '
- . 'INNER JOIN `' . $this->prefix . 'entry` e ON et.id_entry=e.id '
+ $sql = 'SELECT COUNT(*) AS count FROM `_entrytag` et '
+ . 'INNER JOIN `_entry` e ON et.id_entry=e.id '
. 'WHERE et.id_tag=? AND e.is_read=0';
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$values = array($id);
$stm->execute($values);
$res = $stm->fetchAll(PDO::FETCH_ASSOC);
@@ -222,17 +231,17 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function tagEntry($id_tag, $id_entry, $checked = true) {
if ($checked) {
- $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `' . $this->prefix . 'entrytag`(id_tag, id_entry) VALUES(?, ?)';
+ $sql = 'INSERT ' . $this->sqlIgnore() . ' INTO `_entrytag`(id_tag, id_entry) VALUES(?, ?)';
} else {
- $sql = 'DELETE FROM `' . $this->prefix . 'entrytag` WHERE id_tag=? AND id_entry=?';
+ $sql = 'DELETE FROM `_entrytag` WHERE id_tag=? AND id_entry=?';
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$values = array($id_tag, $id_entry);
if ($stm && $stm->execute($values)) {
return true;
} else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
Minz_Log::error('SQL error tagEntry: ' . $info[2]);
return false;
}
@@ -240,11 +249,11 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function getTagsForEntry($id_entry) {
$sql = 'SELECT t.id, t.name, et.id_entry IS NOT NULL as checked '
- . 'FROM `' . $this->prefix . 'tag` t '
- . 'LEFT OUTER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
+ . 'FROM `_tag` t '
+ . 'LEFT OUTER JOIN `_entrytag` et ON et.id_tag = t.id AND et.id_entry=? '
. 'ORDER BY t.name';
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
$values = array($id_entry);
if ($stm && $stm->execute($values)) {
@@ -255,7 +264,7 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
return $lines;
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->getTagsForEntry($id_entry);
}
@@ -266,8 +275,8 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
public function getTagsForEntries($entries) {
$sql = 'SELECT et.id_entry, et.id_tag, t.name '
- . 'FROM `' . $this->prefix . 'tag` t '
- . 'INNER JOIN `' . $this->prefix . 'entrytag` et ON et.id_tag = t.id';
+ . 'FROM `_tag` t '
+ . 'INNER JOIN `_entrytag` et ON et.id_tag = t.id';
$values = array();
if (is_array($entries) && count($entries) > 0) {
@@ -286,12 +295,12 @@ class FreshRSS_TagDAO extends Minz_ModelPdo implements FreshRSS_Searchable {
}
}
}
- $stm = $this->bd->prepare($sql);
+ $stm = $this->pdo->prepare($sql);
if ($stm && $stm->execute($values)) {
return $stm->fetchAll(PDO::FETCH_ASSOC);
} else {
- $info = $stm == null ? array(0 => '', 1 => '', 2 => 'syntax error') : $stm->errorInfo();
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
if ($this->autoUpdateDb($info)) {
return $this->getTagsForEntries($entries);
}
diff --git a/app/Models/TagDAOSQLite.php b/app/Models/TagDAOSQLite.php
index b1deb6c65..ca0fce7ca 100644
--- a/app/Models/TagDAOSQLite.php
+++ b/app/Models/TagDAOSQLite.php
@@ -7,7 +7,7 @@ class FreshRSS_TagDAOSQLite extends FreshRSS_TagDAO {
}
protected function autoUpdateDb($errorInfo) {
- if ($tableInfo = $this->bd->query("SELECT sql FROM sqlite_master where name='tag'")) {
+ if ($tableInfo = $this->pdo->query("SELECT sql FROM sqlite_master where name='tag'")) {
$showCreate = $tableInfo->fetchColumn();
if (stripos($showCreate, 'tag') === false) {
return $this->createTagTable(); //v1.12.0
diff --git a/app/Models/UserDAO.php b/app/Models/UserDAO.php
index e9d3a7329..4e824cf01 100644
--- a/app/Models/UserDAO.php
+++ b/app/Models/UserDAO.php
@@ -1,83 +1,52 @@
<?php
class FreshRSS_UserDAO extends Minz_ModelPdo {
- public function createUser($username, $new_user_language, $insertDefaultFeeds = true) {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
-
- $userPDO = new Minz_ModelPdo($username);
-
- $currentLanguage = Minz_Translate::language();
+ public function createUser($insertDefaultFeeds = false) {
+ require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
try {
- Minz_Translate::reset($new_user_language);
- $ok = false;
- $bd_prefix_user = $db['prefix'] . $username . '_';
- if (defined('SQL_CREATE_TABLES')) { //E.g. MySQL
- $sql = sprintf(SQL_CREATE_TABLES . SQL_CREATE_TABLE_ENTRYTMP . SQL_CREATE_TABLE_TAGS, $bd_prefix_user, _t('gen.short.default_category'));
- $stm = $userPDO->bd->prepare($sql);
- $ok = $stm && $stm->execute();
- } else { //E.g. SQLite
- global $SQL_CREATE_TABLES, $SQL_CREATE_TABLE_ENTRYTMP, $SQL_CREATE_TABLE_TAGS;
- if (is_array($SQL_CREATE_TABLES)) {
- $instructions = array_merge($SQL_CREATE_TABLES, $SQL_CREATE_TABLE_ENTRYTMP, $SQL_CREATE_TABLE_TAGS);
- $ok = !empty($instructions);
- foreach ($instructions as $instruction) {
- $sql = sprintf($instruction, $bd_prefix_user, _t('gen.short.default_category'));
- $stm = $userPDO->bd->prepare($sql);
- $ok &= ($stm && $stm->execute());
- }
- }
- }
+ $sql = $SQL_CREATE_TABLES . $SQL_CREATE_TABLE_ENTRYTMP . $SQL_CREATE_TABLE_TAGS;
+ $ok = $this->pdo->exec($sql) !== false; //Note: Only exec() can take multiple statements safely.
if ($ok && $insertDefaultFeeds) {
- if (defined('SQL_INSERT_FEEDS')) { //E.g. MySQL
- $sql = sprintf(SQL_INSERT_FEEDS, $bd_prefix_user);
- $stm = $userPDO->bd->prepare($sql);
- $ok &= $stm && $stm->execute();
- } else { //E.g. SQLite
- global $SQL_INSERT_FEEDS;
- if (is_array($SQL_INSERT_FEEDS)) {
- foreach ($SQL_INSERT_FEEDS as $instruction) {
- $sql = sprintf($instruction, $bd_prefix_user);
- $stm = $userPDO->bd->prepare($sql);
- $ok &= ($stm && $stm->execute());
- }
- }
+ $default_feeds = FreshRSS_Context::$system_conf->default_feeds;
+ $stm = $this->pdo->prepare($SQL_INSERT_FEED);
+ foreach ($default_feeds as $feed) {
+ $parameters = [
+ ':url' => $feed['url'],
+ ':name' => $feed['name'],
+ ':website' => $feed['website'],
+ ':description' => $feed['description'],
+ ];
+ $ok &= ($stm && $stm->execute($parameters));
}
}
} catch (Exception $e) {
- Minz_Log::error('Error while creating user: ' . $e->getMessage());
+ Minz_Log::error('Error while creating database for user: ' . $e->getMessage());
}
- Minz_Translate::reset($currentLanguage);
-
if ($ok) {
return true;
} else {
- $info = empty($stm) ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error: ' . $info[2]);
+ $info = empty($stm) ? $this->pdo->errorInfo() : $stm->errorInfo();
+ Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
return false;
}
}
- public function deleteUser($username) {
- $db = FreshRSS_Context::$system_conf->db;
- require_once(APP_PATH . '/SQL/install.sql.' . $db['type'] . '.php');
+ public function deleteUser() {
+ if (defined('STDERR')) {
+ fwrite(STDERR, 'Deleting SQL data for user “' . $this->current_user . "”…\n");
+ }
+
+ require(APP_PATH . '/SQL/install.sql.' . $this->pdo->dbType() . '.php');
+ $ok = $this->pdo->exec($SQL_DROP_TABLES) !== false;
- if ($db['type'] === 'sqlite') {
- return unlink(USERS_PATH . '/' . $username . '/db.sqlite');
+ if ($ok) {
+ return true;
} else {
- $userPDO = new Minz_ModelPdo($username);
-
- $sql = sprintf(SQL_DROP_TABLES, $db['prefix'] . $username . '_');
- $stm = $userPDO->bd->prepare($sql);
- if ($stm && $stm->execute()) {
- return true;
- } else {
- $info = $stm == null ? array(2 => 'syntax error') : $stm->errorInfo();
- Minz_Log::error('SQL error : ' . $info[2]);
- return false;
- }
+ $info = $stm == null ? $this->pdo->errorInfo() : $stm->errorInfo();
+ Minz_Log::error(__METHOD__ . ' error: ' . $info[2]);
+ return false;
}
}