diff options
Diffstat (limited to 'src/Wallabag')
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/ContentProxy.php | 8 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | 82 | ||||
-rw-r--r-- | src/Wallabag/CoreBundle/Resources/config/services.yml | 14 |
3 files changed, 102 insertions, 2 deletions
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; | |||
13 | class ContentProxy | 13 | class ContentProxy |
14 | { | 14 | { |
15 | protected $graby; | 15 | protected $graby; |
16 | protected $tagger; | ||
16 | 17 | ||
17 | public function __construct(Graby $graby) | 18 | public function __construct(Graby $graby, RuleBasedTagger $tagger) |
18 | { | 19 | { |
19 | $this->graby = $graby; | 20 | $this->graby = $graby; |
21 | $this->tagger = $tagger; | ||
20 | } | 22 | } |
21 | 23 | ||
22 | /** | 24 | /** |
@@ -59,6 +61,8 @@ class ContentProxy | |||
59 | $entry->setPreviewPicture($content['open_graph']['og_image']); | 61 | $entry->setPreviewPicture($content['open_graph']['og_image']); |
60 | } | 62 | } |
61 | 63 | ||
64 | $this->tagger->tag($entry); | ||
65 | |||
62 | return $entry; | 66 | return $entry; |
63 | } | 67 | } |
64 | } | 68 | } |
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use RulerZ\RulerZ; | ||
6 | |||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Wallabag\CoreBundle\Entity\Tag; | ||
9 | use Wallabag\CoreBundle\Repository\TagRepository; | ||
10 | use Wallabag\UserBundle\Entity\User; | ||
11 | |||
12 | class RuleBasedTagger | ||
13 | { | ||
14 | private $rulerz; | ||
15 | private $tagRepository; | ||
16 | |||
17 | public function __construct(RulerZ $rulerz, TagRepository $tagRepository) | ||
18 | { | ||
19 | $this->rulerz = $rulerz; | ||
20 | $this->tagRepository = $tagRepository; | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * Add tags from rules defined by the user. | ||
25 | * | ||
26 | * @param Entry $entry Entry to tag. | ||
27 | */ | ||
28 | public function tag(Entry $entry) | ||
29 | { | ||
30 | $rules = $this->getRulesForUser($entry->getUser()); | ||
31 | |||
32 | foreach ($rules as $rule) { | ||
33 | if (!$this->rulerz->satisfies($entry, $rule['rule'])) { | ||
34 | continue; | ||
35 | } | ||
36 | |||
37 | foreach ($rule['tags'] as $label) { | ||
38 | $tag = $this->getTag($entry->getUser(), $label); | ||
39 | |||
40 | $entry->addTag($tag); | ||
41 | } | ||
42 | } | ||
43 | } | ||
44 | |||
45 | /** | ||
46 | * Fetch a tag for a user. | ||
47 | * | ||
48 | * @param User $user | ||
49 | * @param string $label The tag's label. | ||
50 | * | ||
51 | * @return Tag | ||
52 | */ | ||
53 | private function getTag(User $user, $label) | ||
54 | { | ||
55 | $tag = $this->tagRepository->findOneByLabelAndUserId($label, $user->getId()); | ||
56 | |||
57 | if (!$tag) { | ||
58 | $tag = new Tag($user); | ||
59 | $tag->setLabel($label); | ||
60 | } | ||
61 | |||
62 | return $tag; | ||
63 | } | ||
64 | |||
65 | private function getRulesForUser(User $user) | ||
66 | { | ||
67 | return [ | ||
68 | [ | ||
69 | 'rule' => 'domainName = "github.com"', | ||
70 | 'tags' => ['github'], | ||
71 | ], | ||
72 | [ | ||
73 | 'rule' => 'readingTime >= 15', | ||
74 | 'tags' => ['long reading'], | ||
75 | ], | ||
76 | [ | ||
77 | 'rule' => 'readingTime <= 3 ', | ||
78 | 'tags' => ['short reading'], | ||
79 | ], | ||
80 | ]; | ||
81 | } | ||
82 | } | ||
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: | |||
53 | class: Wallabag\CoreBundle\Helper\ContentProxy | 53 | class: Wallabag\CoreBundle\Helper\ContentProxy |
54 | arguments: | 54 | arguments: |
55 | - @wallabag_core.graby | 55 | - @wallabag_core.graby |
56 | - @wallabag_core.rule_based_tagger | ||
57 | |||
58 | wallabag_core.rule_based_tagger: | ||
59 | class: Wallabag\CoreBundle\Helper\RuleBasedTagger | ||
60 | arguments: | ||
61 | - @rulerz | ||
62 | - @wallabag_core.tag_repository | ||
63 | |||
64 | wallabag_core.tag_repository: | ||
65 | class: Wallabag\CoreBundle\Repository\TagRepository | ||
66 | factory_service: doctrine.orm.default_entity_manager | ||
67 | factory_method: getRepository | ||
68 | arguments: | ||
69 | - WallabagCoreBundle:Tag | ||
56 | 70 | ||
57 | wallabag_core.registration_confirmed: | 71 | wallabag_core.registration_confirmed: |
58 | class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener | 72 | class: Wallabag\CoreBundle\EventListener\RegistrationConfirmedListener |