X-Git-Url: https://git.immae.eu/?a=blobdiff_plain;f=src%2FWallabag%2FCoreBundle%2FTwig%2FWallabagExtension.php;h=47af3c8ec69afb6028bd5b08f05a488b5af5c2a5;hb=33e3eeaec851158289e1a236cfe08a475cc6364a;hp=3780b13e7db5884abf3b9e156dff8db0e0b8c397;hpb=6f23289e721bd14710af1acc23466432c1312850;p=github%2Fwallabag%2Fwallabag.git diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php index 3780b13e..47af3c8e 100644 --- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php +++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php @@ -2,39 +2,56 @@ namespace Wallabag\CoreBundle\Twig; -use Doctrine\ORM\Query; use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; +use Symfony\Component\Translation\TranslatorInterface; +use Twig\Extension\AbstractExtension; +use Twig\Extension\GlobalsInterface; +use Twig\TwigFilter; +use Twig\TwigFunction; use Wallabag\CoreBundle\Repository\EntryRepository; use Wallabag\CoreBundle\Repository\TagRepository; -class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface +class WallabagExtension extends AbstractExtension implements GlobalsInterface { private $tokenStorage; private $entryRepository; private $tagRepository; private $lifeTime; + private $translator; + private $rootDir; - 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, string $rootDir) { $this->entryRepository = $entryRepository; $this->tagRepository = $tagRepository; $this->tokenStorage = $tokenStorage; $this->lifeTime = $lifeTime; + $this->translator = $translator; + $this->rootDir = $rootDir; + } + + public function getGlobals() + { + return []; } public function getFilters() { return [ - new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']), + new TwigFilter('removeWww', [$this, 'removeWww']), + new TwigFilter('removeScheme', [$this, 'removeScheme']), + new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), ]; } public function getFunctions() { - return array( - new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']), - new \Twig_SimpleFunction('count_tags', [$this, 'countTags']), - ); + return [ + new TwigFunction('count_entries', [$this, 'countEntries']), + new TwigFunction('count_tags', [$this, 'countTags']), + new TwigFunction('display_stats', [$this, 'displayStats']), + new TwigFunction('asset_file_exists', [$this, 'assetFileExists']), + ]; } public function removeWww($url) @@ -42,6 +59,16 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa return preg_replace('/^www\./i', '', $url); } + public function removeScheme($url) + { + return preg_replace('#^https?://#i', '', $url); + } + + public function removeSchemeAndWww($url) + { + return $this->removeWww($this->removeScheme($url)); + } + /** * Return number of entries depending of the type (unread, archive, starred or all). * @@ -53,7 +80,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 +88,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)); } @@ -85,10 +108,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()); } /** @@ -100,32 +124,53 @@ 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; } - $qb = $this->tagRepository->findAllTags($user->getId()); - - $data = $this->enableCache($qb->getQuery()) - ->getArrayResult(); - - return count($data); + return $this->tagRepository->countAllTags($user->getId()); } /** - * Enable cache for a query. - * - * @param Query $query + * Display a single line about reading stats. * - * @return Query + * @return string */ - private function enableCache(Query $query) + 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); - return $query; + $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 $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 assetFileExists($name) + { + return file_exists(realpath($this->rootDir . '/../web/' . $name)); } public function getName()