Skip to content
22 changes: 8 additions & 14 deletions apps/updatenotification/lib/Notification/Notifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[] */
Expand All @@ -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();
}

/**
Expand Down Expand Up @@ -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();

Expand All @@ -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);
}
}
31 changes: 18 additions & 13 deletions apps/updatenotification/tests/Notification/NotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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();
Expand All @@ -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);
}

/**
Expand All @@ -57,7 +58,9 @@ protected function getNotifier(array $methods = []) {
$this->notificationManager,
$this->l10nFactory,
$this->userSession,
$this->groupManager
$this->groupManager,
$this->appManager,
$this->serverVersion,
);
}
{
Expand All @@ -69,6 +72,8 @@ protected function getNotifier(array $methods = []) {
$this->l10nFactory,
$this->userSession,
$this->groupManager,
$this->appManager,
$this->serverVersion,
])
->onlyMethods($methods)
->getMock();
Expand Down
12 changes: 6 additions & 6 deletions core/Command/App/ListApps.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class ListApps extends Base {
public function __construct(
protected IAppManager $manager,
protected IAppManager $appManager,
) {
parent::__construct();
}
Expand Down Expand Up @@ -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;
Expand All @@ -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] . ')' : '');
}
}

Expand Down
9 changes: 9 additions & 0 deletions lib/private/App/AppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
public function getAppInstalledVersions(): array {
return $this->getAppConfig()->getAppInstalledVersions();
}

/**
* Returns a list of apps incompatible with the given version
*
Expand Down
16 changes: 16 additions & 0 deletions lib/private/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@ class AppConfig implements IAppConfig {
/** @var array<array-key, array{entries: array<array-key, ConfigLexiconEntry>, strictness: ConfigLexiconStrictness}> ['app_id' => ['strictness' => ConfigLexiconStrictness, 'entries' => ['config_key' => ConfigLexiconEntry[]]] */
private array $configLexiconDetails = [];

/** @var ?array<string, string> */
private ?array $appVersionsCache = null;

public function __construct(
protected IDBConnection $connection,
protected LoggerInterface $logger,
Expand Down Expand Up @@ -1647,4 +1650,17 @@ private function getConfigDetailsFromLexicon(string $appId): array {

return $this->configLexiconDetails[$appId];
}

/**
* Returns the installed versions of all apps
*
* @return array<string, string>
*/
public function getAppInstalledVersions(): array {
if ($this->appVersionsCache === null) {
/** @var array<string, string> */
$this->appVersionsCache = $this->searchValues('installed_version', false, IAppConfig::VALUE_STRING);
}
return $this->appVersionsCache;
}
}
9 changes: 9 additions & 0 deletions lib/private/AppFramework/Services/AppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
*/
public function getAppInstalledVersions(): array {
return $this->appConfig->getAppInstalledVersions();
}
}
14 changes: 7 additions & 7 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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)
);
});
Expand Down
7 changes: 4 additions & 3 deletions lib/private/TemplateLayout.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function __construct(
private InitialStateService $initialState,
private INavigationManager $navigationManager,
private ITemplateManager $templateManager,
private ServerVersion $serverVersion,
) {
}

Expand Down Expand Up @@ -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 {
Expand All @@ -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,
Expand Down
15 changes: 4 additions & 11 deletions lib/private/legacy/OC_App.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

/**
Expand Down
8 changes: 8 additions & 0 deletions lib/public/App/IAppManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
* @since 32.0.0
*/
public function getAppInstalledVersions(): array;

/**
* Returns the app icon or null if none is found
*
Expand Down
8 changes: 8 additions & 0 deletions lib/public/IAppConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string>
* @since 32.0.0
*/
public function getAppInstalledVersions(): array;
}
4 changes: 4 additions & 0 deletions tests/lib/TemplateLayoutTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;

Expand All @@ -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 */
Expand Down Expand Up @@ -69,6 +72,7 @@ public function testVersionHash($path, $file, $installed, $debug, $expected): vo
$this->initialState,
$this->navigationManager,
$this->templateManager,
$this->serverVersion,
])
->getMock();

Expand Down
Loading