Skip to content

Commit d5ad9b8

Browse files
authored
Merge pull request #51676 from nextcloud/fix/add-getappversions-replacement
Add OC_App::getAppVersions replacement in IAppManager
2 parents 444fca0 + c7037d7 commit d5ad9b8

12 files changed

Lines changed: 101 additions & 54 deletions

File tree

apps/updatenotification/lib/Notification/Notifier.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@
2020
use OCP\Notification\INotification;
2121
use OCP\Notification\INotifier;
2222
use OCP\Notification\UnknownNotificationException;
23-
use OCP\Server;
24-
use OCP\Util;
23+
use OCP\ServerVersion;
2524

2625
class Notifier implements INotifier {
2726
/** @var string[] */
@@ -44,8 +43,10 @@ public function __construct(
4443
protected IFactory $l10NFactory,
4544
protected IUserSession $userSession,
4645
protected IGroupManager $groupManager,
46+
protected IAppManager $appManager,
47+
protected ServerVersion $serverVersion,
4748
) {
48-
$this->appVersions = $this->getAppVersions();
49+
$this->appVersions = $this->appManager->getAppInstalledVersions();
4950
}
5051

5152
/**
@@ -144,15 +145,12 @@ public function prepare(INotification $notification, string $languageCode): INot
144145
* @param string $installedVersion
145146
* @throws AlreadyProcessedException When the update is already installed
146147
*/
147-
protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion) {
148+
protected function updateAlreadyInstalledCheck(INotification $notification, $installedVersion): void {
148149
if (version_compare($notification->getObjectId(), $installedVersion, '<=')) {
149150
throw new AlreadyProcessedException();
150151
}
151152
}
152153

153-
/**
154-
* @return bool
155-
*/
156154
protected function isAdmin(): bool {
157155
$user = $this->userSession->getUser();
158156

@@ -164,14 +162,10 @@ protected function isAdmin(): bool {
164162
}
165163

166164
protected function getCoreVersions(): string {
167-
return implode('.', Util::getVersion());
168-
}
169-
170-
protected function getAppVersions(): array {
171-
return \OC_App::getAppVersions();
165+
return implode('.', $this->serverVersion->getVersion());
172166
}
173167

174-
protected function getAppInfo($appId, $languageCode) {
175-
return Server::get(IAppManager::class)->getAppInfo($appId, false, $languageCode);
168+
protected function getAppInfo(string $appId, ?string $languageCode): ?array {
169+
return $this->appManager->getAppInfo($appId, false, $languageCode);
176170
}
177171
}

apps/updatenotification/tests/Notification/NotifierTest.php

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace OCA\UpdateNotification\Tests\Notification;
1010

1111
use OCA\UpdateNotification\Notification\Notifier;
12+
use OCP\App\IAppManager;
1213
use OCP\IConfig;
1314
use OCP\IGroupManager;
1415
use OCP\IURLGenerator;
@@ -17,22 +18,20 @@
1718
use OCP\Notification\AlreadyProcessedException;
1819
use OCP\Notification\IManager;
1920
use OCP\Notification\INotification;
21+
use OCP\ServerVersion;
22+
use PHPUnit\Framework\MockObject\MockObject;
2023
use Test\TestCase;
2124

2225
class NotifierTest extends TestCase {
2326

24-
/** @var IURLGenerator|\PHPUnit\Framework\MockObject\MockObject */
25-
protected $urlGenerator;
26-
/** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
27-
protected $config;
28-
/** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
29-
protected $notificationManager;
30-
/** @var IFactory|\PHPUnit\Framework\MockObject\MockObject */
31-
protected $l10nFactory;
32-
/** @var IUserSession|\PHPUnit\Framework\MockObject\MockObject */
33-
protected $userSession;
34-
/** @var IGroupManager|\PHPUnit\Framework\MockObject\MockObject */
35-
protected $groupManager;
27+
protected IURLGenerator&MockObject $urlGenerator;
28+
protected IConfig&MockObject $config;
29+
protected IManager&MockObject $notificationManager;
30+
protected IFactory&MockObject $l10nFactory;
31+
protected IUserSession&MockObject $userSession;
32+
protected IGroupManager&MockObject $groupManager;
33+
protected IAppManager&MockObject $appManager;
34+
protected ServerVersion&MockObject $serverVersion;
3635

3736
protected function setUp(): void {
3837
parent::setUp();
@@ -43,6 +42,8 @@ protected function setUp(): void {
4342
$this->l10nFactory = $this->createMock(IFactory::class);
4443
$this->userSession = $this->createMock(IUserSession::class);
4544
$this->groupManager = $this->createMock(IGroupManager::class);
45+
$this->appManager = $this->createMock(IAppManager::class);
46+
$this->serverVersion = $this->createMock(ServerVersion::class);
4647
}
4748

4849
/**
@@ -57,7 +58,9 @@ protected function getNotifier(array $methods = []) {
5758
$this->notificationManager,
5859
$this->l10nFactory,
5960
$this->userSession,
60-
$this->groupManager
61+
$this->groupManager,
62+
$this->appManager,
63+
$this->serverVersion,
6164
);
6265
}
6366
{
@@ -69,6 +72,8 @@ protected function getNotifier(array $methods = []) {
6972
$this->l10nFactory,
7073
$this->userSession,
7174
$this->groupManager,
75+
$this->appManager,
76+
$this->serverVersion,
7277
])
7378
->onlyMethods($methods)
7479
->getMock();

core/Command/App/ListApps.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
class ListApps extends Base {
1616
public function __construct(
17-
protected IAppManager $manager,
17+
protected IAppManager $appManager,
1818
) {
1919
parent::__construct();
2020
}
@@ -56,16 +56,16 @@ protected function execute(InputInterface $input, OutputInterface $output): int
5656
$showEnabledApps = $input->getOption('enabled') || !$input->getOption('disabled');
5757
$showDisabledApps = $input->getOption('disabled') || !$input->getOption('enabled');
5858

59-
$apps = \OC_App::getAllApps();
59+
$apps = $this->appManager->getAllAppsInAppsFolders();
6060
$enabledApps = $disabledApps = [];
61-
$versions = \OC_App::getAppVersions();
61+
$versions = $this->appManager->getAppInstalledVersions();
6262

6363
//sort enabled apps above disabled apps
6464
foreach ($apps as $app) {
65-
if ($shippedFilter !== null && $this->manager->isShipped($app) !== $shippedFilter) {
65+
if ($shippedFilter !== null && $this->appManager->isShipped($app) !== $shippedFilter) {
6666
continue;
6767
}
68-
if ($this->manager->isEnabledForAnyone($app)) {
68+
if ($this->appManager->isEnabledForAnyone($app)) {
6969
$enabledApps[] = $app;
7070
} else {
7171
$disabledApps[] = $app;
@@ -88,7 +88,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
8888

8989
sort($disabledApps);
9090
foreach ($disabledApps as $app) {
91-
$apps['disabled'][$app] = $this->manager->getAppVersion($app) . (isset($versions[$app]) ? ' (installed ' . $versions[$app] . ')' : '');
91+
$apps['disabled'][$app] = $this->appManager->getAppVersion($app) . (isset($versions[$app]) ? ' (installed ' . $versions[$app] . ')' : '');
9292
}
9393
}
9494

lib/private/App/AppManager.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -811,6 +811,15 @@ public function getAppVersion(string $appId, bool $useCache = true): string {
811811
return $this->appVersions[$appId];
812812
}
813813

814+
/**
815+
* Returns the installed versions of all apps
816+
*
817+
* @return array<string, string>
818+
*/
819+
public function getAppInstalledVersions(): array {
820+
return $this->getAppConfig()->getAppInstalledVersions();
821+
}
822+
814823
/**
815824
* Returns a list of apps incompatible with the given version
816825
*

lib/private/AppConfig.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,9 @@ class AppConfig implements IAppConfig {
6262
/** @var array<array-key, array{entries: array<array-key, ConfigLexiconEntry>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */
6363
private array $configLexiconDetails = [];
6464

65+
/** @var ?array<string, string> */
66+
private ?array $appVersionsCache = null;
67+
6568
public function __construct(
6669
protected IDBConnection $connection,
6770
protected LoggerInterface $logger,
@@ -1647,4 +1650,17 @@ private function getConfigDetailsFromLexicon(string $appId): array {
16471650

16481651
return $this->configLexiconDetails[$appId];
16491652
}
1653+
1654+
/**
1655+
* Returns the installed versions of all apps
1656+
*
1657+
* @return array<string, string>
1658+
*/
1659+
public function getAppInstalledVersions(): array {
1660+
if ($this->appVersionsCache === null) {
1661+
/** @var array<string, string> */
1662+
$this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING);
1663+
}
1664+
return $this->appVersionsCache;
1665+
}
16501666
}

lib/private/AppFramework/Services/AppConfig.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -337,4 +337,13 @@ public function getUserValue(string $userId, string $key, string $default = ''):
337337
public function deleteUserValue(string $userId, string $key): void {
338338
$this->config->deleteUserValue($userId, $this->appName, $key);
339339
}
340+
341+
/**
342+
* Returns the installed versions of all apps
343+
*
344+
* @return array<string, string>
345+
*/
346+
public function getAppInstalledVersions(): array {
347+
return $this->appConfig->getAppInstalledVersions();
348+
}
340349
}

lib/private/Server.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -607,10 +607,10 @@ public function __construct($webRoot, \OC\Config $config) {
607607

608608
if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
609609
$logQuery = $config->getValue('log_query');
610-
$prefixClosure = function () use ($logQuery, $serverVersion) {
610+
$prefixClosure = function () use ($logQuery, $serverVersion): ?string {
611611
if (!$logQuery) {
612612
try {
613-
$v = \OC_App::getAppVersions();
613+
$v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions();
614614
} catch (\Doctrine\DBAL\Exception $e) {
615615
// Database service probably unavailable
616616
// Probably related to https://github.com/nextcloud/server/issues/37424
@@ -867,15 +867,15 @@ public function __construct($webRoot, \OC\Config $config) {
867867

868868
$this->registerDeprecatedAlias('IntegrityCodeChecker', Checker::class);
869869
$this->registerService(Checker::class, function (ContainerInterface $c) {
870-
// IConfig and IAppManager requires a working database. This code
871-
// might however be called when ownCloud is not yet setup.
870+
// IConfig requires a working database. This code
871+
// might however be called when Nextcloud is not yet setup.
872872
if (\OC::$server->get(SystemConfig::class)->getValue('installed', false)) {
873873
$config = $c->get(\OCP\IConfig::class);
874874
$appConfig = $c->get(\OCP\IAppConfig::class);
875875
} else {
876-
$config = $appConfig = $appManager = null;
876+
$config = null;
877+
$appConfig = null;
877878
}
878-
$appManager = $c->get(IAppManager::class);
879879

880880
return new Checker(
881881
$c->get(ServerVersion::class),
@@ -885,7 +885,7 @@ public function __construct($webRoot, \OC\Config $config) {
885885
$config,
886886
$appConfig,
887887
$c->get(ICacheFactory::class),
888-
$appManager,
888+
$c->get(IAppManager::class),
889889
$c->get(IMimeTypeDetector::class)
890890
);
891891
});

lib/private/TemplateLayout.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public function __construct(
4848
private InitialStateService $initialState,
4949
private INavigationManager $navigationManager,
5050
private ITemplateManager $templateManager,
51+
private ServerVersion $serverVersion,
5152
) {
5253
}
5354

@@ -204,8 +205,8 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate {
204205

205206
if ($this->config->getSystemValueBool('installed', false)) {
206207
if (empty(self::$versionHash)) {
207-
$v = \OC_App::getAppVersions();
208-
$v['core'] = implode('.', \OCP\Util::getVersion());
208+
$v = $this->appManager->getAppInstalledVersions();
209+
$v['core'] = implode('.', $this->serverVersion->getVersion());
209210
self::$versionHash = substr(md5(implode(',', $v)), 0, 8);
210211
}
211212
} else {
@@ -220,7 +221,7 @@ public function getPageTemplate(string $renderAs, string $appId): ITemplate {
220221
// this is on purpose outside of the if statement below so that the initial state is prefilled (done in the getConfig() call)
221222
// see https://github.com/nextcloud/server/pull/22636 for details
222223
$jsConfigHelper = new JSConfigHelper(
223-
\OCP\Server::get(ServerVersion::class),
224+
$this->serverVersion,
224225
\OCP\Util::getL10N('lib'),
225226
\OCP\Server::get(Defaults::class),
226227
$this->appManager,

lib/private/legacy/OC_App.php

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -456,10 +456,9 @@ public static function getAllApps(): array {
456456
/**
457457
* List all supported apps
458458
*
459-
* @return array
459+
* @deprecated 32.0.0 Use \OCP\Support\Subscription\IRegistry::delegateGetSupportedApps instead
460460
*/
461461
public function getSupportedApps(): array {
462-
/** @var \OCP\Support\Subscription\IRegistry $subscriptionRegistry */
463462
$subscriptionRegistry = \OCP\Server::get(\OCP\Support\Subscription\IRegistry::class);
464463
$supportedApps = $subscriptionRegistry->delegateGetSupportedApps();
465464
return $supportedApps;
@@ -643,16 +642,10 @@ public static function isAppCompatible(string $ocVersion, array $appInfo, bool $
643642

644643
/**
645644
* get the installed version of all apps
645+
* @deprecated 32.0.0 Use IAppManager::getAppInstalledVersions or IAppConfig::getAppInstalledVersions instead
646646
*/
647-
public static function getAppVersions() {
648-
static $versions;
649-
650-
if (!$versions) {
651-
/** @var IAppConfig $appConfig */
652-
$appConfig = \OCP\Server::get(IAppConfig::class);
653-
$versions = $appConfig->searchValues('installed_version');
654-
}
655-
return $versions;
647+
public static function getAppVersions(): array {
648+
return \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions();
656649
}
657650

658651
/**

lib/public/App/IAppManager.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,14 @@ public function getAppInfoByPath(string $path, ?string $lang = null): ?array;
5151
*/
5252
public function getAppVersion(string $appId, bool $useCache = true): string;
5353

54+
/**
55+
* Returns the installed version of all apps
56+
*
57+
* @return array<string, string>
58+
* @since 32.0.0
59+
*/
60+
public function getAppInstalledVersions(): array;
61+
5462
/**
5563
* Returns the app icon or null if none is found
5664
*

0 commit comments

Comments
 (0)