Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streamline Glossary Comand and fix some Bug #326

Merged
merged 5 commits into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions Build/phpstan/Core11/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^Access to constant OK on an unknown class TYPO3\\\\CMS\\\\Core\\\\Type\\\\ContextualFeedbackSeverity\\.$#"
count: 1
path: ../../../Classes/Controller/GlossarySyncController.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryEntryRepository\\:\\:findEntriesByGlossary\\(\\) should return array\\<string, mixed\\> but returns array\\<int, array\\<string, mixed\\>\\>\\.$#"
count: 1
Expand Down
10 changes: 0 additions & 10 deletions Build/phpstan/Core12/phpstan-baseline.neon
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
parameters:
ignoreErrors:
-
message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException is not subtype of Throwable$#"
count: 2
path: ../../../Classes/Command/GlossaryCleanupCommand.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryEntryRepository\\:\\:findEntriesByGlossary\\(\\) should return array\\<string, mixed\\> but returns array\\<int, array\\<string, mixed\\>\\>\\.$#"
count: 1
Expand Down Expand Up @@ -150,11 +145,6 @@ parameters:
count: 1
path: ../../../Classes/Service/DeeplGlossaryService.php

-
message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException\\|Doctrine\\\\DBAL\\\\Driver\\\\Exception\\|Doctrine\\\\DBAL\\\\Exception\\|TYPO3\\\\CMS\\\\Core\\\\Exception\\\\SiteNotFoundException is not subtype of Throwable$#"
count: 1
path: ../../../Classes/Service/DeeplGlossaryService.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
Expand Down
163 changes: 93 additions & 70 deletions Classes/Command/GlossaryCleanupCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,138 @@

namespace WebVision\WvDeepltranslate\Command;

use Doctrine\DBAL\DBALException;
use DeepL\GlossaryInfo;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Helper\ProgressBar;
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;
use Symfony\Component\Console\Style\SymfonyStyle;
use WebVision\WvDeepltranslate\Domain\Repository\GlossaryRepository;
use WebVision\WvDeepltranslate\Service\DeeplGlossaryService;

/**
* ToDo: Rename Command
* ToDo: Split command in housekeeping and remove glossary from API/remote storage
*/
class GlossaryCleanupCommand extends Command
{
use GlossaryCommandTrait;

private SymfonyStyle $io;

protected function configure(): void
{
$this->addOption(
'yes',
'y',
InputOption::VALUE_NONE,
'Force deletion without asking'
);
$this
->addOption(
'glossaryId',
null,
InputOption::VALUE_OPTIONAL,
'Deleted single Glossary',
null
)
->addOption(
'all',
null,
InputOption::VALUE_NONE,
'Deleted all Glossaries',
)
->addOption(
'notinsync',
null,
InputOption::VALUE_NONE,
'Deleted all Glossaries without synchronization information',
)
;
}

protected function interact(InputInterface $input, OutputInterface $output): void
protected function execute(InputInterface $input, OutputInterface $output): int
{
if (empty($input->getOption('yes'))) {
$io = new SymfonyStyle($input, $output);
$yes = $io->ask('Really all delete? [yY]');
if (strtolower($yes) !== 'y') {
$output->writeln('Abort.');
exit;
}
$input->setOption('yes', true);
$this->io = new SymfonyStyle($input, $output);
$this->io->title('Glossary cleanup');

$question = new ConfirmationQuestion(
'Do you will execute the glossary cleanup?',
false,
'/^(y|j)/i'
);

if (!$this->io->askQuestion($question)) {
$this->io->writeln('<warning>Delete not confirmed, process was cancel.</warning>');
return Command::SUCCESS;
}
}

/**
* @throws DBALException
*/
protected function execute(
InputInterface $input,
OutputInterface $output
): int {
if ($input->getOption('yes') === false) {
$output->writeln('Deletion not confirmed. Cancel.');

return Command::INVALID;
// Remove single glossary by deepl-id
$glossaryId = $input->getOption('glossaryId');
if ($glossaryId !== null) {
$this->removeGlossaries($glossaryId);
}
// Remove all glossaries
if (!empty($input->getOption('all'))) {
$glossaries = $this->deeplGlossaryService->listGlossaries();
if (empty($glossaries)) {
$this->io->writeln('No glossaries found with sync to API');
return Command::FAILURE;
}

$this->removeAllGlossaryEntries($output);
$output->writeln('Success!');
$this->removeGlossaries($glossaries);
}
// Remove glossaries without api sync id
if (!empty($input->getOption('notinsync'))) {
$this->removeGlossariesWithNoSync();
}

$this->io->writeln('Success!');

return Command::SUCCESS;
}

private function removeGlossary(string $id): bool
{
$this->deeplGlossaryService->deleteGlossary($id);
return $this->glossaryRepository->removeGlossarySync($id);
}

/**
* @throws DBALException
* @param GlossaryInfo[] $glossaries
*/
private function removeAllGlossaryEntries(OutputInterface $output): void
private function removeGlossaries(array $glossaries): void
{
$glossaries = $this->deeplGlossaryService->listGlossaries();

if (empty($glossaries['glossaries'])) {
$output->writeln('No glossaries found with sync to API');
return;
}

$progress = new ProgressBar($output, count($glossaries));
$progress->start();

$removedGlossary = [];
$rows = [];
$this->io->progressStart(count($glossaries));

foreach ($glossaries as $glossary) {
$id = $glossary->glossaryId;
$this->deeplGlossaryService->deleteGlossary($id);
$databaseUpdated = $this->glossaryRepository->removeGlossarySync($id);
$removedGlossary[$id] = $databaseUpdated;
$progress->advance();
$dbUpdated = $this->removeGlossary($glossary->glossaryId);
$rows[] = [$glossary->glossaryId, $dbUpdated ? 'yes' : 'no'];
$this->io->progressAdvance();
}

$progress->finish();

$table = new Table($output);
$this->io->progressFinish();

$table->setHeaders([
'Glossary ID',
'Database sync removed',
]);
foreach ($removedGlossary as $glossaryId => $dbUpdated) {
$table->addRow([$glossaryId, $dbUpdated ? 'yes' : 'no']);
}

$output->writeln('');
$table->render();
$output->writeln('');
$this->io->table(
[
'Glossary ID',
'Database sync removed',
],
$rows
);
}

private function removeGlossariesWithNoSync(): void
{
$findNotConnected = $this->glossaryRepository->getGlossariesDeeplConnected();

if (count($findNotConnected) === 0) {
$output->writeln('No glossaries with sync mismatch.');
$this->io->writeln('No glossaries with sync mismatch.');
}

$this->io->progressStart(count($findNotConnected));
foreach ($findNotConnected as $notConnected) {
$this->glossaryRepository->removeGlossarySync($notConnected['glossary_id']);
$this->io->progressAdvance();
}
$this->io->progressFinish();

$output->writeln([
sprintf('Found %d glossaries with possible sync mismatch. Cleaned up.', count($findNotConnected)),
]);
$this->io->writeln(
sprintf('Found %d glossaries with possible sync mismatch. Cleaned up.', count($findNotConnected))
);
}
}
2 changes: 1 addition & 1 deletion Classes/Command/GlossaryCommandTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,4 @@ public function injectGlossaryRepository(GlossaryRepository $glossaryRepository)
{
$this->glossaryRepository = $glossaryRepository;
}
}
}
Loading