2015-10-11 14:54:21 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Wallabag\Helper;
|
2015-10-11 14:54:21 +00:00
|
|
|
|
2023-08-08 01:27:21 +00:00
|
|
|
use Doctrine\Common\Collections\ArrayCollection;
|
2017-07-01 07:52:38 +00:00
|
|
|
use Psr\Log\LoggerInterface;
|
2015-10-11 14:54:21 +00:00
|
|
|
use RulerZ\RulerZ;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\Entry;
|
|
|
|
use Wallabag\Entity\Tag;
|
|
|
|
use Wallabag\Entity\TaggingRule;
|
|
|
|
use Wallabag\Entity\User;
|
|
|
|
use Wallabag\Repository\EntryRepository;
|
|
|
|
use Wallabag\Repository\TagRepository;
|
2015-10-11 14:54:21 +00:00
|
|
|
|
|
|
|
class RuleBasedTagger
|
|
|
|
{
|
|
|
|
private $rulerz;
|
|
|
|
private $tagRepository;
|
2015-10-24 13:11:06 +00:00
|
|
|
private $entryRepository;
|
2017-05-30 09:39:15 +00:00
|
|
|
private $logger;
|
2015-10-11 14:54:21 +00:00
|
|
|
|
2017-05-12 11:13:19 +00:00
|
|
|
public function __construct(RulerZ $rulerz, TagRepository $tagRepository, EntryRepository $entryRepository, LoggerInterface $logger)
|
2015-10-11 14:54:21 +00:00
|
|
|
{
|
2015-12-08 08:20:03 +00:00
|
|
|
$this->rulerz = $rulerz;
|
|
|
|
$this->tagRepository = $tagRepository;
|
2015-10-24 13:11:06 +00:00
|
|
|
$this->entryRepository = $entryRepository;
|
2017-05-12 11:13:19 +00:00
|
|
|
$this->logger = $logger;
|
2015-10-11 14:54:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add tags from rules defined by the user.
|
|
|
|
*
|
2016-08-17 12:18:28 +00:00
|
|
|
* @param Entry $entry Entry to tag
|
2015-10-11 14:54:21 +00:00
|
|
|
*/
|
|
|
|
public function tag(Entry $entry)
|
|
|
|
{
|
|
|
|
$rules = $this->getRulesForUser($entry->getUser());
|
|
|
|
|
2022-03-02 18:11:32 +00:00
|
|
|
$clonedEntry = $this->fixEntry($entry);
|
|
|
|
|
2015-10-11 14:54:21 +00:00
|
|
|
foreach ($rules as $rule) {
|
2022-03-02 18:11:32 +00:00
|
|
|
if (!$this->rulerz->satisfies($clonedEntry, $rule->getRule())) {
|
2015-10-11 14:54:21 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-05-12 11:13:19 +00:00
|
|
|
$this->logger->info('Matching rule.', [
|
|
|
|
'rule' => $rule->getRule(),
|
|
|
|
'tags' => $rule->getTags(),
|
|
|
|
]);
|
|
|
|
|
2015-10-11 15:14:50 +00:00
|
|
|
foreach ($rule->getTags() as $label) {
|
2015-12-29 13:50:52 +00:00
|
|
|
$tag = $this->getTag($label);
|
2015-10-11 14:54:21 +00:00
|
|
|
|
|
|
|
$entry->addTag($tag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-24 13:11:06 +00:00
|
|
|
/**
|
|
|
|
* Apply all the tagging rules defined by a user on its entries.
|
|
|
|
*
|
2016-08-17 12:18:28 +00:00
|
|
|
* @return array<Entry> A list of modified entries
|
2015-10-24 13:11:06 +00:00
|
|
|
*/
|
|
|
|
public function tagAllForUser(User $user)
|
|
|
|
{
|
2015-12-08 08:20:03 +00:00
|
|
|
$rules = $this->getRulesForUser($user);
|
2022-03-02 18:11:32 +00:00
|
|
|
$entriesToUpdate = [];
|
2016-10-09 16:32:17 +00:00
|
|
|
$tagsCache = [];
|
2015-10-24 13:11:06 +00:00
|
|
|
|
2022-03-02 18:11:32 +00:00
|
|
|
$entries = $this->entryRepository
|
|
|
|
->getBuilderForAllByUser($user->getId())
|
|
|
|
->getQuery()
|
|
|
|
->getResult();
|
|
|
|
|
|
|
|
foreach ($entries as $entry) {
|
|
|
|
$clonedEntry = $this->fixEntry($entry);
|
|
|
|
|
|
|
|
foreach ($rules as $rule) {
|
|
|
|
if (!$this->rulerz->satisfies($clonedEntry, $rule->getRule())) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-10-24 13:11:06 +00:00
|
|
|
|
|
|
|
foreach ($rule->getTags() as $label) {
|
2016-10-09 16:32:17 +00:00
|
|
|
// avoid new tag duplicate by manually caching them
|
|
|
|
if (!isset($tagsCache[$label])) {
|
|
|
|
$tagsCache[$label] = $this->getTag($label);
|
|
|
|
}
|
|
|
|
|
|
|
|
$tag = $tagsCache[$label];
|
2015-10-24 13:11:06 +00:00
|
|
|
|
|
|
|
$entry->addTag($tag);
|
2022-03-02 18:11:32 +00:00
|
|
|
|
|
|
|
$entriesToUpdate[] = $entry;
|
2015-10-24 13:11:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-02 18:11:32 +00:00
|
|
|
return $entriesToUpdate;
|
2015-10-24 13:11:06 +00:00
|
|
|
}
|
|
|
|
|
2015-10-11 14:54:21 +00:00
|
|
|
/**
|
2015-12-29 13:50:52 +00:00
|
|
|
* Fetch a tag.
|
2015-10-11 14:54:21 +00:00
|
|
|
*
|
2016-08-17 12:18:28 +00:00
|
|
|
* @param string $label The tag's label
|
2015-10-11 14:54:21 +00:00
|
|
|
*
|
|
|
|
* @return Tag
|
|
|
|
*/
|
2015-12-29 13:50:52 +00:00
|
|
|
private function getTag($label)
|
2015-10-11 14:54:21 +00:00
|
|
|
{
|
2021-03-06 12:34:36 +00:00
|
|
|
$label = mb_convert_case($label, \MB_CASE_LOWER);
|
2015-12-29 13:50:52 +00:00
|
|
|
$tag = $this->tagRepository->findOneByLabel($label);
|
2015-10-11 14:54:21 +00:00
|
|
|
|
|
|
|
if (!$tag) {
|
2015-12-29 13:50:52 +00:00
|
|
|
$tag = new Tag();
|
2015-10-11 14:54:21 +00:00
|
|
|
$tag->setLabel($label);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tag;
|
|
|
|
}
|
|
|
|
|
2015-10-11 15:14:50 +00:00
|
|
|
/**
|
|
|
|
* Retrieves the tagging rules for a given user.
|
|
|
|
*
|
2023-08-08 01:27:21 +00:00
|
|
|
* @return ArrayCollection<TaggingRule>
|
2015-10-11 15:14:50 +00:00
|
|
|
*/
|
2015-10-11 14:54:21 +00:00
|
|
|
private function getRulesForUser(User $user)
|
|
|
|
{
|
2015-10-11 15:14:50 +00:00
|
|
|
return $user->getConfig()->getTaggingRules();
|
2015-10-11 14:54:21 +00:00
|
|
|
}
|
2022-03-02 18:11:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Update reading time on the fly to match the proper words per minute from the user.
|
|
|
|
*/
|
|
|
|
private function fixEntry(Entry $entry)
|
|
|
|
{
|
|
|
|
$clonedEntry = clone $entry;
|
|
|
|
$clonedEntry->setReadingTime($entry->getReadingTime() / $entry->getUser()->getConfig()->getReadingSpeed() * 200);
|
|
|
|
|
|
|
|
return $clonedEntry;
|
|
|
|
}
|
2015-10-11 14:54:21 +00:00
|
|
|
}
|