From c3510620ad0718d2ab1f856e3a838360a5ade314 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Gomez?= Date: Sun, 11 Oct 2015 16:54:21 +0200 Subject: PoC of rule-based tagging --- src/Wallabag/CoreBundle/Helper/ContentProxy.php | 8 ++- src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | 82 ++++++++++++++++++++++ .../CoreBundle/Resources/config/services.yml | 14 ++++ 3 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php (limited to 'src') diff --git a/src/Wallabag/CoreBundle/Helper/ContentProxy.php b/src/Wallabag/CoreBundle/Helper/ContentProxy.php index 7fb41393..dc6e1184 100644 --- a/src/Wallabag/CoreBundle/Helper/ContentProxy.php +++ b/src/Wallabag/CoreBundle/Helper/ContentProxy.php @@ -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; } } diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php new file mode 100644 index 00000000..012450b6 --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -0,0 +1,82 @@ +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'], + ], + ]; + } +} diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 8e21b052..4cf46b33 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml @@ -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 -- cgit v1.2.3