Skip to content

Commit

Permalink
[BUGFIX] Working with glossary without a request not possible
Browse files Browse the repository at this point in the history
The DeeplBackendUtility tried to detect the current working page by
guessing the page id via the global request. In some cases no request is
given, like on CLI calls. Avoiding errors not using the glossary when
not calling translation via backend, a new service
`ProcessingInstruction` is added, saving the current instructions inside
the global runtime cache. This information can now get detected in every
point of the translation and allows a correct detection of the current
working page including a valid glossary, if one exists.

A regression test is added demonstrating the glossary is now detected
and respected, even if no request is set up.
  • Loading branch information
calien666 committed Nov 29, 2024
1 parent 0b76067 commit cc62452
Show file tree
Hide file tree
Showing 15 changed files with 403 additions and 86 deletions.
95 changes: 75 additions & 20 deletions Build/phpstan/Core11/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ parameters:
count: 1
path: ../../../Classes/Domain/Repository/GlossaryEntryRepository.php

-
message: "#^Cannot call method getLanguageCode\\(\\) on string\\.$#"
count: 2
path: ../../../Classes/Domain/Repository/GlossaryRepository.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryRepository\\:\\:getGlossariesInRootByCurrentPage\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
Expand Down Expand Up @@ -40,11 +45,6 @@ parameters:
count: 1
path: ../../../Classes/Domain/Repository/GlossaryRepository.php

-
message: "#^Parameter \\#3 \\$pageUid of method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryRepository\\:\\:getGlossary\\(\\) expects int, array\\|float\\|int\\|string\\|false\\|null given\\.$#"
count: 1
path: ../../../Classes/Domain/Repository/GlossaryRepository.php

-
message: "#^Call to method getButtonBar\\(\\) on an unknown class TYPO3\\\\CMS\\\\Backend\\\\Template\\\\Components\\\\ModifyButtonBarEvent\\.$#"
count: 1
Expand Down Expand Up @@ -200,21 +200,6 @@ parameters:
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Parameter \\#1 \\$id of static method WebVision\\\\WvDeepltranslate\\\\Utility\\\\DeeplBackendUtility\\:\\:getPageRecord\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Parameter \\#2 \\$id of static method WebVision\\\\WvDeepltranslate\\\\Utility\\\\DeeplBackendUtility\\:\\:getPageIdFromRecord\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Utility\\\\HtmlUtility\\:\\:stripSpecificTags\\(\\) should return string but returns string\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -320,6 +305,76 @@ parameters:
count: 1
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor invoked with 3 parameters, 1\\-2 required\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildErrorHandlingConfiguration\\(\\) has parameter \\$codes with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildErrorHandlingConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildLanguageConfiguration\\(\\) has parameter \\$fallbackIdentifiers with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildSiteConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:failIfArrayIsNotEmpty\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:mergeSiteConfiguration\\(\\) has parameter \\$overrides with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$errorHandling with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$site with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Parameter \\#2 \\$coreCache of class TYPO3\\\\CMS\\\\Core\\\\Configuration\\\\SiteConfiguration constructor expects TYPO3\\\\CMS\\\\Core\\\\Cache\\\\Frontend\\\\PhpFrontend\\|null, Psr\\\\EventDispatcher\\\\EventDispatcherInterface given\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Cannot access property \\$code on DeepL\\\\Language\\|null\\.$#"
count: 7
Expand Down
80 changes: 60 additions & 20 deletions Build/phpstan/Core12/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,6 @@ parameters:
count: 1
path: ../../../Classes/Domain/Repository/GlossaryRepository.php

-
message: "#^Parameter \\#3 \\$pageUid of method WebVision\\\\WvDeepltranslate\\\\Domain\\\\Repository\\\\GlossaryRepository\\:\\:getGlossary\\(\\) expects int, array\\|float\\|int\\|string\\|false\\|null given\\.$#"
count: 1
path: ../../../Classes/Domain/Repository/GlossaryRepository.php

-
message: "#^PHPDoc tag @throws with type Doctrine\\\\DBAL\\\\DBALException\\|Doctrine\\\\DBAL\\\\Driver\\\\Exception is not subtype of Throwable$#"
count: 1
Expand Down Expand Up @@ -200,21 +195,6 @@ parameters:
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Offset 'pid' does not exist on array\\|null\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Parameter \\#1 \\$id of static method WebVision\\\\WvDeepltranslate\\\\Utility\\\\DeeplBackendUtility\\:\\:getPageRecord\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Parameter \\#2 \\$id of static method WebVision\\\\WvDeepltranslate\\\\Utility\\\\DeeplBackendUtility\\:\\:getPageIdFromRecord\\(\\) expects int, int\\|string given\\.$#"
count: 1
path: ../../../Classes/Utility/DeeplBackendUtility.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Utility\\\\HtmlUtility\\:\\:stripSpecificTags\\(\\) should return string but returns string\\|null\\.$#"
count: 1
Expand Down Expand Up @@ -305,6 +285,66 @@ parameters:
count: 1
path: ../../../Tests/Functional/Hooks/TranslateHookTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildDefaultLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildErrorHandlingConfiguration\\(\\) has parameter \\$codes with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildErrorHandlingConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildLanguageConfiguration\\(\\) has parameter \\$fallbackIdentifiers with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildLanguageConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:buildSiteConfiguration\\(\\) return type has no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:failIfArrayIsNotEmpty\\(\\) has parameter \\$items with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:mergeSiteConfiguration\\(\\) has parameter \\$overrides with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$errorHandling with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$languages with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:writeSiteConfiguration\\(\\) has parameter \\$site with no value type specified in iterable type array\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Parameter \\#1 \\$current of method WebVision\\\\WvDeepltranslate\\\\Tests\\\\Functional\\\\Regression\\\\GlossaryRegressionTest\\:\\:mergeInstruction\\(\\) expects TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction, TYPO3\\\\TestingFramework\\\\Core\\\\Functional\\\\Framework\\\\Frontend\\\\Internal\\\\AbstractInstruction\\|null given\\.$#"
count: 1
path: ../../../Tests/Functional/Regression/GlossaryRegressionTest.php

-
message: "#^Cannot access property \\$code on DeepL\\\\Language\\|null\\.$#"
count: 7
Expand Down
19 changes: 14 additions & 5 deletions Classes/Domain/Repository/GlossaryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Domain\Repository\PageRepository;
use TYPO3\CMS\Core\Exception\SiteNotFoundException;
use TYPO3\CMS\Core\Information\Typo3Version;
use TYPO3\CMS\Core\Site\Entity\Site;
use TYPO3\CMS\Core\Site\SiteFinder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
Expand Down Expand Up @@ -50,7 +51,11 @@ public function getGlossaryInformationForSync(int $pageId): array
$localizationLanguageIds = $this->getAvailableLocalizations($pageId);
$site = GeneralUtility::makeInstance(SiteFinder::class)
->getSiteByPageId($pageId);
$sourceLangIsoCode = $site->getDefaultLanguage()->getTwoLetterIsoCode();
if ((new Typo3Version())->getMajorVersion() >= 12) {
$sourceLangIsoCode = $site->getDefaultLanguage()->getLocale()->getLanguageCode();
} else {
$sourceLangIsoCode = $site->getDefaultLanguage()->getTwoLetterIsoCode();
}

$localizationArray[$sourceLangIsoCode] = $entries;

Expand Down Expand Up @@ -225,7 +230,7 @@ public function getGlossaryBySourceAndTarget(
return $this->getGlossary(
$lowerSourceLang,
$lowerTargetLang,
$page['uid'],
(int)$page['uid'],
true
) ?? $defaultGlossary;
}
Expand Down Expand Up @@ -254,7 +259,7 @@ public function getGlossaryBySourceAndTargetForSync(
$lowerTargetLang = substr($lowerTargetLang, 0, 2);
}

$result = $this->getGlossary($lowerSourceLang, $lowerTargetLang, $page['uid']);
$result = $this->getGlossary($lowerSourceLang, $lowerTargetLang, (int)$page['uid']);

if ($result === null) {
$insert = [
Expand Down Expand Up @@ -403,15 +408,19 @@ private function getAvailableLocalizations(int $pageId): array
}
$availableTranslations = [];
foreach ($translations['translations'] as $translation) {
$availableTranslations[] = $translation['sys_language_uid'];
$availableTranslations[] = (int)$translation['sys_language_uid'];
}

return $availableTranslations;
}

protected function getTargetLanguageIsoCode(Site $site, int $languageId): string
{
return $site->getLanguageById($languageId)->getTwoLetterIsoCode();
if ((new Typo3Version())->getMajorVersion() >= 12) {
return $site->getLanguageById($languageId)->getLocale()->getLanguageCode();
} else {
return $site->getLanguageById($languageId)->getTwoLetterIsoCode();
}
}

/**
Expand Down
21 changes: 6 additions & 15 deletions Classes/Hooks/AbstractTranslateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,33 +17,27 @@
use WebVision\WvDeepltranslate\Exception\LanguageRecordNotFoundException;
use WebVision\WvDeepltranslate\Service\DeeplService;
use WebVision\WvDeepltranslate\Service\LanguageService;
use WebVision\WvDeepltranslate\Service\ProcessingInstruction;

abstract class AbstractTranslateHook
{
/**
* @var array{tableName: string|null, id: string|int|null, mode: string|false}
*/
protected static array $coreProcessorsInformation = [
'tableName' => null,
'id' => null,
// @todo rename identifier to "deepl"
'mode' => false,
];

protected DeeplService $deeplService;

protected PageRepository $pageRepository;

protected LanguageService $languageService;
protected ProcessingInstruction $processingInstruction;

public function __construct(
PageRepository $pageRepository,
DeeplService $deeplService,
LanguageService $languageService
LanguageService $languageService,
ProcessingInstruction $processingInstruction
) {
$this->deeplService = $deeplService;
$this->pageRepository = $pageRepository;
$this->languageService = $languageService;
$this->processingInstruction = $processingInstruction;
}

/**
Expand Down Expand Up @@ -133,9 +127,6 @@ public function processCmdmap(
if ($commandIsProcessed !== false) {
return;
}

self::$coreProcessorsInformation['tableName'] = $table;
self::$coreProcessorsInformation['id'] = $id;
self::$coreProcessorsInformation['mode'] = $dataHandler->cmdmap['localization']['custom']['mode'] ?? false;
$this->processingInstruction->setProcessingInstruction($table, $id, $dataHandler->cmdmap['localization']['custom']['mode'] ?? false);
}
}
6 changes: 3 additions & 3 deletions Classes/Hooks/TranslateHook.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public function processTranslateTo_copyAction(
DataHandler $dataHandler
): void {
// Table Information are importen to find deepl configuration for site
$tableName = self::$coreProcessorsInformation['tableName'];
$tableName = $this->processingInstruction->getProcessingTable();
if ($tableName === null) {
return;
}

// Record Information are importen to find deepl configuration for site
$currentRecordId = self::$coreProcessorsInformation['id'];
$currentRecordId = $this->processingInstruction->getProcessingId();
if ($currentRecordId === null) {
return;
}
Expand All @@ -39,7 +39,7 @@ public function processTranslateTo_copyAction(
}

// Translation mode not set to DeepL translate skip the translation
if (self::$coreProcessorsInformation['mode'] !== 'deepl') {
if ($this->processingInstruction->isDeeplMode() === false) {
return;
}

Expand Down
Loading

0 comments on commit cc62452

Please sign in to comment.