]>
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 | ||
117efb6f AD |
83 | print "<br/>"; |
84 | print_notice("Enable the plugin for specific feeds in the feed editor."); | |
85 | ||
86 | print "<h3>" . __("Global settings") . "</h3>"; | |
87 | ||
88 | print "<table>"; | |
89 | ||
90 | print "<tr><td width=\"40%\">".__("Minimum similarity:")."</td>"; | |
91 | print "<td> | |
92 | <input dojoType=\"dijit.form.ValidationTextBox\" | |
93 | placeholder=\"0.75\" | |
94 | required=\"1\" name=\"similarity\" value=\"$similarity\"></td></tr>"; | |
95 | print "<tr><td width=\"40%\">".__("Minimum title length:")."</td>"; | |
96 | print "<td> | |
97 | <input dojoType=\"dijit.form.ValidationTextBox\" | |
98 | placeholder=\"32\" | |
99 | required=\"1\" name=\"min_title_length\" value=\"$min_title_length\"></td></tr>"; | |
100 | ||
101 | ||
102 | print "</table>"; | |
103 | ||
104 | print "<p><button dojoType=\"dijit.form.Button\" type=\"submit\">". | |
105 | __("Save")."</button>"; | |
106 | ||
107 | print "</form>"; | |
108 | ||
109 | print "</div>"; | |
110 | } | |
111 | ||
117efb6f AD |
112 | function hook_prefs_edit_feed($feed_id) { |
113 | print "<div class=\"dlgSec\">".__("Similarity (pg_trgm)")."</div>"; | |
114 | print "<div class=\"dlgSecCont\">"; | |
115 | ||
116 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
117 | if (!array($enabled_feeds)) $enabled_feeds = array(); | |
118 | ||
119 | $key = array_search($feed_id, $enabled_feeds); | |
120 | $checked = $key !== FALSE ? "checked" : ""; | |
121 | ||
122 | print "<hr/><input dojoType=\"dijit.form.CheckBox\" type=\"checkbox\" id=\"trgm_similarity_enabled\" | |
123 | name=\"trgm_similarity_enabled\" | |
124 | $checked> <label for=\"trgm_similarity_enabled\">".__('Mark similar articles as read')."</label>"; | |
125 | ||
126 | print "</div>"; | |
127 | } | |
128 | ||
129 | function hook_prefs_save_feed($feed_id) { | |
130 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
131 | if (!is_array($enabled_feeds)) $enabled_feeds = array(); | |
132 | ||
133 | $enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"]) == 'true'; | |
134 | $key = array_search($feed_id, $enabled_feeds); | |
135 | ||
136 | if ($enable) { | |
137 | if ($key === FALSE) { | |
138 | array_push($enabled_feeds, $feed_id); | |
139 | } | |
140 | } else { | |
141 | if ($key !== FALSE) { | |
142 | unset($enabled_feeds[$key]); | |
143 | } | |
144 | } | |
145 | ||
146 | $this->host->set($this, "enabled_feeds", $enabled_feeds); | |
147 | } | |
148 | ||
149 | function hook_article_filter($article) { | |
150 | ||
151 | if (DB_TYPE != "pgsql") return $article; | |
152 | ||
153 | $result = db_query("select 'similarity'::regproc"); | |
154 | if (db_num_rows($result) == 0) return $article; | |
155 | ||
156 | $enabled_feeds = $this->host->get($this, "enabled_feeds"); | |
157 | $key = array_search($article["feed"]["id"], $enabled_feeds); | |
158 | if ($key === FALSE) return $article; | |
159 | ||
160 | $similarity = (float) $this->host->get($this, "similarity"); | |
161 | if ($similarity < 0.01) return $article; | |
162 | ||
163 | $min_title_length = (int) $this->host->get($this, "min_length"); | |
164 | if (mb_strlen($article["title"]) < $min_title_length) return $article; | |
165 | ||
166 | $owner_uid = $article["owner_uid"]; | |
167 | $feed_id = $article["feed"]["id"]; | |
168 | ||
169 | $title_escaped = db_escape_string($article["title"]); | |
170 | ||
171 | $result = db_query("SELECT MAX(SIMILARITY(title, '$title_escaped')) AS ms | |
172 | FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND | |
173 | date_entered >= NOW() - interval '1 day' AND | |
117efb6f AD |
174 | owner_uid = $owner_uid"); |
175 | ||
176 | $similarity_result = db_fetch_result($result, 0, "ms"); | |
177 | ||
178 | //_debug("similarity result: $similarity_result"); | |
179 | ||
180 | if ($similarity_result >= $similarity) { | |
181 | $article["force_catchup"] = true; | |
182 | } | |
183 | ||
184 | return $article; | |
185 | ||
186 | } | |
187 | ||
188 | function api_version() { | |
189 | return 2; | |
190 | } | |
191 | ||
192 | } | |
193 | ?> |