]>
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; |
8315130a | 21 | |
1264029c | 22 | public function __construct(EntryRepository $entryRepository, TagRepository $tagRepository, TokenStorageInterface $tokenStorage, $lifeTime, TranslatorInterface $translator) |
8315130a | 23 | { |
429d86f3 NL |
24 | $this->entryRepository = $entryRepository; |
25 | $this->tagRepository = $tagRepository; | |
8315130a | 26 | $this->tokenStorage = $tokenStorage; |
429d86f3 | 27 | $this->lifeTime = $lifeTime; |
1264029c | 28 | $this->translator = $translator; |
8315130a NL |
29 | } |
30 | ||
a2f4efe6 JB |
31 | public function getGlobals() |
32 | { | |
33 | return []; | |
34 | } | |
35 | ||
72fcaf8a NL |
36 | public function getFilters() |
37 | { | |
4094ea47 | 38 | return [ |
a2f4efe6 JB |
39 | new TwigFilter('removeWww', [$this, 'removeWww']), |
40 | new TwigFilter('removeScheme', [$this, 'removeScheme']), | |
41 | new TwigFilter('removeSchemeAndWww', [$this, 'removeSchemeAndWww']), | |
4094ea47 | 42 | ]; |
72fcaf8a NL |
43 | } |
44 | ||
59ddb9ae JB |
45 | public function getFunctions() |
46 | { | |
f808b016 | 47 | return [ |
a2f4efe6 JB |
48 | new TwigFunction('count_entries', [$this, 'countEntries']), |
49 | new TwigFunction('count_tags', [$this, 'countTags']), | |
50 | new TwigFunction('display_stats', [$this, 'displayStats']), | |
f808b016 | 51 | ]; |
59ddb9ae JB |
52 | } |
53 | ||
72fcaf8a NL |
54 | public function removeWww($url) |
55 | { | |
cfb28c9d | 56 | return preg_replace('/^www\./i', '', $url); |
72fcaf8a NL |
57 | } |
58 | ||
531c8d0a TC |
59 | public function removeScheme($url) |
60 | { | |
61 | return preg_replace('#^https?://#i', '', $url); | |
62 | } | |
63 | ||
e50e45d6 KD |
64 | public function removeSchemeAndWww($url) |
65 | { | |
f277bc04 | 66 | return $this->removeWww($this->removeScheme($url)); |
e50e45d6 KD |
67 | } |
68 | ||
59ddb9ae | 69 | /** |
234ad944 | 70 | * Return number of entries depending of the type (unread, archive, starred or all). |
59ddb9ae | 71 | * |
234ad944 | 72 | * @param string $type Type of entries to count |
59ddb9ae JB |
73 | * |
74 | * @return int | |
75 | */ | |
76 | public function countEntries($type) | |
8315130a NL |
77 | { |
78 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
79 | ||
2a1ceb67 | 80 | if (null === $user || !\is_object($user)) { |
5173fd1c | 81 | return 0; |
8315130a NL |
82 | } |
83 | ||
59ddb9ae JB |
84 | switch ($type) { |
85 | case 'starred': | |
429d86f3 | 86 | $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId()); |
59ddb9ae | 87 | break; |
59ddb9ae | 88 | case 'archive': |
429d86f3 | 89 | $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId()); |
59ddb9ae | 90 | break; |
59ddb9ae | 91 | case 'unread': |
429d86f3 | 92 | $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId()); |
59ddb9ae | 93 | break; |
59ddb9ae | 94 | case 'all': |
429d86f3 | 95 | $qb = $this->entryRepository->getBuilderForAllByUser($user->getId()); |
59ddb9ae | 96 | break; |
59ddb9ae JB |
97 | default: |
98 | throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); | |
99 | } | |
100 | ||
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 | |
103 | $query = $qb | |
104 | ->select('e.id') | |
105 | ->groupBy('e.id') | |
106 | ->getQuery(); | |
107 | ||
faa86e06 JB |
108 | $query->useQueryCache(true); |
109 | $query->useResultCache(true); | |
110 | $query->setResultCacheLifetime($this->lifeTime); | |
59ddb9ae | 111 | |
2a1ceb67 | 112 | return \count($query->getArrayResult()); |
8315130a NL |
113 | } |
114 | ||
429d86f3 NL |
115 | /** |
116 | * Return number of tags. | |
117 | * | |
118 | * @return int | |
119 | */ | |
120 | public function countTags() | |
121 | { | |
122 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
123 | ||
2a1ceb67 | 124 | if (null === $user || !\is_object($user)) { |
5173fd1c | 125 | return 0; |
429d86f3 NL |
126 | } |
127 | ||
28987583 | 128 | return $this->tagRepository->countAllTags($user->getId()); |
429d86f3 NL |
129 | } |
130 | ||
1264029c JB |
131 | /** |
132 | * Display a single line about reading stats. | |
133 | * | |
134 | * @return string | |
135 | */ | |
136 | public function displayStats() | |
137 | { | |
138 | $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; | |
139 | ||
2a1ceb67 | 140 | if (null === $user || !\is_object($user)) { |
1264029c JB |
141 | return 0; |
142 | } | |
143 | ||
144 | $query = $this->entryRepository->getBuilderForArchiveByUser($user->getId()) | |
145 | ->select('e.id') | |
146 | ->groupBy('e.id') | |
147 | ->getQuery(); | |
148 | ||
149 | $query->useQueryCache(true); | |
150 | $query->useResultCache(true); | |
151 | $query->setResultCacheLifetime($this->lifeTime); | |
152 | ||
2a1ceb67 | 153 | $nbArchives = \count($query->getArrayResult()); |
1264029c JB |
154 | |
155 | $interval = $user->getCreatedAt()->diff(new \DateTime('now')); | |
156 | $nbDays = (int) $interval->format('%a') ?: 1; | |
157 | ||
576d285d | 158 | // force setlocale for date translation |
f808b016 | 159 | setlocale(LC_TIME, strtolower($user->getConfig()->getLanguage()) . '_' . strtoupper(strtolower($user->getConfig()->getLanguage()))); |
576d285d | 160 | |
1264029c | 161 | return $this->translator->trans('footer.stats', [ |
576d285d | 162 | '%user_creation%' => strftime('%e %B %Y', $user->getCreatedAt()->getTimestamp()), |
1264029c JB |
163 | '%nb_archives%' => $nbArchives, |
164 | '%per_day%' => round($nbArchives / $nbDays, 2), | |
165 | ]); | |
166 | } | |
167 | ||
72fcaf8a NL |
168 | public function getName() |
169 | { | |
170 | return 'wallabag_extension'; | |
171 | } | |
172 | } |