XliffUtils.php
156 lines
| 1 | <?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 | namespace IAWPSCOPED\Symfony\Component\Translation\Util; |
| 12 | |
| 13 | use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidArgumentException; |
| 14 | use IAWPSCOPED\Symfony\Component\Translation\Exception\InvalidResourceException; |
| 15 | /** |
| 16 | * Provides some utility methods for XLIFF translation files, such as validating |
| 17 | * their contents according to the XSD schema. |
| 18 | * |
| 19 | * @author Fabien Potencier <fabien@symfony.com> |
| 20 | * @internal |
| 21 | */ |
| 22 | class XliffUtils |
| 23 | { |
| 24 | /** |
| 25 | * Gets xliff file version based on the root "version" attribute. |
| 26 | * |
| 27 | * Defaults to 1.2 for backwards compatibility. |
| 28 | * |
| 29 | * @throws InvalidArgumentException |
| 30 | */ |
| 31 | public static function getVersionNumber(\DOMDocument $dom) : string |
| 32 | { |
| 33 | /** @var \DOMNode $xliff */ |
| 34 | foreach ($dom->getElementsByTagName('xliff') as $xliff) { |
| 35 | $version = $xliff->attributes->getNamedItem('version'); |
| 36 | if ($version) { |
| 37 | return $version->nodeValue; |
| 38 | } |
| 39 | $namespace = $xliff->attributes->getNamedItem('xmlns'); |
| 40 | if ($namespace) { |
| 41 | if (0 !== \substr_compare('urn:oasis:names:tc:xliff:document:', $namespace->nodeValue, 0, 34)) { |
| 42 | throw new InvalidArgumentException(\sprintf('Not a valid XLIFF namespace "%s".', $namespace)); |
| 43 | } |
| 44 | return \substr($namespace, 34); |
| 45 | } |
| 46 | } |
| 47 | // Falls back to v1.2 |
| 48 | return '1.2'; |
| 49 | } |
| 50 | /** |
| 51 | * Validates and parses the given file into a DOMDocument. |
| 52 | * |
| 53 | * @throws InvalidResourceException |
| 54 | */ |
| 55 | public static function validateSchema(\DOMDocument $dom) : array |
| 56 | { |
| 57 | $xliffVersion = static::getVersionNumber($dom); |
| 58 | $internalErrors = \libxml_use_internal_errors(\true); |
| 59 | if ($shouldEnable = self::shouldEnableEntityLoader()) { |
| 60 | $disableEntities = \libxml_disable_entity_loader(\false); |
| 61 | } |
| 62 | try { |
| 63 | $isValid = @$dom->schemaValidateSource(self::getSchema($xliffVersion)); |
| 64 | if (!$isValid) { |
| 65 | return self::getXmlErrors($internalErrors); |
| 66 | } |
| 67 | } finally { |
| 68 | if ($shouldEnable) { |
| 69 | \libxml_disable_entity_loader($disableEntities); |
| 70 | } |
| 71 | } |
| 72 | $dom->normalizeDocument(); |
| 73 | \libxml_clear_errors(); |
| 74 | \libxml_use_internal_errors($internalErrors); |
| 75 | return []; |
| 76 | } |
| 77 | private static function shouldEnableEntityLoader() : bool |
| 78 | { |
| 79 | static $dom, $schema; |
| 80 | if (null === $dom) { |
| 81 | $dom = new \DOMDocument(); |
| 82 | $dom->loadXML('<?xml version="1.0"?><test/>'); |
| 83 | $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony'); |
| 84 | \register_shutdown_function(static function () use($tmpfile) { |
| 85 | @\unlink($tmpfile); |
| 86 | }); |
| 87 | $schema = '<?xml version="1.0" encoding="utf-8"?> |
| 88 | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 89 | <xsd:include schemaLocation="file:///' . \str_replace('\\', '/', $tmpfile) . '" /> |
| 90 | </xsd:schema>'; |
| 91 | \file_put_contents($tmpfile, '<?xml version="1.0" encoding="utf-8"?> |
| 92 | <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"> |
| 93 | <xsd:element name="test" type="testType" /> |
| 94 | <xsd:complexType name="testType"/> |
| 95 | </xsd:schema>'); |
| 96 | } |
| 97 | return !@$dom->schemaValidateSource($schema); |
| 98 | } |
| 99 | public static function getErrorsAsString(array $xmlErrors) : string |
| 100 | { |
| 101 | $errorsAsString = ''; |
| 102 | foreach ($xmlErrors as $error) { |
| 103 | $errorsAsString .= \sprintf("[%s %s] %s (in %s - line %d, column %d)\n", \LIBXML_ERR_WARNING === $error['level'] ? 'WARNING' : 'ERROR', $error['code'], $error['message'], $error['file'], $error['line'], $error['column']); |
| 104 | } |
| 105 | return $errorsAsString; |
| 106 | } |
| 107 | private static function getSchema(string $xliffVersion) : string |
| 108 | { |
| 109 | if ('1.2' === $xliffVersion) { |
| 110 | $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-1.2-strict.xsd'); |
| 111 | $xmlUri = 'http://www.w3.org/2001/xml.xsd'; |
| 112 | } elseif ('2.0' === $xliffVersion) { |
| 113 | $schemaSource = \file_get_contents(__DIR__ . '/../Resources/schemas/xliff-core-2.0.xsd'); |
| 114 | $xmlUri = 'informativeCopiesOf3rdPartySchemas/w3c/xml.xsd'; |
| 115 | } else { |
| 116 | throw new InvalidArgumentException(\sprintf('No support implemented for loading XLIFF version "%s".', $xliffVersion)); |
| 117 | } |
| 118 | return self::fixXmlLocation($schemaSource, $xmlUri); |
| 119 | } |
| 120 | /** |
| 121 | * Internally changes the URI of a dependent xsd to be loaded locally. |
| 122 | */ |
| 123 | private static function fixXmlLocation(string $schemaSource, string $xmlUri) : string |
| 124 | { |
| 125 | $newPath = \str_replace('\\', '/', __DIR__) . '/../Resources/schemas/xml.xsd'; |
| 126 | $parts = \explode('/', $newPath); |
| 127 | $locationstart = 'file:///'; |
| 128 | if (0 === \stripos($newPath, 'phar://')) { |
| 129 | $tmpfile = \tempnam(\sys_get_temp_dir(), 'symfony'); |
| 130 | if ($tmpfile) { |
| 131 | \copy($newPath, $tmpfile); |
| 132 | $parts = \explode('/', \str_replace('\\', '/', $tmpfile)); |
| 133 | } else { |
| 134 | \array_shift($parts); |
| 135 | $locationstart = 'phar:///'; |
| 136 | } |
| 137 | } |
| 138 | $drive = '\\' === \DIRECTORY_SEPARATOR ? \array_shift($parts) . '/' : ''; |
| 139 | $newPath = $locationstart . $drive . \implode('/', \array_map('rawurlencode', $parts)); |
| 140 | return \str_replace($xmlUri, $newPath, $schemaSource); |
| 141 | } |
| 142 | /** |
| 143 | * Returns the XML errors of the internal XML parser. |
| 144 | */ |
| 145 | private static function getXmlErrors(bool $internalErrors) : array |
| 146 | { |
| 147 | $errors = []; |
| 148 | foreach (\libxml_get_errors() as $error) { |
| 149 | $errors[] = ['level' => \LIBXML_ERR_WARNING == $error->level ? 'WARNING' : 'ERROR', 'code' => $error->code, 'message' => \trim($error->message), 'file' => $error->file ?: 'n/a', 'line' => $error->line, 'column' => $error->column]; |
| 150 | } |
| 151 | \libxml_clear_errors(); |
| 152 | \libxml_use_internal_errors($internalErrors); |
| 153 | return $errors; |
| 154 | } |
| 155 | } |
| 156 |