]> git.wh0rd.org - tt-rss.git/blobdiff - plugins/af_psql_trgm/init.php
various minor style updates, add danger buttons
[tt-rss.git] / plugins / af_psql_trgm / init.php
index f55aa115621dfc916e0b3b4ad969aee297a839ab..f93f7c02b51614ddd944746c1621d9984c680015 100644 (file)
@@ -12,6 +12,7 @@ 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"]);
+               $enable_globally = checkbox_to_sql_bool($_POST["enable_globally"]) == "true";
 
                if ($similarity < 0) $similarity = 0;
                if ($similarity > 1) $similarity = 1;
@@ -22,8 +23,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) {
@@ -33,9 +35,83 @@ 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 get_js() {
+               return file_get_contents(__DIR__ . "/init.js");
+       }
+
+       function showrelated() {
+               $id = (int) db_escape_string($_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");
+
+               $title = db_fetch_result($result, 0, "title");
+
+               print "<h2>$title</h2>";
+
+               $title = db_escape_string($title);
+               $result = db_query("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");
+
+               print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
+
+               while ($line = db_fetch_assoc($result)) {
+                       print "<li>";
+                       print "<div class='insensitive small' style='margin-left : 20px; float : right'>" .
+                               smart_date_time(strtotime($line["updated"]))
+                               . "</div>";
+
+                       $sm = sprintf("%.2f", $line['sm']);
+                       print "<img src='images/score_high.png' title='$sm'
+                               style='vertical-align : middle'>";
+
+                       $article_link = htmlspecialchars($line["link"]);
+                       print " <a target=\"_blank\" href=\"$article_link\">".
+                               $line["title"]."</a>";
+
+                       print " (<a href=\"#\" onclick=\"viewfeed({feed:".$line["feed_id"]."})\">".
+                               htmlspecialchars($line["feed_title"])."</a>)";
+
+                       print " <span class='insensitive'>($sm)</span>";
+
+                       print "</li>";
+               }
+
+               print "</ul>";
+
+               print "<div style='text-align : center'>";
+               print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>";
+               print "</div>";
+
+
+       }
+
+       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;
 
@@ -53,10 +129,13 @@ class Af_Psql_Trgm extends Plugin {
 
                $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';
 
+               $enable_globally_checked = $enable_globally ? "checked" : "";
+
                print "<form dojoType=\"dijit.form.Form\">";
 
                print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
@@ -77,9 +156,7 @@ class Af_Psql_Trgm extends Plugin {
                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\">";
 
-               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 "<br/>";
+               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>";
@@ -96,6 +173,10 @@ class Af_Psql_Trgm extends Plugin {
                        <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>
+                       <input dojoType=\"dijit.form.CheckBox\"
+                       $enable_globally_checked name=\"enable_globally\"></td></tr>";
 
                print "</table>";
 
@@ -107,6 +188,9 @@ class Af_Psql_Trgm extends Plugin {
                $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>";
 
@@ -168,9 +252,13 @@ class Af_Psql_Trgm extends Plugin {
                $result = db_query("select 'similarity'::regproc");
                if (db_num_rows($result) == 0) 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;
@@ -178,19 +266,37 @@ class Af_Psql_Trgm extends Plugin {
                $min_title_length = (int) $this->host->get($this, "min_length");
                if (mb_strlen($article["title"]) < $min_title_length) return $article;
 
-               $owner_uid = $article["owner_uid"];
-               $feed_id = $article["feed"]["id"];
 
+               $owner_uid = $article["owner_uid"];
+               $entry_guid = $article["guid_hashed"];
                $title_escaped = db_escape_string($article["title"]);
 
+               // trgm does not return similarity=1 for completely equal strings
+
+               $result = db_query("SELECT COUNT(id) AS nequal
+                 FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
+                 date_entered >= NOW() - interval '1 day' AND
+                 title = '$title_escaped' AND
+                 guid != '$entry_guid' AND
+                 owner_uid = $owner_uid");
+
+               $nequal = db_fetch_result($result, 0, "nequal");
+               _debug("af_psql_trgm: num equals: $nequal");
+
+               if ($nequal != 0) {
+                       $article["force_catchup"] = true;
+                       return $article;
+               }
+
                $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms
                  FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
                  date_entered >= NOW() - interval '1 day' AND
+                 guid != '$entry_guid' AND
                  owner_uid = $owner_uid");
 
                $similarity_result = db_fetch_result($result, 0, "ms");
 
-               //_debug("similarity result: $similarity_result");
+               _debug("af_psql_trgm: similarity result: $similarity_result");
 
                if ($similarity_result >= $similarity) {
                        $article["force_catchup"] = true;
@@ -204,5 +310,20 @@ class Af_Psql_Trgm extends Plugin {
                return 2;
        }
 
+       private function filter_unknown_feeds($enabled_feeds) {
+               $tmp = array();
+
+               foreach ($enabled_feeds as $feed) {
+
+                       $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
+
+                       if (db_num_rows($result) != 0) {
+                               array_push($tmp, $feed);
+                       }
+               }
+
+               return $tmp;
+       }
+
 }
 ?>