init.php 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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\" 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. $enable_globally_checked = $enable_globally ? "checked" : "";
  101. print "<form dojoType=\"dijit.form.Form\">";
  102. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  103. evt.preventDefault();
  104. if (this.validate()) {
  105. console.log(dojo.objectToQuery(this.getValues()));
  106. new Ajax.Request('backend.php', {
  107. parameters: dojo.objectToQuery(this.getValues()),
  108. onComplete: function(transport) {
  109. notify_info(transport.responseText);
  110. }
  111. });
  112. //this.reset();
  113. }
  114. </script>";
  115. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
  116. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
  117. print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_psql_trgm\">";
  118. 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>";
  119. print_notice("Enable the plugin for specific feeds in the feed editor.");
  120. print "<h3>" . __("Global settings") . "</h3>";
  121. print "<table>";
  122. print "<tr><td width=\"40%\">" . __("Minimum similarity:") . "</td>";
  123. print "<td>
  124. <input dojoType=\"dijit.form.ValidationTextBox\"
  125. placeholder=\"0.75\"
  126. required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>";
  127. print "<tr><td width=\"40%\">" . __("Minimum title length:") . "</td>";
  128. print "<td>
  129. <input dojoType=\"dijit.form.ValidationTextBox\"
  130. placeholder=\"32\"
  131. required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>";
  132. print "<tr><td width=\"40%\">" . __("Enable for all feeds:") . "</td>";
  133. print "<td>
  134. <input dojoType=\"dijit.form.CheckBox\"
  135. $enable_globally_checked name=\"enable_globally\"></td></tr>";
  136. print "</table>";
  137. print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">" .
  138. __("Save") . "</button>";
  139. print "</form>";
  140. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  141. if (!array($enabled_feeds)) $enabled_feeds = array();
  142. $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
  143. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  144. if (count($enabled_feeds) > 0) {
  145. print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
  146. print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
  147. foreach ($enabled_feeds as $f) {
  148. print "<li>" .
  149. "<img src='images/pub_set.png'
  150. style='vertical-align : middle'> <a href='#'
  151. onclick='editFeed($f)'>" .
  152. getFeedTitle($f) . "</a></li>";
  153. }
  154. print "</ul>";
  155. }
  156. }
  157. print "</div>";
  158. }
  159. function hook_prefs_edit_feed($feed_id) {
  160. print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>";
  161. print "<div class=\"dlgSecCont\">";
  162. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  163. if (!array($enabled_feeds)) $enabled_feeds = array();
  164. $key = array_search($feed_id, $enabled_feeds);
  165. $checked = $key !== FALSE ? "checked" : "";
  166. print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"trgm_similarity_enabled\"
  167. name=\"trgm_similarity_enabled\"
  168. $checked>&nbsp;<label for=\"trgm_similarity_enabled\">".__('Mark similar articles as read')."</label>";
  169. print "</div>";
  170. }
  171. function hook_prefs_save_feed($feed_id) {
  172. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  173. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  174. $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true';
  175. $key = array_search($feed_id, $enabled_feeds);
  176. if ($enable) {
  177. if ($key === FALSE) {
  178. array_push($enabled_feeds, $feed_id);
  179. }
  180. } else {
  181. if ($key !== FALSE) {
  182. unset($enabled_feeds[$key]);
  183. }
  184. }
  185. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  186. }
  187. function hook_article_filter($article) {
  188. if (DB_TYPE != "pgsql") return $article;
  189. $result = db_query("select 'similarity'::regproc");
  190. if (db_num_rows($result) == 0) return $article;
  191. $enable_globally = $this->host->get($this, "enable_globally");
  192. if (!$enable_globally) {
  193. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  194. $key = array_search($article["feed"]["id"], $enabled_feeds);
  195. if ($key === FALSE) return $article;
  196. }
  197. $similarity = (float) $this->host->get($this, "similarity");
  198. if ($similarity < 0.01) return $article;
  199. $min_title_length = (int) $this->host->get($this, "min_title_length");
  200. if (mb_strlen($article["title"]) < $min_title_length) return $article;
  201. $owner_uid = $article["owner_uid"];
  202. $entry_guid = $article["guid_hashed"];
  203. $title_escaped = db_escape_string($article["title"]);
  204. // trgm does not return similarity=1 for completely equal strings
  205. $result = db_query("SELECT COUNT(id) AS nequal
  206. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  207. date_entered >= NOW() - interval '3 days' AND
  208. title = '$title_escaped' AND
  209. guid != '$entry_guid' AND
  210. owner_uid = $owner_uid");
  211. $nequal = db_fetch_result($result, 0, "nequal");
  212. _debug("af_psql_trgm: num equals: $nequal");
  213. if ($nequal != 0) {
  214. $article["force_catchup"] = true;
  215. return $article;
  216. }
  217. $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms
  218. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  219. date_entered >= NOW() - interval '1 day' AND
  220. guid != '$entry_guid' AND
  221. owner_uid = $owner_uid");
  222. $similarity_result = db_fetch_result($result, 0, "ms");
  223. _debug("af_psql_trgm: similarity result: $similarity_result");
  224. if ($similarity_result >= $similarity) {
  225. $article["force_catchup"] = true;
  226. }
  227. return $article;
  228. }
  229. function api_version() {
  230. return 2;
  231. }
  232. private function filter_unknown_feeds($enabled_feeds) {
  233. $tmp = array();
  234. foreach ($enabled_feeds as $feed) {
  235. $result = db_query("SELECT id FROM ttrss_feeds WHERE id = '$feed' AND owner_uid = " . $_SESSION["uid"]);
  236. if (db_num_rows($result) != 0) {
  237. array_push($tmp, $feed);
  238. }
  239. }
  240. return $tmp;
  241. }
  242. }
  243. ?>