use orm for a bunch of short feed/cat queries

This commit is contained in:
Andrew Dolgov 2021-03-01 20:25:53 +03:00
parent 8b1a2406e6
commit 56f658711f
3 changed files with 74 additions and 107 deletions

View File

@ -1843,18 +1843,13 @@ class Feeds extends Handler_Protected {
}
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()) {
return $row["cat_id"];
if ($feed) {
return $feed->cat_id;
} else {
return false;
return null;
}
}
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;
}
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) {
if (!$feed_cat) return false;
@ -1942,42 +1946,31 @@ class Feeds extends Handler_Protected {
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"];
$is_cat = bool_to_sql_bool($is_cat);
$key = ORM::for_table('ttrss_access_keys')
->where('owner_uid', $owner_uid)
->where('feed_id', $feed_id)
->where('is_cat', $is_cat)
->find_one();
$pdo = Db::pdo();
$sth = $pdo->prepare("SELECT access_key FROM ttrss_access_keys
WHERE feed_id = ? AND is_cat = ?
AND owner_uid = ?");
$sth->execute([$feed_id, $is_cat, $owner_uid]);
if ($row = $sth->fetch()) {
return $row["access_key"];
if ($key) {
return $key->access_key;
} 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 (?, ?, ?, ?)");
$key->owner_uid = $owner_uid;
$key->feed_id = $feed_id;
$key->is_cat = $is_cat;
$key->access_key = uniqid_short();
$sth->execute([$key, $feed_id, $is_cat, $owner_uid]);
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) {
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) {
$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
WHERE id = ?");
$sth->execute([$feed_id]);
if ($feed->purge_interval != 0)
return $feed->purge_interval;
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 {
return -1;
}

View File

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

View File

@ -1713,9 +1713,9 @@ class RSSUtils {
$filters = array();
$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";
else
$null_cat_qpart = "";