X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FTwig%2FWallabagExtension.php;h=00b1e595499d05f79de855e460a7a863af8a00a6;hb=2a1ceb67b4400f46f4d3067e887ff54aa906f0a2;hp=0a6896c95367f6b3ad0006875e8ca8cf9422bd92;hpb=faa86e06ba3032fdb98f3c0f79c72e8581d3c96f;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 0a6896c9..00b1e595 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,8 +2,8 @@ namespace Wallabag\CoreBundle\Twig; -use Doctrine\ORM\Query; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Translation\TranslatorInterface; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; @@ -13,28 +13,32 @@ 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() { return [ new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), + new \Twig_SimpleFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), ]; } public function getFunctions() { - return array( + return [ new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), - ); + new \Twig_SimpleFunction('display_stats', [$this, 'displayStats']), + ]; } public function removeWww($url) @@ -42,6 +46,13 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return preg_replace('/^www\./i', '', $url); } + public function removeSchemeAndWww($url) + { + return $this->removeWww( + preg_replace('@^https?://@i', '', $url) + ); + } + /** * Return number of entries depending of the type (unread, archive, starred or all). * @@ -53,7 +64,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)) { + if (null === $user || !\is_object($user)) { return 0; } @@ -61,19 +72,15 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa case 'starred': $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); break; - case 'archive': $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); break; - case 'unread': $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); break; - case 'all': $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); break; - default: throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); } @@ -89,7 +96,7 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa $query->useResultCache(true); $query->setResultCacheLifetime($this->lifeTime); - return count($query->getArrayResult()); + return \count($query->getArrayResult()); } /** @@ -101,13 +108,48 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa { $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; - if (null === $user || !is_object($user)) { + if (null === $user || !\is_object($user)) { + return 0; + } + + 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; } - $data = $this->tagRepository->findAllTags($user->getId()); + $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; + + // force setlocale for date translation + setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()))); - return count($data); + return $this->translator->trans('footer.stats', [ + '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()), + '%nb_archives%' => $nbArchives, + '%per_day%' => round($nbArchives / $nbDays, 2), + ]); } public function getName()