use orm for a bunch of short feed/cat queries

master
Andrew Dolgov 4 years ago
parent 8b1a2406e6
commit 56f658711f

@ -1843,18 +1843,13 @@ class Feeds extends Handler_Protected {
} }
static function _cat_of_feed($feed) { static function _cat_of_feed($feed) {
$pdo = Db::pdo(); $feed = ORM::for_table('ttrss_feeds')->find_one($feed);
$sth = $pdo->prepare("SELECT cat_id FROM ttrss_feeds
WHERE id = ?");
$sth->execute([$feed]);
if ($row = $sth->fetch()) { if ($feed) {
return $row["cat_id"]; return $feed->cat_id;
} else { } else {
return false; return null;
} }
} }
private function _color_of($name) { private function _color_of($name) {
@ -1905,6 +1900,15 @@ class Feeds extends Handler_Protected {
return preg_match("/<html|DOCTYPE html/i", substr($content, 0, 8192)) !== 0; return preg_match("/<html|DOCTYPE html/i", substr($content, 0, 8192)) !== 0;
} }
static function _remove_cat(int $id, int $owner_uid) {
$cat = ORM::for_table('ttrss_feed_categories')
->where('owner_uid', $owner_uid)
->find_one($id);
if ($cat)
$cat->delete();
}
static function _add_cat($feed_cat, $parent_cat_id = false, $order_id = 0) { static function _add_cat($feed_cat, $parent_cat_id = false, $order_id = 0) {
if (!$feed_cat) return false; if (!$feed_cat) return false;
@ -1942,42 +1946,31 @@ class Feeds extends Handler_Protected {
return false; return false;
} }
static function _get_access_key($feed_id, $is_cat, $owner_uid = false) { static function _get_access_key($feed_id, bool $is_cat, int $owner_uid = 0) {
if (!$owner_uid) $owner_uid = $_SESSION["uid"]; if (!$owner_uid) $owner_uid = $_SESSION["uid"];
$is_cat = bool_to_sql_bool($is_cat); $key = ORM::for_table('ttrss_access_keys')
->where('owner_uid', $owner_uid)
$pdo = Db::pdo(); ->where('feed_id', $feed_id)
->where('is_cat', $is_cat)
$sth = $pdo->prepare("SELECT access_key FROM ttrss_access_keys ->find_one();
WHERE feed_id = ? AND is_cat = ?
AND owner_uid = ?");
$sth->execute([$feed_id, $is_cat, $owner_uid]);
if ($row = $sth->fetch()) { if ($key) {
return $row["access_key"]; return $key->access_key;
} else { } else {
$key = uniqid_short(); $key = ORM::for_table('ttrss_access_keys')->create();
$sth = $pdo->prepare("INSERT INTO ttrss_access_keys
(access_key, feed_id, is_cat, owner_uid)
VALUES (?, ?, ?, ?)");
$sth->execute([$key, $feed_id, $is_cat, $owner_uid]); $key->owner_uid = $owner_uid;
$key->feed_id = $feed_id;
$key->is_cat = $is_cat;
$key->access_key = uniqid_short();
return $key; if ($key->save()) {
return $key->access_key;
}
} }
} }
/**
* Purge a feed old posts.
*
* @param mixed $feed_id The id of the purged feed.
* @param mixed $purge_interval Olderness of purged posts.
* @access public
* @return mixed
*/
static function _purge($feed_id, $purge_interval) { static function _purge($feed_id, $purge_interval) {
if (!$purge_interval) $purge_interval = self::_get_purge_interval($feed_id); if (!$purge_interval) $purge_interval = self::_get_purge_interval($feed_id);
@ -2049,21 +2042,15 @@ class Feeds extends Handler_Protected {
} }
private static function _get_purge_interval($feed_id) { private static function _get_purge_interval($feed_id) {
$feed = ORM::for_table('ttrss_feeds')->find_one($feed_id);
$pdo = Db::pdo(); if ($feed) {
$sth = $pdo->prepare("SELECT purge_interval, owner_uid FROM ttrss_feeds if ($feed->purge_interval != 0)
WHERE id = ?"); return $feed->purge_interval;
$sth->execute([$feed_id]); else
return get_pref(Prefs::PURGE_OLD_DAYS, $feed->owner_uid);
if ($row = $sth->fetch()) {
$purge_interval = $row["purge_interval"];
$owner_uid = $row["owner_uid"];
if ($purge_interval == 0)
$purge_interval = get_pref(Prefs::PURGE_OLD_DAYS, $owner_uid);
return $purge_interval;
} else { } else {
return -1; return -1;
} }

@ -22,14 +22,16 @@ class Pref_Feeds extends Handler_Protected {
return $rv; return $rv;
} }
function renamecat() { function renameCat() {
$cat = ORM::for_table("ttrss_feed_categories")
->where("owner_uid", $_SESSION["uid"])
->find_one($_REQUEST['id']);
$title = clean($_REQUEST['title']); $title = clean($_REQUEST['title']);
$id = clean($_REQUEST['id']);
if ($title) { if ($cat && $title) {
$sth = $this->pdo->prepare("UPDATE ttrss_feed_categories SET $cat->title = $title;
title = ? WHERE id = ? AND owner_uid = ?"); $cat->save();
$sth->execute([$title, $id, $_SESSION['uid']]);
} }
} }
@ -513,11 +515,11 @@ class Pref_Feeds extends Handler_Protected {
$feed_id = (int)clean($_REQUEST["id"]); $feed_id = (int)clean($_REQUEST["id"]);
$sth = $this->pdo->prepare("SELECT * FROM ttrss_feeds WHERE id = ? AND $row = ORM::for_table('ttrss_feeds')
owner_uid = ?"); ->where("owner_uid", $_SESSION["uid"])
$sth->execute([$feed_id, $_SESSION['uid']]); ->find_one($feed_id)->as_array();
if ($row = $sth->fetch(PDO::FETCH_ASSOC)) { if ($row) {
ob_start(); ob_start();
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_EDIT_FEED, $feed_id); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_EDIT_FEED, $feed_id);
@ -720,48 +722,32 @@ class Pref_Feeds extends Handler_Protected {
$reset_basic_info = $orig_feed_url != $feed_url; */ $reset_basic_info = $orig_feed_url != $feed_url; */
$sth = $this->pdo->prepare("UPDATE ttrss_feeds SET $feed = ORM::for_table('ttrss_feeds')
cat_id = :cat_id, ->where('owner_uid', $_SESSION['uid'])
title = :title, ->find_one($feed_id);
feed_url = :feed_url,
site_url = :site_url, if ($feed) {
update_interval = :upd_intl,
purge_interval = :purge_intl, $feed->title = $feed_title;
auth_login = :auth_login, $feed->cat_id = $cat_id ? $cat_id : null;
auth_pass = :auth_pass, $feed->feed_url = $feed_url;
auth_pass_encrypted = false, $feed->site_url = $site_url;
private = :private, $feed->update_interval = $upd_intl;
cache_images = :cache_images, $feed->purge_interval = $purge_intl;
hide_images = :hide_images, $feed->auth_login = $auth_login;
include_in_digest = :include_in_digest, $feed->auth_pass = $auth_pass;
always_display_enclosures = :always_display_enclosures, $feed->private = (int)$private;
mark_unread_on_update = :mark_unread_on_update, $feed->cache_images = (int)$cache_images;
feed_language = :feed_language $feed->hide_images = (int)$hide_images;
WHERE id = :id AND owner_uid = :uid"); $feed->feed_language = $feed_language;
$feed->include_in_digest = (int)$include_in_digest;
$sth->execute([":title" => $feed_title, $feed->always_display_enclosures = (int)$always_display_enclosures;
":cat_id" => $cat_id ? $cat_id : null, $feed->mark_unread_on_update = (int)$mark_unread_on_update;
":feed_url" => $feed_url,
":site_url" => $site_url, $feed->save();
":upd_intl" => $upd_intl,
":purge_intl" => $purge_intl,
":auth_login" => $auth_login,
":auth_pass" => $auth_pass,
":private" => (int)$private,
":cache_images" => (int)$cache_images,
":hide_images" => (int)$hide_images,
":include_in_digest" => (int)$include_in_digest,
":always_display_enclosures" => (int)$always_display_enclosures,
":mark_unread_on_update" => (int)$mark_unread_on_update,
":feed_language" => $feed_language,
":id" => $feed_id,
":uid" => $_SESSION['uid']]);
/* if ($reset_basic_info) {
RSSUtils::set_basic_feed_info($feed_id);
} */
PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_SAVE_FEED, $feed_id); PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_SAVE_FEED, $feed_id);
}
} else { } else {
$feed_data = array(); $feed_data = array();
@ -874,7 +860,7 @@ class Pref_Feeds extends Handler_Protected {
function removeCat() { function removeCat() {
$ids = explode(",", clean($_REQUEST["ids"])); $ids = explode(",", clean($_REQUEST["ids"]));
foreach ($ids as $id) { foreach ($ids as $id) {
$this->remove_feed_category($id, $_SESSION["uid"]); Feeds::_remove_cat((int)$id, $_SESSION["uid"]);
} }
} }
@ -1188,12 +1174,6 @@ class Pref_Feeds extends Handler_Protected {
print json_encode($rv); print json_encode($rv);
} }
private function remove_feed_category($id, $owner_uid) {
$sth = $this->pdo->prepare("DELETE FROM ttrss_feed_categories
WHERE id = ? AND owner_uid = ?");
$sth->execute([$id, $owner_uid]);
}
static function remove_feed($id, $owner_uid) { static function remove_feed($id, $owner_uid) {
if (PluginHost::getInstance()->run_hooks_until(PluginHost::HOOK_UNSUBSCRIBE_FEED, true, $id, $owner_uid)) if (PluginHost::getInstance()->run_hooks_until(PluginHost::HOOK_UNSUBSCRIBE_FEED, true, $id, $owner_uid))

@ -1713,9 +1713,9 @@ class RSSUtils {
$filters = array(); $filters = array();
$feed_id = (int) $feed_id; $feed_id = (int) $feed_id;
$cat_id = (int)Feeds::_cat_of_feed($feed_id); $cat_id = Feeds::_cat_of_feed($feed_id);
if ($cat_id == 0) if (empty($cat_id))
$null_cat_qpart = "cat_id IS NULL OR"; $null_cat_qpart = "cat_id IS NULL OR";
else else
$null_cat_qpart = ""; $null_cat_qpart = "";

Loading…
Cancel
Save