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

Add xliff import for articles #510

Merged
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ npm-debug.log
# tests
/Tests/Application/config/parameters.yml
/Tests/Application/var
/Tests/Functional/Import/export.test.xliff
141 changes: 141 additions & 0 deletions Command/ArticleImportCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
<?php

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ArticleBundle\Command;

use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Sulu\Bundle\ArticleBundle\Import\ArticleImportInterface;
use Sulu\Bundle\ArticleBundle\Import\ImportResult;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ConfirmationQuestion;

class ArticleImportCommand extends Command
{
protected static $defaultName = 'sulu:article:import';

/**
* @var ArticleImportInterface
*/
private $articleImporter;

/**
* @var LoggerInterface
*/
private $logger;

public function __construct(ArticleImportInterface $articleImporter, LoggerInterface $logger = null)
{
parent::__construct();

$this->articleImporter = $articleImporter;
$this->logger = $logger ?: new NullLogger();
}

protected function configure()
{
$this->addArgument('file', InputArgument::REQUIRED, 'export.xliff')
->addArgument('locale', InputArgument::REQUIRED)
->addOption('format', 'f', InputOption::VALUE_REQUIRED, '', '1.2.xliff')
->addOption('uuid', 'u', InputOption::VALUE_REQUIRED)
->addOption('overrideSettings', 'o', InputOption::VALUE_NONE, 'Override Settings-Tab')
->setDescription('Import Articles');
}

protected function execute(InputInterface $input, OutputInterface $output)
{
$filePath = $input->getArgument('file');
if (0 === !\strpos($filePath, '/')) {
$filePath = \getcwd() . '/' . $filePath;
}
$locale = $input->getArgument('locale');
$format = $input->getOption('format');
$overrideSettings = $input->getOption('overrideSettings');

$output->writeln([
'<info>Language Import</info>',
'<info>===============</info>',
'',
'<info>Options</info>',
'Locale: ' . $locale,
'Format: ' . $format,
'Override Setting: ' . ($overrideSettings ? 'YES' : 'NO'),
'---------------',
'',
]);

$helper = $this->getHelper('question');
$question = new ConfirmationQuestion('<question>Continue with this options? Be careful! (y/n)</question> ', false);

if (!$helper->ask($input, $output, $question)) {
$output->writeln('<error>Abort!</error>');

return -1;
}

$output->writeln('<info>Continue!</info>');

$import = $this->articleImporter->import(
$locale,
$filePath,
$output,
$format,
$overrideSettings
);

if ($output->getVerbosity() >= OutputInterface::VERBOSITY_VERBOSE) {
$output->writeln(\sprintf('<info>Imported %s/%s</info>', $import->getSuccesses(), $import->getCount()));
}

$this->printExceptions($import, $output);

return $import->getFails();
}

protected function printExceptions(ImportResult $import, $output = null)
{
if (null === $output) {
$output = new NullOutput();
}

$output->writeln([
'',
'',
'<info>Import Result</info>',
'<info>===============</info>',
'<info>' . $import->getSuccesses() . ' Documents imported.</info>',
'<comment>' . \count($import->getFailed()) . ' Documents ignored.</comment>',
]);

if (!isset($import->getExceptionStore()['ignore'])) {
return;
}

// If more than 20 exceptions write only into log.
if (\count($import->getExceptionStore()['ignore']) > 20) {
foreach ($import->getExceptionStore()['ignore'] as $msg) {
$this->logger->info($msg);
}

return;
}

foreach ($import->getExceptionStore()['ignore'] as $msg) {
$output->writeln('<comment>' . $msg . '</comment>');
$this->logger->info($msg);
}
}
}
Loading