From be807f5585e8d77ac1d40ab2c76ed3b531197d97 Mon Sep 17 00:00:00 2001 From: Benni Mack <benni@typo3.org> Date: Fri, 24 Nov 2017 23:24:48 +0100 Subject: [PATCH] [TASK] Move llXmlAutoFileName() into AbstractXmlParser The global function in GeneralUtility is moved into the AbstractXmlParser. Resolves: #83083 Releases: master Change-Id: If4caa3c6253f994e572e5fe36bcb791e1aec2599 Reviewed-on: https://review.typo3.org/54752 Reviewed-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: Christian Kuhn <lolli@schwarzbu.ch> Tested-by: TYPO3com <no-reply@typo3.com> Reviewed-by: Susanne Moog <susanne.moog@typo3.org> Tested-by: Susanne Moog <susanne.moog@typo3.org> --- .../Localization/Parser/AbstractXmlParser.php | 53 ++++++++++++++++++- .../Parser/LocallangXmlParser.php | 2 +- .../core/Classes/Utility/GeneralUtility.php | 2 + ...-83083-GeneralUtilityllXmlAutoFileName.rst | 33 ++++++++++++ .../Php/MethodCallStaticMatcher.php | 7 +++ 5 files changed, 94 insertions(+), 3 deletions(-) create mode 100644 typo3/sysext/core/Documentation/Changelog/master/Deprecation-83083-GeneralUtilityllXmlAutoFileName.rst diff --git a/typo3/sysext/core/Classes/Localization/Parser/AbstractXmlParser.php b/typo3/sysext/core/Classes/Localization/Parser/AbstractXmlParser.php index 88cdd453fc87..406f77636579 100644 --- a/typo3/sysext/core/Classes/Localization/Parser/AbstractXmlParser.php +++ b/typo3/sysext/core/Classes/Localization/Parser/AbstractXmlParser.php @@ -46,10 +46,10 @@ abstract class AbstractXmlParser implements LocalizationParserInterface $this->sourcePath = $sourcePath; $this->languageKey = $languageKey; if ($this->languageKey !== 'default') { - $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey)); + $this->sourcePath = $this->getLocalizedFileName($this->sourcePath, $this->languageKey); if (!@is_file($this->sourcePath)) { // Global localization is not available, try split localization file - $this->sourcePath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($sourcePath, $languageKey, true)); + $this->sourcePath = $this->getLocalizedFileName($sourcePath, $languageKey, true); } if (!@is_file($this->sourcePath)) { throw new FileNotFoundException('Localization file does not exist', 1306332397); @@ -83,6 +83,55 @@ abstract class AbstractXmlParser implements LocalizationParserInterface return $this->doParsingFromRoot($rootXmlNode); } + /** + * Checks if a localized file is found in typo3conf/l10n/ (e.g. a language pack was downloaded in the backend) + * or if $sameLocation is set, then checks for a file located in "{language}.locallang.xlf" at the same directory + * + * @param string $fileRef Absolute file reference to locallang file + * @param string $language Language key + * @param bool $sameLocation If TRUE, then locallang localization file name will be returned with same directory as $fileRef + * @return string|null Absolute path to the language file, or null if error occurred + */ + protected function getLocalizedFileName($fileRef, $language, $sameLocation = false) + { + // If $fileRef is already prefixed with "[language key]" then we should return it as is + $fileName = basename($fileRef); + if (GeneralUtility::isFirstPartOfStr($fileName, $language . '.')) { + return GeneralUtility::getFileAbsFileName($fileRef); + } + + if ($sameLocation) { + return GeneralUtility::getFileAbsFileName(str_replace($fileName, $language . '.' . $fileName, $fileRef)); + } + + // Analyse file reference: + // Is system: + if (GeneralUtility::isFirstPartOfStr($fileRef, PATH_typo3 . 'sysext/')) { + $validatedPrefix = PATH_typo3 . 'sysext/'; + } elseif (GeneralUtility::isFirstPartOfStr($fileRef, PATH_typo3 . 'ext/')) { + // Is global: + $validatedPrefix = PATH_typo3 . 'ext/'; + } elseif (GeneralUtility::isFirstPartOfStr($fileRef, PATH_typo3conf . 'ext/')) { + // Is local: + $validatedPrefix = PATH_typo3conf . 'ext/'; + } else { + $validatedPrefix = ''; + } + if ($validatedPrefix) { + // Divide file reference into extension key, directory (if any) and base name: + list($extensionKey, $file_extPath) = explode('/', substr($fileRef, strlen($validatedPrefix)), 2); + $temp = GeneralUtility::revExplode('/', $file_extPath, 2); + if (count($temp) === 1) { + array_unshift($temp, ''); + } + // Add empty first-entry if not there. + list($file_extPath, $file_fileName) = $temp; + // The filename is prefixed with "[language key]." because it prevents the llxmltranslate tool from detecting it. + return PATH_site . 'typo3conf/l10n/' . $language . '/' . $extensionKey . '/' . ($file_extPath ? $file_extPath . '/' : '') . $language . '.' . $file_fileName; + } + return null; + } + /** * Returns array representation of XML data, starting from a root node. * diff --git a/typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php b/typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php index 1d8dd52884f1..fbf1ebfe92c8 100644 --- a/typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php +++ b/typo3/sysext/core/Classes/Localization/Parser/LocallangXmlParser.php @@ -45,7 +45,7 @@ class LocallangXmlParser extends AbstractXmlParser // Parse source $parsedSource = $this->parseXmlFile(); // Parse target - $localizedTargetPath = GeneralUtility::getFileAbsFileName(GeneralUtility::llXmlAutoFileName($this->sourcePath, $this->languageKey)); + $localizedTargetPath = $this->getLocalizedFileName($this->sourcePath, $this->languageKey); $targetPath = $this->languageKey !== 'default' && @is_file($localizedTargetPath) ? $localizedTargetPath : $this->sourcePath; try { $parsedTarget = $this->getParsedTargetData($targetPath); diff --git a/typo3/sysext/core/Classes/Utility/GeneralUtility.php b/typo3/sysext/core/Classes/Utility/GeneralUtility.php index ca61e9400681..d6ee404eda8f 100644 --- a/typo3/sysext/core/Classes/Utility/GeneralUtility.php +++ b/typo3/sysext/core/Classes/Utility/GeneralUtility.php @@ -3336,9 +3336,11 @@ class GeneralUtility * @param string $language Language key * @param bool $sameLocation If TRUE, then locallang localization file name will be returned with same directory as $fileRef * @return string Returns the filename reference for the language unless error occurred in which case it will be NULL + * @deprecated in TYPO3 v9.0, will be removed in TYPO3 v10.0, as the functionality has been moved into AbstractXmlParser. */ public static function llXmlAutoFileName($fileRef, $language, $sameLocation = false) { + trigger_error('This method will be removed in TYPO3 v10.0, the functionality has been moved into AbstractXmlParser', E_USER_DEPRECATED); // If $fileRef is already prefixed with "[language key]" then we should return it as is $fileName = basename($fileRef); if (self::isFirstPartOfStr($fileName, $language . '.')) { diff --git a/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83083-GeneralUtilityllXmlAutoFileName.rst b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83083-GeneralUtilityllXmlAutoFileName.rst new file mode 100644 index 000000000000..7b95a78f8636 --- /dev/null +++ b/typo3/sysext/core/Documentation/Changelog/master/Deprecation-83083-GeneralUtilityllXmlAutoFileName.rst @@ -0,0 +1,33 @@ +.. include:: ../../Includes.txt + +========================================================= +Deprecation: #83083 - GeneralUtility::llXmlAutoFileName() +========================================================= + +See :issue:`83083` + +Description +=========== + +The method ``GeneralUtility::llXmlAutoFileName()``, which detects a XLF/XML translation file, has been moved into +AbstractXmlParser, as the functionality is solely used in there, and the code belongs in this area. + + +Impact +====== + +Calling the method will trigger a deprecation warning. + + +Affected Installations +====================== + +Any TYPO3 instance with an extension using the method directly. + + +Migration +========= + +If necessary, use the XmlParser functionality, or implement the code directly in your own extension. + +.. index:: PHP-API, FullyScanned \ No newline at end of file diff --git a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php index 3edd1528ac48..5324b3bd0fcd 100644 --- a/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php +++ b/typo3/sysext/install/Configuration/ExtensionScanner/Php/MethodCallStaticMatcher.php @@ -505,4 +505,11 @@ return [ 'Deprecation-82445-PageTranslationRelatedFunctionality.rst', ], ], + 'TYPO3\CMS\Core\Utility\GeneralUtility::llXmlAutoFileName' => [ + 'numberOfMandatoryArguments' => 2, + 'maximumNumberOfArguments' => 3, + 'restFiles' => [ + 'Deprecation-83083-GeneralUtilityllXmlAutoFileName.rst', + ], + ], ]; -- GitLab