blob: f062fb79f4aa35d081f1c6f171725f802351b37a [file] [log] [blame]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +02001<?php
2
3/*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <fabien@symfony.com>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12namespace Symfony\Component\Translation\Command;
13
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010014use Symfony\Component\Console\Attribute\AsCommand;
15use Symfony\Component\Console\CI\GithubActionReporter;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020016use Symfony\Component\Console\Command\Command;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010017use Symfony\Component\Console\Completion\CompletionInput;
18use Symfony\Component\Console\Completion\CompletionSuggestions;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020019use Symfony\Component\Console\Exception\RuntimeException;
20use Symfony\Component\Console\Input\InputArgument;
21use Symfony\Component\Console\Input\InputInterface;
22use Symfony\Component\Console\Input\InputOption;
23use Symfony\Component\Console\Output\OutputInterface;
24use Symfony\Component\Console\Style\SymfonyStyle;
25use Symfony\Component\Translation\Exception\InvalidArgumentException;
26use Symfony\Component\Translation\Util\XliffUtils;
27
28/**
29 * Validates XLIFF files syntax and outputs encountered errors.
30 *
31 * @author Grégoire Pineau <lyrixx@lyrixx.info>
32 * @author Robin Chalas <robin.chalas@gmail.com>
33 * @author Javier Eguiluz <javier.eguiluz@gmail.com>
34 */
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010035#[AsCommand(name: 'lint:xliff', description: 'Lint an XLIFF file and outputs encountered errors')]
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020036class XliffLintCommand extends Command
37{
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010038 private string $format;
39 private bool $displayCorrectFiles;
40 private ?\Closure $directoryIteratorProvider;
41 private ?\Closure $isReadableProvider;
42 private bool $requireStrictFileNames;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020043
44 public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, bool $requireStrictFileNames = true)
45 {
46 parent::__construct($name);
47
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010048 $this->directoryIteratorProvider = null === $directoryIteratorProvider || $directoryIteratorProvider instanceof \Closure ? $directoryIteratorProvider : \Closure::fromCallable($directoryIteratorProvider);
49 $this->isReadableProvider = null === $isReadableProvider || $isReadableProvider instanceof \Closure ? $isReadableProvider : \Closure::fromCallable($isReadableProvider);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020050 $this->requireStrictFileNames = $requireStrictFileNames;
51 }
52
53 /**
54 * {@inheritdoc}
55 */
56 protected function configure()
57 {
58 $this
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020059 ->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010060 ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format')
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020061 ->setHelp(<<<EOF
62The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT
63the first encountered syntax error.
64
65You can validates XLIFF contents passed from STDIN:
66
67 <info>cat filename | php %command.full_name% -</info>
68
69You can also validate the syntax of a file:
70
71 <info>php %command.full_name% filename</info>
72
73Or of a whole directory:
74
75 <info>php %command.full_name% dirname</info>
76 <info>php %command.full_name% dirname --format=json</info>
77
78EOF
79 )
80 ;
81 }
82
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010083 protected function execute(InputInterface $input, OutputInterface $output): int
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020084 {
85 $io = new SymfonyStyle($input, $output);
86 $filenames = (array) $input->getArgument('filename');
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +010087 $this->format = $input->getOption('format') ?? (GithubActionReporter::isGithubActionEnvironment() ? 'github' : 'txt');
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +020088 $this->displayCorrectFiles = $output->isVerbose();
89
90 if (['-'] === $filenames) {
91 return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
92 }
93
94 if (!$filenames) {
95 throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
96 }
97
98 $filesInfo = [];
99 foreach ($filenames as $filename) {
100 if (!$this->isReadable($filename)) {
101 throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
102 }
103
104 foreach ($this->getFiles($filename) as $file) {
105 $filesInfo[] = $this->validate(file_get_contents($file), $file);
106 }
107 }
108
109 return $this->display($io, $filesInfo);
110 }
111
112 private function validate(string $content, string $file = null): array
113 {
114 $errors = [];
115
116 // Avoid: Warning DOMDocument::loadXML(): Empty string supplied as input
117 if ('' === trim($content)) {
118 return ['file' => $file, 'valid' => true];
119 }
120
121 $internal = libxml_use_internal_errors(true);
122
123 $document = new \DOMDocument();
124 $document->loadXML($content);
125
126 if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
127 $normalizedLocalePattern = sprintf('(%s|%s)', preg_quote($targetLanguage, '/'), preg_quote(str_replace('-', '_', $targetLanguage), '/'));
128 // strict file names require translation files to be named '____.locale.xlf'
129 // otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
130 // also, the regexp matching must be case-insensitive, as defined for 'target-language' values
131 // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#target-language
132 $expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocalePattern) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/', $normalizedLocalePattern, $normalizedLocalePattern);
133
134 if (0 === preg_match($expectedFilenamePattern, basename($file))) {
135 $errors[] = [
136 'line' => -1,
137 'column' => -1,
138 'message' => sprintf('There is a mismatch between the language included in the file name ("%s") and the "%s" value used in the "target-language" attribute of the file.', basename($file), $targetLanguage),
139 ];
140 }
141 }
142
143 foreach (XliffUtils::validateSchema($document) as $xmlError) {
144 $errors[] = [
145 'line' => $xmlError['line'],
146 'column' => $xmlError['column'],
147 'message' => $xmlError['message'],
148 ];
149 }
150
151 libxml_clear_errors();
152 libxml_use_internal_errors($internal);
153
154 return ['file' => $file, 'valid' => 0 === \count($errors), 'messages' => $errors];
155 }
156
157 private function display(SymfonyStyle $io, array $files)
158 {
159 switch ($this->format) {
160 case 'txt':
161 return $this->displayTxt($io, $files);
162 case 'json':
163 return $this->displayJson($io, $files);
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100164 case 'github':
165 return $this->displayTxt($io, $files, true);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200166 default:
167 throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
168 }
169 }
170
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100171 private function displayTxt(SymfonyStyle $io, array $filesInfo, bool $errorAsGithubAnnotations = false)
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200172 {
173 $countFiles = \count($filesInfo);
174 $erroredFiles = 0;
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100175 $githubReporter = $errorAsGithubAnnotations ? new GithubActionReporter($io) : null;
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200176
177 foreach ($filesInfo as $info) {
178 if ($info['valid'] && $this->displayCorrectFiles) {
179 $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
180 } elseif (!$info['valid']) {
181 ++$erroredFiles;
182 $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100183 $io->listing(array_map(function ($error) use ($info, $githubReporter) {
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200184 // general document errors have a '-1' line number
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100185 $line = -1 === $error['line'] ? null : $error['line'];
186
187 if ($githubReporter) {
188 $githubReporter->error($error['message'], $info['file'], $line, null !== $line ? $error['column'] : null);
189 }
190
191 return null === $line ? $error['message'] : sprintf('Line %d, Column %d: %s', $line, $error['column'], $error['message']);
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200192 }, $info['messages']));
193 }
194 }
195
196 if (0 === $erroredFiles) {
197 $io->success(sprintf('All %d XLIFF files contain valid syntax.', $countFiles));
198 } else {
199 $io->warning(sprintf('%d XLIFF files have valid syntax and %d contain errors.', $countFiles - $erroredFiles, $erroredFiles));
200 }
201
202 return min($erroredFiles, 1);
203 }
204
205 private function displayJson(SymfonyStyle $io, array $filesInfo)
206 {
207 $errors = 0;
208
209 array_walk($filesInfo, function (&$v) use (&$errors) {
210 $v['file'] = (string) $v['file'];
211 if (!$v['valid']) {
212 ++$errors;
213 }
214 });
215
216 $io->writeln(json_encode($filesInfo, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
217
218 return min($errors, 1);
219 }
220
221 private function getFiles(string $fileOrDirectory)
222 {
223 if (is_file($fileOrDirectory)) {
224 yield new \SplFileInfo($fileOrDirectory);
225
226 return;
227 }
228
229 foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
230 if (!\in_array($file->getExtension(), ['xlf', 'xliff'])) {
231 continue;
232 }
233
234 yield $file;
235 }
236 }
237
238 private function getDirectoryIterator(string $directory)
239 {
240 $default = function ($directory) {
241 return new \RecursiveIteratorIterator(
242 new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
243 \RecursiveIteratorIterator::LEAVES_ONLY
244 );
245 };
246
247 if (null !== $this->directoryIteratorProvider) {
248 return ($this->directoryIteratorProvider)($directory, $default);
249 }
250
251 return $default($directory);
252 }
253
254 private function isReadable(string $fileOrDirectory)
255 {
256 $default = function ($fileOrDirectory) {
257 return is_readable($fileOrDirectory);
258 };
259
260 if (null !== $this->isReadableProvider) {
261 return ($this->isReadableProvider)($fileOrDirectory, $default);
262 }
263
264 return $default($fileOrDirectory);
265 }
266
267 private function getTargetLanguageFromFile(\DOMDocument $xliffContents): ?string
268 {
269 foreach ($xliffContents->getElementsByTagName('file')[0]->attributes ?? [] as $attribute) {
270 if ('target-language' === $attribute->nodeName) {
271 return $attribute->nodeValue;
272 }
273 }
274
275 return null;
276 }
Matthias Andreas Benkard1ba53812022-12-27 17:32:58 +0100277
278 public function complete(CompletionInput $input, CompletionSuggestions $suggestions): void
279 {
280 if ($input->mustSuggestOptionValuesFor('format')) {
281 $suggestions->suggestValues(['txt', 'json', 'github']);
282 }
283 }
Matthias Andreas Benkard7b2a3a12021-08-16 10:57:25 +0200284}