summaryrefslogtreecommitdiff
path: root/plugins/auto_assign_labels
diff options
context:
space:
mode:
authorAndrew Dolgov <[email protected]>2015-03-04 00:26:52 +0300
committerAndrew Dolgov <[email protected]>2015-03-04 00:26:52 +0300
commita29fe121954e08bd5f79d0a1f3ba0905d86112bb (patch)
tree8447c5206d957a897196a55097b806170f02fb23 /plugins/auto_assign_labels
parent0c6f7b314ae663f1f9fc8ade53b9914f46e02954 (diff)
add auto_assign_labels plugin; allow article filter plugins to add labels to articles
Diffstat (limited to 'plugins/auto_assign_labels')
-rw-r--r--plugins/auto_assign_labels/init.php57
1 files changed, 57 insertions, 0 deletions
diff --git a/plugins/auto_assign_labels/init.php b/plugins/auto_assign_labels/init.php
new file mode 100644
index 000000000..36f7c3267
--- /dev/null
+++ b/plugins/auto_assign_labels/init.php
@@ -0,0 +1,57 @@
+<?php
+class Auto_Assign_Labels extends Plugin {
+
+ private $host;
+
+ function about() {
+ return array(1.0,
+ "Assign labels automatically based on article title, content, and tags",
+ "fox");
+ }
+
+ function init($host) {
+ $this->host = $host;
+
+ $host->add_hook($host::HOOK_ARTICLE_FILTER, $this);
+ }
+
+ function get_all_labels_filter_format($owner_uid) {
+ $rv = array();
+
+ $result = db_query("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = " . $owner_uid);
+
+ while ($line = db_fetch_assoc($result)) {
+ array_push($rv, array(label_to_feed_id($line["id"]),
+ $line["caption"], $line["fg_color"], $line["bg_color"]));
+ }
+
+ return $rv;
+ }
+
+
+ function hook_article_filter($article) {
+
+ $owner_uid = $article["owner_uid"];
+ $labels = $this->get_all_labels_filter_format($owner_uid);
+ $tags_str = join(",", $article["tags"]);
+
+ foreach ($labels as $label) {
+ $caption = preg_quote($label[1]);
+
+ if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($article["content"]) . " " . $article["title"])) {
+
+ # defined in rssfuncs.php
+ if (!labels_contains_caption($article["labels"], $caption)) {
+ array_push($article["labels"], $label);
+ }
+ }
+ }
+
+ return $article;
+ }
+
+ function api_version() {
+ return 2;
+ }
+}
+?>