From 8315130a75c8f411f76134b6205a017409583d50 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 21 Apr 2016 22:30:50 +0200 Subject: Display entries number for each category --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 33 ++++++++++++++++++++++ 1 file changed, 33 insertions(+) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 1a308070..c116248f 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,8 +2,20 @@ namespace Wallabag\CoreBundle\Twig; +use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Wallabag\CoreBundle\Repository\EntryRepository; + class WallabagExtension extends \Twig_Extension { + private $tokenStorage; + private $repository; + + public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) + { + $this->repository = $repository; + $this->tokenStorage = $tokenStorage; + } + public function getFilters() { return [ @@ -16,6 +28,27 @@ class WallabagExtension extends \Twig_Extension return preg_replace('/^www\./i', '', $url); } + public function getGlobals() + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return array(); + } + + $unreadEntries = $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()->getResult(); + $starredEntries = $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()->getResult(); + $archivedEntries = $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()->getResult(); + $allEntries = $this->repository->getBuilderForAllByUser($user->getId())->getQuery()->getResult(); + + return array( + 'unreadEntries' => count($unreadEntries), + 'starredEntries' => count($starredEntries), + 'archivedEntries' => count($archivedEntries), + 'allEntries' => count($allEntries), + ); + } + public function getName() { return 'wallabag_extension'; -- cgit v1.2.3 From f997ae6afb72e6a2f3d8ff8bd30a354e35696410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 21 Apr 2016 22:35:42 +0200 Subject: Implement Twig_Extension_GlobalsInterface to remove deprecation --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index c116248f..6e46c701 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -5,7 +5,7 @@ namespace Wallabag\CoreBundle\Twig; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Repository\EntryRepository; -class WallabagExtension extends \Twig_Extension +class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { private $tokenStorage; private $repository; -- cgit v1.2.3 From 0b0233b1ec8208be47c76856a4e317673927b21e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 1 Sep 2016 20:20:12 +0200 Subject: Enable cache for queries --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 27 +++++++++++++++------- 1 file changed, 19 insertions(+), 8 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 6e46c701..5c475d61 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -36,16 +36,27 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return array(); } - $unreadEntries = $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery()->getResult(); - $starredEntries = $this->repository->getBuilderForStarredByUser($user->getId())->getQuery()->getResult(); - $archivedEntries = $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery()->getResult(); - $allEntries = $this->repository->getBuilderForAllByUser($user->getId())->getQuery()->getResult(); + $unreadEntries = $this->repository->enableCache( + $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery() + ); + + $starredEntries = $this->repository->enableCache( + $this->repository->getBuilderForStarredByUser($user->getId())->getQuery() + ); + + $archivedEntries = $this->repository->enableCache( + $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery() + ); + + $allEntries = $this->repository->enableCache( + $this->repository->getBuilderForAllByUser($user->getId())->getQuery() + ); return array( - 'unreadEntries' => count($unreadEntries), - 'starredEntries' => count($starredEntries), - 'archivedEntries' => count($archivedEntries), - 'allEntries' => count($allEntries), + 'unreadEntries' => count($unreadEntries->getResult()), + 'starredEntries' => count($starredEntries->getResult()), + 'archivedEntries' => count($archivedEntries->getResult()), + 'allEntries' => count($allEntries->getResult()), ); } -- cgit v1.2.3 From 543da3e0b7592b1a00a7c5baec1554460609d63f Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 18:11:07 +0200 Subject: Instead of selecting the whole data, just count it Instead of performing a complex select (to retrieve all data for entry, etc...) just select the counter and retrieve it. Down from ~50ms to ~30ms on the unread page (with 500 items) --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 5c475d61..93640dc6 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -33,31 +33,31 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; if (null === $user || !is_object($user)) { - return array(); + return []; } $unreadEntries = $this->repository->enableCache( - $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery() + $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $starredEntries = $this->repository->enableCache( - $this->repository->getBuilderForStarredByUser($user->getId())->getQuery() + $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $archivedEntries = $this->repository->enableCache( - $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery() + $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); $allEntries = $this->repository->enableCache( - $this->repository->getBuilderForAllByUser($user->getId())->getQuery() + $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() ); - return array( - 'unreadEntries' => count($unreadEntries->getResult()), - 'starredEntries' => count($starredEntries->getResult()), - 'archivedEntries' => count($archivedEntries->getResult()), - 'allEntries' => count($allEntries->getResult()), - ); + return [ + 'unreadEntries' => $unreadEntries->getSingleScalarResult(), + 'starredEntries' => $starredEntries->getSingleScalarResult(), + 'archivedEntries' => $archivedEntries->getSingleScalarResult(), + 'allEntries' => $allEntries->getSingleScalarResult(), + ]; } public function getName() -- cgit v1.2.3 From 59ddb9ae99b97a1a8fa3aa3770a4a2afef333699 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 19:09:28 +0200 Subject: Remove Twig globals Twig Global function are called globally. This means even on a query to the api. Using a function we can decide when we want to call it. Also, remove previous `COUNT(e.id)` since it doesn't work on PostgreSQL ... --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 63 +++++++++++++++------- 1 file changed, 44 insertions(+), 19 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 93640dc6..d6ac6117 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -23,12 +23,26 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ]; } + public function getFunctions() + { + return array( + new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), + ); + } + public function removeWww($url) { return preg_replace('/^www\./i', '', $url); } - public function getGlobals() + /** + * Return number of entries depending of the type (unread, archive, starred or all) + * + * @param string $type Type of entries to count + * + * @return int + */ + public function countEntries($type) { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; @@ -36,28 +50,39 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return []; } - $unreadEntries = $this->repository->enableCache( - $this->repository->getBuilderForUnreadByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + switch ($type) { + case 'starred': + $qb = $this->repository->getBuilderForStarredByUser($user->getId()); + break; - $starredEntries = $this->repository->enableCache( - $this->repository->getBuilderForStarredByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'archive': + $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); + break; - $archivedEntries = $this->repository->enableCache( - $this->repository->getBuilderForArchiveByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'unread': + $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); + break; - $allEntries = $this->repository->enableCache( - $this->repository->getBuilderForAllByUser($user->getId())->select('COUNT(e.id)')->getQuery() - ); + case 'all': + $qb = $this->repository->getBuilderForAllByUser($user->getId()); + break; - return [ - 'unreadEntries' => $unreadEntries->getSingleScalarResult(), - 'starredEntries' => $starredEntries->getSingleScalarResult(), - 'archivedEntries' => $archivedEntries->getSingleScalarResult(), - 'allEntries' => $allEntries->getSingleScalarResult(), - ]; + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } + + // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) + // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function + $query = $qb + ->select('e.id') + ->groupBy('e.id') + ->getQuery(); + + $data =$this->repository + ->enableCache($query) + ->getArrayResult(); + + return count($data); } public function getName() -- cgit v1.2.3 From 234ad944534cf51118f63f0b48c206b5d9a70fe5 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 3 Sep 2016 19:26:23 +0200 Subject: CS --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index d6ac6117..974b86a9 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -36,9 +36,9 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa } /** - * Return number of entries depending of the type (unread, archive, starred or all) + * Return number of entries depending of the type (unread, archive, starred or all). * - * @param string $type Type of entries to count + * @param string $type Type of entries to count * * @return int */ @@ -78,7 +78,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ->groupBy('e.id') ->getQuery(); - $data =$this->repository + $data = $this->repository ->enableCache($query) ->getArrayResult(); -- cgit v1.2.3 From 429d86f388da856c9d8d9a649147c5212bee4258 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sun, 4 Sep 2016 20:53:28 +0200 Subject: Added tags counter in sidebar (material theme) --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 61 ++++++++++++++++++---- 1 file changed, 52 insertions(+), 9 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 974b86a9..ed6dadc5 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,18 +2,24 @@ namespace Wallabag\CoreBundle\Twig; +use Doctrine\ORM\Query; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Repository\EntryRepository; +use Wallabag\CoreBundle\Repository\TagRepository; class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { private $tokenStorage; - private $repository; + private $entryRepository; + private $tagRepository; + private $lifeTime; - public function __construct(EntryRepository $repository = null, TokenStorageInterface $tokenStorage = null) + public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) { - $this->repository = $repository; + $this->entryRepository = $entryRepository; + $this->tagRepository = $tagRepository; $this->tokenStorage = $tokenStorage; + $this->lifeTime = $lifeTime; } public function getFilters() @@ -27,6 +33,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { return array( new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), + new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), ); } @@ -52,19 +59,19 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa switch ($type) { case 'starred': - $qb = $this->repository->getBuilderForStarredByUser($user->getId()); + $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); break; case 'archive': - $qb = $this->repository->getBuilderForArchiveByUser($user->getId()); + $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); break; case 'unread': - $qb = $this->repository->getBuilderForUnreadByUser($user->getId()); + $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); break; case 'all': - $qb = $this->repository->getBuilderForAllByUser($user->getId()); + $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); break; default: @@ -78,13 +85,49 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ->groupBy('e.id') ->getQuery(); - $data = $this->repository - ->enableCache($query) + $data = $this->enableCache($query) ->getArrayResult(); return count($data); } + /** + * Return number of tags. + * + * @return int + */ + public function countTags() + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return []; + } + + $qb = $this->tagRepository->findAllTags($user->getId()); + + $data = $this->enableCache($qb->getQuery()) + ->getArrayResult(); + + return count($data); + } + + /** + * Enable cache for a query. + * + * @param Query $query + * + * @return Query + */ + public function enableCache(Query $query) + { + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($this->lifeTime); + + return $query; + } + public function getName() { return 'wallabag_extension'; -- cgit v1.2.3 From 13d9f7c96c131b93c1172d8bdc69098da9b68f68 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Sun, 4 Sep 2016 21:06:52 +0200 Subject: Switched enableCache visibility to private --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index ed6dadc5..59eb57ec 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -119,7 +119,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa * * @return Query */ - public function enableCache(Query $query) + private function enableCache(Query $query) { $query->useQueryCache(true); $query->useResultCache(true); -- cgit v1.2.3 From 5173fd1c3d05d1fadfbdd4db862cd744209e13ac Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Mon, 5 Sep 2016 14:17:44 +0200 Subject: Returned 0 instead of returning empty array for Twig Extension --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 59eb57ec..3780b13e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -54,7 +54,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; if (null === $user || !is_object($user)) { - return []; + return 0; } switch ($type) { @@ -101,7 +101,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; if (null === $user || !is_object($user)) { - return []; + return 0; } $qb = $this->tagRepository->findAllTags($user->getId()); -- cgit v1.2.3 From faa86e06ba3032fdb98f3c0f79c72e8581d3c96f Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 25 Sep 2016 11:21:13 +0200 Subject: Fix tags count in menu Move enable cache for Tag in the Entity because function `find*` should return result and not a Query --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 28 ++++------------------ 1 file changed, 5 insertions(+), 23 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 3780b13e..0a6896c9 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -85,10 +85,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa ->groupBy('e.id') ->getQuery(); - $data = $this->enableCache($query) - ->getArrayResult(); + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($this->lifeTime); - return count($data); + return count($query->getArrayResult()); } /** @@ -104,30 +105,11 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return 0; } - $qb = $this->tagRepository->findAllTags($user->getId()); - - $data = $this->enableCache($qb->getQuery()) - ->getArrayResult(); + $data = $this->tagRepository->findAllTags($user->getId()); return count($data); } - /** - * Enable cache for a query. - * - * @param Query $query - * - * @return Query - */ - private function enableCache(Query $query) - { - $query->useQueryCache(true); - $query->useResultCache(true); - $query->setResultCacheLifetime($this->lifeTime); - - return $query; - } - public function getName() { return 'wallabag_extension'; -- cgit v1.2.3 From 82fc3290d4fec45ede270e2c1ad2079fe3020adc Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 25 Sep 2016 12:03:12 +0200 Subject: CS --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 1 - 1 file changed, 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 0a6896c9..45dc591d 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,7 +2,6 @@ namespace Wallabag\CoreBundle\Twig; -use Doctrine\ORM\Query; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; -- cgit v1.2.3 From 289875836a09944f5993d33753042abfef13809e Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 25 Sep 2016 12:23:44 +0200 Subject: Fix tag count for PostgreSQL --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 45dc591d..fb4c7412 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -104,9 +104,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return 0; } - $data = $this->tagRepository->findAllTags($user->getId()); - - return count($data); + return $this->tagRepository->countAllTags($user->getId()); } public function getName() -- cgit v1.2.3 From 1264029cd413a4e29642a219e2647d886f0de0d6 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 1 Oct 2016 15:58:26 +0200 Subject: Add simple stats in footer --- src/Wallabag/CoreBundle/Twig/WallabagExtension.php | 40 +++++++++++++++++++++- 1 file changed, 39 insertions(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle/Twig/WallabagExtension.php') diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index fb4c7412..783cde3e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -5,6 +5,7 @@ namespace Wallabag\CoreBundle\Twig; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; +use Symfony\Component\Translation\TranslatorInterface; class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface { @@ -12,13 +13,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa private $entryRepository; private $tagRepository; private $lifeTime; + private $translator; - public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0) + public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator) { $this->entryRepository = $entryRepository; $this->tagRepository = $tagRepository; $this->tokenStorage = $tokenStorage; $this->lifeTime = $lifeTime; + $this->translator = $translator; } public function getFilters() @@ -33,6 +36,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return array( new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), + new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), ); } @@ -107,6 +111,40 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return $this->tagRepository->countAllTags($user->getId()); } + /** + * Display a single line about reading stats. + * + * @return string + */ + public function displayStats() + { + $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; + + if (null === $user || !is_object($user)) { + return 0; + } + + $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) + ->select('e.id') + ->groupBy('e.id') + ->getQuery(); + + $query->useQueryCache(true); + $query->useResultCache(true); + $query->setResultCacheLifetime($this->lifeTime); + + $nbArchives = count($query->getArrayResult()); + + $interval = $user->getCreatedAt()->diff(new \DateTime('now')); + $nbDays = (int) $interval->format('%a') ?: 1; + + return $this->translator->trans('footer.stats', [ + '%user_creation%' => $user->getCreatedAt()->format('F jS, Y'), + '%nb_archives%' => $nbArchives, + '%per_day%' => round($nbArchives / $nbDays, 2), + ]); + } + public function getName() { return 'wallabag_extension'; -- cgit v1.2.3