From 7cf12233d7e24b0d745486e378a3351ef7cc67e4 Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Tue, 2 Mar 2021 10:11:42 +0300 Subject: [PATCH] use ORM when subscribing feeds --- classes/feeds.php | 55 +++++++++++++++++------------------ plugins/bookmarklets/init.php | 2 +- 2 files changed, 28 insertions(+), 29 deletions(-) diff --git a/classes/feeds.php b/classes/feeds.php index cc641b4f2..6f8ceeced 100755 --- a/classes/feeds.php +++ b/classes/feeds.php @@ -986,17 +986,18 @@ class Feeds extends Handler_Protected { * to get all possible feeds. * 5 - Couldn't download the URL content. * 6 - Content is an invalid XML. + * 7 - Error while creating feed database entry. */ static function _subscribe($url, $cat_id = 0, - $auth_login = '', $auth_pass = '') { + $auth_login = '', $auth_pass = '') : array { $pdo = Db::pdo(); $url = UrlHelper::validate($url); - if (!$url) return array("code" => 2); + if (!$url) return ["code" => 2]; - $contents = @UrlHelper::fetch($url, false, $auth_login, $auth_pass); + $contents = UrlHelper::fetch($url, false, $auth_login, $auth_pass); PluginHost::getInstance()->chain_hooks_callback(PluginHost::HOOK_SUBSCRIBE_FEED, function ($result) use (&$contents) { @@ -1024,35 +1025,33 @@ class Feeds extends Handler_Protected { $url = key($feedUrls); } - if (!$cat_id) $cat_id = null; - - $sth = $pdo->prepare("SELECT id FROM ttrss_feeds - WHERE feed_url = ? AND owner_uid = ?"); - $sth->execute([$url, $_SESSION['uid']]); + $feed = ORM::for_table('ttrss_feeds') + ->where('feed_url', $url) + ->where('owner_uid', $_SESSION['uid']) + ->find_one(); - if ($row = $sth->fetch()) { - return array("code" => 0, "feed_id" => (int) $row["id"]); + if ($feed) { + return ["code" => 0, "feed_id" => $feed->id]; } else { - $sth = $pdo->prepare( - "INSERT INTO ttrss_feeds - (owner_uid,feed_url,title,cat_id, auth_login,auth_pass,update_method,auth_pass_encrypted) - VALUES (?, ?, ?, ?, ?, ?, 0, false)"); - - $sth->execute([$_SESSION['uid'], $url, "[Unknown]", $cat_id, (string)$auth_login, (string)$auth_pass]); - - $sth = $pdo->prepare("SELECT id FROM ttrss_feeds WHERE feed_url = ? - AND owner_uid = ?"); - $sth->execute([$url, $_SESSION['uid']]); - $row = $sth->fetch(); - - $feed_id = $row["id"]; - - if ($feed_id) { - RSSUtils::set_basic_feed_info($feed_id); + $feed = ORM::for_table('ttrss_feeds')->create(); + + $feed->set([ + 'owner_uid' => $_SESSION['uid'], + 'feed_url' => $url, + 'title' => "[Unknown]", + 'cat_id' => $cat_id ? $cat_id : null, + 'auth_login' => (string)$auth_login, + 'auth_pass' => (string)$auth_pass, + 'update_method' => 0, + 'auth_pass_encrypted' => false, + ]); + + if ($feed->save()) { + RSSUtils::set_basic_feed_info($feed->id); + return ["code" => 1, "feed_id" => (int) $feed->id]; } - return array("code" => 1, "feed_id" => (int) $feed_id); - + return ["code" => 7]; } } diff --git a/plugins/bookmarklets/init.php b/plugins/bookmarklets/init.php index eabb33510..ab88d48e7 100644 --- a/plugins/bookmarklets/init.php +++ b/plugins/bookmarklets/init.php @@ -154,7 +154,7 @@ class Bookmarklets extends Plugin {