diff options
author | Kevin Decherf <kevin@kdecherf.com> | 2019-08-11 23:51:55 +0200 |
---|---|---|
committer | Kevin Decherf <kevin@kdecherf.com> | 2020-04-25 15:59:23 +0200 |
commit | f39c5a2a702036750b4d7c32d02e7f92955a4eed (patch) | |
tree | 267e1488312d8c336ef937bbe6baeddc9441f694 /src/Wallabag | |
parent | 24230a5130005e274e1d8d3fe8eaca13cb978b9c (diff) | |
download | wallabag-f39c5a2a702036750b4d7c32d02e7f92955a4eed.tar.gz wallabag-f39c5a2a702036750b4d7c32d02e7f92955a4eed.tar.zst wallabag-f39c5a2a702036750b4d7c32d02e7f92955a4eed.zip |
Add new Helper to process Ignore Origin rules and RulerZ operator
This commits adds a new helper like RuleBasedTagger for processing
ignore origin rules. It also adds a new custom RulerZ operator for the
'~' pattern matching rule.
Renames 'pattern' with '_all' in IgnoreOriginRule entity.
Signed-off-by: Kevin Decherf <kevin@kdecherf.com>
Diffstat (limited to 'src/Wallabag')
5 files changed, 88 insertions, 4 deletions
diff --git a/src/Wallabag/CoreBundle/Entity/IgnoreOriginInstanceRule.php b/src/Wallabag/CoreBundle/Entity/IgnoreOriginInstanceRule.php index 34aed50c..ce3b6e7d 100644 --- a/src/Wallabag/CoreBundle/Entity/IgnoreOriginInstanceRule.php +++ b/src/Wallabag/CoreBundle/Entity/IgnoreOriginInstanceRule.php | |||
@@ -11,7 +11,6 @@ use Symfony\Component\Validator\Constraints as Assert; | |||
11 | * | 11 | * |
12 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository") | 12 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository") |
13 | * @ORM\Table(name="`ignore_origin_instance_rule`") | 13 | * @ORM\Table(name="`ignore_origin_instance_rule`") |
14 | * @ORM\Entity | ||
15 | */ | 14 | */ |
16 | class IgnoreOriginInstanceRule implements IgnoreOriginRuleInterface, RuleInterface | 15 | class IgnoreOriginInstanceRule implements IgnoreOriginRuleInterface, RuleInterface |
17 | { | 16 | { |
@@ -30,7 +29,7 @@ class IgnoreOriginInstanceRule implements IgnoreOriginRuleInterface, RuleInterfa | |||
30 | * @Assert\NotBlank() | 29 | * @Assert\NotBlank() |
31 | * @Assert\Length(max=255) | 30 | * @Assert\Length(max=255) |
32 | * @RulerZAssert\ValidRule( | 31 | * @RulerZAssert\ValidRule( |
33 | * allowed_variables={"host","pattern"}, | 32 | * allowed_variables={"host","_all"}, |
34 | * allowed_operators={"=","~"} | 33 | * allowed_operators={"=","~"} |
35 | * ) | 34 | * ) |
36 | * @ORM\Column(name="rule", type="string", nullable=false) | 35 | * @ORM\Column(name="rule", type="string", nullable=false) |
diff --git a/src/Wallabag/CoreBundle/Entity/IgnoreOriginUserRule.php b/src/Wallabag/CoreBundle/Entity/IgnoreOriginUserRule.php index befd6090..0b6f318d 100644 --- a/src/Wallabag/CoreBundle/Entity/IgnoreOriginUserRule.php +++ b/src/Wallabag/CoreBundle/Entity/IgnoreOriginUserRule.php | |||
@@ -11,7 +11,6 @@ use Symfony\Component\Validator\Constraints as Assert; | |||
11 | * | 11 | * |
12 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\IgnoreOriginUserRuleRepository") | 12 | * @ORM\Entity(repositoryClass="Wallabag\CoreBundle\Repository\IgnoreOriginUserRuleRepository") |
13 | * @ORM\Table(name="`ignore_origin_user_rule`") | 13 | * @ORM\Table(name="`ignore_origin_user_rule`") |
14 | * @ORM\Entity | ||
15 | */ | 14 | */ |
16 | class IgnoreOriginUserRule implements IgnoreOriginRuleInterface, RuleInterface | 15 | class IgnoreOriginUserRule implements IgnoreOriginRuleInterface, RuleInterface |
17 | { | 16 | { |
@@ -30,7 +29,7 @@ class IgnoreOriginUserRule implements IgnoreOriginRuleInterface, RuleInterface | |||
30 | * @Assert\NotBlank() | 29 | * @Assert\NotBlank() |
31 | * @Assert\Length(max=255) | 30 | * @Assert\Length(max=255) |
32 | * @RulerZAssert\ValidRule( | 31 | * @RulerZAssert\ValidRule( |
33 | * allowed_variables={"host","pattern"}, | 32 | * allowed_variables={"host","_all"}, |
34 | * allowed_operators={"=","~"} | 33 | * allowed_operators={"=","~"} |
35 | * ) | 34 | * ) |
36 | * @ORM\Column(name="rule", type="string", nullable=false) | 35 | * @ORM\Column(name="rule", type="string", nullable=false) |
diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessor.php b/src/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessor.php new file mode 100644 index 00000000..333e5b0a --- /dev/null +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedIgnoreOriginProcessor.php | |||
@@ -0,0 +1,50 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Helper; | ||
4 | |||
5 | use Psr\Log\LoggerInterface; | ||
6 | use RulerZ\RulerZ; | ||
7 | use Wallabag\CoreBundle\Entity\Entry; | ||
8 | use Wallabag\CoreBundle\Repository\IgnoreOriginInstanceRuleRepository; | ||
9 | |||
10 | class RuleBasedIgnoreOriginProcessor | ||
11 | { | ||
12 | protected $rulerz; | ||
13 | protected $logger; | ||
14 | protected $ignoreOriginInstanceRuleRepository; | ||
15 | |||
16 | public function __construct(RulerZ $rulerz, LoggerInterface $logger, IgnoreOriginInstanceRuleRepository $ignoreOriginInstanceRuleRepository) | ||
17 | { | ||
18 | $this->rulerz = $rulerz; | ||
19 | $this->logger = $logger; | ||
20 | $this->ignoreOriginInstanceRuleRepository = $ignoreOriginInstanceRuleRepository; | ||
21 | } | ||
22 | |||
23 | /** | ||
24 | * @param Entry $entry Entry to process | ||
25 | * | ||
26 | * @return bool | ||
27 | */ | ||
28 | public function process(Entry $entry) | ||
29 | { | ||
30 | $url = $entry->getUrl(); | ||
31 | $userRules = $entry->getUser()->getConfig()->getIgnoreOriginRules()->toArray(); | ||
32 | $rules = array_merge($this->ignoreOriginInstanceRuleRepository->findAll(), $userRules); | ||
33 | |||
34 | $parsed_url = parse_url($url); | ||
35 | // We add the former url as a new key _all for pattern matching | ||
36 | $parsed_url['_all'] = $url; | ||
37 | |||
38 | foreach ($rules as $rule) { | ||
39 | if ($this->rulerz->satisfies($parsed_url, $rule->getRule())) { | ||
40 | $this->logger->info('Origin url matching ignore rule.', [ | ||
41 | 'rule' => $rule->getRule(), | ||
42 | ]); | ||
43 | |||
44 | return true; | ||
45 | } | ||
46 | } | ||
47 | |||
48 | return false; | ||
49 | } | ||
50 | } | ||
diff --git a/src/Wallabag/CoreBundle/Operator/PHP/PatternMatches.php b/src/Wallabag/CoreBundle/Operator/PHP/PatternMatches.php new file mode 100644 index 00000000..532e2bb3 --- /dev/null +++ b/src/Wallabag/CoreBundle/Operator/PHP/PatternMatches.php | |||
@@ -0,0 +1,23 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Operator\PHP; | ||
4 | |||
5 | /** | ||
6 | * Provides a "~" operator used for ignore origin rules. | ||
7 | * | ||
8 | * It asserts that a subject matches a given regexp pattern, in a | ||
9 | * case-insensitive way. | ||
10 | * | ||
11 | * This operator will be used to compile ignore origin rules in PHP, usable | ||
12 | * directly on Entry objects for instance. | ||
13 | * It's registered in RulerZ using a service (wallabag.operator.array.pattern_matches); | ||
14 | */ | ||
15 | class PatternMatches | ||
16 | { | ||
17 | public function __invoke($subject, $pattern) | ||
18 | { | ||
19 | $count = preg_match("`$pattern`i", $subject); | ||
20 | |||
21 | return \is_int($count) && $count > 0; | ||
22 | } | ||
23 | } | ||
diff --git a/src/Wallabag/CoreBundle/Resources/config/services.yml b/src/Wallabag/CoreBundle/Resources/config/services.yml index 4ece046a..8417ac35 100644 --- a/src/Wallabag/CoreBundle/Resources/config/services.yml +++ b/src/Wallabag/CoreBundle/Resources/config/services.yml | |||
@@ -92,6 +92,7 @@ services: | |||
92 | arguments: | 92 | arguments: |
93 | - "@wallabag_core.graby" | 93 | - "@wallabag_core.graby" |
94 | - "@wallabag_core.rule_based_tagger" | 94 | - "@wallabag_core.rule_based_tagger" |
95 | - "@wallabag_core.rule_based_ignore_origin_processor" | ||
95 | - "@validator" | 96 | - "@validator" |
96 | - "@logger" | 97 | - "@logger" |
97 | - '%wallabag_core.fetching_error_message%' | 98 | - '%wallabag_core.fetching_error_message%' |
@@ -110,6 +111,13 @@ services: | |||
110 | - "@wallabag_core.entry_repository" | 111 | - "@wallabag_core.entry_repository" |
111 | - "@logger" | 112 | - "@logger" |
112 | 113 | ||
114 | wallabag_core.rule_based_ignore_origin_processor: | ||
115 | class: Wallabag\CoreBundle\Helper\RuleBasedIgnoreOriginProcessor | ||
116 | arguments: | ||
117 | - "@rulerz" | ||
118 | - "@logger" | ||
119 | - "@wallabag_core.ignore_origin_instance_rule_repository" | ||
120 | |||
113 | # repository as a service | 121 | # repository as a service |
114 | wallabag_core.entry_repository: | 122 | wallabag_core.entry_repository: |
115 | class: Wallabag\CoreBundle\Repository\EntryRepository | 123 | class: Wallabag\CoreBundle\Repository\EntryRepository |
@@ -164,6 +172,11 @@ services: | |||
164 | tags: | 172 | tags: |
165 | - { name: rulerz.operator, target: doctrine, operator: notmatches, inline: true } | 173 | - { name: rulerz.operator, target: doctrine, operator: notmatches, inline: true } |
166 | 174 | ||
175 | wallabag.operator.array.pattern_matches: | ||
176 | class: Wallabag\CoreBundle\Operator\PHP\PatternMatches | ||
177 | tags: | ||
178 | - { name: rulerz.operator, target: native, operator: "~" } | ||
179 | |||
167 | wallabag_core.helper.redirect: | 180 | wallabag_core.helper.redirect: |
168 | class: Wallabag\CoreBundle\Helper\Redirect | 181 | class: Wallabag\CoreBundle\Helper\Redirect |
169 | arguments: | 182 | arguments: |