3 namespace Wallabag\CoreBundle\Twig
;
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface
;
6 use Symfony\Component\Translation\TranslatorInterface
;
7 use Twig\Extension\AbstractExtension
;
8 use Twig\Extension\GlobalsInterface
;
10 use Twig\TwigFunction
;
11 use Wallabag\CoreBundle\Repository\EntryRepository
;
12 use Wallabag\CoreBundle\Repository\TagRepository
;
14 class WallabagExtension
extends AbstractExtension
implements GlobalsInterface
16 private $tokenStorage;
17 private $entryRepository;
18 private $tagRepository;
22 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, TokenStorageInterface
$tokenStorage, $lifeTime, TranslatorInterface
$translator)
24 $this->entryRepository
= $entryRepository;
25 $this->tagRepository
= $tagRepository;
26 $this->tokenStorage
= $tokenStorage;
27 $this->lifeTime
= $lifeTime;
28 $this->translator
= $translator;
31 public function getGlobals()
36 public function getFilters()
39 new TwigFilter('removeWww', [$this, 'removeWww']),
40 new TwigFilter('removeScheme', [$this, 'removeScheme']),
41 new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
45 public function getFunctions()
48 new TwigFunction('count_entries', [$this, 'countEntries']),
49 new TwigFunction('count_tags', [$this, 'countTags']),
50 new TwigFunction('display_stats', [$this, 'displayStats']),
54 public function removeWww($url)
56 return preg_replace('/^www\./i', '', $url);
59 public function removeScheme($url)
61 return preg_replace('#^https?://#i', '', $url);
64 public function removeSchemeAndWww($url)
66 return $this->removeWww($this->removeScheme($url));
70 * Return number of entries depending of the type (unread, archive, starred or all).
72 * @param string $type Type of entries to count
76 public function countEntries($type)
78 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
80 if (null === $user || !\
is_object($user)) {
86 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
89 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
92 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
95 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
98 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
101 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
102 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
108 $query->useQueryCache(true);
109 $query->useResultCache(true);
110 $query->setResultCacheLifetime($this->lifeTime
);
112 return \
count($query->getArrayResult());
116 * Return number of tags.
120 public function countTags()
122 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
124 if (null === $user || !\
is_object($user)) {
128 return $this->tagRepository
->countAllTags($user->getId());
132 * Display a single line about reading stats.
136 public function displayStats()
138 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
140 if (null === $user || !\
is_object($user)) {
144 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
149 $query->useQueryCache(true);
150 $query->useResultCache(true);
151 $query->setResultCacheLifetime($this->lifeTime
);
153 $nbArchives = \
count($query->getArrayResult());
155 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
156 $nbDays = (int) $interval->format('%a') ?: 1;
158 // force setlocale for date translation
159 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
161 return $this->translator
->trans('footer.stats', [
162 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
163 '%nb_archives%' => $nbArchives,
164 '%per_day%' => round($nbArchives / $nbDays, 2),
168 public function getName()
170 return 'wallabag_extension';