]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/af_psql_trgm/init.php
debug logging system rework:
[tt-rss.git] / plugins / af_psql_trgm / init.php
index a1ee040e8231010c74d58037a9ade78c8d427669..fe5b1a959909ce54fc2c9c4583bbb3263c0584f1 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 class Af_Psql_Trgm extends Plugin {
 
+       /* @var PluginHost $host */
        private $host;
-       private $filters = array();
 
        function about() {
                return array(1.0,
@@ -11,8 +11,9 @@ class Af_Psql_Trgm extends Plugin {
        }
 
        function save() {
-               $similarity = (float) db_escape_string($_POST["similarity"]);
-               $min_title_length = (int) db_escape_string($_POST["min_title_length"]);
+               $similarity = (float) $_POST["similarity"];
+               $min_title_length = (int) $_POST["min_title_length"];
+               $enable_globally = checkbox_to_sql_bool($_POST["enable_globally"]);
 
                if ($similarity < 0) $similarity = 0;
                if ($similarity > 1) $similarity = 1;
@@ -23,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) {
@@ -34,88 +36,181 @@ class Af_Psql_Trgm extends Plugin {
                $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 hook_prefs_tab($args) {
-               if ($args != "prefFeeds") return;
+       function get_js() {
+               return file_get_contents(__DIR__ . "/init.js");
+       }
 
-               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mark similar articles as read')."\">";
+       function showrelated() {
+               $id = (int) $_REQUEST['param'];
+               $owner_uid = $_SESSION["uid"];
 
-               if (DB_TYPE != "pgsql") {
-                       print_error("Database type not supported.");
-               }
+               $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]);
 
-               $result = db_query("select 'similarity'::regproc");
+               if ($row = $sth->fetch()) {
 
-               if (db_num_rows($result) == 0) {
-                       print_error("pg_trgm extension not found.");
-               }
+                       $title = $row['title'];
 
-               $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 "<form dojoType=\"dijit.form.Form\">";
-
-               print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
-                       evt.preventDefault();
-                       if (this.validate()) {
-                               console.log(dojo.objectToQuery(this.getValues()));
-                               new Ajax.Request('backend.php', {
-                                       parameters: dojo.objectToQuery(this.getValues()),
-                                       onComplete: function(transport) {
-                                               notify_info(transport.responseText);
-                                       }
-                               });
-                               //this.reset();
-                       }
-                       </script>";
+                       print "<h2>$title</h2>";
 
-               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
-               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
-               print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_psql_trgm\">";
+                       $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 = ? AND
+                               ttrss_entries.id != ? AND
+                               date_entered >= NOW() - INTERVAL '2 weeks'
+                       ORDER BY
+                               sm DESC, date_entered DESC
+                       LIMIT 10");
 
-               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.");
+                       $sth->execute([$owner_uid, $id]);
 
-               print "<br/>";
-               print_notice("Only data in other feeds is checked, i.e. sequential duplicate posts in one feed will not be detected by this plugin.");
+                       print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
 
-               print "<br/>";
-               print_notice("Enable the plugin for specific feeds in the feed editor.");
+                       while ($line = $sth->fetch()) {
+                               print "<li>";
+                               print "<div class='insensitive small' style='margin-left : 20px; float : right'>" .
+                                       smart_date_time(strtotime($line["updated"]))
+                                       . "</div>";
 
-               print "<h3>" . __("Global settings") . "</h3>";
+                               $sm = sprintf("%.2f", $line['sm']);
+                               print "<img src='images/score_high.png' title='$sm'
+                               style='vertical-align : middle'>";
 
-               print "<table>";
+                               $article_link = htmlspecialchars($line["link"]);
+                               print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">".
+                                       $line["title"]."</a>";
 
-               print "<tr><td width=\"40%\">".__("Minimum similarity:")."</td>";
-               print "<td>
-                       <input dojoType=\"dijit.form.ValidationTextBox\"
-                       placeholder=\"0.75\"
-                       required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>";
-               print "<tr><td width=\"40%\">".__("Minimum title length:")."</td>";
-               print "<td>
-                       <input dojoType=\"dijit.form.ValidationTextBox\"
-                       placeholder=\"32\"
-                       required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>";
+                               print " (<a href=\"#\" onclick=\"viewfeed({feed:".$line["feed_id"]."})\">".
+                                       htmlspecialchars($line["feed_title"])."</a>)";
 
+                               print " <span class='insensitive'>($sm)</span>";
 
-               print "</table>";
+                               print "</li>";
+                       }
 
-               print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
-                       __("Save")."</button>";
+                       print "</ul>";
 
-               print "</form>";
+               }
 
+               print "<div style='text-align : center'>";
+               print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>";
                print "</div>";
+
+
        }
 
-       //PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_EDIT_FEED,
-       //              "hook_prefs_edit_feed", $feed_id);
-       //      PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_SAVE_FEED,
-       //              "hook_prefs_save_feed", $feed_id);
+       function hook_article_button($line) {
+               return "<img src=\"plugins/af_psql_trgm/button.png\"
+                       style=\"cursor : pointer\" style=\"cursor : pointer\"
+                       onclick=\"showTrgmRelated(".$line["id"].")\"
+                       class='tagsPic' title='".__('Show related articles')."'>";
+       }
+
+       function hook_prefs_tab($args) {
+               if ($args != "prefFeeds") return;
+
+               print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mark similar articles as read')."\">";
+
+               if (DB_TYPE != "pgsql") {
+                       print_error("Database type not supported.");
+               } else {
+
+                       $res = $this->pdo->query("select 'similarity'::regproc");
+
+                       if (!$res->fetch()) {
+                               print_error("pg_trgm extension not found.");
+                       }
+
+                       $similarity = $this->host->get($this, "similarity");
+                       $min_title_length = $this->host->get($this, "min_title_length");
+                       $enable_globally = $this->host->get($this, "enable_globally");
+
+                       if (!$similarity) $similarity = '0.75';
+                       if (!$min_title_length) $min_title_length = '32';
+
+                       print "<form dojoType=\"dijit.form.Form\">";
+
+                       print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
+                               evt.preventDefault();
+                               if (this.validate()) {
+                                       console.log(dojo.objectToQuery(this.getValues()));
+                                       new Ajax.Request('backend.php', {
+                                               parameters: dojo.objectToQuery(this.getValues()),
+                                               onComplete: function(transport) {
+                                                       notify_info(transport.responseText);
+                                               }
+                                       });
+                                       //this.reset();
+                               }
+                               </script>";
+
+                       print_hidden("op", "pluginhandler");
+                       print_hidden("method", "save");
+                       print_hidden("plugin", "af_psql_trgm");
+
+                       print "<p>" . __("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking.") . "</p>";
+                       print_notice("Enable the plugin for specific feeds in the feed editor.");
+
+                       print "<h3>" . __("Global settings") . "</h3>";
+
+                       print "<table>";
+
+                       print "<tr><td width=\"40%\">" . __("Minimum similarity:") . "</td>";
+                       print "<td>
+                               <input dojoType=\"dijit.form.ValidationTextBox\"
+                               placeholder=\"0.75\"
+                               required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>";
+                       print "<tr><td width=\"40%\">" . __("Minimum title length:") . "</td>";
+                       print "<td>
+                               <input dojoType=\"dijit.form.ValidationTextBox\"
+                               placeholder=\"32\"
+                               required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>";
+                       print "<tr><td width=\"40%\">" . __("Enable for all feeds:") . "</td>";
+                       print "<td>";
+                       print_checkbox("enable_globally", $enable_globally);
+                       print "</td></tr>";
+
+                       print "</table>";
+
+                       print "<p>"; print_button("submit", __("Save"));
+                       print "</form>";
+
+                       $enabled_feeds = $this->host->get($this, "enabled_feeds");
+                       if (!array($enabled_feeds)) $enabled_feeds = array();
+
+                       $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
+                       $this->host->set($this, "enabled_feeds", $enabled_feeds);
+
+                       if (count($enabled_feeds) > 0) {
+                               print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
+
+                               print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
+                               foreach ($enabled_feeds as $f) {
+                                       print "<li>" .
+                                               "<img src='images/pub_set.png'
+                                                       style='vertical-align : middle'> <a href='#'
+                                                       onclick='editFeed($f)'>" .
+                                               Feeds::getFeedTitle($f) . "</a></li>";
+                               }
+                               print "</ul>";
+                       }
+               }
+
+               print "</div>";
+       }
 
        function hook_prefs_edit_feed($feed_id) {
                print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>";
@@ -138,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) {
@@ -158,33 +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
-                 feed_id != $feed_id 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;
@@ -198,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