1212use OC \Config \ConfigManager ;
1313use OC \DB \MigrationService ;
1414use OC \Migration \BackgroundRepair ;
15+ use OC \NeedsUpdateException ;
16+ use OC \Repair ;
17+ use OC \Repair \Events \RepairErrorEvent ;
1518use OCP \Activity \IManager as IActivityManager ;
1619use OCP \App \AppPathNotFoundException ;
1720use OCP \App \Events \AppDisableEvent ;
@@ -258,7 +261,7 @@ public function loadApps(array $types = []): bool {
258261 return false ;
259262 }
260263 // Load the enabled apps here
261- $ apps = \OC_App:: getEnabledApps ();
264+ $ apps = $ this -> getEnabledApps ();
262265
263266 // Add each apps' folder as allowed class path
264267 foreach ($ apps as $ app ) {
@@ -708,7 +711,7 @@ public function disableApp($appId, $automaticDisabled = false): void {
708711 // run uninstall steps
709712 $ appData = $ this ->getAppInfo ($ appId );
710713 if (!is_null ($ appData )) {
711- \OC_App:: executeRepairSteps ($ appId , $ appData ['repair-steps ' ]['uninstall ' ]);
714+ $ this -> executeRepairSteps ($ appId , $ appData ['repair-steps ' ]['uninstall ' ]);
712715 }
713716
714717 $ this ->dispatcher ->dispatchTyped (new AppDisableEvent ($ appId ));
@@ -1109,30 +1112,24 @@ public function upgradeApp(string $appId): bool {
11091112 $ appPath = $ this ->getAppPath ($ appId , true );
11101113
11111114 $ this ->clearAppsCache ();
1112- $ l = \OC ::$ server ->getL10N ('core ' );
1113- $ appData = $ this ->getAppInfo ($ appId , false , $ l ->getLanguageCode ());
1114- if ($ appData === null ) {
1115+ $ appInfo = $ this ->getAppInfo ($ appId );
1116+ if ($ appInfo === null ) {
11151117 throw new AppPathNotFoundException ('Could not find ' . $ appId );
11161118 }
11171119
11181120 $ ignoreMaxApps = $ this ->config ->getSystemValue ('app_install_overwrite ' , []);
11191121 $ ignoreMax = in_array ($ appId , $ ignoreMaxApps , true );
1120- \OC_App::checkAppDependencies (
1121- $ this ->config ,
1122- $ l ,
1123- $ appData ,
1124- $ ignoreMax
1125- );
1122+ $ this ->checkAppDependencies ($ appId , $ ignoreMax );
11261123
11271124 \OC_App::registerAutoloading ($ appId , $ appPath , true );
1128- \OC_App:: executeRepairSteps ($ appId , $ appData ['repair-steps ' ]['pre-migration ' ]);
1125+ $ this -> executeRepairSteps ($ appId , $ appInfo ['repair-steps ' ]['pre-migration ' ]);
11291126
11301127 $ ms = new MigrationService ($ appId , Server::get (\OC \DB \Connection::class));
11311128 $ ms ->migrate ();
11321129
1133- \OC_App:: executeRepairSteps ($ appId , $ appData ['repair-steps ' ]['post-migration ' ]);
1130+ $ this -> executeRepairSteps ($ appId , $ appInfo ['repair-steps ' ]['post-migration ' ]);
11341131 $ queue = Server::get (IJobList::class);
1135- foreach ($ appData ['repair-steps ' ]['live-migration ' ] as $ step ) {
1132+ foreach ($ appInfo ['repair-steps ' ]['live-migration ' ] as $ step ) {
11361133 $ queue ->add (BackgroundRepair::class, [
11371134 'app ' => $ appId ,
11381135 'step ' => $ step ]);
@@ -1143,19 +1140,19 @@ public function upgradeApp(string $appId): bool {
11431140 $ this ->getAppVersion ($ appId , false );
11441141
11451142 // Setup background jobs
1146- foreach ($ appData ['background-jobs ' ] as $ job ) {
1143+ foreach ($ appInfo ['background-jobs ' ] as $ job ) {
11471144 $ queue ->add ($ job );
11481145 }
11491146
11501147 //set remote/public handlers
1151- foreach ($ appData ['remote ' ] as $ name => $ path ) {
1148+ foreach ($ appInfo ['remote ' ] as $ name => $ path ) {
11521149 $ this ->config ->setAppValue ('core ' , 'remote_ ' . $ name , $ appId . '/ ' . $ path );
11531150 }
1154- foreach ($ appData ['public ' ] as $ name => $ path ) {
1151+ foreach ($ appInfo ['public ' ] as $ name => $ path ) {
11551152 $ this ->config ->setAppValue ('core ' , 'public_ ' . $ name , $ appId . '/ ' . $ path );
11561153 }
11571154
1158- $ this ->setAppTypes ($ appId , $ appData );
1155+ $ this ->setAppTypes ($ appId , $ appInfo );
11591156
11601157 $ version = $ this ->getAppVersion ($ appId );
11611158 $ this ->config ->setAppValue ($ appId , 'installed_version ' , $ version );
@@ -1197,4 +1194,53 @@ public function isUpgradeRequired(string $appId): bool {
11971194 public function isAppCompatible (string $ serverVersion , array $ appInfo , bool $ ignoreMax = false ): bool {
11981195 return count ($ this ->dependencyAnalyzer ->analyzeServerVersion ($ serverVersion , $ appInfo , $ ignoreMax )) === 0 ;
11991196 }
1197+
1198+ /**
1199+ * Check if all dependencies of an app are satisfied.
1200+ *
1201+ * @param string $appId - The app to check
1202+ * @param bool $ignoreMax - Whether to ignore the Nextcloud max version requirement
1203+ * @throws \Exception - If there are missing dependencies
1204+ */
1205+ public function checkAppDependencies (string $ appId , bool $ ignoreMax = false ): void {
1206+ $ info = $ this ->getAppInfo ($ appId );
1207+ $ missing = $ this ->dependencyAnalyzer ->analyze ($ info , $ ignoreMax );
1208+ if (!empty ($ missing )) {
1209+ $ l = \OCP \Server::get (\OCP \L10N \IFactory::class)->get ('core ' );
1210+ $ missingMsg = implode (PHP_EOL , $ missing );
1211+ throw new \Exception (
1212+ $ l ->t ('App "%1$s" cannot be installed because the following dependencies are not fulfilled: %2$s ' ,
1213+ [$ info ['name ' ], $ missingMsg ]
1214+ )
1215+ );
1216+ }
1217+ }
1218+
1219+ /**
1220+ * Run repair steps for an app.
1221+ *
1222+ * @param string $appId - The app to run the repair steps for
1223+ * @param string[] $steps - The repair steps to run
1224+ * @throws NeedsUpdateException
1225+ */
1226+ public function executeRepairSteps (string $ appId , array $ steps ): void {
1227+ if (empty ($ steps )) {
1228+ return ;
1229+ }
1230+
1231+ $ this ->loadApp ($ appId );
1232+
1233+ // load the steps
1234+ $ r = Server::get (Repair::class);
1235+ foreach ($ steps as $ step ) {
1236+ try {
1237+ $ r ->addStep ($ step );
1238+ } catch (\Exception $ ex ) {
1239+ $ this ->dispatcher ->dispatchTyped (new RepairErrorEvent ($ ex ->getMessage ()));
1240+ $ this ->logger ->error ('Failed to add app migration step ' . $ step , ['exception ' => $ ex ]);
1241+ }
1242+ }
1243+ // run the steps
1244+ $ r ->run ();
1245+ }
12001246}
0 commit comments