XliffLintCommand.php 10 KB

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