init.php 1.3 KB

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