From 206bade58a279d7f2e34c2dbada10366b90d2d6b Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 1 Oct 2016 09:26:32 +0200 Subject: Add ability to reset some datas - annotations - tags - entries --- .../CoreBundle/Controller/ConfigController.php | 42 ++++++++++++++++++++++ 1 file changed, 42 insertions(+) (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php') diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index abd35c02..ccbf550a 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -224,6 +224,48 @@ class ConfigController extends Controller return $this->redirect($this->generateUrl('config').'?tagging-rule='.$rule->getId().'#set5'); } + /** + * Remove all annotations OR tags OR entries for the current user. + * + * @Route("/reset/{type}", requirements={"id" = "annotations|tags|entries"}, name="config_reset") + * + * @return RedirectResponse + */ + public function resetAction($type) + { + $em = $this->getDoctrine()->getManager(); + + switch ($type) { + case 'annotations': + $em->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = '.$this->getUser()->getId()) + ->execute(); + break; + + case 'tags': + $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($this->getUser()->getId()); + + if (empty($tags)) { + break; + } + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTags($this->getUser()->getId(), $tags); + break; + + case 'entries': + $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = '.$this->getUser()->getId()) + ->execute(); + } + + $this->get('session')->getFlashBag()->add( + 'notice', + 'flashes.config.notice.'.$type.'_reset' + ); + + return $this->redirect($this->generateUrl('config').'#set3'); + } + /** * Validate that a rule can be edited/deleted by the current user. * -- cgit v1.2.3 From 191564b7f71d01fb4c597c9b9641e23db564278d Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 1 Oct 2016 14:01:13 +0200 Subject: Add custom doctrine subscriber for SQLite MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since SQLite doesn’t handle cascade remove by default, we need to handle it manually. Also some refacto --- .../CoreBundle/Controller/ConfigController.php | 41 ++++++++++++++++------ 1 file changed, 30 insertions(+), 11 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php') diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index ccbf550a..faa85d16 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -237,25 +237,26 @@ class ConfigController extends Controller switch ($type) { case 'annotations': - $em->createQuery('DELETE FROM Wallabag\AnnotationBundle\Entity\Annotation a WHERE a.user = '.$this->getUser()->getId()) - ->execute(); + $this->getDoctrine() + ->getRepository('WallabagAnnotationBundle:Annotation') + ->removeAllByUserId($this->getUser()->getId()); break; case 'tags': - $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($this->getUser()->getId()); + $this->removeAllTagsByUserId($this->getUser()->getId()); + break; - if (empty($tags)) { - break; + case 'entries': + // SQLite doesn't care about cascading remove, so we need to manually remove associated stuf + // otherwise they won't be removed ... + if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { + $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); + $this->removeAllTagsByUserId($this->getUser()->getId()); } $this->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') - ->removeTags($this->getUser()->getId(), $tags); - break; - - case 'entries': - $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.user = '.$this->getUser()->getId()) - ->execute(); + ->removeAllByUserId($this->getUser()->getId()); } $this->get('session')->getFlashBag()->add( @@ -266,6 +267,24 @@ class ConfigController extends Controller return $this->redirect($this->generateUrl('config').'#set3'); } + /** + * Remove all tags for a given user. + * + * @param int $userId + */ + private function removeAllTagsByUserId($userId) + { + $tags = $this->getDoctrine()->getRepository('WallabagCoreBundle:Tag')->findAllTags($userId); + + if (empty($tags)) { + return; + } + + $this->getDoctrine() + ->getRepository('WallabagCoreBundle:Entry') + ->removeTags($userId, $tags); + } + /** * Validate that a rule can be edited/deleted by the current user. * -- cgit v1.2.3 From f71e55ac886a26813ca1171c0aca4921aa8f00ad Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 8 Oct 2016 11:05:03 +0200 Subject: Avoid orphan tags --- src/Wallabag/CoreBundle/Controller/ConfigController.php | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php') diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index faa85d16..e2484064 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -251,9 +251,11 @@ class ConfigController extends Controller // otherwise they won't be removed ... if ($this->get('doctrine')->getConnection()->getDriver() instanceof \Doctrine\DBAL\Driver\PDOSqlite\Driver) { $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); - $this->removeAllTagsByUserId($this->getUser()->getId()); } + // manually remove tags first to avoid orphan tag + $this->removeAllTagsByUserId($this->getUser()->getId()); + $this->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') ->removeAllByUserId($this->getUser()->getId()); @@ -268,7 +270,7 @@ class ConfigController extends Controller } /** - * Remove all tags for a given user. + * Remove all tags for a given user and cleanup orphan tags * * @param int $userId */ @@ -283,6 +285,16 @@ class ConfigController extends Controller $this->getDoctrine() ->getRepository('WallabagCoreBundle:Entry') ->removeTags($userId, $tags); + + $em = $this->getDoctrine()->getManager(); + + foreach ($tags as $tag) { + if (count($tag->getEntries()) === 0) { + $em->remove($tag); + } + } + + $em->flush(); } /** -- cgit v1.2.3 From 8c61fd12b1df50d481e9f82c39521cca7b8ad060 Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sat, 8 Oct 2016 11:14:09 +0200 Subject: CS --- src/Wallabag/CoreBundle/Controller/ConfigController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'src/Wallabag/CoreBundle/Controller/ConfigController.php') diff --git a/src/Wallabag/CoreBundle/Controller/ConfigController.php b/src/Wallabag/CoreBundle/Controller/ConfigController.php index e2484064..8d391917 100644 --- a/src/Wallabag/CoreBundle/Controller/ConfigController.php +++ b/src/Wallabag/CoreBundle/Controller/ConfigController.php @@ -253,7 +253,7 @@ class ConfigController extends Controller $this->getDoctrine()->getRepository('WallabagAnnotationBundle:Annotation')->removeAllByUserId($this->getUser()->getId()); } - // manually remove tags first to avoid orphan tag + // manually remove tags to avoid orphan tag $this->removeAllTagsByUserId($this->getUser()->getId()); $this->getDoctrine() @@ -270,9 +270,9 @@ class ConfigController extends Controller } /** - * Remove all tags for a given user and cleanup orphan tags + * Remove all tags for a given user and cleanup orphan tags. * - * @param int $userId + * @param int $userId */ private function removeAllTagsByUserId($userId) { @@ -286,6 +286,7 @@ class ConfigController extends Controller ->getRepository('WallabagCoreBundle:Entry') ->removeTags($userId, $tags); + // cleanup orphan tags $em = $this->getDoctrine()->getManager(); foreach ($tags as $tag) { -- cgit v1.2.3