summaryrefslogtreecommitdiff
path: root/plugins/af_psql_trgm/init.php
blob: 28a0ab933616847fd733528045c5ac025c7a6fff (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
<?php
class Af_Psql_Trgm extends Plugin {

	/** @var PluginHost $host */
	private $host;

	/** @var float */
	private $default_similarity = 0.75;

	/** @var int */
	private $default_min_length = 32;

	function about() {
		return array(null,
			"Marks similar articles as read (requires pg_trgm)",
			"fox");
	}

	/** @return void */
	function save() {
		$similarity = (float) $_POST["similarity"];
		$min_title_length = (int) $_POST["min_title_length"];
		$enable_globally = checkbox_to_sql_bool($_POST["enable_globally"] ?? "");

		if ($similarity < 0) $similarity = 0;
		if ($min_title_length < 0) $min_title_length = 0;

		$similarity = sprintf("%.2f", $similarity);

		$this->host->set($this, "similarity", $similarity);
		$this->host->set($this, "min_title_length", $min_title_length);
		$this->host->set($this, "enable_globally", $enable_globally);

		echo T_sprintf("Data saved (%s, %d)", $similarity, $enable_globally);
	}

	function init($host) {
		$this->host = $host;

		$host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
		$host->add_hook($host::HOOK_PREFS_TAB, $this);
		$host->add_hook($host::HOOK_PREFS_EDIT_FEED, $this);
		$host->add_hook($host::HOOK_PREFS_SAVE_FEED, $this);
		$host->add_hook($host::HOOK_ARTICLE_BUTTON, $this);
	}

	function get_js() {
		return file_get_contents(__DIR__ . "/init.js");
	}

	/** @return void */
	function showrelated() {
		$id = (int) $_REQUEST['id'];
		$owner_uid = $_SESSION["uid"];

		$sth = $this->pdo->prepare("SELECT title FROM ttrss_entries, ttrss_user_entries
			WHERE ref_id = id AND id = ? AND owner_uid = ?");
		$sth->execute([$id, $owner_uid]);

		if ($row = $sth->fetch()) {

			$title = $row['title'];

			print "<p>$title</p>";

			if (Config::get(Config::DB_TYPE) == "pgsql") {
				$sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id,
					feed_id,
					ttrss_entries.title AS title,
					updated, link,
					ttrss_feeds.title AS feed_title,
					SIMILARITY(ttrss_entries.title, :title) AS sm
				FROM
					ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id)
				WHERE
					ttrss_entries.id = ref_id AND
					ttrss_user_entries.owner_uid = :owner_uid AND
					ttrss_entries.id != :id AND
					date_entered >= NOW() - INTERVAL '2 weeks'
				ORDER BY
					sm DESC, date_entered DESC
				LIMIT 10");
			} else {
				$sth = $this->pdo->prepare("SELECT ttrss_entries.id AS id,
					feed_id,
					ttrss_entries.title AS title,
					updated, link,
					ttrss_feeds.title AS feed_title,
					(MATCH (ttrss_entries.title) AGAINST (:title) / LENGTH(ttrss_entries.title)) AS sm
				FROM
					ttrss_entries, ttrss_user_entries LEFT JOIN ttrss_feeds ON (ttrss_feeds.id = feed_id)
				WHERE
					ttrss_entries.id = ref_id AND
					ttrss_user_entries.owner_uid = :owner_uid AND
					ttrss_entries.id != :id AND
					date_entered >= DATE_SUB(NOW(), INTERVAL 2 WEEK)
				ORDER BY
					sm DESC, date_entered DESC
				LIMIT 10");
			}

			$sth->execute(['title' => $title, "owner_uid" => $owner_uid, "id" => $id]);

			print "<ul class='panel panel-scrollable'>";

			while ($line = $sth->fetch()) {
				print "<li style='display : flex'>";
				print "<i class='material-icons'>bookmark_outline</i>";

				$sm = sprintf("%.2f", $line['sm']);
				$article_link = htmlspecialchars($line["link"]);

				print "<div style='flex-grow : 2'>";

				print " <a target=\"_blank\" rel=\"noopener noreferrer\" href=\"$article_link\">".
					$line["title"]."</a>";

				print " (<a href=\"#\" onclick=\"Feeds.open({feed:".$line["feed_id"]."})\">".
					htmlspecialchars($line["feed_title"])."</a>)";

				print " &mdash; $sm";

				print "</div>";

				print "<div style='text-align : right' class='text-muted'>" . TimeHelper::smart_date_time(strtotime($line["updated"] ?? '')) . "</div>";

				print "</li>";
			}

			print "</ul>";

		}

		print "<footer class='text-center'>
			<button dojoType='dijit.form.Button' type='submit' class='alt-primary'>".__('Close this window')."</button>
		</footer>";


	}

	function hook_article_button($line) {
		return "<i style=\"cursor : pointer\" class='material-icons'
			onclick=\"Plugins.Psql_Trgm.showRelated(".$line["id"].")\"
			title=\"".__('Show related articles')."\">bookmark_outline</i>";
	}

	function hook_prefs_tab($args) {
		if ($args != "prefFeeds") return;

		$similarity = $this->host->get($this, "similarity", $this->default_similarity);
		$min_title_length = $this->host->get($this, "min_title_length", $this->default_min_length);
		$enable_globally = sql_bool_to_bool($this->host->get($this, "enable_globally"));

		?>

		<div dojoType="dijit.layout.AccordionPane"
			title="<i class='material-icons'>extension</i> <?= __('Mark similar articles as read (af_psql_trgm)') ?>">

			<?php
			if (Config::get(Config::DB_TYPE) == "pgsql") {
				$res = $this->pdo->query("select 'similarity'::regproc");

				if (!$res || !$res->fetch()) {
					print_error("pg_trgm extension not found.");
				}
			} ?>

			<form dojoType="dijit.form.Form">

				<?= \Controls\pluginhandler_tags($this, "save") ?>

				<script type="dojo/method" event="onSubmit" args="evt">
					evt.preventDefault();
					if (this.validate()) {
						Notify.progress('Saving data...', true);
						xhr.post("backend.php", this.getValues(), (reply) => {
							Notify.info(reply);
						})
					}
				</script>

				<?= format_notice("Enable for specific feeds in the feed editor.") ?>

				<fieldset>
					<label><?= __("Minimum similarity:") ?></label>
					<input dojoType="dijit.form.NumberSpinner"
						placeholder="<?= $this->default_similarity ?>"
						id='psql_trgm_similarity'
						required="1"
						name="similarity" value="<?= htmlspecialchars($similarity) ?>">

					<div dojoType='dijit.Tooltip' connectId='psql_trgm_similarity' position='below'>
						<?php if (Config::get(Config::DB_TYPE) == "pgsql") { ?>
							<?= __("PostgreSQL trigram extension returns string similarity as a floating point number (0-1). Setting it too low might produce false positives, zero disables checking.") ?>
						<?php } else { ?>
							<?= __("Setting this value too low might produce false positives, zero disables checking.") ?>
						<?php } ?>
					</div>
				</fieldset>

				<fieldset>
					<label><?= __("Minimum title length:") ?></label>
					<input dojoType="dijit.form.NumberSpinner"
						placeholder="<?= $this->default_min_length ?>"
						required="1"
						name="min_title_length" value="<?= htmlspecialchars($min_title_length) ?>">
				</fieldset>

				<fieldset>
					<label class='checkbox'>
						<?= \Controls\checkbox_tag("enable_globally", $enable_globally) ?>
						<?= __("Enable for all feeds.") ?>
					</label>
				</fieldset>

				<hr/>

				<?= \Controls\submit_tag(__("Save")) ?>
			</form>

			<?php
				/* cleanup */
				$enabled_feeds = $this->filter_unknown_feeds(
					$this->host->get_array($this, "enabled_feeds"));

				$this->host->set($this, "enabled_feeds", $enabled_feeds);
			?>

			<?php	if (count($enabled_feeds) > 0) {	?>
				<hr/>
				<h3><?= __("Currently enabled for (click to edit):") ?></h3>

				<ul class="panel panel-scrollable list list-unstyled">
					<?php foreach ($enabled_feeds as $f) { ?>
						<li>
							<i class='material-icons'>rss_feed</i>
							<a href='#'	onclick="CommonDialogs.editFeed(<?= $f ?>)">
									<?= Feeds::_get_title($f) ?>
							</a>
						</li>
					<?php } ?>
				</ul>
			<?php	} ?>
		</div>
		<?php
	}

	function hook_prefs_edit_feed($feed_id) {
			$enabled_feeds = $this->host->get_array($this, "enabled_feeds");
		?>
			<header><?= __("Similarity (af_psql_trgm)") ?></header>

			<section>
				<fieldset>
					<label class="checkbox">
						<?= \Controls\checkbox_tag("trgm_similarity_enabled", in_array($feed_id, $enabled_feeds)) ?>
						<?= __('Mark similar articles as read') ?>
					</label>
				</fieldset>
			</section>
		</section>
		<?php
	}

	function hook_prefs_save_feed($feed_id) {
		$enabled_feeds = $this->host->get_array($this, "enabled_feeds");

		$enable = checkbox_to_sql_bool($_POST["trgm_similarity_enabled"] ?? "");
		$key = array_search($feed_id, $enabled_feeds);

		if ($enable) {
			if ($key === false) {
				array_push($enabled_feeds, $feed_id);
			}
		} else {
			if ($key !== false) {
				unset($enabled_feeds[$key]);
			}
		}

		$this->host->set($this, "enabled_feeds", $enabled_feeds);
	}

	function hook_article_filter($article) {

		if (Config::get(Config::DB_TYPE) == "pgsql") {
			$res = $this->pdo->query("select 'similarity'::regproc");
			if (!$res || !$res->fetch()) return $article;
		}

		$enable_globally = $this->host->get($this, "enable_globally");

		if (!$enable_globally &&
				!in_array($article["feed"]["id"],
					$this->host->get_array($this,"enabled_feeds"))) {

			return $article;
		}

		$similarity = (float) $this->host->get($this, "similarity", $this->default_similarity);

		if ($similarity < 0.01) {
			Debug::log("af_psql_trgm: similarity is set too low ($similarity)", Debug::LOG_EXTENDED);
			return $article;
		}

		$min_title_length = (int) $this->host->get($this, "min_title_length", $this->default_min_length);

		if (mb_strlen($article["title"]) < $min_title_length) {
			Debug::log("af_psql_trgm: article title is too short (min: $min_title_length)", Debug::LOG_EXTENDED);
			return $article;
		}

		$owner_uid = $article["owner_uid"];
		$entry_guid = $article["guid_hashed"];
		$title_escaped = $article["title"];

		// trgm does not return similarity=1 for completely equal strings
		// this seems to be no longer the case (fixed in upstream?)

		/* $sth = $this->pdo->prepare("SELECT COUNT(id) AS nequal
		  FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
		  date_entered >= NOW() - interval '3 days' AND
		  title = ? AND
		  guid != ? AND
		  owner_uid = ?");
		$sth->execute([$title_escaped, $entry_guid, $owner_uid]);

		$row = $sth->fetch();
		$nequal = $row['nequal'];

		Debug::log("af_psql_trgm: num equals: $nequal", Debug::$LOG_EXTENDED);

		if ($nequal != 0) {
			$article["force_catchup"] = true;
			return $article;
		} */

		if (Config::get(Config::DB_TYPE) == "pgsql") {
			$sth = $this->pdo->prepare("SELECT MAX(SIMILARITY(title, :title)) AS ms
				FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
				date_entered >= NOW() - interval '1 day' AND
				guid != :guid AND
				owner_uid = :uid");
		} else {
			$sth = $this->pdo->prepare("SELECT (MATCH(title) AGAINST (:title) / LENGTH(title)) AS ms
				FROM ttrss_entries, ttrss_user_entries WHERE ref_id = id AND
					date_entered >= DATE_SUB(NOW(), INTERVAL 1 DAY) AND
					guid != :guid AND
					owner_uid = :uid
					ORDER BY ms DESC
					LIMIT 1");
		}

		$sth->execute(['title' => $title_escaped, 'guid' => $entry_guid, 'uid' => $owner_uid]);

		$row = $sth->fetch();
		$similarity_result = $row['ms'];

		Debug::log("af_psql_trgm: similarity result for $title_escaped: $similarity_result", Debug::LOG_EXTENDED);

		if ($similarity_result >= $similarity) {
			Debug::log("af_psql_trgm: marking article as read ($similarity_result >= $similarity)", Debug::LOG_EXTENDED);

			$article["force_catchup"] = true;
		}

		return $article;
	}

	function api_version() {
		return 2;
	}

	/**
	 * @param array<int> $enabled_feeds
	 * @return array<int>
	 * @throws PDOException
	 */
	private function filter_unknown_feeds($enabled_feeds) : array {
		$tmp = array();

		foreach ($enabled_feeds as $feed) {

			$sth = $this->pdo->prepare("SELECT id FROM ttrss_feeds WHERE id = ? AND owner_uid = ?");
			$sth->execute([$feed, $_SESSION['uid']]);

			if ($sth->fetch()) {
				array_push($tmp, $feed);
			}
		}

		return $tmp;
	}
}