]> git.wh0rd.org - tt-rss.git/blame - plugins/auto_assign_labels/init.php
plugins/auto_assign_labels: use PDO
[tt-rss.git] / plugins / auto_assign_labels / init.php
CommitLineData
a29fe121
AD
1<?php
2class Auto_Assign_Labels extends Plugin {
3
d8a924d9 4 /* @var PluginHost $host */
a29fe121
AD
5 private $host;
6
7 function about() {
8 return array(1.0,
9 "Assign labels automatically based on article title, content, and tags",
10 "fox");
11 }
12
13 function init($host) {
14 $this->host = $host;
15
16 $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
17 }
18
19 function get_all_labels_filter_format($owner_uid) {
20 $rv = array();
21
d8a924d9
AD
22 $sth = $this->pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = ?");
23 $sth->execute([$owner_uid]);
a29fe121 24
d8a924d9 25 while ($line = $sth->fetch()) {
7c9b5a3f 26 array_push($rv, array(Labels::label_to_feed_id($line["id"]),
a29fe121
AD
27 $line["caption"], $line["fg_color"], $line["bg_color"]));
28 }
29
30 return $rv;
31 }
32
33
34 function hook_article_filter($article) {
35
36 $owner_uid = $article["owner_uid"];
37 $labels = $this->get_all_labels_filter_format($owner_uid);
38 $tags_str = join(",", $article["tags"]);
39
40 foreach ($labels as $label) {
41 $caption = preg_quote($label[1]);
42
43 if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($article["content"]) . " " . $article["title"])) {
44
e6c886bf 45 if (!RSSUtils::labels_contains_caption($article["labels"], $caption)) {
a29fe121
AD
46 array_push($article["labels"], $label);
47 }
48 }
49 }
50
51 return $article;
52 }
53
54 function api_version() {
55 return 2;
56 }
21ce7d9e 57}