From 0ab7404f93670ed161b67528c1a61c5bfa92d42b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:10:06 +0200 Subject: Refactorize the way to retrieve entries One place to retrieve entries in Entry & Rss controller. More simple and easy to maintain. --- .../CoreBundle/Controller/EntryController.php | 105 ++++++++++----------- .../CoreBundle/Controller/RssController.php | 73 +++++++------- .../CoreBundle/Repository/EntryRepository.php | 47 +++++---- 3 files changed, 117 insertions(+), 108 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Controller/EntryController.php b/src/Wallabag/CoreBundle/Controller/EntryController.php index 006fa396..dc399b8a 100644 --- a/src/Wallabag/CoreBundle/Controller/EntryController.php +++ b/src/Wallabag/CoreBundle/Controller/EntryController.php @@ -113,34 +113,7 @@ class EntryController extends Controller */ public function showUnreadAction(Request $request, $page) { - $form = $this->get('form.factory')->create(new EntryFilterType()); - - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findUnreadByUser($this->getUser()->getId()); - - if ($request->query->has($form->getName())) { - // manually bind values from the request - $form->submit($request->query->get($form->getName())); - - // build the query from the given form object - $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); - } - - $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); - $entries->setCurrentPage($page); - - return $this->render( - 'WallabagCoreBundle:Entry:entries.html.twig', - array( - 'form' => $form->createView(), - 'entries' => $entries, - 'currentPage' => $page, - ) - ); + return $this->showEntries('unread', $request, $page); } /** @@ -155,21 +128,66 @@ class EntryController extends Controller */ public function showArchiveAction(Request $request, $page) { - $form = $this->get('form.factory')->create(new EntryFilterType()); + return $this->showEntries('archive', $request, $page); + } + + /** + * Shows starred entries for current user. + * + * @param Request $request + * @param int $page + * + * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) + * + * @return \Symfony\Component\HttpFoundation\Response + */ + public function showStarredAction(Request $request, $page) + { + return $this->showEntries('starred', $request, $page); + } - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findArchiveByUser($this->getUser()->getId()); + /** + * Global method to retrieve entries depending on the given type + * It returns the response to be send. + * + * @param string $type Entries type: unread, starred or archive + * @param Request $request + * @param int $page + * + * @return \Symfony\Component\HttpFoundation\Response + */ + private function showEntries($type, Request $request, $page) + { + $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); + + switch ($type) { + case 'starred': + $qb = $repository->getBuilderForStarredByUser($this->getUser()->getId()); + break; + + case 'archive': + $qb = $repository->getBuilderForArchiveByUser($this->getUser()->getId()); + break; + + case 'unread': + $qb = $repository->getBuilderForUnreadByUser($this->getUser()->getId()); + break; + + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } + + $form = $this->get('form.factory')->create(new EntryFilterType()); if ($request->query->has($form->getName())) { // manually bind values from the request $form->submit($request->query->get($form->getName())); // build the query from the given form object - $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $filterBuilder); + $this->get('lexik_form_filter.query_builder_updater')->addFilterConditions($form, $qb); } - $pagerAdapter = new DoctrineORMAdapter($filterBuilder->getQuery()); + $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); $entries = new Pagerfanta($pagerAdapter); $entries->setMaxPerPage($this->getUser()->getConfig()->getItemsPerPage()); @@ -183,25 +201,6 @@ class EntryController extends Controller 'currentPage' => $page, ) ); - } - - /** - * Shows starred entries for current user. - * - * @param Request $request - * @param int $page - * - * @Route("/starred/list/{page}", name="starred", defaults={"page" = "1"}) - * - * @return \Symfony\Component\HttpFoundation\Response - */ - public function showStarredAction(Request $request, $page) - { - $form = $this->get('form.factory')->create(new EntryFilterType()); - - $filterBuilder = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findStarredByUser($this->getUser()->getId()); if ($request->query->has($form->getName())) { // manually bind values from the request diff --git a/src/Wallabag/CoreBundle/Controller/RssController.php b/src/Wallabag/CoreBundle/Controller/RssController.php index 0558c53b..6121f361 100644 --- a/src/Wallabag/CoreBundle/Controller/RssController.php +++ b/src/Wallabag/CoreBundle/Controller/RssController.php @@ -22,22 +22,7 @@ class RssController extends Controller */ public function showUnreadAction(User $user) { - $qb = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findUnreadByUser( - $user->getId() - ); - - $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit'); - $entries->setMaxPerPage($perPage); - - return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array( - 'type' => 'unread', - 'entries' => $entries, - )); + return $this->showEntries('unread', $user); } /** @@ -50,22 +35,7 @@ class RssController extends Controller */ public function showArchiveAction(User $user) { - $qb = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findArchiveByUser( - $user->getId() - ); - - $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); - $entries = new Pagerfanta($pagerAdapter); - - $perPage = $user->getConfig()->getRssLimit() ?: $this->container->getParameter('rss_limit'); - $entries->setMaxPerPage($perPage); - - return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array( - 'type' => 'archive', - 'entries' => $entries, - )); + return $this->showEntries('archive', $user); } /** @@ -78,11 +48,38 @@ class RssController extends Controller */ public function showStarredAction(User $user) { - $qb = $this->getDoctrine() - ->getRepository('WallabagCoreBundle:Entry') - ->findStarredByUser( - $user->getId() - ); + return $this->showEntries('starred', $user); + } + + /** + * Global method to retrieve entries depending on the given type + * It returns the response to be send. + * + * @param string $type Entries type: unread, starred or archive + * @param User $user + * + * @return \Symfony\Component\HttpFoundation\Response + */ + private function showEntries($type, User $user) + { + $repository = $this->getDoctrine()->getRepository('WallabagCoreBundle:Entry'); + + switch ($type) { + case 'starred': + $qb = $repository->getBuilderForStarredByUser($user->getId()); + break; + + case 'archive': + $qb = $repository->getBuilderForArchiveByUser($user->getId()); + break; + + case 'unread': + $qb = $repository->getBuilderForUnreadByUser($user->getId()); + break; + + default: + throw new \InvalidArgumentException(sprintf('Type "%s" is not implemented.', $type)); + } $pagerAdapter = new DoctrineORMAdapter($qb->getQuery()); $entries = new Pagerfanta($pagerAdapter); @@ -91,7 +88,7 @@ class RssController extends Controller $entries->setMaxPerPage($perPage); return $this->render('WallabagCoreBundle:Entry:entries.xml.twig', array( - 'type' => 'starred', + 'type' => $type, 'entries' => $entries, )); } diff --git a/src/Wallabag/CoreBundle/Repository/EntryRepository.php b/src/Wallabag/CoreBundle/Repository/EntryRepository.php index f885ee94..5538ae82 100644 --- a/src/Wallabag/CoreBundle/Repository/EntryRepository.php +++ b/src/Wallabag/CoreBundle/Repository/EntryRepository.php @@ -9,19 +9,34 @@ use Pagerfanta\Pagerfanta; class EntryRepository extends EntityRepository { /** - * Retrieves unread entries for a user. + * Return a query builder to used by other getBuilderFor* method. * * @param int $userId * * @return QueryBuilder */ - public function findUnreadByUser($userId) + private function getBuilderByUser($userId) { return $this->createQueryBuilder('e') ->leftJoin('e.user', 'u') - ->where('e.isArchived = false') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + ->andWhere('u.id = :userId')->setParameter('userId', $userId) + ->orderBy('e.id', 'desc') + ; + } + + /** + * Retrieves unread entries for a user. + * + * @param int $userId + * + * @return QueryBuilder + */ + public function getBuilderForUnreadByUser($userId) + { + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = false') + ; } /** @@ -31,13 +46,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findArchiveByUser($userId) + public function getBuilderForArchiveByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isArchived = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isArchived = true') + ; } /** @@ -47,13 +61,12 @@ class EntryRepository extends EntityRepository * * @return QueryBuilder */ - public function findStarredByUser($userId) + public function getBuilderForStarredByUser($userId) { - return $this->createQueryBuilder('e') - ->leftJoin('e.user', 'u') - ->where('e.isStarred = true') - ->andWhere('u.id =:userId')->setParameter('userId', $userId) - ->orderBy('e.id', 'desc'); + return $this + ->getBuilderByUser($userId) + ->andWhere('e.isStarred = true') + ; } /** -- cgit v1.2.3 From 6eebd8c90941ddfe8eeca1961f7a7a493e227687 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:10:32 +0200 Subject: Remove unsed things --- .../CoreBundle/Controller/StaticController.php | 8 -- src/Wallabag/CoreBundle/Entity/Entry.php | 4 +- src/Wallabag/CoreBundle/Helper/Entry.php | 7 -- src/Wallabag/CoreBundle/Helper/Tools.php | 133 --------------------- .../CoreBundle/Resources/config/routing.yml | 7 -- src/Wallabag/CoreBundle/Tools/Utils.php | 13 ++ 6 files changed, 15 insertions(+), 157 deletions(-) delete mode 100644 src/Wallabag/CoreBundle/Helper/Entry.php delete mode 100755 src/Wallabag/CoreBundle/Helper/Tools.php (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Controller/StaticController.php b/src/Wallabag/CoreBundle/Controller/StaticController.php index 3b844b44..64875a66 100644 --- a/src/Wallabag/CoreBundle/Controller/StaticController.php +++ b/src/Wallabag/CoreBundle/Controller/StaticController.php @@ -28,12 +28,4 @@ class StaticController extends Controller array() ); } - - /** - * @Route("/", name="homepage") - */ - public function apiAction() - { - return $this->redirect($this->generateUrl('nelmio_api_doc_index')); - } } diff --git a/src/Wallabag/CoreBundle/Entity/Entry.php b/src/Wallabag/CoreBundle/Entity/Entry.php index 7d2d2027..f88d189d 100644 --- a/src/Wallabag/CoreBundle/Entity/Entry.php +++ b/src/Wallabag/CoreBundle/Entity/Entry.php @@ -7,7 +7,7 @@ use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Constraints as Assert; use Hateoas\Configuration\Annotation as Hateoas; use JMS\Serializer\Annotation\XmlRoot; -use Wallabag\CoreBundle\Helper\Tools; +use Wallabag\CoreBundle\Tools\Utils; /** * Entry. @@ -265,7 +265,7 @@ class Entry public function setContent($content) { $this->content = $content; - $this->readingTime = Tools::getReadingTime($content); + $this->readingTime = Utils::getReadingTime($content); $this->domainName = parse_url($this->url, PHP_URL_HOST); return $this; diff --git a/src/Wallabag/CoreBundle/Helper/Entry.php b/src/Wallabag/CoreBundle/Helper/Entry.php deleted file mode 100644 index 219711b3..00000000 --- a/src/Wallabag/CoreBundle/Helper/Entry.php +++ /dev/null @@ -1,7 +0,0 @@ - array( - 'timeout' => $timeout, - 'header' => 'User-Agent: '.$useragent, - 'follow_location' => true, - ), - 'ssl' => array( - 'verify_peer' => false, - 'allow_self_signed' => true, - ), - ) - ); - - # only download page lesser than 4MB - $data = @file_get_contents($url, false, $context, -1, 4000000); - - if (isset($http_response_header) and isset($http_response_header[0])) { - $httpcodeOK = isset($http_response_header) and isset($http_response_header[0]) and ((strpos($http_response_header[0], '200 OK') !== false) or (strpos($http_response_header[0], '301 Moved Permanently') !== false)); - } - } - - # if response is not empty and response is OK - if (isset($data) and isset($httpcodeOK) and $httpcodeOK) { - # take charset of page and get it - preg_match('##Usi', $data, $meta); - - # if meta tag is found - if (!empty($meta[0])) { - preg_match('#charset="?(.*)"#si', $meta[0], $encoding); - # if charset is found set it otherwise, set it to utf-8 - $html_charset = (!empty($encoding[1])) ? strtolower($encoding[1]) : 'utf-8'; - if (empty($encoding[1])) { - $encoding[1] = 'utf-8'; - } - } else { - $html_charset = 'utf-8'; - $encoding[1] = ''; - } - - # replace charset of url to charset of page - $data = str_replace('charset='.$encoding[1], 'charset='.$html_charset, $data); - - return $data; - } else { - return false; - } - } - - /** - * Encode a URL by using a salt. - * - * @param $string - * - * @return string - */ - public static function encodeString($string) - { - return sha1($string.SALT); - } - - public static function generateToken() - { - if (ini_get('open_basedir') === '') { - if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') { - // alternative to /dev/urandom for Windows - $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); - } else { - $token = substr(base64_encode(file_get_contents('/dev/urandom', false, null, 0, 20)), 0, 15); - } - } else { - $token = substr(base64_encode(uniqid(mt_rand(), true)), 0, 20); - } - - return str_replace('+', '', $token); - } - - /** - * For a given text, we calculate reading time for an article. - * - * @param $text - * - * @return float - */ - public static function getReadingTime($text) - { - return floor(str_word_count(strip_tags($text)) / 200); - } -} diff --git a/src/Wallabag/CoreBundle/Resources/config/routing.yml b/src/Wallabag/CoreBundle/Resources/config/routing.yml index f3502e15..e69de29b 100644 --- a/src/Wallabag/CoreBundle/Resources/config/routing.yml +++ b/src/Wallabag/CoreBundle/Resources/config/routing.yml @@ -1,7 +0,0 @@ -entry: - resource: "@WallabagCoreBundle/Controller/EntryController.php" - type: annotation - -config: - resource: "@WallabagCoreBundle/Controller/ConfigController.php" - type: annotation diff --git a/src/Wallabag/CoreBundle/Tools/Utils.php b/src/Wallabag/CoreBundle/Tools/Utils.php index 7e2968e7..a4fbcffd 100644 --- a/src/Wallabag/CoreBundle/Tools/Utils.php +++ b/src/Wallabag/CoreBundle/Tools/Utils.php @@ -25,4 +25,17 @@ class Utils // remove character which can broken the url return str_replace(array('+', '/'), '', $token); } + + /** + * For a given text, we calculate reading time for an article + * based on 200 words per minute + * + * @param $text + * + * @return float + */ + public static function getReadingTime($text) + { + return floor(str_word_count(strip_tags($text)) / 200); + } } -- cgit v1.2.3 From 4793ee6509719aca06b17e7a334afa3a090436e8 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:13:11 +0200 Subject: Fixed my name & website :ok_hand: --- .../CoreBundle/Resources/views/themes/material/Static/about.html.twig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/about.html.twig b/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/about.html.twig index 8c6269ec..5de71d77 100755 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/about.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/Static/about.html.twig @@ -21,7 +21,7 @@
{% trans %}Developed by{% endtrans %}
Nicolas Lœuillet{% trans %}website{% endtrans %}
Thomas Citharel — {% trans %}website{% endtrans %}
-
Jérémy Besnoit — {% trans %}website{% endtrans %}
+
Jérémy Benoist — {% trans %}website{% endtrans %}
{% trans %}And many others contributors ♥{% endtrans %} {% trans %}on Github{% endtrans %}
{% trans %}Project website{% endtrans %}
https://www.wallabag.org
-- cgit v1.2.3 From 1dbcd63b59db8551866bf280616825885da63167 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:23:20 +0200 Subject: Update baggy about page According to what have been done on the material theme --- .../Resources/views/Static/about.html.twig | 38 ++++++++++------------ 1 file changed, 18 insertions(+), 20 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Resources/views/Static/about.html.twig b/src/Wallabag/CoreBundle/Resources/views/Static/about.html.twig index 9e188cd9..311b5067 100755 --- a/src/Wallabag/CoreBundle/Resources/views/Static/about.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/Static/about.html.twig @@ -3,38 +3,34 @@ {% block title %}{% trans %}About{% endtrans %}{% endblock %} {% block content %} -

{% trans %}About wallabag{% endtrans %}

+

{% trans %}Who is behind wallabag{% endtrans %}

-
{% trans %}Project website{% endtrans %}
-
https://www.wallabag.org
- -
{% trans %}Main developer{% endtrans %}
+
{% trans %}Developed by{% endtrans %}
Nicolas Lœuillet{% trans %}website{% endtrans %}
+
Thomas Citharel — {% trans %}website{% endtrans %}
+
Jérémy Benoist — {% trans %}website{% endtrans %}
-
{% trans %}Contributors ♥:{% endtrans %}
-
{% trans %}on Github{% endtrans %}
+
{% trans %}And many others contributors ♥{% endtrans %} {% trans %}on Github{% endtrans %}
-
{% trans %}Bug reports{% endtrans %}
-
{% trans %}On our support website{% endtrans %} {% trans %}or{% endtrans %} {% trans %}on Github{% endtrans %}
+
{% trans %}Project website{% endtrans %}
+
https://www.wallabag.org
-
{% trans %}License{% endtrans %}
-
MIT
+
{% trans %}License{% endtrans %}: MIT
-
{% trans %}Version{% endtrans %}
-
{{ version }}
+
{% trans %}Version{% endtrans %}: {{ version }}
-

{% trans %}wallabag is a read-it-later application: you can save a web page by keeping only content. Elements like ads or menus are deleted.{% endtrans %}

-

{% trans %}Getting help{% endtrans %}

{% trans %}Documentation{% endtrans %}
-
Online documentation
+
english
+
français
+
deutsch
-
{% trans %}Support{% endtrans %}
-
http://support.wallabag.org/
+
{% trans %}Bug reports{% endtrans %}
+
{% trans %}On our support website{% endtrans %} {% trans %}or{% endtrans %} {% trans %}on Github{% endtrans %}

{% trans %}Helping wallabag{% endtrans %}

@@ -42,8 +38,10 @@

{% trans %}wallabag is free and opensource. You can help us:{% endtrans %}

-
{% trans %}via Paypal{% endtrans %}
+
{% trans %}wallabag is free and opensource. You can help us:{% endtrans %}
+
by contributing to the project: an issue lists all our needs
+
{% trans %}via Paypal{% endtrans %}
-
{% trans %}via Flattr{% endtrans %}
+
{% trans %}via Flattr{% endtrans %}
{% endblock %} -- cgit v1.2.3 From eccf5eb2e0b7b10b274d00e616d1d9a7e56caffe Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:28:47 +0200 Subject: Add title in "picto-link" Picto aren't always obvious for every one. So it's better to have some title when we move the mouse over. --- .../CoreBundle/Resources/views/themes/material/layout.html.twig | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/CoreBundle') 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 554865d7..1456d5dd 100644 --- a/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/themes/material/layout.html.twig @@ -55,9 +55,9 @@
-- cgit v1.2.3 From 6682139ec50e29308953a50dcbd088e6d8a07e81 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Thu, 20 Aug 2015 20:39:52 +0200 Subject: CS --- src/Wallabag/CoreBundle/Resources/views/base.html.twig | 2 +- src/Wallabag/CoreBundle/Tools/Utils.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle') diff --git a/src/Wallabag/CoreBundle/Resources/views/base.html.twig b/src/Wallabag/CoreBundle/Resources/views/base.html.twig index e27aceae..ac5d2bf9 100644 --- a/src/Wallabag/CoreBundle/Resources/views/base.html.twig +++ b/src/Wallabag/CoreBundle/Resources/views/base.html.twig @@ -71,7 +71,7 @@
  • {% trans %}unread{% endtrans %}
  • {% trans %}favorites{% endtrans %}
  • {% trans %}archive{% endtrans %}
  • -
  • {% trans %}tags{% endtrans %}
  • +
  • {% trans %}tags{% endtrans %}
  • {% trans %}save a link{% endtrans %}
  • {% trans %}search{% endtrans %}