‪TYPO3CMS  9.5
AbstractXmlParser.php
Go to the documentation of this file.
1 <?php
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 
22 
28 {
32  protected ‪$sourcePath;
33 
37  protected ‪$languageKey;
38 
48  {
49  $this->sourcePath = ‪$sourcePath;
50  $this->languageKey = ‪$languageKey;
51  if ($this->languageKey !== 'default') {
52  $this->sourcePath = $this->‪getLocalizedFileName($this->sourcePath, $this->languageKey);
53  if (!@is_file($this->sourcePath)) {
54  // Global localization is not available, try split localization file
55  $this->sourcePath = $this->‪getLocalizedFileName(‪$sourcePath, ‪$languageKey, true);
56  }
57  if (!@is_file($this->sourcePath)) {
58  throw new ‪FileNotFoundException('Localization file does not exist', 1306332397);
59  }
60  }
61  $LOCAL_LANG = [];
62  $LOCAL_LANG[‪$languageKey] = $this->‪parseXmlFile();
63  return $LOCAL_LANG;
64  }
65 
72  protected function ‪parseXmlFile()
73  {
74  $xmlContent = file_get_contents($this->sourcePath);
75  // Disables the functionality to allow external entities to be loaded when parsing the XML, must be kept
76  $previousValueOfEntityLoader = libxml_disable_entity_loader(true);
77  $rootXmlNode = simplexml_load_string($xmlContent, 'SimpleXMLElement', LIBXML_NOWARNING);
78  libxml_disable_entity_loader($previousValueOfEntityLoader);
79  if (!isset($rootXmlNode) || $rootXmlNode === false) {
80  $xmlError = libxml_get_last_error();
82  'The path provided does not point to existing and accessible well-formed XML file. Reason: ' . $xmlError->message . ' in ' . $this->sourcePath . ', line ' . $xmlError->line,
83  1278155988
84  );
85  }
86  return $this->‪doParsingFromRoot($rootXmlNode);
87  }
88 
98  protected function ‪getLocalizedFileName($fileRef, $language, $sameLocation = false)
99  {
100  // If $fileRef is already prefixed with "[language key]" then we should return it as is
101  $fileName = ‪PathUtility::basename($fileRef);
102  if (GeneralUtility::isFirstPartOfStr($fileName, $language . '.')) {
103  return GeneralUtility::getFileAbsFileName($fileRef);
104  }
105 
106  if ($sameLocation) {
107  return GeneralUtility::getFileAbsFileName(str_replace($fileName, $language . '.' . $fileName, $fileRef));
108  }
109 
110  // Analyze file reference
111  if (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getFrameworkBasePath() . '/')) {
112  // Is system
113  $validatedPrefix = ‪Environment::getFrameworkBasePath() . '/';
114  } elseif (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getBackendPath() . '/ext/')) {
115  // Is global
116  $validatedPrefix = ‪Environment::getBackendPath() . '/ext/';
117  } elseif (GeneralUtility::isFirstPartOfStr($fileRef, ‪Environment::getExtensionsPath() . '/')) {
118  // Is local
119  $validatedPrefix = ‪Environment::getExtensionsPath() . '/';
120  } else {
121  $validatedPrefix = '';
122  }
123  if ($validatedPrefix) {
124  // Divide file reference into extension key, directory (if any) and base name:
125  list($extensionKey, $file_extPath) = explode('/', substr($fileRef, strlen($validatedPrefix)), 2);
126  $temp = GeneralUtility::revExplode('/', $file_extPath, 2);
127  if (count($temp) === 1) {
128  array_unshift($temp, '');
129  }
130  // Add empty first-entry if not there.
131  list($file_extPath, $file_fileName) = $temp;
132  // The filename is prefixed with "[language key]." because it prevents the llxmltranslate tool from detecting it.
133  return ‪Environment::getLabelsPath() . '/' . $language . '/' . $extensionKey . '/' . ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName;
134  }
135  return null;
136  }
137 
144  abstract protected function ‪doParsingFromRoot(\SimpleXMLElement $root);
145 }
‪TYPO3\CMS\Core\Utility\PathUtility
Definition: PathUtility.php:23
‪TYPO3\CMS\Core\Localization\Parser\LocalizationParserInterface
Definition: LocalizationParserInterface.php:21
‪TYPO3\CMS\Core\Core\Environment\getLabelsPath
‪static string getLabelsPath()
Definition: Environment.php:209
‪TYPO3\CMS\Core\Localization\Exception\FileNotFoundException
Definition: FileNotFoundException.php:21
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$sourcePath
‪string $sourcePath
Definition: AbstractXmlParser.php:31
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getLocalizedFileName
‪string null getLocalizedFileName($fileRef, $language, $sameLocation=false)
Definition: AbstractXmlParser.php:96
‪TYPO3\CMS\Core\Utility\PathUtility\basename
‪static string basename($path)
Definition: PathUtility.php:164
‪TYPO3\CMS\Core\Core\Environment\getFrameworkBasePath
‪static string getFrameworkBasePath()
Definition: Environment.php:234
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser
Definition: AbstractXmlParser.php:28
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\doParsingFromRoot
‪array doParsingFromRoot(\SimpleXMLElement $root)
‪TYPO3\CMS\Core\Core\Environment\getBackendPath
‪static string getBackendPath()
Definition: Environment.php:223
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\$languageKey
‪string $languageKey
Definition: AbstractXmlParser.php:35
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:39
‪TYPO3\CMS\Core\Localization\Parser
Definition: AbstractXmlParser.php:2
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\parseXmlFile
‪array parseXmlFile()
Definition: AbstractXmlParser.php:70
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:45
‪TYPO3\CMS\Core\Localization\Exception\InvalidXmlFileException
Definition: InvalidXmlFileException.php:21
‪TYPO3\CMS\Core\Localization\Parser\AbstractXmlParser\getParsedData
‪array getParsedData($sourcePath, $languageKey)
Definition: AbstractXmlParser.php:45
‪TYPO3\CMS\Core\Core\Environment\getExtensionsPath
‪static string getExtensionsPath()
Definition: Environment.php:245