ff3a6780012ea8718cbd3859ab1af43d5cc8f3a1
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Localization / Parser / LocallangXmlParser.php
1 <?php
2 namespace TYPO3\CMS\Core\Localization\Parser;
3
4 /*
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 use TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException;
18 use TYPO3\CMS\Core\Utility\ArrayUtility;
19 use TYPO3\CMS\Core\Utility\GeneralUtility;
20 use TYPO3\CMS\Core\Utility\PathUtility;
21
22 /**
23 * Parser for XML locallang file.
24 */
25 class LocallangXmlParser extends AbstractXmlParser
26 {
27 /**
28 * Associative array of "filename => parsed data" pairs.
29 *
30 * @var array
31 */
32 protected $parsedTargetFiles;
33
34 /**
35 * Returns parsed representation of XML file.
36 *
37 * @param string $sourcePath Source file path
38 * @param string $languageKey Language key
39 * @param string $charset File charset, not in use anymore and deprecated since TYPO3 v8, will be removed in TYPO3 v9 as UTF-8 is expected for all language files
40 * @return array
41 */
42 public function getParsedData($sourcePath, $languageKey, $charset = '')
43 {
44 $this->sourcePath = $sourcePath;
45 $this->languageKey = $languageKey;
46 // Parse source
47 $parsedSource = $this->parseXmlFile();
48 // Parse target
49 $localizedTargetPath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
50 $targetPath = $this->languageKey !== 'default' && @is_file($localizedTargetPath) ? $localizedTargetPath : $this->sourcePath;
51 try {
52 $parsedTarget = $this->getParsedTargetData($targetPath);
53 } catch (InvalidXmlFileException $e) {
54 $parsedTarget = $this->getParsedTargetData($this->sourcePath);
55 }
56 $LOCAL_LANG = [];
57 $LOCAL_LANG[$languageKey] = $parsedSource;
58 ArrayUtility::mergeRecursiveWithOverrule($LOCAL_LANG[$languageKey], $parsedTarget);
59 return $LOCAL_LANG;
60 }
61
62 /**
63 * Returns array representation of XLIFF data, starting from a root node.
64 *
65 * @param \SimpleXMLElement $root XML root element
66 * @param string $element Target or Source
67 * @return array
68 * @throws InvalidXmlFileException
69 */
70 protected function doParsingFromRootForElement(\SimpleXMLElement $root, $element)
71 {
72 $bodyOfFileTag = $root->data->languageKey;
73 if ($bodyOfFileTag === null) {
74 throw new InvalidXmlFileException('Invalid locallang.xml language file "' . PathUtility::stripPathSitePrefix($this->sourcePath) . '"', 1487944884);
75 }
76
77 // Check if the source llxml file contains localized records
78 $localizedBodyOfFileTag = $root->data->xpath('languageKey[@index=\'' . $this->languageKey . '\']');
79 if ($element === 'source' || $this->languageKey === 'default') {
80 $parsedData = $this->getParsedDataForElement($bodyOfFileTag, $element);
81 } else {
82 $parsedData = [];
83 }
84 if ($element === 'target' && isset($localizedBodyOfFileTag[0]) && $localizedBodyOfFileTag[0] instanceof \SimpleXMLElement) {
85 $parsedDataTarget = $this->getParsedDataForElement($localizedBodyOfFileTag[0], $element);
86 $mergedData = $parsedDataTarget + $parsedData;
87 if ($this->languageKey === 'default') {
88 $parsedData = array_intersect_key($mergedData, $parsedData, $parsedDataTarget);
89 } else {
90 $parsedData = array_intersect_key($mergedData, $parsedDataTarget);
91 }
92 }
93 return $parsedData;
94 }
95
96 /**
97 * Parse the given language key tag
98 *
99 * @param \SimpleXMLElement $bodyOfFileTag
100 * @param string $element
101 * @return array
102 */
103 protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, $element)
104 {
105 $parsedData = [];
106 $children = $bodyOfFileTag->children();
107 if ($children->count() === 0) {
108 // Check for externally-referenced resource:
109 // <languageKey index="fr">EXT:yourext/path/to/localized/locallang.xml</languageKey>
110 $reference = sprintf('%s', $bodyOfFileTag);
111 if (substr($reference, -4) === '.xml') {
112 return $this->getParsedTargetData(GeneralUtility::getFileAbsFileName($reference));
113 }
114 }
115 /** @var \SimpleXMLElement $translationElement */
116 foreach ($children as $translationElement) {
117 if ($translationElement->getName() === 'label') {
118 $parsedData[(string)$translationElement['index']][0] = [
119 $element => (string)$translationElement
120 ];
121 }
122 }
123 return $parsedData;
124 }
125
126 /**
127 * Returns array representation of XLIFF data, starting from a root node.
128 *
129 * @param \SimpleXMLElement $root A root node
130 * @return array An array representing parsed XLIFF
131 */
132 protected function doParsingFromRoot(\SimpleXMLElement $root)
133 {
134 return $this->doParsingFromRootForElement($root, 'source');
135 }
136
137 /**
138 * Returns array representation of XLIFF data, starting from a root node.
139 *
140 * @param \SimpleXMLElement $root A root node
141 * @return array An array representing parsed XLIFF
142 */
143 protected function doParsingTargetFromRoot(\SimpleXMLElement $root)
144 {
145 return $this->doParsingFromRootForElement($root, 'target');
146 }
147
148 /**
149 * Returns parsed representation of XML file.
150 *
151 * Parses XML if it wasn't done before. Caches parsed data.
152 *
153 * @param string $path An absolute path to XML file
154 * @return array Parsed XML file
155 */
156 public function getParsedTargetData($path)
157 {
158 if (!isset($this->parsedTargetFiles[$path])) {
159 $this->parsedTargetFiles[$path] = $this->parseXmlTargetFile($path);
160 }
161 return $this->parsedTargetFiles[$path];
162 }
163
164 /**
165 * Reads and parses XML file and returns internal representation of data.
166 *
167 * @param string $targetPath Path of the target file
168 * @return array
169 * @throws \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
170 */
171 protected function parseXmlTargetFile($targetPath)
172 {
173 $rootXmlNode = false;
174 if (file_exists($targetPath)) {
175 $xmlContent = file_get_contents($targetPath);
176 // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
177 $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
178 $rootXmlNode = simplexml_load_string($xmlContent, 'SimpleXMLElement', LIBXML_NOWARNING);
179 libxml_disable_entity_loader($previousValueOfEntityLoader);
180 }
181 if (!isset($rootXmlNode) || $rootXmlNode === false) {
182 $xmlError = libxml_get_last_error();
183 throw new InvalidXmlFileException(
184 'The path provided does not point to existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $targetPath . ', line ' . $xmlError->line,
185 1278155987
186 );
187 }
188 return $this->doParsingTargetFromRoot($rootXmlNode);
189 }
190 }