init.php 1.3 KB

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