From 32db1cb8722d353d2529305b0b3194576f7548fd Mon Sep 17 00:00:00 2001 From: Andrew Dolgov Date: Mon, 25 Feb 2019 14:06:26 +0300 Subject: [PATCH] OPML: include (and import) ttrss per-feed update interval and sort order --- classes/opml.php | 58 ++++++++++++++++++++++++++++++++----------- include/functions.php | 8 +++--- 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/classes/opml.php b/classes/opml.php index fe43a096c..2f6c03ea6 100644 --- a/classes/opml.php +++ b/classes/opml.php @@ -9,10 +9,10 @@ class Opml extends Handler_Protected { function export() { $output_name = "tt-rss_".date("Y-m-d").".opml"; - $show_settings = $_REQUEST["include_settings"]; + $include_settings = $_REQUEST["include_settings"] == "1"; $owner_uid = $_SESSION["uid"]; - $rc = $this->opml_export($output_name, $owner_uid, false, ($show_settings == 1)); + $rc = $this->opml_export($output_name, $owner_uid, false, $include_settings); return $rc; } @@ -48,7 +48,7 @@ class Opml extends Handler_Protected { // Export - private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds=false) { + private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds = false, $include_settings = true) { $cat_id = (int) $cat_id; @@ -59,15 +59,25 @@ class Opml extends Handler_Protected { $out = ""; + $ttrss_specific_qpart = ""; + if ($cat_id) { - $sth = $this->pdo->prepare("SELECT title FROM ttrss_feed_categories WHERE id = ? - AND owner_uid = ?"); + $sth = $this->pdo->prepare("SELECT title,order_id + FROM ttrss_feed_categories WHERE id = ? + AND owner_uid = ?"); $sth->execute([$cat_id, $owner_uid]); $row = $sth->fetch(); $cat_title = htmlspecialchars($row['title']); + + if ($include_settings) { + $order_id = (int)$row["order_id"]; + $ttrss_specific_qpart = "ttrssSortOrder=\"$order_id\""; + } + } else { + $cat_title = ""; } - if ($cat_title) $out .= "\n"; + if ($cat_title) $out .= "\n"; $sth = $this->pdo->prepare("SELECT id,title FROM ttrss_feed_categories WHERE @@ -77,10 +87,10 @@ class Opml extends Handler_Protected { $sth->execute([':cat' => $cat_id, ':uid' => $owner_uid]); while ($line = $sth->fetch()) { - $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds); + $out .= $this->opml_export_category($owner_uid, $line["id"], $hide_private_feeds, $include_settings); } - $fsth = $this->pdo->prepare("select title, feed_url, site_url + $fsth = $this->pdo->prepare("select title, feed_url, site_url, update_interval, order_id FROM ttrss_feeds WHERE (cat_id = :cat OR (:cat = 0 AND cat_id IS NULL)) AND owner_uid = :uid AND $hide_qpart ORDER BY order_id, title"); @@ -92,13 +102,22 @@ class Opml extends Handler_Protected { $url = htmlspecialchars($fline["feed_url"]); $site_url = htmlspecialchars($fline["site_url"]); + if ($include_settings) { + $update_interval = (int)$fline["update_interval"]; + $order_id = (int)$fline["order_id"]; + + $ttrss_specific_qpart = "ttrssSortOrder=\"$order_id\" ttrssUpdateInterval=\"$update_interval\""; + } else { + $ttrss_specific_qpart = ""; + } + if ($site_url) { $html_url_qpart = "htmlUrl=\"$site_url\""; } else { $html_url_qpart = ""; } - $out .= "\n"; + $out .= "\n"; } if ($cat_title) $out .= "\n"; @@ -106,7 +125,7 @@ class Opml extends Handler_Protected { return $out; } - function opml_export($name, $owner_uid, $hide_private_feeds=false, $include_settings=true) { + function opml_export($name, $owner_uid, $hide_private_feeds = false, $include_settings = true) { if (!$owner_uid) return; if (!isset($_REQUEST["debug"])) { @@ -125,7 +144,7 @@ class Opml extends Handler_Protected { "; $out .= ""; - $out .= $this->opml_export_category($owner_uid, 0, $hide_private_feeds); + $out .= $this->opml_export_category($owner_uid, 0, $hide_private_feeds, $include_settings); # export tt-rss settings @@ -298,11 +317,17 @@ class Opml extends Handler_Protected { if (!$cat_id) $cat_id = null; + $update_interval = (int) $attrs->getNamedItem('ttrssUpdateInterval')->nodeValue; + if (!$update_interval) $update_interval = 0; + + $order_id = (int) $attrs->getNamedItem('ttrssSortOrder')->nodeValue; + if (!$order_id) $order_id = 0; + $sth = $this->pdo->prepare("INSERT INTO ttrss_feeds - (title, feed_url, owner_uid, cat_id, site_url, order_id) VALUES - (?, ?, ?, ?, ?, 0)"); + (title, feed_url, owner_uid, cat_id, site_url, order_id, update_interval) VALUES + (?, ?, ?, ?, ?, ?, ?)"); - $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url]); + $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url, $order_id, $update_interval]); } else { $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title)); @@ -487,7 +512,10 @@ class Opml extends Handler_Protected { $cat_id = $this->get_feed_category($cat_title, $parent_id); if ($cat_id === false) { - add_feed_category($cat_title, $parent_id); + $order_id = (int) $root_node->attributes->getNamedItem('ttrssSortOrder')->nodeValue; + if (!$order_id) $order_id = 0; + + add_feed_category($cat_title, $parent_id, $order_id); $cat_id = $this->get_feed_category($cat_title, $parent_id); } diff --git a/include/functions.php b/include/functions.php index a1aeb7135..6a612115e 100755 --- a/include/functions.php +++ b/include/functions.php @@ -1981,7 +1981,7 @@ return true; } - function add_feed_category($feed_cat, $parent_cat_id = false) { + function add_feed_category($feed_cat, $parent_cat_id = false, $order_id = 0) { if (!$feed_cat) return false; @@ -2004,9 +2004,9 @@ if (!$sth->fetch()) { - $sth = $pdo->prepare("INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat) - VALUES (?, ?, ?)"); - $sth->execute([$_SESSION['uid'], $feed_cat, $parent_cat_id]); + $sth = $pdo->prepare("INSERT INTO ttrss_feed_categories (owner_uid,title,parent_cat,order_id) + VALUES (?, ?, ?, ?)"); + $sth->execute([$_SESSION['uid'], $feed_cat, $parent_cat_id, (int)$order_id]); if (!$tr_in_progress) $pdo->commit();