Skip to content

Commit 9579170

Browse files
authored
Merge pull request #60071 from nextcloud/chore/legacy
chore(OC_App): migrate more legacy function and usage
2 parents 37d1ee6 + 1c23fea commit 9579170

16 files changed

Lines changed: 146 additions & 137 deletions

File tree

build/psalm-baseline.xml

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2954,7 +2954,6 @@
29542954
</file>
29552955
<file src="core/Command/Config/ListConfigs.php">
29562956
<DeprecatedMethod>
2957-
<code><![CDATA[\OC_App::getAllApps()]]></code>
29582957
<code><![CDATA[getFilteredValues]]></code>
29592958
<code><![CDATA[getValues]]></code>
29602959
</DeprecatedMethod>
@@ -2977,26 +2976,11 @@
29772976
<code><![CDATA[getDatabasePlatform]]></code>
29782977
</DeprecatedMethod>
29792978
</file>
2980-
<file src="core/Command/Db/Migrations/ExecuteCommand.php">
2981-
<DeprecatedMethod>
2982-
<code><![CDATA[\OC_App::getAllApps()]]></code>
2983-
</DeprecatedMethod>
2984-
</file>
29852979
<file src="core/Command/Db/Migrations/GenerateCommand.php">
29862980
<DeprecatedMethod>
29872981
<code><![CDATA[Util::getVersion()]]></code>
29882982
</DeprecatedMethod>
29892983
</file>
2990-
<file src="core/Command/Db/Migrations/MigrateCommand.php">
2991-
<DeprecatedMethod>
2992-
<code><![CDATA[\OC_App::getAllApps()]]></code>
2993-
</DeprecatedMethod>
2994-
</file>
2995-
<file src="core/Command/Db/Migrations/StatusCommand.php">
2996-
<DeprecatedMethod>
2997-
<code><![CDATA[\OC_App::getAllApps()]]></code>
2998-
</DeprecatedMethod>
2999-
</file>
30002984
<file src="core/Command/Encryption/ChangeKeyStorageRoot.php">
30012985
<DeprecatedClass>
30022986
<code><![CDATA[\OC_Util::setupFS($uid)]]></code>

core/Command/App/Disable.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,9 @@ public function completeOptionValues($optionName, CompletionContext $context): a
8080
#[\Override]
8181
public function completeArgumentValues($argumentName, CompletionContext $context): array {
8282
if ($argumentName === 'app-id') {
83-
return array_diff(\OC_App::getEnabledApps(true, true), $this->appManager->getAlwaysEnabledApps());
83+
$enabledApps = $this->appManager->getEnabledApps();
84+
$coreApps = $this->appManager->getAlwaysEnabledApps();
85+
return array_diff($enabledApps, $coreApps);
8486
}
8587
return [];
8688
}

core/Command/App/Enable.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,8 @@ public function completeOptionValues($optionName, CompletionContext $context): a
148148
public function completeArgumentValues($argumentName, CompletionContext $context): array {
149149
if ($argumentName === 'app-id') {
150150
$allApps = $this->appManager->getAllAppsInAppsFolders();
151-
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
151+
$enabledApps = $this->appManager->getEnabledApps();
152+
return array_diff($allApps, $enabledApps);
152153
}
153154
return [];
154155
}

core/Command/Config/ListConfigs.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\Config\ConfigManager;
1111
use OC\Core\Command\Base;
1212
use OC\SystemConfig;
13+
use OCP\App\IAppManager;
1314
use OCP\IAppConfig;
1415
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
1516
use Symfony\Component\Console\Input\InputArgument;
@@ -24,6 +25,7 @@ public function __construct(
2425
protected SystemConfig $systemConfig,
2526
protected IAppConfig $appConfig,
2627
protected ConfigManager $configManager,
28+
protected IAppManager $appManager,
2729
) {
2830
parent::__construct();
2931
}
@@ -141,7 +143,7 @@ protected function getAppConfigs(string $app, bool $noSensitiveValues) {
141143
#[\Override]
142144
public function completeArgumentValues($argumentName, CompletionContext $context) {
143145
if ($argumentName === 'app') {
144-
return array_merge(['all', 'system'], \OC_App::getAllApps());
146+
return array_merge(['all', 'system'], $this->appManager->getAllAppsInAppsFolders());
145147
}
146148
return [];
147149
}

core/Command/Db/ExpectedSchema.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use OC\DB\MigrationService;
1515
use OC\DB\SchemaWrapper;
1616
use OC\Migration\NullOutput;
17+
use OCP\App\IAppManager;
1718
use Override;
1819
use Symfony\Component\Console\Input\InputArgument;
1920
use Symfony\Component\Console\Input\InputInterface;
@@ -23,6 +24,7 @@
2324
class ExpectedSchema extends Base {
2425
public function __construct(
2526
protected readonly Connection $connection,
27+
protected readonly IAppManager $appManager,
2628
) {
2729
parent::__construct();
2830
}
@@ -45,7 +47,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4547

4648
$this->applyMigrations('core', $schema);
4749

48-
$apps = \OC_App::getEnabledApps();
50+
$apps = $this->appManager->getEnabledApps();
4951
foreach ($apps as $app) {
5052
$this->applyMigrations($app, $schema);
5153
}

core/Command/Db/Migrations/ExecuteCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\DB\Connection;
1111
use OC\DB\MigrationService;
1212
use OC\Migration\ConsoleOutput;
13+
use OCP\App\IAppManager;
1314
use OCP\IConfig;
1415
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
1516
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
@@ -22,6 +23,7 @@ class ExecuteCommand extends Command implements CompletionAwareInterface {
2223
public function __construct(
2324
private Connection $connection,
2425
private IConfig $config,
26+
private IAppManager $appManager,
2527
) {
2628
parent::__construct();
2729
}
@@ -81,8 +83,8 @@ public function completeOptionValues($optionName, CompletionContext $context) {
8183
#[\Override]
8284
public function completeArgumentValues($argumentName, CompletionContext $context) {
8385
if ($argumentName === 'app') {
84-
$allApps = \OC_App::getAllApps();
85-
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
86+
$allApps = $this->appManager->getAllAppsInAppsFolders();
87+
return array_diff($allApps, $this->appManager->getEnabledApps());
8688
}
8789

8890
if ($argumentName === 'version') {

core/Command/Db/Migrations/GenerateCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,8 @@ public function completeOptionValues($optionName, CompletionContext $context) {
164164
public function completeArgumentValues($argumentName, CompletionContext $context) {
165165
if ($argumentName === 'app') {
166166
$allApps = $this->appManager->getAllAppsInAppsFolders();
167-
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
167+
$enabledApps = $this->appManager->getEnabledApps();
168+
return array_diff($allApps, $enabledApps);
168169
}
169170

170171
if ($argumentName === 'version') {

core/Command/Db/Migrations/MigrateCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\DB\Connection;
1111
use OC\DB\MigrationService;
1212
use OC\Migration\ConsoleOutput;
13+
use OCP\App\IAppManager;
1314
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
1415
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
1516
use Symfony\Component\Console\Command\Command;
@@ -20,6 +21,7 @@
2021
class MigrateCommand extends Command implements CompletionAwareInterface {
2122
public function __construct(
2223
private Connection $connection,
24+
private IAppManager $appManager,
2325
) {
2426
parent::__construct();
2527
}
@@ -63,8 +65,8 @@ public function completeOptionValues($optionName, CompletionContext $context) {
6365
#[\Override]
6466
public function completeArgumentValues($argumentName, CompletionContext $context) {
6567
if ($argumentName === 'app') {
66-
$allApps = \OC_App::getAllApps();
67-
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
68+
$allApps = $this->appManager->getAllAppsInAppsFolders();
69+
return array_diff($allApps, $this->appManager->getEnabledApps());
6870
}
6971

7072
if ($argumentName === 'version') {

core/Command/Db/Migrations/StatusCommand.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use OC\DB\Connection;
1111
use OC\DB\MigrationService;
1212
use OC\Migration\ConsoleOutput;
13+
use OCP\App\IAppManager;
1314
use Stecman\Component\Symfony\Console\BashCompletion\Completion\CompletionAwareInterface;
1415
use Stecman\Component\Symfony\Console\BashCompletion\CompletionContext;
1516
use Symfony\Component\Console\Command\Command;
@@ -20,6 +21,7 @@
2021
class StatusCommand extends Command implements CompletionAwareInterface {
2122
public function __construct(
2223
private Connection $connection,
24+
private IAppManager $appManager,
2325
) {
2426
parent::__construct();
2527
}
@@ -69,8 +71,8 @@ public function completeOptionValues($optionName, CompletionContext $context) {
6971
#[\Override]
7072
public function completeArgumentValues($argumentName, CompletionContext $context) {
7173
if ($argumentName === 'app') {
72-
$allApps = \OC_App::getAllApps();
73-
return array_diff($allApps, \OC_App::getEnabledApps(true, true));
74+
$allApps = $this->appManager->getAllAppsInAppsFolders();
75+
return array_diff($allApps, $this->appManager->getEnabledApps());
7476
}
7577
return [];
7678
}

lib/private/App/AppManager.php

Lines changed: 77 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
use OC\Config\ConfigManager;
1313
use OC\DB\MigrationService;
1414
use OC\Migration\BackgroundRepair;
15+
use OC\NeedsUpdateException;
16+
use OC\Repair;
17+
use OC\Repair\Events\RepairErrorEvent;
1518
use OCP\Activity\IManager as IActivityManager;
1619
use OCP\App\AppPathNotFoundException;
1720
use OCP\App\Events\AppDisableEvent;
@@ -153,6 +156,10 @@ private function getUrlGenerator(): IURLGenerator {
153156
* @return array<string,string> appId => enabled (may be 'yes', or a json encoded list of group ids)
154157
*/
155158
private function getEnabledAppsValues(): array {
159+
if (!$this->config->getSystemValueBool('installed')) {
160+
return [];
161+
}
162+
156163
if (!$this->enabledAppsCache) {
157164
/** @var array<string,string> */
158165
$values = $this->getAppConfig()->searchValues('enabled', false, IAppConfig::VALUE_STRING);
@@ -246,25 +253,18 @@ public function getEnabledAppsForGroup(IGroup $group): array {
246253
return array_keys($appsForGroups);
247254
}
248255

249-
/**
250-
* Loads all apps
251-
*
252-
* @param string[] $types
253-
* @return bool
254-
*
255-
* This function walks through the Nextcloud directory and loads all apps
256-
* it can find. A directory contains an app if the file /appinfo/info.xml
257-
* exists.
258-
*
259-
* if $types is set to non-empty array, only apps of those types will be loaded
260-
*/
261256
#[\Override]
262257
public function loadApps(array $types = []): bool {
263258
if ($this->config->getSystemValueBool('maintenance', false)) {
264259
return false;
265260
}
261+
if ($this->config->getSystemValueBool('installed') === false) {
262+
// can only access the apps folder after installation, so we can't load any apps before that
263+
return false;
264+
}
265+
266266
// Load the enabled apps here
267-
$apps = \OC_App::getEnabledApps();
267+
$apps = $this->getEnabledApps();
268268

269269
// Add each apps' folder as allowed class path
270270
foreach ($apps as $app) {
@@ -301,13 +301,6 @@ public function loadApps(array $types = []): bool {
301301
return true;
302302
}
303303

304-
/**
305-
* check if an app is of a specific type
306-
*
307-
* @param string $app
308-
* @param array $types
309-
* @return bool
310-
*/
311304
#[\Override]
312305
public function isType(string $app, array $types): bool {
313306
$appTypes = $this->getAppTypes($app);
@@ -714,7 +707,7 @@ public function disableApp($appId, $automaticDisabled = false): void {
714707
// run uninstall steps
715708
$appData = $this->getAppInfo($appId);
716709
if (!is_null($appData)) {
717-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
710+
$this->executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
718711
}
719712

720713
$this->dispatcher->dispatchTyped(new AppDisableEvent($appId));
@@ -1109,30 +1102,24 @@ public function upgradeApp(string $appId): bool {
11091102
$appPath = $this->getAppPath($appId, true);
11101103

11111104
$this->clearAppsCache();
1112-
$l = \OC::$server->getL10N('core');
1113-
$appData = $this->getAppInfo($appId, false, $l->getLanguageCode());
1114-
if ($appData === null) {
1105+
$appInfo = $this->getAppInfo($appId);
1106+
if ($appInfo === null) {
11151107
throw new AppPathNotFoundException('Could not find ' . $appId);
11161108
}
11171109

11181110
$ignoreMaxApps = $this->config->getSystemValue('app_install_overwrite', []);
11191111
$ignoreMax = in_array($appId, $ignoreMaxApps, true);
1120-
\OC_App::checkAppDependencies(
1121-
$this->config,
1122-
$l,
1123-
$appData,
1124-
$ignoreMax
1125-
);
1112+
$this->checkAppDependencies($appId, $ignoreMax);
11261113

11271114
\OC_App::registerAutoloading($appId, $appPath, true);
1128-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['pre-migration']);
1115+
$this->executeRepairSteps($appId, $appInfo['repair-steps']['pre-migration']);
11291116

11301117
$ms = new MigrationService($appId, Server::get(\OC\DB\Connection::class));
11311118
$ms->migrate();
11321119

1133-
\OC_App::executeRepairSteps($appId, $appData['repair-steps']['post-migration']);
1120+
$this->executeRepairSteps($appId, $appInfo['repair-steps']['post-migration']);
11341121
$queue = Server::get(IJobList::class);
1135-
foreach ($appData['repair-steps']['live-migration'] as $step) {
1122+
foreach ($appInfo['repair-steps']['live-migration'] as $step) {
11361123
$queue->add(BackgroundRepair::class, [
11371124
'app' => $appId,
11381125
'step' => $step]);
@@ -1143,19 +1130,19 @@ public function upgradeApp(string $appId): bool {
11431130
$this->getAppVersion($appId, false);
11441131

11451132
// Setup background jobs
1146-
foreach ($appData['background-jobs'] as $job) {
1133+
foreach ($appInfo['background-jobs'] as $job) {
11471134
$queue->add($job);
11481135
}
11491136

11501137
//set remote/public handlers
1151-
foreach ($appData['remote'] as $name => $path) {
1138+
foreach ($appInfo['remote'] as $name => $path) {
11521139
$this->config->setAppValue('core', 'remote_' . $name, $appId . '/' . $path);
11531140
}
1154-
foreach ($appData['public'] as $name => $path) {
1141+
foreach ($appInfo['public'] as $name => $path) {
11551142
$this->config->setAppValue('core', 'public_' . $name, $appId . '/' . $path);
11561143
}
11571144

1158-
$this->setAppTypes($appId, $appData);
1145+
$this->setAppTypes($appId, $appInfo);
11591146

11601147
$version = $this->getAppVersion($appId);
11611148
$this->config->setAppValue($appId, 'installed_version', $version);
@@ -1237,4 +1224,57 @@ public function getAppFromNamespace(string $className): ?string {
12371224

12381225
return null;
12391226
}
1227+
1228+
/**
1229+
* Check if all dependencies of an app are satisfied.
1230+
*
1231+
* @param string $appId - The app to check
1232+
* @param bool $ignoreMax - Whether to ignore the Nextcloud max version requirement
1233+
* @throws \Exception - If there are missing dependencies
1234+
*/
1235+
public function checkAppDependencies(string $appId, bool $ignoreMax = false): void {
1236+
$info = $this->getAppInfo($appId);
1237+
if ($info === null) {
1238+
throw new \RuntimeException("App $appId not found");
1239+
}
1240+
1241+
$missing = $this->dependencyAnalyzer->analyze($info, $ignoreMax);
1242+
if ($missing !== []) {
1243+
$l = \OCP\Server::get(\OCP\L10N\IFactory::class)->get('core');
1244+
$missingMsg = implode(PHP_EOL, $missing);
1245+
throw new \Exception(
1246+
$l->t('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s',
1247+
[$info['name'], $missingMsg]
1248+
)
1249+
);
1250+
}
1251+
}
1252+
1253+
/**
1254+
* Run repair steps for an app.
1255+
*
1256+
* @param string $appId - The app to run the repair steps for
1257+
* @param string[] $steps - The repair steps to run
1258+
* @throws NeedsUpdateException
1259+
*/
1260+
public function executeRepairSteps(string $appId, array $steps): void {
1261+
if ($steps === []) {
1262+
return;
1263+
}
1264+
1265+
$this->loadApp($appId);
1266+
1267+
// load the steps
1268+
$r = Server::get(Repair::class);
1269+
foreach ($steps as $step) {
1270+
try {
1271+
$r->addStep($step);
1272+
} catch (\Exception $ex) {
1273+
$this->dispatcher->dispatchTyped(new RepairErrorEvent($ex->getMessage()));
1274+
$this->logger->error('Failed to add app migration step ' . $step, ['exception' => $ex]);
1275+
}
1276+
}
1277+
// run the steps
1278+
$r->run();
1279+
}
12401280
}

0 commit comments

Comments
 (0)