X-Git-Url: https://git.wh0rd.org/?a=blobdiff_plain;f=plugins%2Faf_psql_trgm%2Finit.php;h=fe5b1a959909ce54fc2c9c4583bbb3263c0584f1;hb=c10a43069ec1e71b6608574a81fb29c76919e132;hp=559a5a71c9b151bbfe18f5086f8ca364c35f7410;hpb=d806953471684c45b3ebe93de46fb4294f55ca49;p=tt-rss.git diff --git a/plugins/af_psql_trgm/init.php b/plugins/af_psql_trgm/init.php index 559a5a71..fe5b1a95 100644 --- a/plugins/af_psql_trgm/init.php +++ b/plugins/af_psql_trgm/init.php @@ -1,6 +1,7 @@ 1) $similarity = 1; @@ -22,8 +24,9 @@ class Af_Psql_Trgm extends Plugin { $this->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)", $similarity); + echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally); } function init($host) { @@ -42,18 +45,20 @@ class Af_Psql_Trgm extends Plugin { } function showrelated() { - $id = (int) db_escape_string($_REQUEST['param']); + $id = (int) $_REQUEST['param']; $owner_uid = $_SESSION["uid"]; - $result = db_query("SELECT title FROM ttrss_entries, ttrss_user_entries - WHERE ref_id = id AND id = $id AND owner_uid = $owner_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]); - $title = db_fetch_result($result, 0, "title"); + if ($row = $sth->fetch()) { - print "

$title

"; + $title = $row['title']; - $title = db_escape_string($title); - $result = db_query("SELECT ttrss_entries.id AS id, + print "

$title

"; + + $sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id, feed_id, ttrss_entries.title AS title, updated, link, @@ -63,35 +68,42 @@ class Af_Psql_Trgm extends Plugin { 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 + ttrss_user_entries.owner_uid = ? AND + ttrss_entries.id != ? AND date_entered >= NOW() - INTERVAL '2 weeks' ORDER BY sm DESC, date_entered DESC LIMIT 10"); - print ""; + + } print "
"; print ""; @@ -114,82 +126,87 @@ class Af_Psql_Trgm extends Plugin { if (DB_TYPE != "pgsql") { print_error("Database type not supported."); - } + } else { - $result = db_query("select 'similarity'::regproc"); + $res = $this->pdo->query("select 'similarity'::regproc"); - if (db_num_rows($result) == 0) { - print_error("pg_trgm extension not found."); - } - - $similarity = $this->host->get($this, "similarity"); - $min_title_length = $this->host->get($this, "min_title_length"); - - if (!$similarity) $similarity = '0.75'; - if (!$min_title_length) $min_title_length = '32'; - - print "
"; - - print ""; - - print ""; - print ""; - print ""; - - print_notice("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking."); - - print "
"; - print_notice("Enable the plugin for specific feeds in the feed editor."); - - print "

" . __("Global settings") . "

"; - - print ""; - - print ""; - print ""; - print ""; - print ""; - - print "
".__("Minimum similarity:")." -
".__("Minimum title length:")." -
"; - - print "

"; - - print "

"; - - $enabled_feeds = $this->host->get($this, "enabled_feeds"); - if (!array($enabled_feeds)) $enabled_feeds = array(); - - if (count($enabled_feeds) > 0) { - print "

" . __("Currently enabled for (click to edit):") . "

"; - print ""; } print "
"; @@ -216,7 +233,7 @@ class Af_Psql_Trgm extends Plugin { $enabled_feeds = $this->host->get($this, "enabled_feeds"); if (!is_array($enabled_feeds)) $enabled_feeds = array(); - $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true'; + $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]); $key = array_search($feed_id, $enabled_feeds); if ($enable) { @@ -236,32 +253,58 @@ class Af_Psql_Trgm extends Plugin { if (DB_TYPE != "pgsql") return $article; - $result = db_query("select 'similarity'::regproc"); - if (db_num_rows($result) == 0) return $article; + $res = $this->pdo->query("select 'similarity'::regproc"); + if (!$res->fetch()) return $article; - $enabled_feeds = $this->host->get($this, "enabled_feeds"); - $key = array_search($article["feed"]["id"], $enabled_feeds); - if ($key === FALSE) return $article; + $enable_globally = $this->host->get($this, "enable_globally"); + + if (!$enable_globally) { + $enabled_feeds = $this->host->get($this, "enabled_feeds"); + $key = array_search($article["feed"]["id"], $enabled_feeds); + if ($key === FALSE) return $article; + } $similarity = (float) $this->host->get($this, "similarity"); if ($similarity < 0.01) return $article; - $min_title_length = (int) $this->host->get($this, "min_length"); + $min_title_length = (int) $this->host->get($this, "min_title_length"); if (mb_strlen($article["title"]) < $min_title_length) return $article; $owner_uid = $article["owner_uid"]; - $feed_id = $article["feed"]["id"]; + $entry_guid = $article["guid_hashed"]; + $title_escaped = $article["title"]; - $title_escaped = db_escape_string($article["title"]); + // trgm does not return similarity=1 for completely equal strings - $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms + $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; + } + + $sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, ?)) AS ms FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND date_entered >= NOW() - interval '1 day' AND - owner_uid = $owner_uid"); + guid != ? AND + owner_uid = ?"); + $sth->execute([$title_escaped, $entry_guid, $owner_uid]); - $similarity_result = db_fetch_result($result, 0, "ms"); + $row = $sth->fetch(); + $similarity_result = $row['ms']; - //_debug("similarity result: $similarity_result"); + Debug::log("af_psql_trgm: similarity result: $similarity_result", Debug::$LOG_EXTENDED); if ($similarity_result >= $similarity) { $article["force_catchup"] = true; @@ -275,5 +318,20 @@ class Af_Psql_Trgm extends Plugin { return 2; } -} -?> + private function filter_unknown_feeds($enabled_feeds) { + $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; + } + +} \ No newline at end of file