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