XliffLintCommand.php 9.3 KB

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