aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorJeremy Benoist <j0k3r@users.noreply.github.com>2016-09-04 20:07:45 +0200
committerGitHub <noreply@github.com>2016-09-04 20:07:45 +0200
commitc3b53188d75a01e92f2be7204fc961a7636a1827 (patch)
tree691f7415a480757dce600fcae15f2ab1f47e3e96
parent8f8654913ce82be12219a37a24630066bbe950c2 (diff)
parent234ad944534cf51118f63f0b48c206b5d9a70fe5 (diff)
downloadwallabag-c3b53188d75a01e92f2be7204fc961a7636a1827.tar.gz
wallabag-c3b53188d75a01e92f2be7204fc961a7636a1827.tar.zst
wallabag-c3b53188d75a01e92f2be7204fc961a7636a1827.zip
Merge pull request #2263 from wallabag/speed-up-count
Instead of selecting the whole data, just count it
-rw-r--r--src/Wallabag/CoreBundle/Controller/DeveloperController.php4
-rw-r--r--src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig8
-rw-r--r--src/Wallabag/CoreBundle/Twig/WallabagExtension.php65
3 files changed, 51 insertions, 26 deletions
diff --git a/src/Wallabag/CoreBundle/Controller/DeveloperController.php b/src/Wallabag/CoreBundle/Controller/DeveloperController.php
index 1ea220e5..63386db0 100644
--- a/src/Wallabag/CoreBundle/Controller/DeveloperController.php
+++ b/src/Wallabag/CoreBundle/Controller/DeveloperController.php
@@ -49,7 +49,7 @@ class DeveloperController extends Controller
49 49
50 $this->get('session')->getFlashBag()->add( 50 $this->get('session')->getFlashBag()->add(
51 'notice', 51 'notice',
52 $this->get('translator')->trans('flashes.developer.notice.client_created', array('%name%' => $client->getName())) 52 $this->get('translator')->trans('flashes.developer.notice.client_created', ['%name%' => $client->getName()])
53 ); 53 );
54 54
55 return $this->render('WallabagCoreBundle:Developer:client_parameters.html.twig', [ 55 return $this->render('WallabagCoreBundle:Developer:client_parameters.html.twig', [
@@ -81,7 +81,7 @@ class DeveloperController extends Controller
81 81
82 $this->get('session')->getFlashBag()->add( 82 $this->get('session')->getFlashBag()->add(
83 'notice', 83 'notice',
84 $this->get('translator')->trans('flashes.developer.notice.client_deleted', array('%name%' => $client->getName())) 84 $this->get('translator')->trans('flashes.developer.notice.client_deleted', ['%name%' => $client->getName()])
85 ); 85 );
86 86
87 return $this->redirect($this->generateUrl('developer')); 87 return $this->redirect($this->generateUrl('developer'));
diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
index f64c3da2..06ecbf3d 100644
--- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
+++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig
@@ -35,16 +35,16 @@
35 {% set currentRoute = app.request.attributes.get('_route') %} 35 {% set currentRoute = app.request.attributes.get('_route') %}
36 36
37 <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}"> 37 <li class="bold {% if currentRoute == 'unread' or currentRoute == 'homepage' %}active{% endif %}">
38 <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ unreadEntries }}</span></a> 38 <a class="waves-effect" href="{{ path('unread') }}">{{ 'menu.left.unread'|trans }} <span class="numberItems grey-text">{{ count_entries('unread') }}</span></a>
39 </li> 39 </li>
40 <li class="bold {% if currentRoute == 'starred' %}active{% endif %}"> 40 <li class="bold {% if currentRoute == 'starred' %}active{% endif %}">
41 <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ starredEntries }}</span></a> 41 <a class="waves-effect" href="{{ path('starred') }}">{{ 'menu.left.starred'|trans }} <span class="numberItems grey-text">{{ count_entries('starred') }}</span></a>
42 </li> 42 </li>
43 <li class="bold {% if currentRoute == 'archive' %}active{% endif %}"> 43 <li class="bold {% if currentRoute == 'archive' %}active{% endif %}">
44 <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ archivedEntries }}</span></a> 44 <a class="waves-effect" href="{{ path('archive') }}">{{ 'menu.left.archive'|trans }} <span class="numberItems grey-text">{{ count_entries('archive') }}</span></a>
45 </li> 45 </li>
46 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}"> 46 <li class="bold border-bottom {% if currentRoute == 'all' %}active{% endif %}">
47 <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ allEntries }}</span></a> 47 <a class="waves-effect" href="{{ path('all') }}">{{ 'menu.left.all_articles'|trans }} <span class="numberItems grey-text">{{ count_entries('all') }}</span></a>
48 </li> 48 </li>
49 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}"> 49 <li class="bold border-bottom {% if currentRoute == 'tags' %}active{% endif %}">
50 <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a> 50 <a class="waves-effect" href="{{ path('tag') }}">{{ 'menu.left.tags'|trans }}</a>
diff --git a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
index 5c475d61..974b86a9 100644
--- a/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
+++ b/src/Wallabag/CoreBundle/Twig/WallabagExtension.php
@@ -23,41 +23,66 @@ class WallabagExtension extends \Twig_Extension implements \Twig_Extension_Globa
23 ]; 23 ];
24 } 24 }
25 25
26 public function getFunctions()
27 {
28 return array(
29 new \Twig_SimpleFunction('count_entries', [$this, 'countEntries']),
30 );
31 }
32
26 public function removeWww($url) 33 public function removeWww($url)
27 { 34 {
28 return preg_replace('/^www\./i', '', $url); 35 return preg_replace('/^www\./i', '', $url);
29 } 36 }
30 37
31 public function getGlobals() 38 /**
39 * Return number of entries depending of the type (unread, archive, starred or all).
40 *
41 * @param string $type Type of entries to count
42 *
43 * @return int
44 */
45 public function countEntries($type)
32 { 46 {
33 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null; 47 $user = $this->tokenStorage->getToken() ? $this->tokenStorage->getToken()->getUser() : null;
34 48
35 if (null === $user || !is_object($user)) { 49 if (null === $user || !is_object($user)) {
36 return array(); 50 return [];
37 } 51 }
38 52
39 $unreadEntries = $this->repository->enableCache( 53 switch ($type) {
40 $this->repository->getBuilderForUnreadByUser($user->getId())->getQuery() 54 case 'starred':
41 ); 55 $qb = $this->repository->getBuilderForStarredByUser($user->getId());
56 break;
42 57
43 $starredEntries = $this->repository->enableCache( 58 case 'archive':
44 $this->repository->getBuilderForStarredByUser($user->getId())->getQuery() 59 $qb = $this->repository->getBuilderForArchiveByUser($user->getId());
45 ); 60 break;
46 61
47 $archivedEntries = $this->repository->enableCache( 62 case 'unread':
48 $this->repository->getBuilderForArchiveByUser($user->getId())->getQuery() 63 $qb = $this->repository->getBuilderForUnreadByUser($user->getId());
49 ); 64 break;
50 65
51 $allEntries = $this->repository->enableCache( 66 case 'all':
52 $this->repository->getBuilderForAllByUser($user->getId())->getQuery() 67 $qb = $this->repository->getBuilderForAllByUser($user->getId());
53 ); 68 break;
54 69
55 return array( 70 default:
56 'unreadEntries' => count($unreadEntries->getResult()), 71 throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type));
57 'starredEntries' => count($starredEntries->getResult()), 72 }
58 'archivedEntries' => count($archivedEntries->getResult()), 73
59 'allEntries' => count($allEntries->getResult()), 74 // THANKS to PostgreSQL we CAN'T make a DEAD SIMPLE count(e.id)
60 ); 75 // ERROR: column "e0_.id" must appear in the GROUP BY clause or be used in an aggregate function
76 $query = $qb
77 ->select('e.id')
78 ->groupBy('e.id')
79 ->getQuery();
80
81 $data = $this->repository
82 ->enableCache($query)
83 ->getArrayResult();
84
85 return count($data);
61 } 86 }
62 87
63 public function getName() 88 public function getName()