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