2015-03-03 21:26:52 +00:00
|
|
|
<?php
|
|
|
|
class Auto_Assign_Labels extends Plugin {
|
|
|
|
|
2017-12-03 07:46:00 +00:00
|
|
|
/* @var PluginHost $host */
|
2015-03-03 21:26:52 +00:00
|
|
|
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();
|
|
|
|
|
2021-02-21 06:35:07 +00:00
|
|
|
// TODO: use Labels::get_all()
|
2017-12-03 07:46:00 +00:00
|
|
|
$sth = $this->pdo->prepare("SELECT id, fg_color, bg_color, caption FROM ttrss_labels2 WHERE owner_uid = ?");
|
|
|
|
$sth->execute([$owner_uid]);
|
2015-03-03 21:26:52 +00:00
|
|
|
|
2017-12-03 07:46:00 +00:00
|
|
|
while ($line = $sth->fetch()) {
|
2017-05-04 12:57:40 +00:00
|
|
|
array_push($rv, array(Labels::label_to_feed_id($line["id"]),
|
2015-03-03 21:26:52 +00:00
|
|
|
$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) {
|
2018-02-23 10:31:08 +00:00
|
|
|
$caption = preg_quote($label[1], "/");
|
2015-03-03 21:26:52 +00:00
|
|
|
|
|
|
|
if ($caption && preg_match("/\b$caption\b/i", "$tags_str " . strip_tags($article["content"]) . " " . $article["title"])) {
|
|
|
|
|
2017-05-05 15:10:07 +00:00
|
|
|
if (!RSSUtils::labels_contains_caption($article["labels"], $caption)) {
|
2015-03-03 21:26:52 +00:00
|
|
|
array_push($article["labels"], $label);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $article;
|
|
|
|
}
|
|
|
|
|
|
|
|
function api_version() {
|
|
|
|
return 2;
|
|
|
|
}
|
2018-02-23 10:31:08 +00:00
|
|
|
}
|