]> git.immae.eu Git - github/wallabag/wallabag.git/blob - src/Wallabag/CoreBundle/Twig/WallabagExtension.php
Fix tag count for PostgreSQL
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Twig / WallabagExtension.php
1 <?php
2
3 namespace Wallabag\CoreBundle\Twig;
4
5 use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
6 use Wallabag\CoreBundle\Repository\EntryRepository;
7 use Wallabag\CoreBundle\Repository\TagRepository;
8
9 class WallabagExtension extends \Twig_Extension implements \Twig_Extension_GlobalsInterface
10 {
11 private $tokenStorage;
12 private $entryRepository;
13 private $tagRepository;
14 private $lifeTime;
15
16 public function __construct(EntryRepository $entryRepository = null, TagRepository $tagRepository = null, TokenStorageInterface $tokenStorage = null, $lifeTime = 0)
17 {
18 $this->entryRepository = $entryRepository;
19 $this->tagRepository = $tagRepository;
20 $this->tokenStorage = $tokenStorage;
21 $this->lifeTime = $lifeTime;
22 }
23
24 public function getFilters()
25 {
26 return [
27 new \Twig_SimpleFilter('removeWww', [$this, 'removeWww']),
28 ];
29 }
30
31 public function getFunctions()
32 {
33 return array(
34 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
35 new \Twig_SimpleFunction('count_tags', [$this, 'countTags']),
36 );
37 }
38
39 public function removeWww($url)
40 {
41 return preg_replace('/^www\./i', '', $url);
42 }
43
44 /**
45 * Return number of entries depending of the type (unread, archive, starred or all).
46 *
47 * @param string $type Type of entries to count
48 *
49 * @return int
50 */
51 public function countEntries($type)
52 {
53 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
54
55 if (null === $user || !is_object($user)) {
56 return 0;
57 }
58
59 switch ($type) {
60 case 'starred':
61 $qb = $this->entryRepository->getBuilderForStarredByUser($user->getId());
62 break;
63
64 case 'archive':
65 $qb = $this->entryRepository->getBuilderForArchiveByUser($user->getId());
66 break;
67
68 case 'unread':
69 $qb = $this->entryRepository->getBuilderForUnreadByUser($user->getId());
70 break;
71
72 case 'all':
73 $qb = $this->entryRepository->getBuilderForAllByUser($user->getId());
74 break;
75
76 default:
77 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
78 }
79
80 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
81 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
82 $query = $qb
83 ->select('e.id')
84 ->groupBy('e.id')
85 ->getQuery();
86
87 $query->useQueryCache(true);
88 $query->useResultCache(true);
89 $query->setResultCacheLifetime($this->lifeTime);
90
91 return count($query->getArrayResult());
92 }
93
94 /**
95 * Return number of tags.
96 *
97 * @return int
98 */
99 public function countTags()
100 {
101 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
102
103 if (null === $user || !is_object($user)) {
104 return 0;
105 }
106
107 return $this->tagRepository->countAllTags($user->getId());
108 }
109
110 public function getName()
111 {
112 return 'wallabag_extension';
113 }
114 }