]>
Commit | Line | Data |
---|---|---|
117efb6f AD |
1 | <?php |
2 | class Af_Psql_Trgm extends Plugin { | |
3 | ||
4 | private $host; | |
5 | private $filters = array(); | |
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() { | |
14 | $similarity = (float) db_escape_string($_POST["similarity"]); | |
15 | $min_title_length = (int) db_escape_string($_POST["min_title_length"]); | |
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 | ||
27 | echo T_sprintf("Data saved (%s)", $similarity); | |
28 | } | |
29 | ||
30 | function init($host) { | |
31 | $this->host = $host; | |
32 | ||
33 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
34 | $host->add_hook($host::HOOK_PREFS_TAB, $this); | |
35 | $host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this); | |
36 | $host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this); | |
37 | ||
38 | } | |
39 | ||
40 | function hook_prefs_tab($args) { | |
41 | if ($args != "prefFeeds") return; | |
42 | ||
43 | print "<div dojoType=\"dijit.layout.AccordionPane\" title=\"".__('Mark similar articles as read')."\">"; | |
44 | ||
45 | if (DB_TYPE != "pgsql") { | |
46 | print_error("Database type not supported."); | |
47 | } | |
48 | ||
49 | $result = db_query("select 'similarity'::regproc"); | |
50 | ||
51 | if (db_num_rows($result) == 0) { | |
52 | print_error("pg_trgm extension not found."); | |
53 | } | |
54 | ||
55 | $similarity = $this->host->get($this, "similarity"); | |
56 | $min_title_length = $this->host->get($this, "min_title_length"); | |
57 | ||
58 | if (!$similarity) $similarity = '0.75'; | |
59 | if (!$min_title_length) $min_title_length = '32'; | |
60 | ||
61 | print "<form dojoType=\"dijit.form.Form\">"; | |
62 | ||
63 | print "<script type=\"dojo/method\" event=\"onSubmit\" args=\"evt\"> | |
64 | evt.preventDefault(); | |
65 | if (this.validate()) { | |
66 | console.log(dojo.objectToQuery(this.getValues())); | |
67 | new Ajax.Request('backend.php', { | |
68 | parameters: dojo.objectToQuery(this.getValues()), | |
69 | onComplete: function(transport) { | |
70 | notify_info(transport.responseText); | |
71 | } | |
72 | }); | |
73 | //this.reset(); | |
74 | } | |
75 | </script>"; | |
76 | ||
77 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"op\" value=\"pluginhandler\">"; | |
78 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"method\" value=\"save\">"; | |
79 | print "<input dojoType=\"dijit.form.TextBox\" style=\"display : none\" name=\"plugin\" value=\"af_psql_trgm\">"; | |
80 | ||
81 | 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."); | |
82 | ||
83 | print "<br/>"; | |
84 | print_notice("Only data in other feeds is checked, i.e. sequential duplicate posts in one feed will not be detected by this plugin."); | |
85 | ||
86 | print "<br/>"; | |
87 | print_notice("Enable the plugin for specific feeds in the feed editor."); | |
88 | ||
89 | print "<h3>" . __("Global settings") . "</h3>"; | |
90 | ||
91 | print "<table>"; | |
92 | ||
93 | print "<tr><td width=\"40%\">".__("Minimum similarity:")."</td>"; | |
94 | print "<td> | |
95 | <input dojoType=\"dijit.form.ValidationTextBox\" | |
96 | placeholder=\"0.75\" | |
97 | required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>"; | |
98 | print "<tr><td width=\"40%\">".__("Minimum title length:")."</td>"; | |
99 | print "<td> | |
100 | <input dojoType=\"dijit.form.ValidationTextBox\" | |
101 | placeholder=\"32\" | |
102 | required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>"; | |
103 | ||
104 | ||
105 | print "</table>"; | |
106 | ||
107 | print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">". | |
108 | __("Save")."</button>"; | |
109 | ||
110 | print "</form>"; | |
111 | ||
112 | print "</div>"; | |
113 | } | |
114 | ||
115 | //PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_EDIT_FEED, | |
116 | // "hook_prefs_edit_feed", $feed_id); | |
117 | // PluginHost::getInstance()->run_hooks(PluginHost::HOOK_PREFS_SAVE_FEED, | |
118 | // "hook_prefs_save_feed", $feed_id); | |
119 | ||
120 | function hook_prefs_edit_feed($feed_id) { | |
121 | print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>"; | |
122 | print "<div class=\"dlgSecCont\">"; | |
123 | ||
124 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
125 | if (!array($enabled_feeds)) $enabled_feeds = array(); | |
126 | ||
127 | $key = array_search($feed_id, $enabled_feeds); | |
128 | $checked = $key !== FALSE ? "checked" : ""; | |
129 | ||
130 | print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"trgm_similarity_enabled\" | |
131 | name=\"trgm_similarity_enabled\" | |
132 | $checked> <label for=\"trgm_similarity_enabled\">".__('Mark similar articles as read')."</label>"; | |
133 | ||
134 | print "</div>"; | |
135 | } | |
136 | ||
137 | function hook_prefs_save_feed($feed_id) { | |
138 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
139 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); | |
140 | ||
141 | $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true'; | |
142 | $key = array_search($feed_id, $enabled_feeds); | |
143 | ||
144 | if ($enable) { | |
145 | if ($key === FALSE) { | |
146 | array_push($enabled_feeds, $feed_id); | |
147 | } | |
148 | } else { | |
149 | if ($key !== FALSE) { | |
150 | unset($enabled_feeds[$key]); | |
151 | } | |
152 | } | |
153 | ||
154 | $this->host->set($this, "enabled_feeds", $enabled_feeds); | |
155 | } | |
156 | ||
157 | function hook_article_filter($article) { | |
158 | ||
159 | if (DB_TYPE != "pgsql") return $article; | |
160 | ||
161 | $result = db_query("select 'similarity'::regproc"); | |
162 | if (db_num_rows($result) == 0) return $article; | |
163 | ||
164 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
165 | $key = array_search($article["feed"]["id"], $enabled_feeds); | |
166 | if ($key === FALSE) return $article; | |
167 | ||
168 | $similarity = (float) $this->host->get($this, "similarity"); | |
169 | if ($similarity < 0.01) return $article; | |
170 | ||
171 | $min_title_length = (int) $this->host->get($this, "min_length"); | |
172 | if (mb_strlen($article["title"]) < $min_title_length) return $article; | |
173 | ||
174 | $owner_uid = $article["owner_uid"]; | |
175 | $feed_id = $article["feed"]["id"]; | |
176 | ||
177 | $title_escaped = db_escape_string($article["title"]); | |
178 | ||
179 | $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms | |
180 | FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND | |
181 | date_entered >= NOW() - interval '1 day' AND | |
182 | feed_id != $feed_id AND | |
183 | owner_uid = $owner_uid"); | |
184 | ||
185 | $similarity_result = db_fetch_result($result, 0, "ms"); | |
186 | ||
187 | //_debug("similarity result: $similarity_result"); | |
188 | ||
189 | if ($similarity_result >= $similarity) { | |
190 | $article["force_catchup"] = true; | |
191 | } | |
192 | ||
193 | return $article; | |
194 | ||
195 | } | |
196 | ||
197 | function api_version() { | |
198 | return 2; | |
199 | } | |
200 | ||
201 | } | |
202 | ?> |