]> git.wh0rd.org - tt-rss.git/commitdiff
add auto_assign_labels plugin; allow article filter plugins to add labels to articles
authorAndrew Dolgov <noreply@fakecake.org>
Tue, 3 Mar 2015 21:26:52 +0000 (00:26 +0300)
committerAndrew Dolgov <noreply@fakecake.org>
Tue, 3 Mar 2015 21:26:52 +0000 (00:26 +0300)
include/rssfuncs.php
plugins/auto_assign_labels/init.php [new file with mode: 0644]

index b285f74c0cf6694ec081a54a159ee4939b56333b..1a26e0485a3dac320df9fed0fd533d6681ab513e 100644 (file)
                                if (db_num_rows($result) != 0) {
                                        $base_entry_id = db_fetch_result($result, 0, "id");
                                        $entry_stored_hash = db_fetch_result($result, 0, "content_hash");
+                                       $article_labels = get_article_labels($base_entry_id, $owner_uid);
                                } else {
                                        $base_entry_id = false;
                                        $entry_stored_hash = "";
+                                       $article_labels = array();
                                }
 
                                $article = array("owner_uid" => $owner_uid, // read only
                                        "title" => $entry_title,
                                        "content" => $entry_content,
                                        "link" => $entry_link,
+                                       "labels" => $article_labels, // current limitation: can add labels to article, can't remove them
                                        "tags" => $entry_tags,
                                        "author" => $entry_author,
                                        "force_catchup" => false, // ugly hack for the time being
                                $entry_link = db_escape_string($article["link"]);
                                $entry_content = $article["content"]; // escaped below
                                $entry_force_catchup = $article["force_catchup"];
+                               $article_labels = $article["labels"];
+
+                               if ($debug_enabled) {
+                                       _debug("article labels:", $debug_enabled);
+                                       print_r($article_labels);
+                               }
 
                                _debug("force catchup: $entry_force_catchup");
 
                                                        '$entry_language',
                                                        '$entry_author')");
 
-                                       $article_labels = array();
-
                                } else {
                                        $base_entry_id = db_fetch_result($result, 0, "id");
-
-                                       $article_labels = get_article_labels($base_entry_id, $owner_uid);
                                }
 
                                // now it should exist, if not - bad luck then
 
                                db_query("COMMIT");
 
-                               _debug("assigning labels...", $debug_enabled);
+                               _debug("assigning labels [other]...", $debug_enabled);
+
+                               foreach ($article_labels as $label) {
+                                       label_add_article($entry_ref_id, $label[1], $owner_uid);
+                               }
+
+                               _debug("assigning labels [filters]...", $debug_enabled);
 
                                assign_article_to_label_filters($entry_ref_id, $article_filters,
                                        $owner_uid, $article_labels);
diff --git a/plugins/auto_assign_labels/init.php b/plugins/auto_assign_labels/init.php
new file mode 100644 (file)
index 0000000..36f7c32
--- /dev/null
@@ -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;
+       }
+}
+?>