diff --git a/apps/updatenotification/lib/Notification/Notifier.php b/apps/updatenotification/lib/Notification/Notifier.php index 8cee44144b295..8ce6bfc2d3d5a 100644 --- a/apps/updatenotification/lib/Notification/Notifier.php +++ b/apps/updatenotification/lib/Notification/Notifier.php @@ -20,8 +20,7 @@ use OCP\Notification\INotification; use OCP\Notification\INotifier; use OCP\Notification\UnknownNotificationException; -use OCP\Server; -use OCP\Util; +use OCP\ServerVersion; class Notifier implements INotifier { /** @var string[] */ @@ -44,8 +43,10 @@ public function __construct( protected IFactory $l10NFactory, protected IUserSession $userSession, protected IGroupManager $groupManager, + protected IAppManager $appManager, + protected ServerVersion $serverVersion, ) { - $this->appVersions = $this->getAppVersions(); + $this->appVersions = $this->appManager->getAppInstalledVersions(); } /** @@ -144,15 +145,12 @@ public function prepare(INotification $notification, string $languageCode): INot * @param string $installedVersion * @throws AlreadyProcessedException When the update is already installed */ - protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) { + protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion): void { if (version_compare($notification->getObjectId(), $installedVersion, '<=')) { throw new AlreadyProcessedException(); } } - /** - * @return bool - */ protected function isAdmin(): bool { $user = $this->userSession->getUser(); @@ -164,14 +162,10 @@ protected function isAdmin(): bool { } protected function getCoreVersions(): string { - return implode('.', Util::getVersion()); - } - - protected function getAppVersions(): array { - return \OC_App::getAppVersions(); + return implode('.', $this->serverVersion->getVersion()); } - protected function getAppInfo($appId, $languageCode) { - return Server::get(IAppManager::class)->getAppInfo($appId, false, $languageCode); + protected function getAppInfo(string $appId, ?string $languageCode): ?array { + return $this->appManager->getAppInfo($appId, false, $languageCode); } } diff --git a/apps/updatenotification/tests/Notification/NotifierTest.php b/apps/updatenotification/tests/Notification/NotifierTest.php index 1e53b8d4aea24..45c4fc9960fe3 100644 --- a/apps/updatenotification/tests/Notification/NotifierTest.php +++ b/apps/updatenotification/tests/Notification/NotifierTest.php @@ -9,6 +9,7 @@ namespace OCA\UpdateNotification\Tests\Notification; use OCA\UpdateNotification\Notification\Notifier; +use OCP\App\IAppManager; use OCP\IConfig; use OCP\IGroupManager; use OCP\IURLGenerator; @@ -17,22 +18,20 @@ use OCP\Notification\AlreadyProcessedException; use OCP\Notification\IManager; use OCP\Notification\INotification; +use OCP\ServerVersion; +use PHPUnit\Framework\MockObject\MockObject; use Test\TestCase; class NotifierTest extends TestCase { - /** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */ - protected $urlGenerator; - /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */ - protected $config; - /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $notificationManager; - /** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */ - protected $l10nFactory; - /** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */ - protected $userSession; - /** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */ - protected $groupManager; + protected IURLGenerator&MockObject $urlGenerator; + protected IConfig&MockObject $config; + protected IManager&MockObject $notificationManager; + protected IFactory&MockObject $l10nFactory; + protected IUserSession&MockObject $userSession; + protected IGroupManager&MockObject $groupManager; + protected IAppManager&MockObject $appManager; + protected ServerVersion&MockObject $serverVersion; protected function setUp(): void { parent::setUp(); @@ -43,6 +42,8 @@ protected function setUp(): void { $this->l10nFactory = $this->createMock(IFactory::class); $this->userSession = $this->createMock(IUserSession::class); $this->groupManager = $this->createMock(IGroupManager::class); + $this->appManager = $this->createMock(IAppManager::class); + $this->serverVersion = $this->createMock(ServerVersion::class); } /** @@ -57,7 +58,9 @@ protected function getNotifier(array $methods = []) { $this->notificationManager, $this->l10nFactory, $this->userSession, - $this->groupManager + $this->groupManager, + $this->appManager, + $this->serverVersion, ); } { @@ -69,6 +72,8 @@ protected function getNotifier(array $methods = []) { $this->l10nFactory, $this->userSession, $this->groupManager, + $this->appManager, + $this->serverVersion, ]) ->onlyMethods($methods) ->getMock(); diff --git a/core/Command/App/ListApps.php b/core/Command/App/ListApps.php index 5244d66e0d8dd..bb59e4411195b 100644 --- a/core/Command/App/ListApps.php +++ b/core/Command/App/ListApps.php @@ -14,7 +14,7 @@ class ListApps extends Base { public function __construct( - protected IAppManager $manager, + protected IAppManager $appManager, ) { parent::__construct(); } @@ -56,16 +56,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int $showEnabledApps = $input->getOption('enabled') || !$input->getOption('disabled'); $showDisabledApps = $input->getOption('disabled') || !$input->getOption('enabled'); - $apps = \OC_App::getAllApps(); + $apps = $this->appManager->getAllAppsInAppsFolders(); $enabledApps = $disabledApps = []; - $versions = \OC_App::getAppVersions(); + $versions = $this->appManager->getAppInstalledVersions(); //sort enabled apps above disabled apps foreach ($apps as $app) { - if ($shippedFilter !== null && $this->manager->isShipped($app) !== $shippedFilter) { + if ($shippedFilter !== null && $this->appManager->isShipped($app) !== $shippedFilter) { continue; } - if ($this->manager->isEnabledForAnyone($app)) { + if ($this->appManager->isEnabledForAnyone($app)) { $enabledApps[] = $app; } else { $disabledApps[] = $app; @@ -88,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int sort($disabledApps); foreach ($disabledApps as $app) { - $apps['disabled'][$app] = $this->manager->getAppVersion($app) . (isset($versions[$app]) ? ' (installed ' . $versions[$app] . ')' : ''); + $apps['disabled'][$app] = $this->appManager->getAppVersion($app) . (isset($versions[$app]) ? ' (installed ' . $versions[$app] . ')' : ''); } } diff --git a/lib/private/App/AppManager.php b/lib/private/App/AppManager.php index 067cc89d67429..740da31770d1f 100644 --- a/lib/private/App/AppManager.php +++ b/lib/private/App/AppManager.php @@ -811,6 +811,15 @@ public function getAppVersion(string $appId, bool $useCache = true): string { return $this->appVersions[$appId]; } + /** + * Returns the installed versions of all apps + * + * @return array + */ + public function getAppInstalledVersions(): array { + return $this->getAppConfig()->getAppInstalledVersions(); + } + /** * Returns a list of apps incompatible with the given version * diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index ad9299f01fdaa..1228b9c20e381 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -62,6 +62,9 @@ class AppConfig implements IAppConfig { /** @var array, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */ private array $configLexiconDetails = []; + /** @var ?array */ + private ?array $appVersionsCache = null; + public function __construct( protected IDBConnection $connection, protected LoggerInterface $logger, @@ -1647,4 +1650,17 @@ private function getConfigDetailsFromLexicon(string $appId): array { return $this->configLexiconDetails[$appId]; } + + /** + * Returns the installed versions of all apps + * + * @return array + */ + public function getAppInstalledVersions(): array { + if ($this->appVersionsCache === null) { + /** @var array */ + $this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING); + } + return $this->appVersionsCache; + } } diff --git a/lib/private/AppFramework/Services/AppConfig.php b/lib/private/AppFramework/Services/AppConfig.php index 423a9eb5814ea..77c5ea4de0cb2 100644 --- a/lib/private/AppFramework/Services/AppConfig.php +++ b/lib/private/AppFramework/Services/AppConfig.php @@ -337,4 +337,13 @@ public function getUserValue(string $userId, string $key, string $default = ''): public function deleteUserValue(string $userId, string $key): void { $this->config->deleteUserValue($userId, $this->appName, $key); } + + /** + * Returns the installed versions of all apps + * + * @return array + */ + public function getAppInstalledVersions(): array { + return $this->appConfig->getAppInstalledVersions(); + } } diff --git a/lib/private/Server.php b/lib/private/Server.php index 523fc7b9914c1..4d79fefd2615f 100644 --- a/lib/private/Server.php +++ b/lib/private/Server.php @@ -607,10 +607,10 @@ public function __construct($webRoot, \OC\Config $config) { if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) { $logQuery = $config->getValue('log_query'); - $prefixClosure = function () use ($logQuery, $serverVersion) { + $prefixClosure = function () use ($logQuery, $serverVersion): ?string { if (!$logQuery) { try { - $v = \OC_App::getAppVersions(); + $v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(); } catch (\Doctrine\DBAL\Exception $e) { // Database service probably unavailable // Probably related to https://github.com/nextcloud/server/issues/37424 @@ -867,15 +867,15 @@ public function __construct($webRoot, \OC\Config $config) { $this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class); $this->registerService(Checker::class, function (ContainerInterface $c) { - // IConfig and IAppManager requires a working database. This code - // might however be called when ownCloud is not yet setup. + // IConfig requires a working database. This code + // might however be called when Nextcloud is not yet setup. if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) { $config = $c->get(\OCP\IConfig::class); $appConfig = $c->get(\OCP\IAppConfig::class); } else { - $config = $appConfig = $appManager = null; + $config = null; + $appConfig = null; } - $appManager = $c->get(IAppManager::class); return new Checker( $c->get(ServerVersion::class), @@ -885,7 +885,7 @@ public function __construct($webRoot, \OC\Config $config) { $config, $appConfig, $c->get(ICacheFactory::class), - $appManager, + $c->get(IAppManager::class), $c->get(IMimeTypeDetector::class) ); }); diff --git a/lib/private/TemplateLayout.php b/lib/private/TemplateLayout.php index 9c1d7ca1d2c06..01d22edc0fb06 100644 --- a/lib/private/TemplateLayout.php +++ b/lib/private/TemplateLayout.php @@ -48,6 +48,7 @@ public function __construct( private InitialStateService $initialState, private INavigationManager $navigationManager, private ITemplateManager $templateManager, + private ServerVersion $serverVersion, ) { } @@ -204,8 +205,8 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate { if ($this->config->getSystemValueBool('installed', false)) { if (empty(self::$versionHash)) { - $v = \OC_App::getAppVersions(); - $v['core'] = implode('.', \OCP\Util::getVersion()); + $v = $this->appManager->getAppInstalledVersions(); + $v['core'] = implode('.', $this->serverVersion->getVersion()); self::$versionHash = substr(md5(implode(',', $v)), 0, 8); } } else { @@ -220,7 +221,7 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate { // this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call) // see https://github.com/nextcloud/server/pull/22636 for details $jsConfigHelper = new JSConfigHelper( - \OCP\Server::get(ServerVersion::class), + $this->serverVersion, \OCP\Util::getL10N('lib'), \OCP\Server::get(Defaults::class), $this->appManager, diff --git a/lib/private/legacy/OC_App.php b/lib/private/legacy/OC_App.php index 7fee946b77682..ecceafa65b332 100644 --- a/lib/private/legacy/OC_App.php +++ b/lib/private/legacy/OC_App.php @@ -456,10 +456,9 @@ public static function getAllApps(): array { /** * List all supported apps * - * @return array + * @deprecated 32.0.0 Use \OCP\Support\Subscription\IRegistry::delegateGetSupportedApps instead */ public function getSupportedApps(): array { - /** @var \OCP\Support\Subscription\IRegistry $subscriptionRegistry */ $subscriptionRegistry = \OCP\Server::get(\OCP\Support\Subscription\IRegistry::class); $supportedApps = $subscriptionRegistry->delegateGetSupportedApps(); return $supportedApps; @@ -643,16 +642,10 @@ public static function isAppCompatible(string $ocVersion, array $appInfo, bool $ /** * get the installed version of all apps + * @deprecated 32.0.0 Use IAppManager::getAppInstalledVersions or IAppConfig::getAppInstalledVersions instead */ - public static function getAppVersions() { - static $versions; - - if (!$versions) { - /** @var IAppConfig $appConfig */ - $appConfig = \OCP\Server::get(IAppConfig::class); - $versions = $appConfig->searchValues('installed_version'); - } - return $versions; + public static function getAppVersions(): array { + return \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(); } /** diff --git a/lib/public/App/IAppManager.php b/lib/public/App/IAppManager.php index fa35819b779c8..67ef2d796be51 100644 --- a/lib/public/App/IAppManager.php +++ b/lib/public/App/IAppManager.php @@ -51,6 +51,14 @@ public function getAppInfoByPath(string $path, ?string $lang = null): ?array; */ public function getAppVersion(string $appId, bool $useCache = true): string; + /** + * Returns the installed version of all apps + * + * @return array + * @since 32.0.0 + */ + public function getAppInstalledVersions(): array; + /** * Returns the app icon or null if none is found * diff --git a/lib/public/IAppConfig.php b/lib/public/IAppConfig.php index d4d5c1c09c740..f42107934760b 100644 --- a/lib/public/IAppConfig.php +++ b/lib/public/IAppConfig.php @@ -507,4 +507,12 @@ public function getValues($app, $key); * @deprecated 29.0.0 Use {@see getAllValues()} or {@see searchValues()} */ public function getFilteredValues($app); + + /** + * Returns the installed version of all apps + * + * @return array + * @since 32.0.0 + */ + public function getAppInstalledVersions(): array; } diff --git a/tests/lib/TemplateLayoutTest.php b/tests/lib/TemplateLayoutTest.php index 29e31b3f3911a..b389552540871 100644 --- a/tests/lib/TemplateLayoutTest.php +++ b/tests/lib/TemplateLayoutTest.php @@ -15,6 +15,7 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\IConfig; use OCP\INavigationManager; +use OCP\ServerVersion; use OCP\Template\ITemplateManager; use PHPUnit\Framework\MockObject\MockObject; @@ -24,6 +25,7 @@ class TemplateLayoutTest extends \Test\TestCase { private InitialStateService&MockObject $initialState; private INavigationManager&MockObject $navigationManager; private ITemplateManager&MockObject $templateManager; + private ServerVersion&MockObject $serverVersion; private TemplateLayout $templateLayout; @@ -35,6 +37,7 @@ protected function setUp(): void { $this->initialState = $this->createMock(InitialStateService::class); $this->navigationManager = $this->createMock(INavigationManager::class); $this->templateManager = $this->createMock(ITemplateManager::class); + $this->serverVersion = $this->createMock(ServerVersion::class); } /** @dataProvider dataVersionHash */ @@ -69,6 +72,7 @@ public function testVersionHash($path, $file, $installed, $debug, $expected): vo $this->initialState, $this->navigationManager, $this->templateManager, + $this->serverVersion, ]) ->getMock();