]> git.wh0rd.org - tt-rss.git/blobdiff - include/rssfuncs.php
add ability to auto-assign articles to labels (bump schema)
[tt-rss.git] / include / rssfuncs.php
index 14fa5f34838640c5de79e435fadd53f923eb3987..1e2feb3cba46ed611cdcc548ee35a0bf9d7e3723 100644 (file)
                        }
 
                        if ($debug_enabled) {
-                               _debug("update_rss_feed: loading filters...");
+                               _debug("update_rss_feed: loading filters & labels...");
                        }
 
                        $filters = load_filters($link, $feed, $owner_uid);
+                       $labels = get_all_labels($link, $owner_uid);
 
                        if ($debug_enabled) {
                                //print_r($filters);
                                        //print_r($entry_tags);
                                }
 
-                               # sanitize content
-
-                               $entry_content = sanitize_article_content($entry_content);
-                               $entry_title = sanitize_article_content($entry_title);
-
                                if ($debug_enabled) {
                                        _debug("update_rss_feed: done collecting data [TITLE:$entry_title]");
                                }
                                                }
                                        }
 
+                                       $article_labels = get_article_labels($link, $entry_ref_id);
+
                                        if (find_article_filter($article_filters, "filter")) {
                                                db_query($link, "COMMIT"); // close transaction in progress
                                                continue;
                                        _debug("update_rss_feed: assigning labels...");
                                }
 
-                               assign_article_to_labels($link, $entry_ref_id, $article_filters,
-                                       $owner_uid);
+                               assign_article_to_label_filters($link, $entry_ref_id, $article_filters,
+                                       $owner_uid, $article_labels);
 
                                if ($debug_enabled) {
                                        _debug("update_rss_feed: looking for enclosures...");
                                        db_query($link, "COMMIT");
                                }
 
+                               if (get_pref($link, "AUTO_ASSIGN_LABELS", $owner_uid, false)) {
+                                       if ($debug_enabled) {
+                                               _debug("update_rss_feed: auto-assigning labels...");
+                                       }
+
+                                       foreach ($labels as $label) {
+                                               $caption = $label["caption"];
+
+                                               if (preg_match("/\b$caption\b/i", "$tags_str $entry_content $entry_title")) {
+                                                       if (!labels_contains_caption($article_labels, $caption)) {
+                                                               label_add_article($link, $entry_ref_id, $caption, $owner_uid);
+                                                       }
+                                               }
+                                       }
+                               }
+
                                if ($debug_enabled) {
                                        _debug("update_rss_feed: article processed");
                                }
 
                return $params;
        }
+
+       function get_article_filters($filters, $title, $content, $link, $timestamp, $author, $tags) {
+               $matches = array();
+
+               foreach ($filters as $filter) {
+                       $match_any_rule = $filter["match_any_rule"];
+                       $filter_match = false;
+
+                       foreach ($filter["rules"] as $rule) {
+                               $match = false;
+                               $reg_exp = $rule["reg_exp"];
+
+                               if (!$reg_exp)
+                                       continue;
+
+                               switch ($rule["type"]) {
+                               case "title":
+                                       $match = @preg_match("/$reg_exp/i", $title);
+                                       break;
+                               case "content":
+                                       $match = @preg_match("/$reg_exp/i", $content);
+                                       break;
+                               case "both":
+                                       $match = (@preg_match("/$reg_exp/i", $title) || @preg_match("/$reg_exp/i", $title));
+                                       break;
+                               case "link":
+                                       $match = @preg_match("/$reg_exp/i", $link);
+                                       break;
+                               case "author":
+                                       $match = @preg_match("/$reg_exp/i", $author);
+                                       break;
+                               case "tag":
+                                       $tag_string = join(",", $tags);
+                                       $match = @preg_match("/$reg_exp/i", $tag_string);
+                                       break;
+                               }
+
+                               if ($match_any_rule) {
+                                       if ($match) {
+                                               $filter_match = true;
+                                               break;
+                                       }
+                               } else {
+                                       $filter_match = $match;
+                                       if (!$match) {
+                                               break;
+                                       }
+                               }
+                       }
+
+                       if ($filter_match) {
+                               foreach ($filter["actions"] AS $action) {
+                                       array_push($matches, $action);
+                               }
+                       }
+               }
+
+               return $matches;
+       }
+
+       function find_article_filter($filters, $filter_name) {
+               foreach ($filters as $f) {
+                       if ($f["type"] == $filter_name) {
+                               return $f;
+                       };
+               }
+               return false;
+       }
+
+       function find_article_filters($filters, $filter_name) {
+               $results = array();
+
+               foreach ($filters as $f) {
+                       if ($f["type"] == $filter_name) {
+                               array_push($results, $f);
+                       };
+               }
+               return $results;
+       }
+
+       function calculate_article_score($filters) {
+               $score = 0;
+
+               foreach ($filters as $f) {
+                       if ($f["type"] == "score") {
+                               $score += $f["param"];
+                       };
+               }
+               return $score;
+       }
+
+       function labels_contains_caption($labels, $caption) {
+               foreach ($labels as $label) {
+                       if ($label[1] == $caption) {
+                               return true;
+                       }
+               }
+
+               return false;
+       }
+
+       function assign_article_to_label_filters($link, $id, $filters, $owner_uid, $article_labels) {
+               foreach ($filters as $f) {
+                       if ($f["type"] == "label") {
+                               if (!labels_contains_caption($article_labels, $f["param"])) {
+                                       label_add_article($link, $id, $f["param"], $owner_uid);
+                               }
+                       }
+               }
+       }
 ?>