]>
Commit | Line | Data |
---|---|---|
72fcaf8a NL |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Twig; | |
4 | ||
8315130a | 5 | use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface; |
f808b016 | 6 | use Symfony\Component\Translation\TranslatorInterface; |
a2f4efe6 JB |
7 | use Twig\Extension\AbstractExtension; |
8 | use Twig\Extension\GlobalsInterface; | |
9 | use Twig\TwigFilter; | |
10 | use Twig\TwigFunction; | |
8315130a | 11 | use Wallabag\CoreBundle\Repository\EntryRepository; |
429d86f3 | 12 | use Wallabag\CoreBundle\Repository\TagRepository; |
8315130a | 13 | |
a2f4efe6 | 14 | class WallabagExtension extends AbstractExtension implements GlobalsInterface |
72fcaf8a | 15 | { |
8315130a | 16 | private $tokenStorage; |
429d86f3 NL |
17 | private $entryRepository; |
18 | private $tagRepository; | |
19 | private $lifeTime; | |
1264029c | 20 | private $translator; |
33e3eeae | 21 | private $rootDir; |
8315130a | 22 | |
33e3eeae | 23 | public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator, string $rootDir) |
8315130a | 24 | { |
429d86f3 NL |
25 | $this->entryRepository = $entryRepository; |
26 | $this->tagRepository = $tagRepository; | |
8315130a | 27 | $this->tokenStorage = $tokenStorage; |
429d86f3 | 28 | $this->lifeTime = $lifeTime; |
1264029c | 29 | $this->translator = $translator; |
33e3eeae | 30 | $this->rootDir = $rootDir; |
8315130a NL |
31 | } |
32 | ||
a2f4efe6 JB |
33 | public function getGlobals() |
34 | { | |
35 | return []; | |
36 | } | |
37 | ||
72fcaf8a NL |
38 | public function getFilters() |
39 | { | |
4094ea47 | 40 | return [ |
a2f4efe6 JB |
41 | new TwigFilter('removeWww', [$this, 'removeWww']), |
42 | new TwigFilter('removeScheme', [$this, 'removeScheme']), | |
43 | new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), | |
4094ea47 | 44 | ]; |
72fcaf8a NL |
45 | } |
46 | ||
59ddb9ae JB |
47 | public function getFunctions() |
48 | { | |
f808b016 | 49 | return [ |
a2f4efe6 JB |
50 | new TwigFunction('count_entries', [$this, 'countEntries']), |
51 | new TwigFunction('count_tags', [$this, 'countTags']), | |
52 | new TwigFunction('display_stats', [$this, 'displayStats']), | |
33e3eeae | 53 | new TwigFunction('asset_file_exists', [$this, 'assetFileExists']), |
f808b016 | 54 | ]; |
59ddb9ae JB |
55 | } |
56 | ||
72fcaf8a NL |
57 | public function removeWww($url) |
58 | { | |
cfb28c9d | 59 | return preg_replace('/^www\./i', '', $url); |
72fcaf8a NL |
60 | } |
61 | ||
531c8d0a TC |
62 | public function removeScheme($url) |
63 | { | |
64 | return preg_replace('#^https?://#i', '', $url); | |
65 | } | |
66 | ||
e50e45d6 KD |
67 | public function removeSchemeAndWww($url) |
68 | { | |
f277bc04 | 69 | return $this->removeWww($this->removeScheme($url)); |
e50e45d6 KD |
70 | } |
71 | ||
59ddb9ae | 72 | /** |
234ad944 | 73 | * Return number of entries depending of the type (unread, archive, starred or all). |
59ddb9ae | 74 | * |
234ad944 | 75 | * @param string $type Type of entries to count |
59ddb9ae JB |
76 | * |
77 | * @return int | |
78 | */ | |
79 | public function countEntries($type) | |
8315130a NL |
80 | { |
81 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
82 | ||
2a1ceb67 | 83 | if (null === $user || !\is_object($user)) { |
5173fd1c | 84 | return 0; |
8315130a NL |
85 | } |
86 | ||
59ddb9ae JB |
87 | switch ($type) { |
88 | case 'starred': | |
429d86f3 | 89 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); |
59ddb9ae | 90 | break; |
59ddb9ae | 91 | case 'archive': |
429d86f3 | 92 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); |
59ddb9ae | 93 | break; |
59ddb9ae | 94 | case 'unread': |
429d86f3 | 95 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); |
59ddb9ae | 96 | break; |
4b997bc8 NL |
97 | case 'with_annotations': |
98 | $qb = $this->entryRepository->getBuilderForAnnotationsByUser($user->getId()); | |
99 | break; | |
59ddb9ae | 100 | case 'all': |
429d86f3 | 101 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); |
59ddb9ae | 102 | break; |
59ddb9ae JB |
103 | default: |
104 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
105 | } | |
106 | ||
107 | // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id) | |
108 | // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function | |
109 | $query = $qb | |
110 | ->select('e.id') | |
111 | ->groupBy('e.id') | |
112 | ->getQuery(); | |
113 | ||
faa86e06 JB |
114 | $query->useQueryCache(true); |
115 | $query->useResultCache(true); | |
116 | $query->setResultCacheLifetime($this->lifeTime); | |
59ddb9ae | 117 | |
2a1ceb67 | 118 | return \count($query->getArrayResult()); |
8315130a NL |
119 | } |
120 | ||
429d86f3 NL |
121 | /** |
122 | * Return number of tags. | |
123 | * | |
124 | * @return int | |
125 | */ | |
126 | public function countTags() | |
127 | { | |
128 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
129 | ||
2a1ceb67 | 130 | if (null === $user || !\is_object($user)) { |
5173fd1c | 131 | return 0; |
429d86f3 NL |
132 | } |
133 | ||
28987583 | 134 | return $this->tagRepository->countAllTags($user->getId()); |
429d86f3 NL |
135 | } |
136 | ||
1264029c JB |
137 | /** |
138 | * Display a single line about reading stats. | |
139 | * | |
140 | * @return string | |
141 | */ | |
142 | public function displayStats() | |
143 | { | |
144 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
145 | ||
2a1ceb67 | 146 | if (null === $user || !\is_object($user)) { |
1264029c JB |
147 | return 0; |
148 | } | |
149 | ||
150 | $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) | |
151 | ->select('e.id') | |
152 | ->groupBy('e.id') | |
153 | ->getQuery(); | |
154 | ||
155 | $query->useQueryCache(true); | |
156 | $query->useResultCache(true); | |
157 | $query->setResultCacheLifetime($this->lifeTime); | |
158 | ||
2a1ceb67 | 159 | $nbArchives = \count($query->getArrayResult()); |
1264029c JB |
160 | |
161 | $interval = $user->getCreatedAt()->diff(new \DateTime('now')); | |
162 | $nbDays = (int) $interval->format('%a') ?: 1; | |
163 | ||
576d285d | 164 | // force setlocale for date translation |
f808b016 | 165 | setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()))); |
576d285d | 166 | |
1264029c | 167 | return $this->translator->trans('footer.stats', [ |
576d285d | 168 | '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()), |
1264029c JB |
169 | '%nb_archives%' => $nbArchives, |
170 | '%per_day%' => round($nbArchives / $nbDays, 2), | |
171 | ]); | |
172 | } | |
173 | ||
33e3eeae S |
174 | public function assetFileExists($name) |
175 | { | |
176 | return file_exists(realpath($this->rootDir . '/../web/' . $name)); | |
177 | } | |
178 | ||
72fcaf8a NL |
179 | public function getName() |
180 | { | |
181 | return 'wallabag_extension'; | |
182 | } | |
183 | } |