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