1
0
Fork 0
mirror of https://github.com/wallabag/wallabag.git synced 2025-07-22 17:18:37 +00:00

PoC of rule-based tagging

This commit is contained in:
Kévin Gomez 2015-10-11 16:54:21 +02:00
parent 0a0c600887
commit c3510620ad
6 changed files with 855 additions and 30 deletions

View file

@ -13,10 +13,12 @@ use Wallabag\CoreBundle\Tools\Utils;
class ContentProxy
{
protected $graby;
protected $tagger;
public function __construct(Graby $graby)
public function __construct(Graby $graby, RuleBasedTagger $tagger)
{
$this->graby = $graby;
$this->graby = $graby;
$this->tagger = $tagger;
}
/**
@ -59,6 +61,8 @@ class ContentProxy
$entry->setPreviewPicture($content['open_graph']['og_image']);
}
$this->tagger->tag($entry);
return $entry;
}
}

View file

@ -0,0 +1,82 @@
<?php
namespace Wallabag\CoreBundle\Helper;
use RulerZ\RulerZ;
use Wallabag\CoreBundle\Entity\Entry;
use Wallabag\CoreBundle\Entity\Tag;
use Wallabag\CoreBundle\Repository\TagRepository;
use Wallabag\UserBundle\Entity\User;
class RuleBasedTagger
{
private $rulerz;
private $tagRepository;
public function __construct(RulerZ $rulerz, TagRepository $tagRepository)
{
$this->rulerz = $rulerz;
$this->tagRepository = $tagRepository;
}
/**
* Add tags from rules defined by the user.
*
* @param Entry $entry Entry to tag.
*/
public function tag(Entry $entry)
{
$rules = $this->getRulesForUser($entry->getUser());
foreach ($rules as $rule) {
if (!$this->rulerz->satisfies($entry, $rule['rule'])) {
continue;
}
foreach ($rule['tags'] as $label) {
$tag = $this->getTag($entry->getUser(), $label);
$entry->addTag($tag);
}
}
}
/**
* Fetch a tag for a user.
*
* @param User $user
* @param string $label The tag's label.
*
* @return Tag
*/
private function getTag(User $user, $label)
{
$tag = $this->tagRepository->findOneByLabelAndUserId($label, $user->getId());
if (!$tag) {
$tag = new Tag($user);
$tag->setLabel($label);
}
return $tag;
}
private function getRulesForUser(User $user)
{
return [
[
'rule' => 'domainName = "github.com"',
'tags' => ['github'],
],
[
'rule' => 'readingTime >= 15',
'tags' => ['long reading'],
],
[
'rule' => 'readingTime <= 3 ',
'tags' => ['short reading'],
],
];
}
}

View file

@ -53,6 +53,20 @@ services:
class: Wallabag\CoreBundle\Helper\ContentProxy
arguments:
- @wallabag_core.graby
- @wallabag_core.rule_based_tagger
wallabag_core.rule_based_tagger:
class: Wallabag\CoreBundle\Helper\RuleBasedTagger
arguments:
- @rulerz
- @wallabag_core.tag_repository
wallabag_core.tag_repository:
class: Wallabag\CoreBundle\Repository\TagRepository
factory_service: doctrine.orm.default_entity_manager
factory_method: getRepository
arguments:
- WallabagCoreBundle:Tag
wallabag_core.registration_confirmed:
class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener