OPML improvements/fixes:

* allow CLI import of OPML files (--opml-import)
 * visualize OPML structure when importing
 * add strict type hints to most OPML class methods
This commit is contained in:
Andrew Dolgov 2021-10-15 10:06:00 +03:00
parent 9e7e0e84d7
commit a936e80630
2 changed files with 103 additions and 75 deletions

View File

@ -48,9 +48,7 @@ class OPML extends Handler_Protected {
// Export // Export
private function opml_export_category($owner_uid, $cat_id, $hide_private_feeds = false, $include_settings = true) { private function opml_export_category(int $owner_uid, int $cat_id, bool $hide_private_feeds = false, bool $include_settings = true) {
$cat_id = (int) $cat_id;
if ($hide_private_feeds) if ($hide_private_feeds)
$hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')"; $hide_qpart = "(private IS false AND auth_login = '' AND auth_pass = '')";
@ -126,7 +124,7 @@ class OPML extends Handler_Protected {
return $out; return $out;
} }
function opml_export($filename, $owner_uid, $hide_private_feeds = false, $include_settings = true, $file_output = false) { function opml_export(string $filename, int $owner_uid, bool $hide_private_feeds = false, bool $include_settings = true, bool $file_output = false) {
if (!$owner_uid) return; if (!$owner_uid) return;
if (!$file_output) if (!$file_output)
@ -298,7 +296,7 @@ class OPML extends Handler_Protected {
// Import // Import
private function opml_import_feed($node, $cat_id, $owner_uid) { private function opml_import_feed(DOMNode $node, int $cat_id, int $owner_uid, int $nest) {
$attrs = $node->attributes; $attrs = $node->attributes;
$feed_title = mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250); $feed_title = mb_substr($attrs->getNamedItem('text')->nodeValue, 0, 250);
@ -318,7 +316,7 @@ class OPML extends Handler_Protected {
if (!$sth->fetch()) { if (!$sth->fetch()) {
#$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id"); #$this->opml_notice("[FEED] [$feed_title/$feed_url] dst_CAT=$cat_id");
$this->opml_notice(T_sprintf("Adding feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title)); $this->opml_notice(T_sprintf("Adding feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title), $nest);
if (!$cat_id) $cat_id = null; if (!$cat_id) $cat_id = null;
@ -338,12 +336,12 @@ class OPML extends Handler_Protected {
$sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url, $order_id, $update_interval, $purge_interval]); $sth->execute([$feed_title, $feed_url, $owner_uid, $cat_id, $site_url, $order_id, $update_interval, $purge_interval]);
} else { } else {
$this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title)); $this->opml_notice(T_sprintf("Duplicate feed: %s", $feed_title == '[Unknown]' ? $feed_url : $feed_title), $nest);
} }
} }
} }
private function opml_import_label($node, $owner_uid) { private function opml_import_label(DOMNode $node, int $owner_uid, int $nest) {
$attrs = $node->attributes; $attrs = $node->attributes;
$label_name = $attrs->getNamedItem('label-name')->nodeValue; $label_name = $attrs->getNamedItem('label-name')->nodeValue;
@ -351,16 +349,16 @@ class OPML extends Handler_Protected {
$fg_color = $attrs->getNamedItem('label-fg-color')->nodeValue; $fg_color = $attrs->getNamedItem('label-fg-color')->nodeValue;
$bg_color = $attrs->getNamedItem('label-bg-color')->nodeValue; $bg_color = $attrs->getNamedItem('label-bg-color')->nodeValue;
if (!Labels::find_id($label_name, $_SESSION['uid'])) { if (!Labels::find_id($label_name, $owner_uid)) {
$this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name))); $this->opml_notice(T_sprintf("Adding label %s", htmlspecialchars($label_name)), $nest);
Labels::create($label_name, $fg_color, $bg_color, $owner_uid); Labels::create($label_name, $fg_color, $bg_color, $owner_uid);
} else { } else {
$this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name))); $this->opml_notice(T_sprintf("Duplicate label: %s", htmlspecialchars($label_name)), $nest);
} }
} }
} }
private function opml_import_preference($node) { private function opml_import_preference(DOMNode $node, int $owner_uid, int $nest) {
$attrs = $node->attributes; $attrs = $node->attributes;
$pref_name = $attrs->getNamedItem('pref-name')->nodeValue; $pref_name = $attrs->getNamedItem('pref-name')->nodeValue;
@ -368,13 +366,13 @@ class OPML extends Handler_Protected {
$pref_value = $attrs->getNamedItem('value')->nodeValue; $pref_value = $attrs->getNamedItem('value')->nodeValue;
$this->opml_notice(T_sprintf("Setting preference key %s to %s", $this->opml_notice(T_sprintf("Setting preference key %s to %s",
$pref_name, $pref_value)); $pref_name, $pref_value), $nest);
set_pref($pref_name, $pref_value); set_pref($pref_name, $pref_value, $owner_uid);
} }
} }
private function opml_import_filter($node) { private function opml_import_filter(DOMNode $node, int $owner_uid, int $nest) {
$attrs = $node->attributes; $attrs = $node->attributes;
$filter_type = $attrs->getNamedItem('filter-type')->nodeValue; $filter_type = $attrs->getNamedItem('filter-type')->nodeValue;
@ -393,17 +391,17 @@ class OPML extends Handler_Protected {
$sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid) $sth = $this->pdo->prepare("INSERT INTO ttrss_filters2 (match_any_rule,enabled,inverse,title,owner_uid)
VALUES (?, ?, ?, ?, ?)"); VALUES (?, ?, ?, ?, ?)");
$sth->execute([$match_any_rule, $enabled, $inverse, $title, $_SESSION['uid']]); $sth->execute([$match_any_rule, $enabled, $inverse, $title, $owner_uid]);
$sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE $sth = $this->pdo->prepare("SELECT MAX(id) AS id FROM ttrss_filters2 WHERE
owner_uid = ?"); owner_uid = ?");
$sth->execute([$_SESSION['uid']]); $sth->execute([$owner_uid]);
$row = $sth->fetch(); $row = $sth->fetch();
$filter_id = $row['id']; $filter_id = $row['id'];
if ($filter_id) { if ($filter_id) {
$this->opml_notice(T_sprintf("Adding filter %s...", $title)); $this->opml_notice(T_sprintf("Adding filter %s...", $title), $nest);
//$this->opml_notice(json_encode($filter)); //$this->opml_notice(json_encode($filter));
foreach ($filter["rules"] as $rule) { foreach ($filter["rules"] as $rule) {
@ -421,7 +419,7 @@ class OPML extends Handler_Protected {
array_push($match_on, ($is_cat ? "CAT:" : "") . $name); array_push($match_on, ($is_cat ? "CAT:" : "") . $name);
} else { } else {
$match_id = Feeds::_find_by_title($name, $is_cat, $_SESSION['uid']); $match_id = Feeds::_find_by_title($name, $is_cat, $owner_uid);
if ($match_id) { if ($match_id) {
if ($is_cat) { if ($is_cat) {
@ -469,7 +467,7 @@ class OPML extends Handler_Protected {
} else { } else {
$match_id = Feeds::_find_by_title($rule['feed'], $rule['cat_filter'], $_SESSION['uid']); $match_id = Feeds::_find_by_title($rule['feed'] ?? "", $rule['cat_filter'], $owner_uid);
if ($match_id) { if ($match_id) {
if ($rule['cat_filter']) { if ($rule['cat_filter']) {
@ -528,8 +526,8 @@ class OPML extends Handler_Protected {
} }
} }
private function opml_import_category($doc, $root_node, $owner_uid, $parent_id) { private function opml_import_category(DOMDocument $doc, ?DOMNode $root_node, int $owner_uid, int $parent_id, int $nest) {
$default_cat_id = (int) $this->get_feed_category('Imported feeds', false); $default_cat_id = (int) $this->get_feed_category('Imported feeds', $owner_uid, 0);
if ($root_node) { if ($root_node) {
$cat_title = mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250); $cat_title = mb_substr($root_node->attributes->getNamedItem('text')->nodeValue, 0, 250);
@ -538,13 +536,13 @@ class OPML extends Handler_Protected {
$cat_title = mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250); $cat_title = mb_substr($root_node->attributes->getNamedItem('title')->nodeValue, 0, 250);
if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) { if (!in_array($cat_title, array("tt-rss-filters", "tt-rss-labels", "tt-rss-prefs"))) {
$cat_id = $this->get_feed_category($cat_title, $parent_id); $cat_id = $this->get_feed_category($cat_title, $owner_uid, $parent_id);
if ($cat_id === false) { if ($cat_id === 0) {
$order_id = (int) $root_node->attributes->getNamedItem('ttrssSortOrder')->nodeValue; $order_id = (int) $root_node->attributes->getNamedItem('ttrssSortOrder')->nodeValue;
Feeds::_add_cat($cat_title, $_SESSION['uid'], $parent_id ? $parent_id : null, (int)$order_id); Feeds::_add_cat($cat_title, $owner_uid, $parent_id ? $parent_id : null, (int)$order_id);
$cat_id = $this->get_feed_category($cat_title, $parent_id); $cat_id = $this->get_feed_category($cat_title, $owner_uid, $parent_id);
} }
} else { } else {
@ -561,8 +559,8 @@ class OPML extends Handler_Protected {
$cat_title = false; $cat_title = false;
} }
#$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id"); //$this->opml_notice("[CAT] $cat_title id: $cat_id P_id: $parent_id");
$this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized"))); $this->opml_notice(T_sprintf("Processing category: %s", $cat_title ? $cat_title : __("Uncategorized")), $nest);
foreach ($outlines as $node) { foreach ($outlines as $node) {
if ($node->hasAttributes() && strtolower($node->tagName) == "outline") { if ($node->hasAttributes() && strtolower($node->tagName) == "outline") {
@ -575,7 +573,7 @@ class OPML extends Handler_Protected {
$node_feed_url = $attrs->getNamedItem('xmlUrl') ? $attrs->getNamedItem('xmlUrl')->nodeValue : false; $node_feed_url = $attrs->getNamedItem('xmlUrl') ? $attrs->getNamedItem('xmlUrl')->nodeValue : false;
if ($node_cat_title && !$node_feed_url) { if ($node_cat_title && !$node_feed_url) {
$this->opml_import_category($doc, $node, $owner_uid, $cat_id); $this->opml_import_category($doc, $node, $owner_uid, $cat_id, $nest+1);
} else { } else {
if (!$cat_id) { if (!$cat_id) {
@ -586,31 +584,33 @@ class OPML extends Handler_Protected {
switch ($cat_title) { switch ($cat_title) {
case "tt-rss-prefs": case "tt-rss-prefs":
$this->opml_import_preference($node); $this->opml_import_preference($node, $owner_uid, $nest+1);
break; break;
case "tt-rss-labels": case "tt-rss-labels":
$this->opml_import_label($node, $owner_uid); $this->opml_import_label($node, $owner_uid, $nest+1);
break; break;
case "tt-rss-filters": case "tt-rss-filters":
$this->opml_import_filter($node); $this->opml_import_filter($node, $owner_uid, $nest+1);
break; break;
default: default:
$this->opml_import_feed($node, $dst_cat_id, $owner_uid); $this->opml_import_feed($node, $dst_cat_id, $owner_uid, $nest+1);
} }
} }
} }
} }
} }
function opml_import($owner_uid) { /** $filename is optional; assumes HTTP upload with $_FILES otherwise */
function opml_import(int $owner_uid, string $filename = "") {
if (!$owner_uid) return; if (!$owner_uid) return;
$doc = false; $doc = false;
if (!$filename) {
if ($_FILES['opml_file']['error'] != 0) { if ($_FILES['opml_file']['error'] != 0) {
print_error(T_sprintf("Upload failed with error code %d", print_error(T_sprintf("Upload failed with error code %d",
$_FILES['opml_file']['error'])); $_FILES['opml_file']['error']));
return; return false;
} }
if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) { if (is_uploaded_file($_FILES['opml_file']['tmp_name'])) {
@ -621,16 +621,23 @@ class OPML extends Handler_Protected {
if (!$result) { if (!$result) {
print_error(__("Unable to move uploaded file.")); print_error(__("Unable to move uploaded file."));
return; return false;
} }
} else { } else {
print_error(__('Error: please upload OPML file.')); print_error(__('Error: please upload OPML file.'));
return; return false;
}
} else {
$tmp_file = $filename;
}
if (!is_readable($tmp_file)) {
$this->opml_notice(T_sprintf("Error: file is not readable: %s", $filename));
return false;
} }
$loaded = false; $loaded = false;
if (is_file($tmp_file)) {
$doc = new DOMDocument(); $doc = new DOMDocument();
if (version_compare(PHP_VERSION, '8.0.0', '<')) { if (version_compare(PHP_VERSION, '8.0.0', '<')) {
@ -642,43 +649,47 @@ class OPML extends Handler_Protected {
if (version_compare(PHP_VERSION, '8.0.0', '<')) { if (version_compare(PHP_VERSION, '8.0.0', '<')) {
libxml_disable_entity_loader(true); libxml_disable_entity_loader(true);
} }
// only remove temporary i.e. HTTP uploaded files
if (!$filename)
unlink($tmp_file); unlink($tmp_file);
} else if (empty($doc)) {
print_error(__('Error: unable to find moved OPML file.'));
return;
}
if ($loaded) { if ($loaded) {
// we're using ORM while importing so we can't transaction-lock things anymore // we're using ORM while importing so we can't transaction-lock things anymore
//$this->pdo->beginTransaction(); //$this->pdo->beginTransaction();
$this->opml_import_category($doc, false, $owner_uid, false); $this->opml_import_category($doc, null, $owner_uid, 0, 0);
//$this->pdo->commit(); //$this->pdo->commit();
} else { } else {
print_error(__('Error while parsing document.')); $this->opml_notice(__('Error while parsing document.'));
return false;
}
return true;
}
private function opml_notice(string $msg, int $prefix_length = 0) {
if (php_sapi_name() == "cli") {
Debug::log(str_repeat(" ", $prefix_length) . $msg);
} else {
// TODO: use better separator i.e. CSS-defined span of certain width or something
print str_repeat("&nbsp;&nbsp;&nbsp;", $prefix_length) . $msg . "<br/>";
} }
} }
private function opml_notice($msg) { function get_feed_category(string $feed_cat, int $owner_uid, int $parent_cat_id) : int {
print "$msg<br/>";
}
function get_feed_category($feed_cat, $parent_cat_id = false) {
$parent_cat_id = (int) $parent_cat_id;
$sth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories $sth = $this->pdo->prepare("SELECT id FROM ttrss_feed_categories
WHERE title = :title WHERE title = :title
AND (parent_cat = :parent OR (:parent = 0 AND parent_cat IS NULL)) AND (parent_cat = :parent OR (:parent = 0 AND parent_cat IS NULL))
AND owner_uid = :uid"); AND owner_uid = :uid");
$sth->execute([':title' => $feed_cat, ':parent' => $parent_cat_id, ':uid' => $_SESSION['uid']]); $sth->execute([':title' => $feed_cat, ':parent' => $parent_cat_id, ':uid' => $owner_uid]);
if ($row = $sth->fetch()) { if ($row = $sth->fetch()) {
return $row['id']; return $row['id'];
} else { } else {
return false; return 0;
} }
} }
} }

View File

@ -89,6 +89,7 @@
"force-refetch" => "debug update: force refetch feed data", "force-refetch" => "debug update: force refetch feed data",
"force-rehash" => "debug update: force rehash articles", "force-rehash" => "debug update: force rehash articles",
"opml-export:" => ["USER:FILE", "export OPML of USER to FILE"], "opml-export:" => ["USER:FILE", "export OPML of USER to FILE"],
"opml-import:" => ["USER:FILE", "import OPML for USER from FILE"],
"user-list" => "list all users", "user-list" => "list all users",
# "user-add:" => ["USER[:PASSWORD]", "add USER, optionally without prompting for PASSWORD"], # "user-add:" => ["USER[:PASSWORD]", "add USER, optionally without prompting for PASSWORD"],
# "user-remove:" => ["USERNAME", "remove specified user"], # "user-remove:" => ["USERNAME", "remove specified user"],
@ -380,6 +381,22 @@
$rc = $opml->opml_export($filename, $owner_uid, false, true, true); $rc = $opml->opml_export($filename, $owner_uid, false, true, true);
Debug::log($rc ? "Success." : "Failed.");
} else {
Debug::log("User not found: $user");
}
}
if (isset($options["opml-import"])) {
list ($user, $filename) = explode(":", $options["opml-import"], 2);
Debug::log("Importing feeds of user $user from OPML file $filename...");
if ($owner_uid = UserHelper::find_user_by_login($user)) {
$opml = new OPML("");
$rc = $opml->opml_import($owner_uid, $filename);
Debug::log($rc ? "Success." : "Failed."); Debug::log($rc ? "Success." : "Failed.");
} else { } else {
Debug::log("User not found: $user"); Debug::log("User not found: $user");