]>
Commit | Line | Data |
---|---|---|
a29fe121 AD |
1 | <?php |
2 | class Auto_Assign_Labels extends Plugin { | |
3 | ||
4 | private $host; | |
5 | ||
6 | function about() { | |
7 | return array(1.0, | |
8 | "Assign labels automatically based on article title, content, and tags", | |
9 | "fox"); | |
10 | } | |
11 | ||
12 | function init($host) { | |
13 | $this->host = $host; | |
14 | ||
15 | $host->add_hook($host::HOOK_ARTICLE_FILTER, $this); | |
16 | } | |
17 | ||
18 | function get_all_labels_filter_format($owner_uid) { | |
19 | $rv = array(); | |
20 | ||
21 | $result = db_query("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid); | |
22 | ||
23 | while ($line = db_fetch_assoc($result)) { | |
24 | array_push($rv, array(label_to_feed_id($line["id"]), | |
25 | $line["caption"], $line["fg_color"], $line["bg_color"])); | |
26 | } | |
27 | ||
28 | return $rv; | |
29 | } | |
30 | ||
31 | ||
32 | function hook_article_filter($article) { | |
33 | ||
34 | $owner_uid = $article["owner_uid"]; | |
35 | $labels = $this->get_all_labels_filter_format($owner_uid); | |
36 | $tags_str = join(",", $article["tags"]); | |
37 | ||
38 | foreach ($labels as $label) { | |
39 | $caption = preg_quote($label[1]); | |
40 | ||
41 | if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($article["content"]) . " " . $article["title"])) { | |
42 | ||
43 | # defined in rssfuncs.php | |
44 | if (!labels_contains_caption($article["labels"], $caption)) { | |
45 | array_push($article["labels"], $label); | |
46 | } | |
47 | } | |
48 | } | |
49 | ||
50 | return $article; | |
51 | } | |
52 | ||
53 | function api_version() { | |
54 | return 2; | |
55 | } | |
56 | } | |
57 | ?> |