]> git.wh0rd.org Git - tt-rss.git/blob - plugins/af_psql_trgm/init.php
trgm: add disabled for now button part
[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
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);
36                 //$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
37
38         }
39
40         /* function get_js() {
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);
56                 $result = db_query("SELECT id,title,updated
57                         FROM ttrss_entries, ttrss_user_entries
58                         WHERE owner_uid = $owner_uid AND
59                                 id = ref_id AND
60                                 id != $id AND
61                                 date_entered >= NOW() - INTERVAL '1 day' AND
62                                 SIMILARITY(title, '$title') >= 0.5
63                         LIMIT 30");
64
65                 print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
66
67                 while ($line = db_fetch_assoc($result)) {
68                         print "<li>";
69                         print "<div style='float : right'>" . smart_date_time($line["updated"])
70                                 . "</div>";
71                         print $line["title"];
72                         print "</li>";
73                 }
74
75                 print "</ul>";
76
77         } */
78
79         /* function hook_article_button($line) {
80                 return "<img src=\"plugins/af_psql_trgm/button.png\"
81                         style=\"cursor : pointer\" style=\"cursor : pointer\"
82                         onclick=\"showTrgmRelated(".$line["id"].")\"
83                         class='tagsPic' title='".__('Show related articles')."'>";
84         } */
85
86         function hook_prefs_tab($args) {
87                 if ($args != "prefFeeds") return;
88
89                 print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mark similar articles as read')."\">";
90
91                 if (DB_TYPE != "pgsql") {
92                         print_error("Database type not supported.");
93                 }
94
95                 $result = db_query("select 'similarity'::regproc");
96
97                 if (db_num_rows($result) == 0) {
98                         print_error("pg_trgm extension not found.");
99                 }
100
101                 $similarity = $this->host->get($this, "similarity");
102                 $min_title_length = $this->host->get($this, "min_title_length");
103
104                 if (!$similarity) $similarity = '0.75';
105                 if (!$min_title_length) $min_title_length = '32';
106
107                 print "<form dojoType=\"dijit.form.Form\">";
108
109                 print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\">
110                         evt.preventDefault();
111                         if (this.validate()) {
112                                 console.log(dojo.objectToQuery(this.getValues()));
113                                 new Ajax.Request('backend.php', {
114                                         parameters: dojo.objectToQuery(this.getValues()),
115                                         onComplete: function(transport) {
116                                                 notify_info(transport.responseText);
117                                         }
118                                 });
119                                 //this.reset();
120                         }
121                         </script>";
122
123                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">";
124                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">";
125                 print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_psql_trgm\">";
126
127                 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.");
128
129                 print "<br/>";
130                 print_notice("Enable the plugin for specific feeds in the feed editor.");
131
132                 print "<h3>" . __("Global settings") . "</h3>";
133
134                 print "<table>";
135
136                 print "<tr><td width=\"40%\">".__("Minimum similarity:")."</td>";
137                 print "<td>
138                         <input dojoType=\"dijit.form.ValidationTextBox\"
139                         placeholder=\"0.75\"
140                         required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>";
141                 print "<tr><td width=\"40%\">".__("Minimum title length:")."</td>";
142                 print "<td>
143                         <input dojoType=\"dijit.form.ValidationTextBox\"
144                         placeholder=\"32\"
145                         required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>";
146
147                 print "</table>";
148
149                 print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">".
150                         __("Save")."</button>";
151
152                 print "</form>";
153
154                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
155                 if (!array($enabled_feeds)) $enabled_feeds = array();
156
157                 if (count($enabled_feeds) > 0) {
158                         print "<h3>" . __("Currently enabled for (click to edit):") . "</h3>";
159
160                         print "<ul class=\"browseFeedList\" style=\"border-width : 1px\">";
161                         foreach ($enabled_feeds as $f) {
162                                 print "<li>" .
163                                         "<img src='images/pub_set.png'
164                                                 style='vertical-align : middle'> <a href='#'
165                                                 onclick='editFeed($f)'>".
166                                         getFeedTitle($f) . "</a></li>";
167                         }
168                         print "</ul>";
169                 }
170
171                 print "</div>";
172         }
173
174         function hook_prefs_edit_feed($feed_id) {
175                 print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>";
176                 print "<div class=\"dlgSecCont\">";
177
178                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
179                 if (!array($enabled_feeds)) $enabled_feeds = array();
180
181                 $key = array_search($feed_id, $enabled_feeds);
182                 $checked = $key !== FALSE ? "checked" : "";
183
184                 print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"trgm_similarity_enabled\"
185                         name=\"trgm_similarity_enabled\"
186                         $checked>&nbsp;<label for=\"trgm_similarity_enabled\">".__('Mark similar articles as read')."</label>";
187
188                 print "</div>";
189         }
190
191         function hook_prefs_save_feed($feed_id) {
192                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
193                 if (!is_array($enabled_feeds)) $enabled_feeds = array();
194
195                 $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true';
196                 $key = array_search($feed_id, $enabled_feeds);
197
198                 if ($enable) {
199                         if ($key === FALSE) {
200                                 array_push($enabled_feeds, $feed_id);
201                         }
202                 } else {
203                         if ($key !== FALSE) {
204                                 unset($enabled_feeds[$key]);
205                         }
206                 }
207
208                 $this->host->set($this, "enabled_feeds", $enabled_feeds);
209         }
210
211         function hook_article_filter($article) {
212
213                 if (DB_TYPE != "pgsql") return $article;
214
215                 $result = db_query("select 'similarity'::regproc");
216                 if (db_num_rows($result) == 0) return $article;
217
218                 $enabled_feeds = $this->host->get($this, "enabled_feeds");
219                 $key = array_search($article["feed"]["id"], $enabled_feeds);
220                 if ($key === FALSE) return $article;
221
222                 $similarity = (float) $this->host->get($this, "similarity");
223                 if ($similarity < 0.01) return $article;
224
225                 $min_title_length = (int) $this->host->get($this, "min_length");
226                 if (mb_strlen($article["title"]) < $min_title_length) return $article;
227
228                 $owner_uid = $article["owner_uid"];
229                 $feed_id = $article["feed"]["id"];
230
231                 $title_escaped = db_escape_string($article["title"]);
232
233                 $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms
234                   FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
235                   date_entered >= NOW() - interval '1 day' AND
236                   owner_uid = $owner_uid");
237
238                 $similarity_result = db_fetch_result($result, 0, "ms");
239
240                 //_debug("similarity result: $similarity_result");
241
242                 if ($similarity_result >= $similarity) {
243                         $article["force_catchup"] = true;
244                 }
245
246                 return $article;
247
248         }
249
250         function api_version() {
251                 return 2;
252         }
253
254 }
255 ?>