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