From d24b703315e6c4a41d83f3f1b0c91ba4bdc25750 Mon Sep 17 00:00:00 2001 From: Andrea Decorte Date: Sun, 7 Sep 2025 22:27:08 +0200 Subject: [PATCH 1/2] Extract user reading time logic in a single place Extract user reading time computation in a single place under Entry to avoid duplication. This will also fix reading time computation for short entries. RuleTagger logic is slightly changed as we will round when filtering, which will be consistent with frontend display. --- src/Wallabag/CoreBundle/Entity/Entry.php | 8 ++++++++ src/Wallabag/CoreBundle/Helper/EntriesExport.php | 4 ++-- src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php | 2 +- .../Resources/views/Entry/_reading_time.html.twig | 4 ++-- 4 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 5a00c0dbe..225e55fcf 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -658,6 +658,14 @@ class Entry $this->readingTime = $readingTime; } + /** + * @return float + */ + public function getUserReadingTime() + { + return round($this->readingTime / $this->getUser()->getConfig()->getReadingSpeed() * 200); + } + /** * @return string */ diff --git a/src/Wallabag/CoreBundle/Helper/EntriesExport.php b/src/Wallabag/CoreBundle/Helper/EntriesExport.php index 350a396f8..0273f21e6 100644 --- a/src/Wallabag/CoreBundle/Helper/EntriesExport.php +++ b/src/Wallabag/CoreBundle/Helper/EntriesExport.php @@ -210,7 +210,7 @@ class EntriesExport $publishedDate = $entry->getPublishedAt()->format('Y-m-d'); } - $readingTime = round($entry->getReadingTime() / $user->getConfig()->getReadingSpeed() * 200); + $readingTime = $entry->getUserReadingTime(); $titlepage = $content_start . '

' . $entry->getTitle() . '

' . @@ -331,7 +331,7 @@ class EntriesExport $authors = implode(',', $publishedBy); } - $readingTime = $entry->getReadingTime() / $user->getConfig()->getReadingSpeed() * 200; + $readingTime = $entry->getUserReadingTime(); $pdf->addPage(); $html = '

' . $entry->getTitle() . '

' . diff --git a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php index 4014c68e7..cbdf60bd7 100644 --- a/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php +++ b/src/Wallabag/CoreBundle/Helper/RuleBasedTagger.php @@ -133,7 +133,7 @@ class RuleBasedTagger private function fixEntry(Entry $entry) { $clonedEntry = clone $entry; - $clonedEntry->setReadingTime($entry->getReadingTime() / $entry->getUser()->getConfig()->getReadingSpeed() * 200); + $clonedEntry->setReadingTime($entry->getUserReadingTime()); return $clonedEntry; } diff --git a/src/Wallabag/CoreBundle/Resources/views/Entry/_reading_time.html.twig b/src/Wallabag/CoreBundle/Resources/views/Entry/_reading_time.html.twig index 404f69377..daf6b08f0 100644 --- a/src/Wallabag/CoreBundle/Resources/views/Entry/_reading_time.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Entry/_reading_time.html.twig @@ -1,7 +1,7 @@ -{% set reading_time = entry.readingTime / app.user.config.readingSpeed * 200 %} +{% set reading_time = entry.userReadingTime %} timer {% if reading_time > 0 %} - {{ 'entry.list.reading_time_minutes_short'|trans({'%readingTime%': reading_time|round}) }} + {{ 'entry.list.reading_time_minutes_short'|trans({'%readingTime%': reading_time}) }} {% else %} {{ 'entry.list.reading_time_less_one_minute_short'|trans|raw }} {% endif %} From 797d48905c09f0a6ff784687fccf57a79e99acd8 Mon Sep 17 00:00:00 2001 From: Andrea Decorte Date: Sun, 7 Sep 2025 22:32:55 +0200 Subject: [PATCH 2/2] Extract DEFAULT_WORDS_PER_MINUTE in Utils This is done In order to avoid having magic numbers in the code --- src/Wallabag/CoreBundle/Entity/Entry.php | 3 ++- src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php | 5 +++-- src/Wallabag/CoreBundle/Tools/Utils.php | 4 +++- 3 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 225e55fcf..f37882e9f 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -14,6 +14,7 @@ use Symfony\Component\Validator\Constraints as Assert; use Wallabag\AnnotationBundle\Entity\Annotation; use Wallabag\CoreBundle\Helper\EntityTimestampsTrait; use Wallabag\CoreBundle\Helper\UrlHasher; +use Wallabag\CoreBundle\Tools\Utils; use Wallabag\UserBundle\Entity\User; /** @@ -663,7 +664,7 @@ class Entry */ public function getUserReadingTime() { - return round($this->readingTime / $this->getUser()->getConfig()->getReadingSpeed() * 200); + return round($this->readingTime / $this->getUser()->getConfig()->getReadingSpeed() * Utils::DEFAULT_WORDS_PER_MINUTE); } /** diff --git a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php index 2642f9e27..757578444 100644 --- a/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php +++ b/src/Wallabag/CoreBundle/Form/Type/EntryFilterType.php @@ -16,6 +16,7 @@ use Symfony\Component\HttpFoundation\Response; use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Repository\EntryRepository; +use Wallabag\CoreBundle\Tools\Utils; use Wallabag\UserBundle\Entity\User; class EntryFilterType extends AbstractType @@ -57,8 +58,8 @@ class EntryFilterType extends AbstractType return; } - $min = (int) ($lower * $user->getConfig()->getReadingSpeed() / 200); - $max = (int) ($upper * $user->getConfig()->getReadingSpeed() / 200); + $min = (int) ($lower * $user->getConfig()->getReadingSpeed() / Utils::DEFAULT_WORDS_PER_MINUTE); + $max = (int) ($upper * $user->getConfig()->getReadingSpeed() / Utils::DEFAULT_WORDS_PER_MINUTE); if (null === $lower && null !== $upper) { // only lower value is defined: query all entries with reading LOWER THAN this value diff --git a/src/Wallabag/CoreBundle/Tools/Utils.php b/src/Wallabag/CoreBundle/Tools/Utils.php index b7ad79664..6431ba5a0 100644 --- a/src/Wallabag/CoreBundle/Tools/Utils.php +++ b/src/Wallabag/CoreBundle/Tools/Utils.php @@ -4,6 +4,8 @@ namespace Wallabag\CoreBundle\Tools; class Utils { + public const DEFAULT_WORDS_PER_MINUTE = 200; + /** * Generate a token used for Feeds. * @@ -28,6 +30,6 @@ class Utils */ public static function getReadingTime($text) { - return floor(\count(preg_split('~([^\p{L}\p{N}\']+|(\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}){1,2})~u', strip_tags($text))) / 200); + return floor(\count(preg_split('~([^\p{L}\p{N}\']+|(\p{Han}|\p{Hiragana}|\p{Katakana}|\p{Hangul}){1,2})~u', strip_tags($text))) / self::DEFAULT_WORDS_PER_MINUTE); } }