-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FEATURE] Add command to import glossary entries
A common use case is to import glossary entries from a csv file. This command provides the feature to import glossary entries from a csv file, uploaded to the TYPO3 filelist. The entries are first imported to TYPO3 and then to deepl.
- Loading branch information
Showing
5 changed files
with
197 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Command; | ||
|
||
use Symfony\Component\Console\Command\Command; | ||
use Symfony\Component\Console\Input\InputInterface; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
use Symfony\Component\Console\Style\SymfonyStyle; | ||
use Throwable; | ||
use WebVision\WvDeepltranslate\Service\ImportGlossaryEntryService; | ||
|
||
class GlossaryCsvImportCommand extends Command | ||
{ | ||
use GlossaryCommandTrait; | ||
|
||
protected ImportGlossaryEntryService $importGlossaryEntryService; | ||
|
||
public function __construct(ImportGlossaryEntryService $importGlossaryEntryService, ?string $name = null) | ||
{ | ||
$this->importGlossaryEntryService = $importGlossaryEntryService; | ||
parent::__construct($name); | ||
} | ||
|
||
protected function configure(): void | ||
{ | ||
$this->addOption( | ||
'pageId', | ||
'p', | ||
InputOption::VALUE_REQUIRED, | ||
'Page to import to', | ||
null | ||
) | ||
->addOption( | ||
'csvFilePath', | ||
'f', | ||
InputOption::VALUE_REQUIRED, | ||
'combined file identifier [[storage uid]:]<file identifier> e.g. 1:/user_upload/csv_imports', | ||
null | ||
) | ||
->addOption( | ||
'csvSeparator', | ||
's', | ||
InputOption::VALUE_OPTIONAL, | ||
'csv seperator default: ,', | ||
',' | ||
) | ||
->addOption( | ||
'targetSysLanguage', | ||
't', | ||
InputOption::VALUE_REQUIRED, | ||
'The target language sys_language_uid', | ||
null | ||
); | ||
} | ||
|
||
public function execute(InputInterface $input, OutputInterface $output): int | ||
{ | ||
$io = new SymfonyStyle($input, $output); | ||
$io->title('Glossary Entry CSV Import'); | ||
|
||
try { | ||
$glossaryEntries = $this->importGlossaryEntryService->getGlossaryEntriesFromCsv( | ||
(string) $input->getOption('csvFilePath'), | ||
(string) $input->getOption('csvSeparator') | ||
); | ||
|
||
$this->importGlossaryEntryService->insertEntriesLocal( | ||
$glossaryEntries, | ||
(int) $input->getOption('pageId'), | ||
(int) $input->getOption('targetSysLanguage') | ||
); | ||
|
||
$this->deeplGlossaryService->syncGlossaries((int)$input->getOption('pageId')); | ||
} catch (Throwable $e) { | ||
$io->error($e->getMessage()); | ||
return Command::FAILURE; | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Exception; | ||
|
||
use Exception; | ||
|
||
class FileNotFoundException extends Exception | ||
{ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace WebVision\WvDeepltranslate\Service; | ||
|
||
use TYPO3\CMS\Core\Resource\File; | ||
use TYPO3\CMS\Core\Resource\FileReference; | ||
use TYPO3\CMS\Core\Resource\ResourceFactory; | ||
use TYPO3\CMS\Core\Utility\CsvUtility; | ||
use WebVision\WvDeepltranslate\Domain\Repository\GlossaryEntryRepository; | ||
use WebVision\WvDeepltranslate\Exception\FileNotFoundException; | ||
|
||
final class ImportGlossaryEntryService | ||
{ | ||
protected ResourceFactory $resourceFactory; | ||
protected GlossaryEntryRepository $glossaryEntryRepository; | ||
|
||
public function __construct(ResourceFactory $resourceFactory, GlossaryEntryRepository $glossaryEntryRepository) | ||
{ | ||
$this->resourceFactory = $resourceFactory; | ||
$this->glossaryEntryRepository = $glossaryEntryRepository; | ||
} | ||
|
||
/** | ||
* @throws FileNotFoundException | ||
* @return array<mixed, array> | ||
*/ | ||
public function getGlossaryEntriesFromCsv(string $filePath, string $separator): array | ||
{ | ||
$file = $this->getFile($filePath); | ||
|
||
if ($file === null) { | ||
throw new FileNotFoundException('Csv file not found identifier: ' . $filePath, 1729245627); | ||
} | ||
|
||
return CsvUtility::csvToArray($file->getContents(), $separator, '"', 2); | ||
} | ||
|
||
/** | ||
* @param array<mixed, array> $entries | ||
* @param int $pageId | ||
* @param int $sysLanguageUid | ||
* @return void | ||
*/ | ||
public function insertEntriesLocal(array $entries, int $pageId, int $sysLanguageUid): void | ||
{ | ||
foreach ($entries as $entry) { | ||
$originalUid = $this->glossaryEntryRepository->add([ | ||
'pid' => $pageId, | ||
'term' => $entry[0], | ||
]); | ||
|
||
if ($originalUid > 0) { | ||
$this->glossaryEntryRepository->add([ | ||
'l10n_parent' => $originalUid, | ||
'sys_language_uid' => $sysLanguageUid, | ||
'pid' => $pageId, | ||
'term' => $entry[1], | ||
]); | ||
} | ||
} | ||
} | ||
|
||
protected function getFile(string $csvFilePath): ?File | ||
{ | ||
$file = $this->resourceFactory->getFileObjectFromCombinedIdentifier($csvFilePath); | ||
return $file instanceof FileReference ? $file->getOriginalFile() : $file; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters