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;
23 public function __construct(EntryRepository
$entryRepository, TagRepository
$tagRepository, TokenStorageInterface
$tokenStorage, $lifeTime, TranslatorInterface
$translator, string $rootDir)
25 $this->entryRepository
= $entryRepository;
26 $this->tagRepository
= $tagRepository;
27 $this->tokenStorage
= $tokenStorage;
28 $this->lifeTime
= $lifeTime;
29 $this->translator
= $translator;
30 $this->rootDir
= $rootDir;
33 public function getGlobals()
38 public function getFilters()
41 new TwigFilter('removeWww', [$this, 'removeWww']),
42 new TwigFilter('removeScheme', [$this, 'removeScheme']),
43 new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']),
47 public function getFunctions()
50 new TwigFunction('count_entries', [$this, 'countEntries']),
51 new TwigFunction('count_tags', [$this, 'countTags']),
52 new TwigFunction('display_stats', [$this, 'displayStats']),
53 new TwigFunction('asset_file_exists', [$this, 'assetFileExists']),
57 public function removeWww($url)
59 return preg_replace('/^www\./i', '', $url);
62 public function removeScheme($url)
64 return preg_replace('#^https?://#i', '', $url);
67 public function removeSchemeAndWww($url)
69 return $this->removeWww($this->removeScheme($url));
73 * Return number of entries depending of the type (unread, archive, starred or all).
75 * @param string $type Type of entries to count
79 public function countEntries($type)
81 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
83 if (null === $user || !\
is_object($user)) {
89 $qb = $this->entryRepository
->getBuilderForStarredByUser($user->getId());
92 $qb = $this->entryRepository
->getBuilderForArchiveByUser($user->getId());
95 $qb = $this->entryRepository
->getBuilderForUnreadByUser($user->getId());
98 $qb = $this->entryRepository
->getBuilderForAllByUser($user->getId());
101 throw new \
InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
104 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
105 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
111 $query->useQueryCache(true);
112 $query->useResultCache(true);
113 $query->setResultCacheLifetime($this->lifeTime
);
115 return \
count($query->getArrayResult());
119 * Return number of tags.
123 public function countTags()
125 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
127 if (null === $user || !\
is_object($user)) {
131 return $this->tagRepository
->countAllTags($user->getId());
135 * Display a single line about reading stats.
139 public function displayStats()
141 $user = $this->tokenStorage
->getToken() ? $this->tokenStorage
->getToken()->getUser() : null;
143 if (null === $user || !\
is_object($user)) {
147 $query = $this->entryRepository
->getBuilderForArchiveByUser($user->getId())
152 $query->useQueryCache(true);
153 $query->useResultCache(true);
154 $query->setResultCacheLifetime($this->lifeTime
);
156 $nbArchives = \
count($query->getArrayResult());
158 $interval = $user->getCreatedAt()->diff(new \
DateTime('now'));
159 $nbDays = (int) $interval->format('%a') ?: 1;
161 // force setlocale for date translation
162 setlocale(LC_TIME
, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage())));
164 return $this->translator
->trans('footer.stats', [
165 '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()),
166 '%nb_archives%' => $nbArchives,
167 '%per_day%' => round($nbArchives / $nbDays, 2),
171 public function assetFileExists($name)
173 return file_exists(realpath($this->rootDir
. '/../web/' . $name));
176 public function getName()
178 return 'wallabag_extension';