From: Nicolas Lœuillet Date: Thu, 20 Aug 2015 19:51:02 +0000 (+0200) Subject: Merge pull request #1386 from wallabag/v2-refactor X-Git-Tag: 2.0.0-alpha.0~17 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=109d67dbb16478f927c3d6a46ab61ea9994aafae;hp=4fcb7eaf139a4d2cbd0f54574e6c51a0fe852ef1;p=github%2Fwallabag%2Fwallabag.git Merge pull request #1386 from wallabag/v2-refactor WIP – Fixing things around :dash: --- diff --git a/app/config/config.yml b/app/config/config.yml index 5b26b06f..efc815b8 100644 --- a/app/config/config.yml +++ b/app/config/config.yml @@ -51,6 +51,7 @@ twig: form: resources: - LexikFormFilterBundle:Form:form_div_layout.html.twig + # Assetic Configuration assetic: debug: "%kernel.debug%" diff --git a/composer.json b/composer.json index 709b23ac..2c5111fd 100644 --- a/composer.json +++ b/composer.json @@ -19,6 +19,7 @@ }, { "name": "Jérémy Benoist", + "homepage": "http://www.j0k3r.net", "role": "Developer" } ], diff --git a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php index 86c8de1e..7ae54b57 100644 --- a/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php +++ b/src/Wallabag/ApiBundle/Tests/Controller/WallabagRestControllerTest.php @@ -170,6 +170,31 @@ class WallabagRestControllerTest extends WebTestCase $client = $this->createClient(); $headers = $this->generateHeaders('admin', 'mypassword'); + $client->request('GET', '/api/entries', array('star' => 1, 'sort' => 'updated'), array(), $headers); + + $this->assertEquals(200, $client->getResponse()->getStatusCode()); + + $content = json_decode($client->getResponse()->getContent(), true); + + $this->assertGreaterThanOrEqual(1, count($content)); + $this->assertNotEmpty($content['_embedded']['items']); + $this->assertGreaterThanOrEqual(1, $content['total']); + $this->assertEquals(1, $content['page']); + $this->assertGreaterThanOrEqual(1, $content['pages']); + + $this->assertTrue( + $client->getResponse()->headers->contains( + 'Content-Type', + 'application/json' + ) + ); + } + + public function testGetArchiveEntries() + { + $client = $this->createClient(); + $headers = $this->generateHeaders('admin', 'mypassword'); + $client->request('GET', '/api/entries', array('archive' => 1), array(), $headers); $this->assertEquals(200, $client->getResponse()->getStatusCode()); 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/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/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') + ; } /** 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/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 %} 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 %}
    diff --git a/src/Wallabag/CoreBundle/Tools/Utils.php b/src/Wallabag/CoreBundle/Tools/Utils.php index 7e2968e7..a16baca9 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); + } }