blob: 4117d87c855e2b01cb6a8ebbd61b6379b81cb8da [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
14use Symfony\Component\Console\Command\Command;
15use Symfony\Component\Console\Exception\RuntimeException;
16use Symfony\Component\Console\Input\InputArgument;
17use Symfony\Component\Console\Input\InputInterface;
18use Symfony\Component\Console\Input\InputOption;
19use Symfony\Component\Console\Output\OutputInterface;
20use Symfony\Component\Console\Style\SymfonyStyle;
21use Symfony\Component\Translation\Exception\InvalidArgumentException;
22use Symfony\Component\Translation\Util\XliffUtils;
23
24/**
25 * Validates XLIFF files syntax and outputs encountered errors.
26 *
27 * @author Grégoire Pineau <lyrixx@lyrixx.info>
28 * @author Robin Chalas <robin.chalas@gmail.com>
29 * @author Javier Eguiluz <javier.eguiluz@gmail.com>
30 */
31class XliffLintCommand extends Command
32{
33 protected static $defaultName = 'lint:xliff';
34 protected static $defaultDescription = 'Lint an XLIFF file and outputs encountered errors';
35
36 private $format;
37 private $displayCorrectFiles;
38 private $directoryIteratorProvider;
39 private $isReadableProvider;
40 private $requireStrictFileNames;
41
42 public function __construct(string $name = null, callable $directoryIteratorProvider = null, callable $isReadableProvider = null, bool $requireStrictFileNames = true)
43 {
44 parent::__construct($name);
45
46 $this->directoryIteratorProvider = $directoryIteratorProvider;
47 $this->isReadableProvider = $isReadableProvider;
48 $this->requireStrictFileNames = $requireStrictFileNames;
49 }
50
51 /**
52 * {@inheritdoc}
53 */
54 protected function configure()
55 {
56 $this
57 ->setDescription(self::$defaultDescription)
58 ->addArgument('filename', InputArgument::IS_ARRAY, 'A file, a directory or "-" for reading from STDIN')
59 ->addOption('format', null, InputOption::VALUE_REQUIRED, 'The output format', 'txt')
60 ->setHelp(<<<EOF
61The <info>%command.name%</info> command lints an XLIFF file and outputs to STDOUT
62the first encountered syntax error.
63
64You can validates XLIFF contents passed from STDIN:
65
66 <info>cat filename | php %command.full_name% -</info>
67
68You can also validate the syntax of a file:
69
70 <info>php %command.full_name% filename</info>
71
72Or of a whole directory:
73
74 <info>php %command.full_name% dirname</info>
75 <info>php %command.full_name% dirname --format=json</info>
76
77EOF
78 )
79 ;
80 }
81
82 protected function execute(InputInterface $input, OutputInterface $output)
83 {
84 $io = new SymfonyStyle($input, $output);
85 $filenames = (array) $input->getArgument('filename');
86 $this->format = $input->getOption('format');
87 $this->displayCorrectFiles = $output->isVerbose();
88
89 if (['-'] === $filenames) {
90 return $this->display($io, [$this->validate(file_get_contents('php://stdin'))]);
91 }
92
93 if (!$filenames) {
94 throw new RuntimeException('Please provide a filename or pipe file content to STDIN.');
95 }
96
97 $filesInfo = [];
98 foreach ($filenames as $filename) {
99 if (!$this->isReadable($filename)) {
100 throw new RuntimeException(sprintf('File or directory "%s" is not readable.', $filename));
101 }
102
103 foreach ($this->getFiles($filename) as $file) {
104 $filesInfo[] = $this->validate(file_get_contents($file), $file);
105 }
106 }
107
108 return $this->display($io, $filesInfo);
109 }
110
111 private function validate(string $content, string $file = null): array
112 {
113 $errors = [];
114
115 // Avoid: Warning DOMDocument::loadXML(): Empty string supplied as input
116 if ('' === trim($content)) {
117 return ['file' => $file, 'valid' => true];
118 }
119
120 $internal = libxml_use_internal_errors(true);
121
122 $document = new \DOMDocument();
123 $document->loadXML($content);
124
125 if (null !== $targetLanguage = $this->getTargetLanguageFromFile($document)) {
126 $normalizedLocalePattern = sprintf('(%s|%s)', preg_quote($targetLanguage, '/'), preg_quote(str_replace('-', '_', $targetLanguage), '/'));
127 // strict file names require translation files to be named '____.locale.xlf'
128 // otherwise, both '____.locale.xlf' and 'locale.____.xlf' are allowed
129 // also, the regexp matching must be case-insensitive, as defined for 'target-language' values
130 // http://docs.oasis-open.org/xliff/v1.2/os/xliff-core.html#target-language
131 $expectedFilenamePattern = $this->requireStrictFileNames ? sprintf('/^.*\.(?i:%s)\.(?:xlf|xliff)/', $normalizedLocalePattern) : sprintf('/^(?:.*\.(?i:%s)|(?i:%s)\..*)\.(?:xlf|xliff)/', $normalizedLocalePattern, $normalizedLocalePattern);
132
133 if (0 === preg_match($expectedFilenamePattern, basename($file))) {
134 $errors[] = [
135 'line' => -1,
136 'column' => -1,
137 '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),
138 ];
139 }
140 }
141
142 foreach (XliffUtils::validateSchema($document) as $xmlError) {
143 $errors[] = [
144 'line' => $xmlError['line'],
145 'column' => $xmlError['column'],
146 'message' => $xmlError['message'],
147 ];
148 }
149
150 libxml_clear_errors();
151 libxml_use_internal_errors($internal);
152
153 return ['file' => $file, 'valid' => 0 === \count($errors), 'messages' => $errors];
154 }
155
156 private function display(SymfonyStyle $io, array $files)
157 {
158 switch ($this->format) {
159 case 'txt':
160 return $this->displayTxt($io, $files);
161 case 'json':
162 return $this->displayJson($io, $files);
163 default:
164 throw new InvalidArgumentException(sprintf('The format "%s" is not supported.', $this->format));
165 }
166 }
167
168 private function displayTxt(SymfonyStyle $io, array $filesInfo)
169 {
170 $countFiles = \count($filesInfo);
171 $erroredFiles = 0;
172
173 foreach ($filesInfo as $info) {
174 if ($info['valid'] && $this->displayCorrectFiles) {
175 $io->comment('<info>OK</info>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
176 } elseif (!$info['valid']) {
177 ++$erroredFiles;
178 $io->text('<error> ERROR </error>'.($info['file'] ? sprintf(' in %s', $info['file']) : ''));
179 $io->listing(array_map(function ($error) {
180 // general document errors have a '-1' line number
181 return -1 === $error['line'] ? $error['message'] : sprintf('Line %d, Column %d: %s', $error['line'], $error['column'], $error['message']);
182 }, $info['messages']));
183 }
184 }
185
186 if (0 === $erroredFiles) {
187 $io->success(sprintf('All %d XLIFF files contain valid syntax.', $countFiles));
188 } else {
189 $io->warning(sprintf('%d XLIFF files have valid syntax and %d contain errors.', $countFiles - $erroredFiles, $erroredFiles));
190 }
191
192 return min($erroredFiles, 1);
193 }
194
195 private function displayJson(SymfonyStyle $io, array $filesInfo)
196 {
197 $errors = 0;
198
199 array_walk($filesInfo, function (&$v) use (&$errors) {
200 $v['file'] = (string) $v['file'];
201 if (!$v['valid']) {
202 ++$errors;
203 }
204 });
205
206 $io->writeln(json_encode($filesInfo, \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES));
207
208 return min($errors, 1);
209 }
210
211 private function getFiles(string $fileOrDirectory)
212 {
213 if (is_file($fileOrDirectory)) {
214 yield new \SplFileInfo($fileOrDirectory);
215
216 return;
217 }
218
219 foreach ($this->getDirectoryIterator($fileOrDirectory) as $file) {
220 if (!\in_array($file->getExtension(), ['xlf', 'xliff'])) {
221 continue;
222 }
223
224 yield $file;
225 }
226 }
227
228 private function getDirectoryIterator(string $directory)
229 {
230 $default = function ($directory) {
231 return new \RecursiveIteratorIterator(
232 new \RecursiveDirectoryIterator($directory, \FilesystemIterator::SKIP_DOTS | \FilesystemIterator::FOLLOW_SYMLINKS),
233 \RecursiveIteratorIterator::LEAVES_ONLY
234 );
235 };
236
237 if (null !== $this->directoryIteratorProvider) {
238 return ($this->directoryIteratorProvider)($directory, $default);
239 }
240
241 return $default($directory);
242 }
243
244 private function isReadable(string $fileOrDirectory)
245 {
246 $default = function ($fileOrDirectory) {
247 return is_readable($fileOrDirectory);
248 };
249
250 if (null !== $this->isReadableProvider) {
251 return ($this->isReadableProvider)($fileOrDirectory, $default);
252 }
253
254 return $default($fileOrDirectory);
255 }
256
257 private function getTargetLanguageFromFile(\DOMDocument $xliffContents): ?string
258 {
259 foreach ($xliffContents->getElementsByTagName('file')[0]->attributes ?? [] as $attribute) {
260 if ('target-language' === $attribute->nodeName) {
261 return $attribute->nodeValue;
262 }
263 }
264
265 return null;
266 }
267}