Files
wallabag/src/Command/ExportCommand.php

89 lines
2.6 KiB
PHP
Raw Normal View History

<?php
2024-02-19 01:30:12 +01:00
namespace Wallabag\Command;
use Doctrine\ORM\NoResultException;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
2017-07-29 00:30:22 +02:00
use Symfony\Component\Console\Style\SymfonyStyle;
2024-02-19 01:30:12 +01:00
use Wallabag\Helper\EntriesExport;
use Wallabag\Repository\EntryRepository;
use Wallabag\Repository\UserRepository;
class ExportCommand extends Command
{
2024-02-02 23:24:33 +01:00
protected static $defaultName = 'wallabag:export';
protected static $defaultDescription = 'Export all entries for an user';
public function __construct(
private EntryRepository $entryRepository,
private UserRepository $userRepository,
private EntriesExport $entriesExport,
private string $projectDir,
) {
parent::__construct();
}
protected function configure()
{
$this
->setHelp('This command helps you to export all entries for an user')
->addArgument(
'username',
InputArgument::REQUIRED,
'User from which to export entries'
)
->addArgument(
'filepath',
InputArgument::OPTIONAL,
'Path of the exported file'
)
;
}
2025-01-18 23:56:06 +01:00
protected function execute(InputInterface $input, OutputInterface $output): int
{
2017-07-29 00:30:22 +02:00
$io = new SymfonyStyle($input, $output);
try {
$user = $this->userRepository->findOneByUserName($input->getArgument('username'));
} catch (NoResultException $e) {
2024-08-14 16:39:36 +02:00
$io->error(\sprintf('User "%s" not found.', $input->getArgument('username')));
2017-01-24 20:42:02 +01:00
return 1;
}
2017-01-24 20:42:02 +01:00
$entries = $this->entryRepository
->getBuilderForAllByUser($user->getId())
->getQuery()
->getResult();
2024-08-14 16:39:36 +02:00
$io->text(\sprintf('Exporting <info>%d</info> entrie(s) for user <info>%s</info>...', \count($entries), $user->getUserName()));
$filePath = $input->getArgument('filepath');
2017-01-24 20:42:02 +01:00
if (!$filePath) {
2024-08-14 16:39:36 +02:00
$filePath = $this->projectDir . '/' . \sprintf('%s-export.json', $user->getUsername());
}
2017-01-24 20:42:02 +01:00
try {
$data = $this->entriesExport
->setEntries($entries)
->updateTitle('All')
->updateAuthor('All')
->exportJsonData();
file_put_contents($filePath, $data);
} catch (\InvalidArgumentException $e) {
2024-08-14 16:39:36 +02:00
$io->error(\sprintf('Error: "%s"', $e->getMessage()));
2017-01-24 20:42:02 +01:00
return 1;
}
2017-07-29 00:30:22 +02:00
$io->success('Done.');
2017-01-24 20:42:02 +01:00
return 0;
}
}