diff --git a/src/Wallabag/CoreBundle/Command/TagAllCommand.php b/src/Wallabag/CoreBundle/Command/TagAllCommand.php index 4afac2d45..666654c02 100644 --- a/src/Wallabag/CoreBundle/Command/TagAllCommand.php +++ b/src/Wallabag/CoreBundle/Command/TagAllCommand.php @@ -41,7 +41,7 @@ class TagAllCommand extends ContainerAwareCommand $entries = $tagger->tagAllForUser($user); - $io->text('Persist entries... '); + $io->text('Persist ' . \count($entries) . ' entries... '); $em = $this->getDoctrine()->getManager(); foreach ($entries as $entry) { diff --git a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php index d76ba514d..6dde08e81 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php +++ b/src/Wallabag/CoreBundle/DataFixtures/ConfigFixtures.php @@ -45,7 +45,7 @@ class ConfigFixtures extends Fixture implements DependentFixtureInterface $emptyConfig = new Config($this->getReference('empty-user')); $emptyConfig->setTheme('material'); $emptyConfig->setItemsPerPage(10); - $emptyConfig->setReadingSpeed(200); + $emptyConfig->setReadingSpeed(100); $emptyConfig->setLanguage('en'); $emptyConfig->setPocketConsumerKey(null); $emptyConfig->setActionMarkAsRead(0); diff --git a/src/Wallabag/CoreBundle/DataFixtures/TaggingRuleFixtures.php b/src/Wallabag/CoreBundle/DataFixtures/TaggingRuleFixtures.php index b36224119..083c7bc98 100644 --- a/src/Wallabag/CoreBundle/DataFixtures/TaggingRuleFixtures.php +++ b/src/Wallabag/CoreBundle/DataFixtures/TaggingRuleFixtures.php @@ -43,6 +43,20 @@ class TaggingRuleFixtures extends Fixture implements DependentFixtureInterface $manager->persist($tr4); + $tr5 = new TaggingRule(); + $tr5->setRule('readingTime <= 5'); + $tr5->setTags(['shortread']); + $tr5->setConfig($this->getReference('empty-config')); + + $manager->persist($tr5); + + $tr6 = new TaggingRule(); + $tr6->setRule('readingTime > 5'); + $tr6->setTags(['longread']); + $tr6->setConfig($this->getReference('empty-config')); + + $manager->persist($tr6); + $manager->flush(); } diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index 0b18beaaa..4014c68e7 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -35,8 +35,10 @@ class RuleBasedTagger { $rules = $this->getRulesForUser($entry->getUser()); + $clonedEntry = $this->fixEntry($entry); + foreach ($rules as $rule) { - if (!$this->rulerz->satisfies($entry, $rule->getRule())) { + if (!$this->rulerz->satisfies($clonedEntry, $rule->getRule())) { continue; } @@ -61,14 +63,22 @@ class RuleBasedTagger public function tagAllForUser(User $user) { $rules = $this->getRulesForUser($user); - $entries = []; + $entriesToUpdate = []; $tagsCache = []; - foreach ($rules as $rule) { - $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); - $entries = $this->rulerz->filter($qb, $rule->getRule()); + $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; + } - foreach ($entries as $entry) { foreach ($rule->getTags() as $label) { // avoid new tag duplicate by manually caching them if (!isset($tagsCache[$label])) { @@ -78,11 +88,13 @@ class RuleBasedTagger $tag = $tagsCache[$label]; $entry->addTag($tag); + + $entriesToUpdate[] = $entry; } } } - return $entries; + return $entriesToUpdate; } /** @@ -114,4 +126,15 @@ class RuleBasedTagger { return $user->getConfig()->getTaggingRules(); } + + /** + * 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; + } } diff --git a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php index 4820fad09..13ab7aca2 100644 --- a/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php +++ b/tests/Wallabag/CoreBundle/Controller/EntryControllerTest.php @@ -175,6 +175,42 @@ class EntryControllerTest extends WallabagCoreTestCase $client->getContainer()->get('craue_config')->set('store_article_headers', 0); } + /** + * @group NetworkCalls + */ + public function testPostNewOkWithTaggingRules() + { + $this->logInAs('empty'); + $client = $this->getClient(); + + $crawler = $client->request('GET', '/new'); + + $this->assertSame(200, $client->getResponse()->getStatusCode()); + + $form = $crawler->filter('form[name=entry]')->form(); + + $data = [ + 'entry[url]' => $this->url, + ]; + + $client->submit($form, $data); + + $this->assertSame(302, $client->getResponse()->getStatusCode()); + + $content = $client->getContainer() + ->get('doctrine.orm.entity_manager') + ->getRepository('WallabagCoreBundle:Entry') + ->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); + + $tags = $content->getTagsLabel(); + + /* + * Without the custom reading speed of `empty` user, it'll be inversed + */ + $this->assertContains('longread', $tags); + $this->assertNotContains('shortread', $tags); + } + /** * @group NetworkCalls */