You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

267 lines
9.4 KiB

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