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