‪TYPO3CMS  10.4
AbstractXmlParser.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
23 
29 {
33  protected ‪$sourcePath;
34 
38  protected ‪$languageKey;
39 
49  {
50  $this->sourcePath = ‪$sourcePath;
51  $this->languageKey = ‪$languageKey;
52  if ($this->languageKey !== 'default') {
53  $this->sourcePath = $this->‪getLocalizedFileName($this->sourcePath, $this->languageKey);
54  if (!@is_file($this->sourcePath)) {
55  // Global localization is not available, try split localization file
56  $this->sourcePath = $this->‪getLocalizedFileName(‪$sourcePath, ‪$languageKey, true);
57  }
58  if (!@is_file($this->sourcePath)) {
59  throw new ‪FileNotFoundException('Localization file does not exist', 1306332397);
60  }
61  }
62  $LOCAL_LANG = [];
63  $LOCAL_LANG[‪$languageKey] = $this->‪parseXmlFile();
64  return $LOCAL_LANG;
65  }
66 
73  protected function ‪parseXmlFile()
74  {
75  $xmlContent = file_get_contents($this->sourcePath);
76  if ($xmlContent === false) {
78  'The path provided does not point to an existing and accessible file.',
79  1278155986
80  );
81  }
82  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
83  $previousValueOfEntityLoader = null;
84  if (PHP_MAJOR_VERSION < 8) {
85  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
86  }
87  $rootXmlNode = simplexml_load_string($xmlContent, \SimpleXMLElement::class, LIBXML_NOWARNING);
88  if (PHP_MAJOR_VERSION < 8) {
89  libxml_disable_entity_loader($previousValueOfEntityLoader);
90  }
91  if ($rootXmlNode === false) {
92  $xmlError = libxml_get_last_error();
94  'The path provided does not point to an existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
95  1278155988
96  );
97  }
98  return $this->‪doParsingFromRoot($rootXmlNode);
99  }
100 
110  protected function ‪getLocalizedFileName($fileRef, $language, $sameLocation = false)
111  {
112  // If $fileRef is already prefixed with "[language key]" then we should return it as is
113  $fileName = ‪PathUtility::basename($fileRef);
114  if (GeneralUtility::isFirstPartOfStr($fileName, $language . '.')) {
115  return GeneralUtility::getFileAbsFileName($fileRef);
116  }
117 
118  if ($sameLocation) {
119  return GeneralUtility::getFileAbsFileName(str_replace($fileName, $language . '.' . $fileName, $fileRef));
120  }
121 
122  // Analyze file reference
123  if (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getFrameworkBasePath() . '/')) {
124  // Is system
125  $validatedPrefix = ‪Environment::getFrameworkBasePath() . '/';
126  } elseif (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getBackendPath() . '/ext/')) {
127  // Is global
128  $validatedPrefix = ‪Environment::getBackendPath() . '/ext/';
129  } elseif (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getExtensionsPath() . '/')) {
130  // Is local
131  $validatedPrefix = ‪Environment::getExtensionsPath() . '/';
132  } else {
133  $validatedPrefix = '';
134  }
135  if ($validatedPrefix) {
136  // Divide file reference into extension key, directory (if any) and base name:
137  [$extensionKey, $file_extPath] = explode('/', substr($fileRef, strlen($validatedPrefix)), 2);
138  $temp = ‪GeneralUtility::revExplode('/', $file_extPath, 2);
139  if (count($temp) === 1) {
140  array_unshift($temp, '');
141  }
142  // Add empty first-entry if not there.
143  [$file_extPath, $file_fileName] = $temp;
144  // The filename is prefixed with "[language key]." because it prevents the llxmltranslate tool from detecting it.
145  return ‪Environment::getLabelsPath() . '/' . $language . '/' . $extensionKey . '/' . ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName;
146  }
147  return '';
148  }
149 
156  abstract protected function ‪doParsingFromRoot(\SimpleXMLElement $root);
157 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:24
‪TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
Definition: LocalizationParserInterface.php:22
‪TYPO3\CMS\Core\Core\Environment\getLabelsPath
‪static string getLabelsPath()
Definition: Environment.php:236
‪TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
Definition: FileNotFoundException.php:22
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$sourcePath
‪string $sourcePath
Definition: AbstractXmlParser.php:32
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:165
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:261
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser
Definition: AbstractXmlParser.php:29
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\doParsingFromRoot
‪array doParsingFromRoot(\SimpleXMLElement $root)
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:250
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$languageKey
‪string $languageKey
Definition: AbstractXmlParser.php:36
‪TYPO3\CMS\Core\Utility\GeneralUtility\revExplode
‪static string[] revExplode($delimiter, $string, $count=0)
Definition: GeneralUtility.php:1025
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:40
‪TYPO3\CMS\Core\Localization\Parser
Definition: AbstractXmlParser.php:16
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\parseXmlFile
‪array parseXmlFile()
Definition: AbstractXmlParser.php:71
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:46
‪TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
Definition: InvalidXmlFileException.php:22
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getParsedData
‪array getParsedData($sourcePath, $languageKey)
Definition: AbstractXmlParser.php:46
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getLocalizedFileName
‪string getLocalizedFileName($fileRef, $language, $sameLocation=false)
Definition: AbstractXmlParser.php:108
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static string getExtensionsPath()
Definition: Environment.php:271