init.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. <?php
  2. class Af_Psql_Trgm extends Plugin {
  3. private $host;
  4. function about() {
  5. return array(1.0,
  6. "Marks similar articles as read (requires pg_trgm)",
  7. "fox");
  8. }
  9. function save() {
  10. $similarity = (float) db_escape_string($_POST["similarity"]);
  11. $min_title_length = (int) db_escape_string($_POST["min_title_length"]);
  12. $enable_globally = checkbox_to_sql_bool($_POST["enable_globally"]) == "true";
  13. if ($similarity < 0) $similarity = 0;
  14. if ($similarity > 1) $similarity = 1;
  15. if ($min_title_length < 0) $min_title_length = 0;
  16. $similarity = sprintf("%.2f", $similarity);
  17. $this->host->set($this, "similarity", $similarity);
  18. $this->host->set($this, "min_title_length", $min_title_length);
  19. $this->host->set($this, "enable_globally", $enable_globally);
  20. echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally);
  21. }
  22. function init($host) {
  23. $this->host = $host;
  24. $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
  25. $host->add_hook($host::HOOK_PREFS_TAB, $this);
  26. $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
  27. $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
  28. $host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
  29. }
  30. function get_js() {
  31. return file_get_contents(__DIR__ . "/init.js");
  32. }
  33. function showrelated() {
  34. $id = (int) db_escape_string($_REQUEST['param']);
  35. $owner_uid = $_SESSION["uid"];
  36. $result = db_query("SELECT title FROM ttrss_entries, ttrss_user_entries
  37. WHERE ref_id = id AND id = $id AND owner_uid = $owner_uid");
  38. $title = db_fetch_result($result, 0, "title");
  39. print "<h2>$title</h2>";
  40. $title = db_escape_string($title);
  41. $result = db_query("SELECT ttrss_entries.id AS id,
  42. feed_id,
  43. ttrss_entries.title AS title,
  44. updated, link,
  45. ttrss_feeds.title AS feed_title,
  46. SIMILARITY(ttrss_entries.title, '$title') AS sm
  47. FROM
  48. ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id)
  49. WHERE
  50. ttrss_entries.id = ref_id AND
  51. ttrss_user_entries.owner_uid = $owner_uid AND
  52. ttrss_entries.id != $id AND
  53. date_entered >= NOW() - INTERVAL '2 weeks'
  54. ORDER BY
  55. sm DESC, date_entered DESC
  56. LIMIT 10");
  57. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  58. while ($line = db_fetch_assoc($result)) {
  59. print "<li>";
  60. print "<div class='insensitive small' style='margin-left : 20px; float : right'>" .
  61. smart_date_time(strtotime($line["updated"]))
  62. . "</div>";
  63. $sm = sprintf("%.2f", $line['sm']);
  64. print "<img src='images/score_high.png' title='$sm'
  65. style='vertical-align : middle'>";
  66. $article_link = htmlspecialchars($line["link"]);
  67. print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">".
  68. $line["title"]."</a>";
  69. print " (<a href=\"#\" onclick=\"viewfeed({feed:".$line["feed_id"]."})\">".
  70. htmlspecialchars($line["feed_title"])."</a>)";
  71. print " <span class='insensitive'>($sm)</span>";
  72. print "</li>";
  73. }
  74. print "</ul>";
  75. print "<div style='text-align : center'>";
  76. print "<button dojoType=\"dijit.form.Button\" onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>";
  77. print "</div>";
  78. }
  79. function hook_article_button($line) {
  80. return "<img src=\"plugins/af_psql_trgm/button.png\"
  81. style=\"cursor : pointer\" style=\"cursor : pointer\"
  82. onclick=\"showTrgmRelated(".$line["id"].")\"
  83. class='tagsPic' title='".__('Show related articles')."'>";
  84. }
  85. function hook_prefs_tab($args) {
  86. if ($args != "prefFeeds") return;
  87. print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mark similar articles as read')."\">";
  88. if (DB_TYPE != "pgsql") {
  89. print_error("Database type not supported.");
  90. } else {
  91. $result = db_query("select 'similarity'::regproc");
  92. if (db_num_rows($result) == 0) {
  93. print_error("pg_trgm extension not found.");
  94. }
  95. $similarity = $this->host->get($this, "similarity");
  96. $min_title_length = $this->host->get($this, "min_title_length");
  97. $enable_globally = $this->host->get($this, "enable_globally");
  98. if (!$similarity) $similarity = '0.75';
  99. if (!$min_title_length) $min_title_length = '32';
  100. print "<form dojoType=\"dijit.form.Form\">";
  101. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  102. evt.preventDefault();
  103. if (this.validate()) {
  104. console.log(dojo.objectToQuery(this.getValues()));
  105. new Ajax.Request('backend.php', {
  106. parameters: dojo.objectToQuery(this.getValues()),
  107. onComplete: function(transport) {
  108. notify_info(transport.responseText);
  109. }
  110. });
  111. //this.reset();
  112. }
  113. </script>";
  114. print_hidden("op", "pluginhandler");
  115. print_hidden("method", "save");
  116. print_hidden("plugin", "af_psql_trgm");
  117. 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>";
  118. print_notice("Enable the plugin for specific feeds in the feed editor.");
  119. print "<h3>" . __("Global settings") . "</h3>";
  120. print "<table>";
  121. print "<tr><td width=\"40%\">" . __("Minimum similarity:") . "</td>";
  122. print "<td>
  123. <input dojoType=\"dijit.form.ValidationTextBox\"
  124. placeholder=\"0.75\"
  125. required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>";
  126. print "<tr><td width=\"40%\">" . __("Minimum title length:") . "</td>";
  127. print "<td>
  128. <input dojoType=\"dijit.form.ValidationTextBox\"
  129. placeholder=\"32\"
  130. required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>";
  131. print "<tr><td width=\"40%\">" . __("Enable for all feeds:") . "</td>";
  132. print "<td>";
  133. print_checkbox("enable_globally", $enable_globally);
  134. print "</td></tr>";
  135. print "</table>";
  136. print "<p>"; print_button("submit", __("Save"));
  137. print "</form>";
  138. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  139. if (!array($enabled_feeds)) $enabled_feeds = array();
  140. $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
  141. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  142. if (count($enabled_feeds) > 0) {
  143. print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
  144. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  145. foreach ($enabled_feeds as $f) {
  146. print "<li>" .
  147. "<img src='images/pub_set.png'
  148. style='vertical-align : middle'> <a href='#'
  149. onclick='editFeed($f)'>" .
  150. getFeedTitle($f) . "</a></li>";
  151. }
  152. print "</ul>";
  153. }
  154. }
  155. print "</div>";
  156. }
  157. function hook_prefs_edit_feed($feed_id) {
  158. print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>";
  159. print "<div class=\"dlgSecCont\">";
  160. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  161. if (!array($enabled_feeds)) $enabled_feeds = array();
  162. $key = array_search($feed_id, $enabled_feeds);
  163. $checked = $key !== FALSE ? "checked" : "";
  164. print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"trgm_similarity_enabled\"
  165. name=\"trgm_similarity_enabled\"
  166. $checked>&nbsp;<label for=\"trgm_similarity_enabled\">".__('Mark similar articles as read')."</label>";
  167. print "</div>";
  168. }
  169. function hook_prefs_save_feed($feed_id) {
  170. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  171. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  172. $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true';
  173. $key = array_search($feed_id, $enabled_feeds);
  174. if ($enable) {
  175. if ($key === FALSE) {
  176. array_push($enabled_feeds, $feed_id);
  177. }
  178. } else {
  179. if ($key !== FALSE) {
  180. unset($enabled_feeds[$key]);
  181. }
  182. }
  183. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  184. }
  185. function hook_article_filter($article) {
  186. if (DB_TYPE != "pgsql") return $article;
  187. $result = db_query("select 'similarity'::regproc");
  188. if (db_num_rows($result) == 0) return $article;
  189. $enable_globally = $this->host->get($this, "enable_globally");
  190. if (!$enable_globally) {
  191. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  192. $key = array_search($article["feed"]["id"], $enabled_feeds);
  193. if ($key === FALSE) return $article;
  194. }
  195. $similarity = (float) $this->host->get($this, "similarity");
  196. if ($similarity < 0.01) return $article;
  197. $min_title_length = (int) $this->host->get($this, "min_title_length");
  198. if (mb_strlen($article["title"]) < $min_title_length) return $article;
  199. $owner_uid = $article["owner_uid"];
  200. $entry_guid = $article["guid_hashed"];
  201. $title_escaped = db_escape_string($article["title"]);
  202. // trgm does not return similarity=1 for completely equal strings
  203. $result = db_query("SELECT COUNT(id) AS nequal
  204. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  205. date_entered >= NOW() - interval '3 days' AND
  206. title = '$title_escaped' AND
  207. guid != '$entry_guid' AND
  208. owner_uid = $owner_uid");
  209. $nequal = db_fetch_result($result, 0, "nequal");
  210. _debug("af_psql_trgm: num equals: $nequal");
  211. if ($nequal != 0) {
  212. $article["force_catchup"] = true;
  213. return $article;
  214. }
  215. $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms
  216. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  217. date_entered >= NOW() - interval '1 day' AND
  218. guid != '$entry_guid' AND
  219. owner_uid = $owner_uid");
  220. $similarity_result = db_fetch_result($result, 0, "ms");
  221. _debug("af_psql_trgm: similarity result: $similarity_result");
  222. if ($similarity_result >= $similarity) {
  223. $article["force_catchup"] = true;
  224. }
  225. return $article;
  226. }
  227. function api_version() {
  228. return 2;
  229. }
  230. private function filter_unknown_feeds($enabled_feeds) {
  231. $tmp = array();
  232. foreach ($enabled_feeds as $feed) {
  233. $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  234. if (db_num_rows($result) != 0) {
  235. array_push($tmp, $feed);
  236. }
  237. }
  238. return $tmp;
  239. }
  240. }
  241. ?>