3 namespace Wallabag\CoreBundle\Twig
;
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
6 use Symfony\Component\Translation\TranslatorInterface
;
7 use Wallabag\CoreBundle\Repository\EntryRepository
;
8 use Wallabag\CoreBundle\Repository\TagRepository
;
10 class WallabagExtension
extends \Twig_Extension
implements \Twig_Extension_GlobalsInterface
12 private $tokenStorage;
13 private $entryRepository;
14 private $tagRepository;
18 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, TokenStorageInterface
$tokenStorage, $lifeTime, TranslatorInterface
$translator)
20 $this->entryRepository
= $entryRepository;
21 $this->tagRepository
= $tagRepository;
22 $this->tokenStorage
= $tokenStorage;
23 $this->lifeTime
= $lifeTime;
24 $this->translator
= $translator;
27 public function getFilters()
30 new \
Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
31 new \
Twig_SimpleFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
35 public function getFunctions()
38 new \
Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
39 new \
Twig_SimpleFunction('count_tags', [$this, 'countTags']),
40 new \
Twig_SimpleFunction('display_stats', [$this, 'displayStats']),
44 public function removeWww($url)
46 return preg_replace('/^www\./i', '', $url);
49 public function removeSchemeAndWww($url)
51 return $this->removeWww(
52 preg_replace('@^https?://@i', '', $url)
57 * Return number of entries depending of the type (unread, archive, starred or all).
59 * @param string $type Type of entries to count
63 public function countEntries($type)
65 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
67 if (null === $user || !is_object($user)) {
73 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
76 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
79 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
82 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
85 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
88 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
89 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
95 $query->useQueryCache(true);
96 $query->useResultCache(true);
97 $query->setResultCacheLifetime($this->lifeTime
);
99 return count($query->getArrayResult());
103 * Return number of tags.
107 public function countTags()
109 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
111 if (null === $user || !is_object($user)) {
115 return $this->tagRepository
->countAllTags($user->getId());
119 * Display a single line about reading stats.
123 public function displayStats()
125 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
127 if (null === $user || !is_object($user)) {
131 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
136 $query->useQueryCache(true);
137 $query->useResultCache(true);
138 $query->setResultCacheLifetime($this->lifeTime
);
140 $nbArchives = count($query->getArrayResult());
142 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
143 $nbDays = (int) $interval->format('%a') ?: 1;
145 // force setlocale for date translation
146 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
148 return $this->translator
->trans('footer.stats', [
149 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
150 '%nb_archives%' => $nbArchives,
151 '%per_day%' => round($nbArchives / $nbDays, 2),
155 public function getName()
157 return 'wallabag_extension';