TYPO3 CMS  TYPO3_6-2
LocallangXmlParser.php
Go to the documentation of this file.
1 <?php
3 
22 
28  protected $parsedTargetFiles;
29 
38  public function getParsedData($sourcePath, $languageKey, $charset = '') {
39  $this->sourcePath = $sourcePath;
40  $this->languageKey = $languageKey;
41  $this->charset = $this->getCharset($languageKey, $charset);
42  // Parse source
43  $parsedSource = $this->parseXmlFile();
44  // Parse target
45  $localizedTargetPath = \TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName(\TYPO3\CMS\Core\Utility\GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey));
46  $targetPath = $this->languageKey !== 'default' && @is_file($localizedTargetPath) ? $localizedTargetPath : $this->sourcePath;
47  try {
48  $parsedTarget = $this->getParsedTargetData($targetPath);
49  } catch (\TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException $e) {
50  $parsedTarget = $this->getParsedTargetData($this->sourcePath);
51  }
52  $LOCAL_LANG = array();
53  $LOCAL_LANG[$languageKey] = $parsedSource;
55  return $LOCAL_LANG;
56  }
57 
65  protected function doParsingFromRootForElement(\SimpleXMLElement $root, $element) {
66  $bodyOfFileTag = $root->data->languageKey;
67  // Check if the source llxml file contains localized records
68  $localizedBodyOfFileTag = $root->data->xpath('languageKey[@index=\'' . $this->languageKey . '\']');
69  if ($element === 'source' || $this->languageKey === 'default') {
70  $parsedData = $this->getParsedDataForElement($bodyOfFileTag, $element);
71  } else {
72  $parsedData = array();
73  }
74  if ($element === 'target' && isset($localizedBodyOfFileTag[0]) && $localizedBodyOfFileTag[0] instanceof \SimpleXMLElement) {
75  $parsedDataTarget = $this->getParsedDataForElement($localizedBodyOfFileTag[0], $element);
76  $mergedData = $parsedDataTarget + $parsedData;
77  if ($this->languageKey === 'default') {
78  $parsedData = array_intersect_key($mergedData, $parsedData, $parsedDataTarget);
79  } else {
80  $parsedData = array_intersect_key($mergedData, $parsedDataTarget);
81  }
82  }
83  return $parsedData;
84  }
85 
93  protected function getParsedDataForElement(\SimpleXMLElement $bodyOfFileTag, $element) {
94  $parsedData = array();
95  $children = $bodyOfFileTag->children();
96  if ($children->count() == 0) {
97  // Check for externally-referenced resource:
98  // <languageKey index="fr">EXT:yourext/path/to/localized/locallang.xml</languageKey>
99  $reference = sprintf('%s', $bodyOfFileTag);
100  if (substr($reference, -4) === '.xml') {
101  return $this->getParsedTargetData(\TYPO3\CMS\Core\Utility\GeneralUtility::getFileAbsFileName($reference));
102  }
103  }
105  foreach ($children as $translationElement) {
106  if ($translationElement->getName() === 'label') {
107  // If restype would be set, it could be metadata from Gettext to XLIFF conversion (and we don't need this data)
108  $parsedData[(string) $translationElement['index']][0] = array(
109  $element => (string) $translationElement
110  );
111  }
112  }
113  return $parsedData;
114  }
115 
122  protected function doParsingFromRoot(\SimpleXMLElement $root) {
123  return $this->doParsingFromRootForElement($root, 'source');
124  }
125 
132  protected function doParsingTargetFromRoot(\SimpleXMLElement $root) {
133  return $this->doParsingFromRootForElement($root, 'target');
134  }
135 
144  public function getParsedTargetData($path) {
145  if (!isset($this->parsedTargetFiles[$path])) {
146  $this->parsedTargetFiles[$path] = $this->parseXmlTargetFile($path);
147  }
148  return $this->parsedTargetFiles[$path];
149  }
150 
158  protected function parseXmlTargetFile($targetPath) {
159  $rootXmlNode = FALSE;
160  if (file_exists($targetPath)) {
161  $xmlContent = file_get_contents($targetPath);
162  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
163  $previousValueOfEntityLoader = libxml_disable_entity_loader(TRUE);
164  $rootXmlNode = simplexml_load_string($xmlContent, 'SimpleXmlElement', \LIBXML_NOWARNING);
165  libxml_disable_entity_loader($previousValueOfEntityLoader);
166  }
167  if (!isset($rootXmlNode) || $rootXmlNode === FALSE) {
168  throw new \TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException('The path provided does not point to existing and accessible well-formed XML file (' . $targetPath . ').', 1278155987);
169  }
170  return $this->doParsingTargetFromRoot($rootXmlNode);
171  }
172 
173 }
static mergeRecursiveWithOverrule(array &$original, array $overrule, $addKeys=TRUE, $includeEmptyValues=TRUE, $enableUnsetFeature=TRUE)
static llXmlAutoFileName($fileRef, $language, $sameLocation=FALSE)
getParsedData($sourcePath, $languageKey, $charset='')
doParsingFromRootForElement(\SimpleXMLElement $root, $element)
static getFileAbsFileName($filename, $onlyRelative=TRUE, $relToTYPO3_mainDir=FALSE)