From 88c9df9b80b457b5f4a6f7b4ae467fdcbb89a48e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Wed, 21 Jun 2023 13:51:03 +0200 Subject: [PATCH] Add command to clean pictures path when changing instance URL --- .../Command/UpdatePicturesPathCommand.php | 66 +++++++++++++++++++ .../Command/UpdatePicturesPathCommandTest.php | 58 ++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/Wallabag/CoreBundle/Command/UpdatePicturesPathCommand.php create mode 100644 tests/Wallabag/CoreBundle/Command/UpdatePicturesPathCommandTest.php diff --git a/src/Wallabag/CoreBundle/Command/UpdatePicturesPathCommand.php b/src/Wallabag/CoreBundle/Command/UpdatePicturesPathCommand.php new file mode 100644 index 000000000..bac1932b5 --- /dev/null +++ b/src/Wallabag/CoreBundle/Command/UpdatePicturesPathCommand.php @@ -0,0 +1,66 @@ +entityManager = $entityManager; + $this->entryRepository = $entryRepository; + $this->wallabagUrl = $wallabagUrl; + parent::__construct(); + } + + protected function configure() + { + $this + ->setName('wallabag:update-pictures-path') + ->setDescription('Update the path of the pictures for each entry when you changed your wallabag instance URL.') + ->addArgument( + 'old-url', + InputArgument::REQUIRED, + 'URL to replace' + ); + } + + protected function execute(InputInterface $input, OutputInterface $output) + { + $io = new SymfonyStyle($input, $output); + + $oldUrl = $input->getArgument('old-url'); + + $query = $this->entryRepository->createQueryBuilder('e')->getQuery(); + $io->text('Retrieve existing entries'); + $i = 1; + foreach ($query->toIterable() as $entry) { + $content = str_replace($oldUrl, $this->wallabagUrl, $entry->getContent()); + $entry->setContent($content); + + $previewPicture = str_replace($oldUrl, $this->wallabagUrl, $entry->getPreviewPicture()); + $entry->setPreviewPicture($previewPicture); + + if (0 === ($i % 20)) { + $this->entityManager->flush(); + } + ++$i; + } + $this->entityManager->flush(); + + $io->success('Finished updating.'); + + return 0; + } +} diff --git a/tests/Wallabag/CoreBundle/Command/UpdatePicturesPathCommandTest.php b/tests/Wallabag/CoreBundle/Command/UpdatePicturesPathCommandTest.php new file mode 100644 index 000000000..1290cbbb9 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Command/UpdatePicturesPathCommandTest.php @@ -0,0 +1,58 @@ +expectException(RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments (missing: "old-url")'); + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:update-pictures-path'); + + $tester = new CommandTester($command); + $tester->execute([]); + } + + public function testRunGenerateUrlHashesCommandForUser() + { + $application = new Application($this->getTestClient()->getKernel()); + $this->logInAs('admin'); + + $url = 'https://wallabag.org/news/20230620-new-release-wallabag-260/'; + + $command = $application->find('wallabag:update-pictures-path'); + + $client = $this->getTestClient(); + $em = $client->getContainer()->get(EntityManagerInterface::class); + $entry = new Entry($this->getLoggedInUser()); + $entry->setUrl($url); + $entry->setPreviewPicture('https://old-url.test/mypicture.jpg'); + $entry->setContent('my great article with a picture '); + $em->persist($entry); + $em->flush(); + + $tester = new CommandTester($command); + $tester->execute([ + 'old-url' => 'https://old-url.test', + ]); + + $this->assertStringContainsString('Finished updating.', $tester->getDisplay()); + + $entry = $em->getRepository(Entry::class)->findOneByUrl($url); + $this->assertSame($entry->getPreviewPicture(), $client->getContainer()->getParameter('domain_name') . '/mypicture.jpg'); + + $query = $em->createQuery('DELETE FROM Wallabag\CoreBundle\Entity\Entry e WHERE e.url = :url'); + $query->setParameter('url', $url); + $query->execute(); + } +}