init.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 "<p>$title</p>";
  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, ?) 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([$title, $owner_uid, $id]);
  60. print "<ul class='panel panel-scrollable'>";
  61. while ($line = $sth->fetch()) {
  62. print "<li style='display : flex'>";
  63. print "<i class='material-icons'>bookmark_outline</i>";
  64. $sm = sprintf("%.2f", $line['sm']);
  65. $article_link = htmlspecialchars($line["link"]);
  66. print "<div style='flex-grow : 2'>";
  67. print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">".
  68. $line["title"]."</a>";
  69. print " (<a href=\"#\" onclick=\"Feeds.open({feed:".$line["feed_id"]."})\">".
  70. htmlspecialchars($line["feed_title"])."</a>)";
  71. print " &mdash; $sm";
  72. print "</div>";
  73. print "<div style='text-align : right' class='text-muted'>" . smart_date_time(strtotime($line["updated"])) . "</div>";
  74. print "</li>";
  75. }
  76. print "</ul>";
  77. }
  78. print "<footer class='text-center'>";
  79. print "<button dojoType='dijit.form.Button' onclick=\"dijit.byId('trgmRelatedDlg').hide()\">".__('Close this window')."</button>";
  80. print "</footer>";
  81. }
  82. function hook_article_button($line) {
  83. return "<i style=\"cursor : pointer\" class='material-icons'
  84. onclick=\"Plugins.Psql_Trgm.showRelated(".$line["id"].")\"
  85. title='".__('Show related articles')."'>bookmark_outline</i>";
  86. }
  87. function hook_prefs_tab($args) {
  88. if ($args != "prefFeeds") return;
  89. print "<div dojoType=\"dijit.layout.AccordionPane\"
  90. title=\"<i class='material-icons'>extension</i> ".__('Mark similar articles as read')."\">";
  91. if (DB_TYPE != "pgsql") {
  92. print_error("Database type not supported.");
  93. } else {
  94. $res = $this->pdo->query("select 'similarity'::regproc");
  95. if (!$res->fetch()) {
  96. print_error("pg_trgm extension not found.");
  97. }
  98. $similarity = $this->host->get($this, "similarity");
  99. $min_title_length = $this->host->get($this, "min_title_length");
  100. $enable_globally = $this->host->get($this, "enable_globally");
  101. if (!$similarity) $similarity = '0.75';
  102. if (!$min_title_length) $min_title_length = '32';
  103. print "<form dojoType=\"dijit.form.Form\">";
  104. print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
  105. evt.preventDefault();
  106. if (this.validate()) {
  107. console.log(dojo.objectToQuery(this.getValues()));
  108. new Ajax.Request('backend.php', {
  109. parameters: dojo.objectToQuery(this.getValues()),
  110. onComplete: function(transport) {
  111. Notify.info(transport.responseText);
  112. }
  113. });
  114. //this.reset();
  115. }
  116. </script>";
  117. print_hidden("op", "pluginhandler");
  118. print_hidden("method", "save");
  119. print_hidden("plugin", "af_psql_trgm");
  120. print "<h2>" . __("Global settings") . "</h2>";
  121. print_notice("Enable for specific feeds in the feed editor.");
  122. print "<fieldset>";
  123. print "<label>" . __("Minimum similarity:") . "</label> ";
  124. print "<input dojoType=\"dijit.form.NumberSpinner\"
  125. placeholder=\"0.75\" id='psql_trgm_similarity'
  126. required=\"1\" name=\"similarity\" value=\"$similarity\">";
  127. print "<div dojoType='dijit.Tooltip' connectId='psql_trgm_similarity' position='below'>" .
  128. __("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking.") .
  129. "</div>";
  130. print "</fieldset><fieldset>";
  131. print "<label>" . __("Minimum title length:") . "</label> ";
  132. print "<input dojoType=\"dijit.form.NumberSpinner\"
  133. placeholder=\"32\"
  134. required=\"1\" name=\"min_title_length\" value=\"$min_title_length\">";
  135. print "</fieldset><fieldset>";
  136. print "<label class='checkbox'>";
  137. print_checkbox("enable_globally", $enable_globally);
  138. print " " . __("Enable for all feeds:");
  139. print "</label>";
  140. print "</fieldset>";
  141. print_button("submit", __("Save"), "class='alt-primary'");
  142. print "</form>";
  143. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  144. if (!array($enabled_feeds)) $enabled_feeds = array();
  145. $enabled_feeds = $this->filter_unknown_feeds($enabled_feeds);
  146. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  147. if (count($enabled_feeds) > 0) {
  148. print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
  149. print "<ul class=\"panel panel-scrollable list list-unstyled\">";
  150. foreach ($enabled_feeds as $f) {
  151. print "<li>" .
  152. "<i class='material-icons'>rss_feed</i> <a href='#'
  153. onclick='CommonDialogs.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 "<header>".__("Similarity (pg_trgm)")."</header>";
  163. print "<section>";
  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 "<fieldset>";
  169. print "<label class='checkbox'><input dojoType='dijit.form.CheckBox' type='checkbox' id='trgm_similarity_enabled'
  170. name='trgm_similarity_enabled' $checked> ".__('Mark similar articles as read')."</label>";
  171. print "</fieldset>";
  172. print "</section>";
  173. }
  174. function hook_prefs_save_feed($feed_id) {
  175. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  176. if (!is_array($enabled_feeds)) $enabled_feeds = array();
  177. $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]);
  178. $key = array_search($feed_id, $enabled_feeds);
  179. if ($enable) {
  180. if ($key === FALSE) {
  181. array_push($enabled_feeds, $feed_id);
  182. }
  183. } else {
  184. if ($key !== FALSE) {
  185. unset($enabled_feeds[$key]);
  186. }
  187. }
  188. $this->host->set($this, "enabled_feeds", $enabled_feeds);
  189. }
  190. function hook_article_filter($article) {
  191. if (DB_TYPE != "pgsql") return $article;
  192. $res = $this->pdo->query("select 'similarity'::regproc");
  193. if (!$res->fetch()) return $article;
  194. $enable_globally = $this->host->get($this, "enable_globally");
  195. if (!$enable_globally) {
  196. $enabled_feeds = $this->host->get($this, "enabled_feeds");
  197. $key = array_search($article["feed"]["id"], $enabled_feeds);
  198. if ($key === FALSE) return $article;
  199. }
  200. $similarity = (float) $this->host->get($this, "similarity");
  201. if ($similarity < 0.01) return $article;
  202. $min_title_length = (int) $this->host->get($this, "min_title_length");
  203. if (mb_strlen($article["title"]) < $min_title_length) return $article;
  204. $owner_uid = $article["owner_uid"];
  205. $entry_guid = $article["guid_hashed"];
  206. $title_escaped = $article["title"];
  207. // trgm does not return similarity=1 for completely equal strings
  208. $sth = $this->pdo->prepare("SELECT COUNT(id) AS nequal
  209. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  210. date_entered >= NOW() - interval '3 days' AND
  211. title = ? AND
  212. guid != ? AND
  213. owner_uid = ?");
  214. $sth->execute([$title_escaped, $entry_guid, $owner_uid]);
  215. $row = $sth->fetch();
  216. $nequal = $row['nequal'];
  217. Debug::log("af_psql_trgm: num equals: $nequal", Debug::$LOG_EXTENDED);
  218. if ($nequal != 0) {
  219. $article["force_catchup"] = true;
  220. return $article;
  221. }
  222. $sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, ?)) AS ms
  223. FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
  224. date_entered >= NOW() - interval '1 day' AND
  225. guid != ? AND
  226. owner_uid = ?");
  227. $sth->execute([$title_escaped, $entry_guid, $owner_uid]);
  228. $row = $sth->fetch();
  229. $similarity_result = $row['ms'];
  230. Debug::log("af_psql_trgm: similarity result: $similarity_result", Debug::$LOG_EXTENDED);
  231. if ($similarity_result >= $similarity) {
  232. $article["force_catchup"] = true;
  233. }
  234. return $article;
  235. }
  236. function api_version() {
  237. return 2;
  238. }
  239. private function filter_unknown_feeds($enabled_feeds) {
  240. $tmp = array();
  241. foreach ($enabled_feeds as $feed) {
  242. $sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
  243. $sth->execute([$feed, $_SESSION['uid']]);
  244. if ($row = $sth->fetch()) {
  245. array_push($tmp, $feed);
  246. }
  247. }
  248. return $tmp;
  249. }
  250. }