From 1fd0c2979e1edfb667cbb0e7abbc3c342f7d11f6 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Tue, 9 Jan 2024 10:36:52 +0100 Subject: [PATCH 1/6] enh(OCP\Translation): Add ITranslationProviderWithId Signed-off-by: Marcel Klehr --- .../Settings/Admin/ArtificialIntelligence.php | 3 +- .../Translation/TranslationManager.php | 9 ++++- .../ITranslationProviderWithId.php | 37 +++++++++++++++++++ 3 files changed, 46 insertions(+), 3 deletions(-) create mode 100644 lib/public/Translation/ITranslationProviderWithId.php diff --git a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php index e2862139e494b..ab0dc1446bbf7 100644 --- a/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php +++ b/apps/settings/lib/Settings/Admin/ArtificialIntelligence.php @@ -37,6 +37,7 @@ use OCP\TextProcessing\IProviderWithId; use OCP\TextProcessing\ITaskType; use OCP\Translation\ITranslationManager; +use OCP\Translation\ITranslationProviderWithId; use Psr\Container\ContainerExceptionInterface; use Psr\Container\ContainerInterface; use Psr\Container\NotFoundExceptionInterface; @@ -62,7 +63,7 @@ public function getForm() { $translationPreferences = []; foreach ($this->translationManager->getProviders() as $provider) { $translationProviders[] = [ - 'class' => $provider::class, + 'class' => $provider instanceof ITranslationProviderWithId ? $provider->getId() : $provider::class, 'name' => $provider->getName(), ]; $translationPreferences[] = $provider::class; diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php index 48a0e2cdebdda..546ed595aaf6c 100644 --- a/lib/private/Translation/TranslationManager.php +++ b/lib/private/Translation/TranslationManager.php @@ -35,6 +35,7 @@ use OCP\Translation\IDetectLanguageProvider; use OCP\Translation\ITranslationManager; use OCP\Translation\ITranslationProvider; +use OCP\Translation\ITranslationProviderWithId; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; @@ -73,13 +74,17 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua $precedence = json_decode($json, true); $newProviders = []; foreach ($precedence as $className) { - $provider = current(array_filter($providers, fn ($provider) => $provider::class === $className)); + $provider = current(array_filter($providers, fn ($provider) => + $provider instanceof ITranslationProviderWithId ? $provider->getId() === $className : $provider::class === $className)) + ; if ($provider !== false) { $newProviders[] = $provider; } } // Add all providers that haven't been added so far - $newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => $a::class > $b::class ? 1 : ($a::class < $b::class ? -1 : 0)); + $newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => + ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class) + ); $providers = $newProviders; } diff --git a/lib/public/Translation/ITranslationProviderWithId.php b/lib/public/Translation/ITranslationProviderWithId.php new file mode 100644 index 0000000000000..845e997ebc74d --- /dev/null +++ b/lib/public/Translation/ITranslationProviderWithId.php @@ -0,0 +1,37 @@ + + * + * @author Marcel Klehr + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + +namespace OCP\Translation; + +/** + * @since 29.0.0 + */ +interface ITranslationProviderWithId { + /** + * @since 29.0.0 + */ + public function getId(): string; +} From c148f84ff7fac06698891ddd41f2e20d4d11f1ca Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 10 Jan 2024 11:15:22 +0100 Subject: [PATCH 2/6] Rebuild autoloaders Signed-off-by: Marcel Klehr --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index 201b49f3fae37..a239854882612 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -711,6 +711,7 @@ 'OCP\\Translation\\IDetectLanguageProvider' => $baseDir . '/lib/public/Translation/IDetectLanguageProvider.php', 'OCP\\Translation\\ITranslationManager' => $baseDir . '/lib/public/Translation/ITranslationManager.php', 'OCP\\Translation\\ITranslationProvider' => $baseDir . '/lib/public/Translation/ITranslationProvider.php', + 'OCP\\Translation\\ITranslationProviderWithId' => $baseDir . '/lib/public/Translation/ITranslationProviderWithId.php', 'OCP\\Translation\\LanguageTuple' => $baseDir . '/lib/public/Translation/LanguageTuple.php', 'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php', 'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index ebcccb328c24a..045e612b0879d 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -744,6 +744,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Translation\\IDetectLanguageProvider' => __DIR__ . '/../../..' . '/lib/public/Translation/IDetectLanguageProvider.php', 'OCP\\Translation\\ITranslationManager' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationManager.php', 'OCP\\Translation\\ITranslationProvider' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProvider.php', + 'OCP\\Translation\\ITranslationProviderWithId' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProviderWithId.php', 'OCP\\Translation\\LanguageTuple' => __DIR__ . '/../../..' . '/lib/public/Translation/LanguageTuple.php', 'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php', 'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php', From 4bfa3d34fcf7a6386d6533680790e23623a5473b Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 10 Jan 2024 14:08:03 +0100 Subject: [PATCH 3/6] enh(OCP\Translation): Make ITranslationProviderWithId extend ITranslationProvider Signed-off-by: Marcel Klehr --- lib/public/Translation/ITranslationProviderWithId.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/public/Translation/ITranslationProviderWithId.php b/lib/public/Translation/ITranslationProviderWithId.php index 845e997ebc74d..fa08ef7cb17d9 100644 --- a/lib/public/Translation/ITranslationProviderWithId.php +++ b/lib/public/Translation/ITranslationProviderWithId.php @@ -29,7 +29,7 @@ /** * @since 29.0.0 */ -interface ITranslationProviderWithId { +interface ITranslationProviderWithId extends ITranslationProvider { /** * @since 29.0.0 */ From 1db8888f999e97427959f64ae846a05ccbb3d6c4 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 10 Jan 2024 15:17:02 +0100 Subject: [PATCH 4/6] enh(OCP\Translation): Introduce ITranslationProviderWithUserId Signed-off-by: Marcel Klehr --- .../Translation/TranslationManager.php | 9 +++++ .../ITranslationProviderWithUserId.php | 38 +++++++++++++++++++ 2 files changed, 47 insertions(+) create mode 100644 lib/public/Translation/ITranslationProviderWithUserId.php diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php index 546ed595aaf6c..4b5facebac6f6 100644 --- a/lib/private/Translation/TranslationManager.php +++ b/lib/private/Translation/TranslationManager.php @@ -30,12 +30,14 @@ use OC\AppFramework\Bootstrap\Coordinator; use OCP\IConfig; use OCP\IServerContainer; +use OCP\IUserSession; use OCP\PreConditionNotMetException; use OCP\Translation\CouldNotTranslateException; use OCP\Translation\IDetectLanguageProvider; use OCP\Translation\ITranslationManager; use OCP\Translation\ITranslationProvider; use OCP\Translation\ITranslationProviderWithId; +use OCP\Translation\ITranslationProviderWithUserId; use Psr\Container\ContainerExceptionInterface; use Psr\Container\NotFoundExceptionInterface; use Psr\Log\LoggerInterface; @@ -51,6 +53,7 @@ public function __construct( private Coordinator $coordinator, private LoggerInterface $logger, private IConfig $config, + private IUserSession $userSession, ) { } @@ -91,6 +94,9 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua if ($fromLanguage === null) { foreach ($providers as $provider) { if ($provider instanceof IDetectLanguageProvider) { + if ($provider instanceof ITranslationProviderWithUserId) { + $provider->setUserId($this->userSession->getUser()?->getUID()); + } $fromLanguage = $provider->detectLanguage($text); } @@ -110,6 +116,9 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua foreach ($providers as $provider) { try { + if ($provider instanceof ITranslationProviderWithUserId) { + $provider->setUserId($this->userSession->getUser()?->getUID()); + } return $provider->translate($fromLanguage, $toLanguage, $text); } catch (RuntimeException $e) { $this->logger->warning("Failed to translate from {$fromLanguage} to {$toLanguage} using provider {$provider->getName()}", ['exception' => $e]); diff --git a/lib/public/Translation/ITranslationProviderWithUserId.php b/lib/public/Translation/ITranslationProviderWithUserId.php new file mode 100644 index 0000000000000..9a573a8150eb3 --- /dev/null +++ b/lib/public/Translation/ITranslationProviderWithUserId.php @@ -0,0 +1,38 @@ + + * + * @author Marcel Klehr + * + * @license GNU AGPL version 3 or any later version + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + + +namespace OCP\Translation; + +/** + * @since 29.0.0 + */ +interface ITranslationProviderWithUserId extends ITranslationProvider { + /** + * @param string|null $userId The userId of the user requesting the current task + * @since 29.0.0 + */ + public function setUserId(?string $userId); +} From 970eb692d19a15e4f2874d932091119245f32aef Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Wed, 10 Jan 2024 16:15:06 +0100 Subject: [PATCH 5/6] fix(coding style) Signed-off-by: Marcel Klehr --- lib/private/Translation/TranslationManager.php | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/private/Translation/TranslationManager.php b/lib/private/Translation/TranslationManager.php index 4b5facebac6f6..306275121ea8c 100644 --- a/lib/private/Translation/TranslationManager.php +++ b/lib/private/Translation/TranslationManager.php @@ -77,17 +77,17 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua $precedence = json_decode($json, true); $newProviders = []; foreach ($precedence as $className) { - $provider = current(array_filter($providers, fn ($provider) => - $provider instanceof ITranslationProviderWithId ? $provider->getId() === $className : $provider::class === $className)) - ; + $provider = current(array_filter($providers, function ($provider) use ($className) { + return $provider instanceof ITranslationProviderWithId ? $provider->getId() === $className : $provider::class === $className; + })); if ($provider !== false) { $newProviders[] = $provider; } } // Add all providers that haven't been added so far - $newProviders += array_udiff($providers, $newProviders, fn ($a, $b) => - ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class) - ); + $newProviders += array_udiff($providers, $newProviders, function ($a, $b) { + return ($a instanceof ITranslationProviderWithId ? $a->getId() : $a::class) <=> ($b instanceof ITranslationProviderWithId ? $b->getId() : $b::class); + }); $providers = $newProviders; } @@ -116,7 +116,7 @@ public function translate(string $text, ?string &$fromLanguage, string $toLangua foreach ($providers as $provider) { try { - if ($provider instanceof ITranslationProviderWithUserId) { + if ($provider instanceof ITranslationProviderWithUserId) { $provider->setUserId($this->userSession->getUser()?->getUID()); } return $provider->translate($fromLanguage, $toLanguage, $text); From a6513336b47c5ae7ca49f45ce292534c088607d9 Mon Sep 17 00:00:00 2001 From: Marcel Klehr Date: Thu, 11 Jan 2024 09:32:30 +0100 Subject: [PATCH 6/6] fix(autoloaders) Signed-off-by: Marcel Klehr --- lib/composer/composer/autoload_classmap.php | 1 + lib/composer/composer/autoload_static.php | 1 + 2 files changed, 2 insertions(+) diff --git a/lib/composer/composer/autoload_classmap.php b/lib/composer/composer/autoload_classmap.php index a239854882612..63ba07e2e4fc5 100644 --- a/lib/composer/composer/autoload_classmap.php +++ b/lib/composer/composer/autoload_classmap.php @@ -712,6 +712,7 @@ 'OCP\\Translation\\ITranslationManager' => $baseDir . '/lib/public/Translation/ITranslationManager.php', 'OCP\\Translation\\ITranslationProvider' => $baseDir . '/lib/public/Translation/ITranslationProvider.php', 'OCP\\Translation\\ITranslationProviderWithId' => $baseDir . '/lib/public/Translation/ITranslationProviderWithId.php', + 'OCP\\Translation\\ITranslationProviderWithUserId' => $baseDir . '/lib/public/Translation/ITranslationProviderWithUserId.php', 'OCP\\Translation\\LanguageTuple' => $baseDir . '/lib/public/Translation/LanguageTuple.php', 'OCP\\UserInterface' => $baseDir . '/lib/public/UserInterface.php', 'OCP\\UserMigration\\IExportDestination' => $baseDir . '/lib/public/UserMigration/IExportDestination.php', diff --git a/lib/composer/composer/autoload_static.php b/lib/composer/composer/autoload_static.php index 045e612b0879d..bf1a13d49dec0 100644 --- a/lib/composer/composer/autoload_static.php +++ b/lib/composer/composer/autoload_static.php @@ -745,6 +745,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2 'OCP\\Translation\\ITranslationManager' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationManager.php', 'OCP\\Translation\\ITranslationProvider' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProvider.php', 'OCP\\Translation\\ITranslationProviderWithId' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProviderWithId.php', + 'OCP\\Translation\\ITranslationProviderWithUserId' => __DIR__ . '/../../..' . '/lib/public/Translation/ITranslationProviderWithUserId.php', 'OCP\\Translation\\LanguageTuple' => __DIR__ . '/../../..' . '/lib/public/Translation/LanguageTuple.php', 'OCP\\UserInterface' => __DIR__ . '/../../..' . '/lib/public/UserInterface.php', 'OCP\\UserMigration\\IExportDestination' => __DIR__ . '/../../..' . '/lib/public/UserMigration/IExportDestination.php',