init.php 9.7 KB

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