host->set($this, "similarity", $similarity); $this->host->set($this, "min_title_length", $min_title_length); $this->host->set($this, "enable_globally", $enable_globally); echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally); } function init($host) { $this->host = $host; $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); $host->add_hook($host::HOOK_PREFS_TAB, $this); $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this); } function get_js() { return file_get_contents(__DIR__ . "/init.js"); } /** @return void */ function showrelated() { $id = (int) $_REQUEST['id']; $owner_uid = $_SESSION["uid"]; $sth = $this->pdo->prepare("SELECT title FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND id = ? AND owner_uid = ?"); $sth->execute([$id, $owner_uid]); if ($row = $sth->fetch()) { $title = $row['title']; print "

$title

"; if (Config::get(Config::DB_TYPE) == "pgsql") { $sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id, feed_id, ttrss_entries.title AS title, updated, link, ttrss_feeds.title AS feed_title, SIMILARITY(ttrss_entries.title, :title) AS sm FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id) WHERE ttrss_entries.id = ref_id AND ttrss_user_entries.owner_uid = :owner_uid AND ttrss_entries.id != :id AND date_entered >= NOW() - INTERVAL '2 weeks' ORDER BY sm DESC, date_entered DESC LIMIT 10"); } else { $sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id, feed_id, ttrss_entries.title AS title, updated, link, ttrss_feeds.title AS feed_title, (MATCH (ttrss_entries.title) AGAINST (:title) / LENGTH(ttrss_entries.title)) AS sm FROM ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id) WHERE ttrss_entries.id = ref_id AND ttrss_user_entries.owner_uid = :owner_uid AND ttrss_entries.id != :id AND date_entered >= DATE_SUB(NOW(), INTERVAL 2 WEEK) ORDER BY sm DESC, date_entered DESC LIMIT 10"); } $sth->execute(['title' => $title, "owner_uid" => $owner_uid, "id" => $id]); print ""; } print ""; } function hook_article_button($line) { return "bookmark_outline"; } function hook_prefs_tab($args) { if ($args != "prefFeeds") return; $similarity = $this->host->get($this, "similarity", $this->default_similarity); $min_title_length = $this->host->get($this, "min_title_length", $this->default_min_length); $enable_globally = sql_bool_to_bool($this->host->get($this, "enable_globally")); ?>
pdo->query("select 'similarity'::regproc"); if (!$res || !$res->fetch()) { print_error("pg_trgm extension not found."); } } ?>

filter_unknown_feeds( $this->host->get_array($this, "enabled_feeds")); $this->host->set($this, "enabled_feeds", $enabled_feeds); ?> 0) { ?>

host->get_array($this, "enabled_feeds"); ?>
host->get_array($this, "enabled_feeds"); $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"] ?? ""); $key = array_search($feed_id, $enabled_feeds); if ($enable) { if ($key === false) { array_push($enabled_feeds, $feed_id); } } else { if ($key !== false) { unset($enabled_feeds[$key]); } } $this->host->set($this, "enabled_feeds", $enabled_feeds); } function hook_article_filter($article) { if (Config::get(Config::DB_TYPE) == "pgsql") { $res = $this->pdo->query("select 'similarity'::regproc"); if (!$res || !$res->fetch()) return $article; } $enable_globally = $this->host->get($this, "enable_globally"); if (!$enable_globally && !in_array($article["feed"]["id"], $this->host->get_array($this,"enabled_feeds"))) { return $article; } $similarity = (float) $this->host->get($this, "similarity", $this->default_similarity); if ($similarity < 0.01) { Debug::log("af_psql_trgm: similarity is set too low ($similarity)", Debug::LOG_EXTENDED); return $article; } $min_title_length = (int) $this->host->get($this, "min_title_length", $this->default_min_length); if (mb_strlen($article["title"]) < $min_title_length) { Debug::log("af_psql_trgm: article title is too short (min: $min_title_length)", Debug::LOG_EXTENDED); return $article; } $owner_uid = $article["owner_uid"]; $entry_guid = $article["guid_hashed"]; $title_escaped = $article["title"]; // trgm does not return similarity=1 for completely equal strings // this seems to be no longer the case (fixed in upstream?) /* $sth = $this->pdo->prepare("SELECT COUNT(id) AS nequal FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND date_entered >= NOW() - interval '3 days' AND title = ? AND guid != ? AND owner_uid = ?"); $sth->execute([$title_escaped, $entry_guid, $owner_uid]); $row = $sth->fetch(); $nequal = $row['nequal']; Debug::log("af_psql_trgm: num equals: $nequal", Debug::$LOG_EXTENDED); if ($nequal != 0) { $article["force_catchup"] = true; return $article; } */ if (Config::get(Config::DB_TYPE) == "pgsql") { $sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, :title)) AS ms FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND date_entered >= NOW() - interval '1 day' AND guid != :guid AND owner_uid = :uid"); } else { $sth = $this->pdo->prepare("SELECT (MATCH(title) AGAINST (:title) / LENGTH(title)) AS ms FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND date_entered >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND guid != :guid AND owner_uid = :uid ORDER BY ms DESC LIMIT 1"); } $sth->execute(['title' => $title_escaped, 'guid' => $entry_guid, 'uid' => $owner_uid]); $row = $sth->fetch(); $similarity_result = $row['ms']; Debug::log("af_psql_trgm: similarity result for $title_escaped: $similarity_result", Debug::LOG_EXTENDED); if ($similarity_result >= $similarity) { Debug::log("af_psql_trgm: marking article as read ($similarity_result >= $similarity)", Debug::LOG_EXTENDED); $article["force_catchup"] = true; } return $article; } function api_version() { return 2; } /** * @param array $enabled_feeds * @return array * @throws PDOException */ private function filter_unknown_feeds($enabled_feeds) : array { $tmp = array(); foreach ($enabled_feeds as $feed) { $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?"); $sth->execute([$feed, $_SESSION['uid']]); if ($row = $sth->fetch()) { array_push($tmp, $feed); } } return $tmp; } }