Completed
Pull Request — master (#9008)
by Julius
32:07 queued 14:40
created
lib/private/Updater.php 2 patches
Indentation   +550 added lines, -550 removed lines patch added patch discarded remove patch
@@ -54,556 +54,556 @@
 block discarded – undo
54 54
  */
55 55
 class Updater extends BasicEmitter {
56 56
 
57
-	/** @var ILogger $log */
58
-	private $log;
59
-
60
-	/** @var IConfig */
61
-	private $config;
62
-
63
-	/** @var Checker */
64
-	private $checker;
65
-
66
-	/** @var Installer */
67
-	private $installer;
68
-
69
-	private $logLevelNames = [
70
-		0 => 'Debug',
71
-		1 => 'Info',
72
-		2 => 'Warning',
73
-		3 => 'Error',
74
-		4 => 'Fatal',
75
-	];
76
-
77
-	/**
78
-	 * @param IConfig $config
79
-	 * @param Checker $checker
80
-	 * @param ILogger $log
81
-	 * @param Installer $installer
82
-	 */
83
-	public function __construct(IConfig $config,
84
-								Checker $checker,
85
-								ILogger $log = null,
86
-								Installer $installer) {
87
-		$this->log = $log;
88
-		$this->config = $config;
89
-		$this->checker = $checker;
90
-		$this->installer = $installer;
91
-	}
92
-
93
-	/**
94
-	 * runs the update actions in maintenance mode, does not upgrade the source files
95
-	 * except the main .htaccess file
96
-	 *
97
-	 * @return bool true if the operation succeeded, false otherwise
98
-	 */
99
-	public function upgrade() {
100
-		$this->emitRepairEvents();
101
-		$this->logAllEvents();
102
-
103
-		$logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
-		$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
105
-		$this->config->setSystemValue('loglevel', Util::DEBUG);
106
-
107
-		$wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108
-
109
-		if(!$wasMaintenanceModeEnabled) {
110
-			$this->config->setSystemValue('maintenance', true);
111
-			$this->emit('\OC\Updater', 'maintenanceEnabled');
112
-		}
113
-
114
-		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
115
-		$currentVersion = implode('.', \OCP\Util::getVersion());
116
-		$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
117
-
118
-		$success = true;
119
-		try {
120
-			$this->doUpgrade($currentVersion, $installedVersion);
121
-		} catch (HintException $exception) {
122
-			$this->log->logException($exception, ['app' => 'core']);
123
-			$this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
124
-			$success = false;
125
-		} catch (\Exception $exception) {
126
-			$this->log->logException($exception, ['app' => 'core']);
127
-			$this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
128
-			$success = false;
129
-		}
130
-
131
-		$this->emit('\OC\Updater', 'updateEnd', array($success));
132
-
133
-		if(!$wasMaintenanceModeEnabled && $success) {
134
-			$this->config->setSystemValue('maintenance', false);
135
-			$this->emit('\OC\Updater', 'maintenanceDisabled');
136
-		} else {
137
-			$this->emit('\OC\Updater', 'maintenanceActive');
138
-		}
139
-
140
-		$this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
141
-		$this->config->setSystemValue('loglevel', $logLevel);
142
-		$this->config->setSystemValue('installed', true);
143
-
144
-		return $success;
145
-	}
146
-
147
-	/**
148
-	 * Return version from which this version is allowed to upgrade from
149
-	 *
150
-	 * @return array allowed previous versions per vendor
151
-	 */
152
-	private function getAllowedPreviousVersions() {
153
-		// this should really be a JSON file
154
-		require \OC::$SERVERROOT . '/version.php';
155
-		/** @var array $OC_VersionCanBeUpgradedFrom */
156
-		return $OC_VersionCanBeUpgradedFrom;
157
-	}
158
-
159
-	/**
160
-	 * Return vendor from which this version was published
161
-	 *
162
-	 * @return string Get the vendor
163
-	 */
164
-	private function getVendor() {
165
-		// this should really be a JSON file
166
-		require \OC::$SERVERROOT . '/version.php';
167
-		/** @var string $vendor */
168
-		return (string) $vendor;
169
-	}
170
-
171
-	/**
172
-	 * Whether an upgrade to a specified version is possible
173
-	 * @param string $oldVersion
174
-	 * @param string $newVersion
175
-	 * @param array $allowedPreviousVersions
176
-	 * @return bool
177
-	 */
178
-	public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179
-		$version = explode('.', $oldVersion);
180
-		$majorMinor = $version[0] . '.' . $version[1];
181
-
182
-		$currentVendor = $this->config->getAppValue('core', 'vendor', '');
183
-
184
-		// Vendor was not set correctly on install, so we have to white-list known versions
185
-		if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) {
186
-			$currentVendor = 'owncloud';
187
-		}
188
-
189
-		if ($currentVendor === 'nextcloud') {
190
-			return isset($allowedPreviousVersions[$currentVendor][$majorMinor])
191
-				&& (version_compare($oldVersion, $newVersion, '<=') ||
192
-					$this->config->getSystemValue('debug', false));
193
-		}
194
-
195
-		// Check if the instance can be migrated
196
-		return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) ||
197
-			isset($allowedPreviousVersions[$currentVendor][$oldVersion]);
198
-	}
199
-
200
-	/**
201
-	 * runs the update actions in maintenance mode, does not upgrade the source files
202
-	 * except the main .htaccess file
203
-	 *
204
-	 * @param string $currentVersion current version to upgrade to
205
-	 * @param string $installedVersion previous version from which to upgrade from
206
-	 *
207
-	 * @throws \Exception
208
-	 */
209
-	private function doUpgrade($currentVersion, $installedVersion) {
210
-		// Stop update if the update is over several major versions
211
-		$allowedPreviousVersions = $this->getAllowedPreviousVersions();
212
-		if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) {
213
-			throw new \Exception('Updates between multiple major versions and downgrades are unsupported.');
214
-		}
215
-
216
-		// Update .htaccess files
217
-		try {
218
-			Setup::updateHtaccess();
219
-			Setup::protectDataDirectory();
220
-		} catch (\Exception $e) {
221
-			throw new \Exception($e->getMessage());
222
-		}
223
-
224
-		// create empty file in data dir, so we can later find
225
-		// out that this is indeed an ownCloud data directory
226
-		// (in case it didn't exist before)
227
-		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
228
-
229
-		// pre-upgrade repairs
230
-		$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
231
-		$repair->run();
232
-
233
-		$this->doCoreUpgrade();
234
-
235
-		try {
236
-			// TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378
237
-			Setup::installBackgroundJobs();
238
-		} catch (\Exception $e) {
239
-			throw new \Exception($e->getMessage());
240
-		}
241
-
242
-		// update all shipped apps
243
-		$this->checkAppsRequirements();
244
-		$this->doAppUpgrade();
245
-
246
-		// Update the appfetchers version so it downloads the correct list from the appstore
247
-		\OC::$server->getAppFetcher()->setVersion($currentVersion);
248
-
249
-		// upgrade appstore apps
250
-		$this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps());
251
-
252
-		// install new shipped apps on upgrade
253
-		OC_App::loadApps(['authentication']);
254
-		$errors = Installer::installShippedApps(true);
255
-		foreach ($errors as $appId => $exception) {
256
-			/** @var \Exception $exception */
257
-			$this->log->logException($exception, ['app' => $appId]);
258
-			$this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
259
-		}
260
-
261
-		// post-upgrade repairs
262
-		$repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher());
263
-		$repair->run();
264
-
265
-		//Invalidate update feed
266
-		$this->config->setAppValue('core', 'lastupdatedat', 0);
267
-
268
-		// Check for code integrity if not disabled
269
-		if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270
-			$this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271
-			$this->checker->runInstanceVerification();
272
-			$this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
273
-		}
274
-
275
-		// only set the final version if everything went well
276
-		$this->config->setSystemValue('version', implode('.', Util::getVersion()));
277
-		$this->config->setAppValue('core', 'vendor', $this->getVendor());
278
-	}
279
-
280
-	protected function doCoreUpgrade() {
281
-		$this->emit('\OC\Updater', 'dbUpgradeBefore');
282
-
283
-		// execute core migrations
284
-		$ms = new MigrationService('core', \OC::$server->getDatabaseConnection());
285
-		$ms->migrate();
286
-
287
-		$this->emit('\OC\Updater', 'dbUpgrade');
288
-	}
289
-
290
-	/**
291
-	 * @param string $version the oc version to check app compatibility with
292
-	 */
293
-	protected function checkAppUpgrade($version) {
294
-		$apps = \OC_App::getEnabledApps();
295
-		$this->emit('\OC\Updater', 'appUpgradeCheckBefore');
296
-
297
-		$appManager = \OC::$server->getAppManager();
298
-		foreach ($apps as $appId) {
299
-			$info = \OC_App::getAppInfo($appId);
300
-			$compatible = \OC_App::isAppCompatible($version, $info);
301
-			$isShipped = $appManager->isShipped($appId);
302
-
303
-			if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) {
304
-				/**
305
-				 * FIXME: The preupdate check is performed before the database migration, otherwise database changes
306
-				 * are not possible anymore within it. - Consider this when touching the code.
307
-				 * @link https://github.com/owncloud/core/issues/10980
308
-				 * @see \OC_App::updateApp
309
-				 */
310
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
311
-					$this->includePreUpdate($appId);
312
-				}
313
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
314
-					$this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
-					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
316
-				}
317
-			}
318
-		}
319
-
320
-		$this->emit('\OC\Updater', 'appUpgradeCheck');
321
-	}
322
-
323
-	/**
324
-	 * Includes the pre-update file. Done here to prevent namespace mixups.
325
-	 * @param string $appId
326
-	 */
327
-	private function includePreUpdate($appId) {
328
-		include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
329
-	}
330
-
331
-	/**
332
-	 * upgrades all apps within a major ownCloud upgrade. Also loads "priority"
333
-	 * (types authentication, filesystem, logging, in that order) afterwards.
334
-	 *
335
-	 * @throws NeedsUpdateException
336
-	 */
337
-	protected function doAppUpgrade() {
338
-		$apps = \OC_App::getEnabledApps();
339
-		$priorityTypes = array('authentication', 'filesystem', 'logging');
340
-		$pseudoOtherType = 'other';
341
-		$stacks = array($pseudoOtherType => array());
342
-
343
-		foreach ($apps as $appId) {
344
-			$priorityType = false;
345
-			foreach ($priorityTypes as $type) {
346
-				if(!isset($stacks[$type])) {
347
-					$stacks[$type] = array();
348
-				}
349
-				if (\OC_App::isType($appId, [$type])) {
350
-					$stacks[$type][] = $appId;
351
-					$priorityType = true;
352
-					break;
353
-				}
354
-			}
355
-			if (!$priorityType) {
356
-				$stacks[$pseudoOtherType][] = $appId;
357
-			}
358
-		}
359
-		foreach ($stacks as $type => $stack) {
360
-			foreach ($stack as $appId) {
361
-				if (\OC_App::shouldUpgrade($appId)) {
362
-					$this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]);
363
-					\OC_App::updateApp($appId);
364
-					$this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365
-				}
366
-				if($type !== $pseudoOtherType) {
367
-					// load authentication, filesystem and logging apps after
368
-					// upgrading them. Other apps my need to rely on modifying
369
-					// user and/or filesystem aspects.
370
-					\OC_App::loadApp($appId);
371
-				}
372
-			}
373
-		}
374
-	}
375
-
376
-	/**
377
-	 * check if the current enabled apps are compatible with the current
378
-	 * ownCloud version. disable them if not.
379
-	 * This is important if you upgrade ownCloud and have non ported 3rd
380
-	 * party apps installed.
381
-	 *
382
-	 * @return array
383
-	 * @throws \Exception
384
-	 */
385
-	private function checkAppsRequirements() {
386
-		$isCoreUpgrade = $this->isCodeUpgrade();
387
-		$apps = OC_App::getEnabledApps();
388
-		$version = implode('.', Util::getVersion());
389
-		$disabledApps = [];
390
-		$appManager = \OC::$server->getAppManager();
391
-		foreach ($apps as $app) {
392
-			// check if the app is compatible with this version of ownCloud
393
-			$info = OC_App::getAppInfo($app);
394
-			if($info === null || !OC_App::isAppCompatible($version, $info)) {
395
-				if ($appManager->isShipped($app)) {
396
-					throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
397
-				}
398
-				\OC::$server->getAppManager()->disableApp($app);
399
-				$this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
400
-			}
401
-			// no need to disable any app in case this is a non-core upgrade
402
-			if (!$isCoreUpgrade) {
403
-				continue;
404
-			}
405
-			// shipped apps will remain enabled
406
-			if ($appManager->isShipped($app)) {
407
-				continue;
408
-			}
409
-			// authentication and session apps will remain enabled as well
410
-			if (OC_App::isType($app, ['session', 'authentication'])) {
411
-				continue;
412
-			}
413
-		}
414
-		return $disabledApps;
415
-	}
416
-
417
-	/**
418
-	 * @return bool
419
-	 */
420
-	private function isCodeUpgrade() {
421
-		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
422
-		$currentVersion = implode('.', Util::getVersion());
423
-		if (version_compare($currentVersion, $installedVersion, '>')) {
424
-			return true;
425
-		}
426
-		return false;
427
-	}
428
-
429
-	/**
430
-	 * @param array $disabledApps
431
-	 * @throws \Exception
432
-	 */
433
-	private function upgradeAppStoreApps(array $disabledApps) {
434
-		foreach($disabledApps as $app) {
435
-			try {
436
-				$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437
-				if ($this->installer->isUpdateAvailable($app)) {
438
-					$this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
439
-					$this->installer->updateAppstoreApp($app);
440
-				}
441
-				$this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
442
-			} catch (\Exception $ex) {
443
-				$this->log->logException($ex, ['app' => 'core']);
444
-			}
445
-		}
446
-	}
447
-
448
-	/**
449
-	 * Forward messages emitted by the repair routine
450
-	 */
451
-	private function emitRepairEvents() {
452
-		$dispatcher = \OC::$server->getEventDispatcher();
453
-		$dispatcher->addListener('\OC\Repair::warning', function ($event) {
454
-			if ($event instanceof GenericEvent) {
455
-				$this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456
-			}
457
-		});
458
-		$dispatcher->addListener('\OC\Repair::error', function ($event) {
459
-			if ($event instanceof GenericEvent) {
460
-				$this->emit('\OC\Updater', 'repairError', $event->getArguments());
461
-			}
462
-		});
463
-		$dispatcher->addListener('\OC\Repair::info', function ($event) {
464
-			if ($event instanceof GenericEvent) {
465
-				$this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466
-			}
467
-		});
468
-		$dispatcher->addListener('\OC\Repair::step', function ($event) {
469
-			if ($event instanceof GenericEvent) {
470
-				$this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471
-			}
472
-		});
473
-	}
474
-
475
-	private function logAllEvents() {
476
-		$log = $this->log;
477
-
478
-		$dispatcher = \OC::$server->getEventDispatcher();
479
-		$dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) {
480
-			if (!$event instanceof GenericEvent) {
481
-				return;
482
-			}
483
-			$log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
484
-		});
485
-		$dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486
-			if (!$event instanceof GenericEvent) {
487
-				return;
488
-			}
489
-			$log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
490
-		});
491
-
492
-		$repairListener = function($event) use ($log) {
493
-			if (!$event instanceof GenericEvent) {
494
-				return;
495
-			}
496
-			switch ($event->getSubject()) {
497
-				case '\OC\Repair::startProgress':
498
-					$log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
499
-					break;
500
-				case '\OC\Repair::advance':
501
-					$desc = $event->getArgument(1);
502
-					if (empty($desc)) {
503
-						$desc = '';
504
-					}
505
-					$log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
506
-
507
-					break;
508
-				case '\OC\Repair::finishProgress':
509
-					$log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510
-					break;
511
-				case '\OC\Repair::step':
512
-					$log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
513
-					break;
514
-				case '\OC\Repair::info':
515
-					$log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
516
-					break;
517
-				case '\OC\Repair::warning':
518
-					$log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
519
-					break;
520
-				case '\OC\Repair::error':
521
-					$log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
522
-					break;
523
-			}
524
-		};
525
-
526
-		$dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
527
-		$dispatcher->addListener('\OC\Repair::advance', $repairListener);
528
-		$dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
529
-		$dispatcher->addListener('\OC\Repair::step', $repairListener);
530
-		$dispatcher->addListener('\OC\Repair::info', $repairListener);
531
-		$dispatcher->addListener('\OC\Repair::warning', $repairListener);
532
-		$dispatcher->addListener('\OC\Repair::error', $repairListener);
533
-
534
-
535
-		$this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
536
-			$log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537
-		});
538
-		$this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
539
-			$log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540
-		});
541
-		$this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
542
-			$log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543
-		});
544
-		$this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
545
-			if ($success) {
546
-				$log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547
-			} else {
548
-				$log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549
-			}
550
-		});
551
-		$this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
552
-			$log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553
-		});
554
-		$this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
555
-			$log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556
-		});
557
-		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
558
-			$log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559
-		});
560
-		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
561
-			$log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562
-		});
563
-		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
-			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
565
-		});
566
-		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
-			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
568
-		});
569
-		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
-			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
571
-		});
572
-		$this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
-			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
574
-		});
575
-		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
576
-			$log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577
-		});
578
-		$this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
-			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580
-		});
581
-		$this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
582
-			$log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583
-		});
584
-		$this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
-			$log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
586
-		});
587
-		$this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
-			$log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
589
-		});
590
-		$this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
-			$log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
592
-		});
593
-		$this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
594
-			$log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595
-		});
596
-		$this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
-			$log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
598
-		});
599
-		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
600
-			$log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601
-		});
602
-		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
603
-			$log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604
-		});
605
-
606
-	}
57
+    /** @var ILogger $log */
58
+    private $log;
59
+
60
+    /** @var IConfig */
61
+    private $config;
62
+
63
+    /** @var Checker */
64
+    private $checker;
65
+
66
+    /** @var Installer */
67
+    private $installer;
68
+
69
+    private $logLevelNames = [
70
+        0 => 'Debug',
71
+        1 => 'Info',
72
+        2 => 'Warning',
73
+        3 => 'Error',
74
+        4 => 'Fatal',
75
+    ];
76
+
77
+    /**
78
+     * @param IConfig $config
79
+     * @param Checker $checker
80
+     * @param ILogger $log
81
+     * @param Installer $installer
82
+     */
83
+    public function __construct(IConfig $config,
84
+                                Checker $checker,
85
+                                ILogger $log = null,
86
+                                Installer $installer) {
87
+        $this->log = $log;
88
+        $this->config = $config;
89
+        $this->checker = $checker;
90
+        $this->installer = $installer;
91
+    }
92
+
93
+    /**
94
+     * runs the update actions in maintenance mode, does not upgrade the source files
95
+     * except the main .htaccess file
96
+     *
97
+     * @return bool true if the operation succeeded, false otherwise
98
+     */
99
+    public function upgrade() {
100
+        $this->emitRepairEvents();
101
+        $this->logAllEvents();
102
+
103
+        $logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
+        $this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
105
+        $this->config->setSystemValue('loglevel', Util::DEBUG);
106
+
107
+        $wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108
+
109
+        if(!$wasMaintenanceModeEnabled) {
110
+            $this->config->setSystemValue('maintenance', true);
111
+            $this->emit('\OC\Updater', 'maintenanceEnabled');
112
+        }
113
+
114
+        $installedVersion = $this->config->getSystemValue('version', '0.0.0');
115
+        $currentVersion = implode('.', \OCP\Util::getVersion());
116
+        $this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
117
+
118
+        $success = true;
119
+        try {
120
+            $this->doUpgrade($currentVersion, $installedVersion);
121
+        } catch (HintException $exception) {
122
+            $this->log->logException($exception, ['app' => 'core']);
123
+            $this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
124
+            $success = false;
125
+        } catch (\Exception $exception) {
126
+            $this->log->logException($exception, ['app' => 'core']);
127
+            $this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
128
+            $success = false;
129
+        }
130
+
131
+        $this->emit('\OC\Updater', 'updateEnd', array($success));
132
+
133
+        if(!$wasMaintenanceModeEnabled && $success) {
134
+            $this->config->setSystemValue('maintenance', false);
135
+            $this->emit('\OC\Updater', 'maintenanceDisabled');
136
+        } else {
137
+            $this->emit('\OC\Updater', 'maintenanceActive');
138
+        }
139
+
140
+        $this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
141
+        $this->config->setSystemValue('loglevel', $logLevel);
142
+        $this->config->setSystemValue('installed', true);
143
+
144
+        return $success;
145
+    }
146
+
147
+    /**
148
+     * Return version from which this version is allowed to upgrade from
149
+     *
150
+     * @return array allowed previous versions per vendor
151
+     */
152
+    private function getAllowedPreviousVersions() {
153
+        // this should really be a JSON file
154
+        require \OC::$SERVERROOT . '/version.php';
155
+        /** @var array $OC_VersionCanBeUpgradedFrom */
156
+        return $OC_VersionCanBeUpgradedFrom;
157
+    }
158
+
159
+    /**
160
+     * Return vendor from which this version was published
161
+     *
162
+     * @return string Get the vendor
163
+     */
164
+    private function getVendor() {
165
+        // this should really be a JSON file
166
+        require \OC::$SERVERROOT . '/version.php';
167
+        /** @var string $vendor */
168
+        return (string) $vendor;
169
+    }
170
+
171
+    /**
172
+     * Whether an upgrade to a specified version is possible
173
+     * @param string $oldVersion
174
+     * @param string $newVersion
175
+     * @param array $allowedPreviousVersions
176
+     * @return bool
177
+     */
178
+    public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179
+        $version = explode('.', $oldVersion);
180
+        $majorMinor = $version[0] . '.' . $version[1];
181
+
182
+        $currentVendor = $this->config->getAppValue('core', 'vendor', '');
183
+
184
+        // Vendor was not set correctly on install, so we have to white-list known versions
185
+        if ($currentVendor === '' && isset($allowedPreviousVersions['owncloud'][$oldVersion])) {
186
+            $currentVendor = 'owncloud';
187
+        }
188
+
189
+        if ($currentVendor === 'nextcloud') {
190
+            return isset($allowedPreviousVersions[$currentVendor][$majorMinor])
191
+                && (version_compare($oldVersion, $newVersion, '<=') ||
192
+                    $this->config->getSystemValue('debug', false));
193
+        }
194
+
195
+        // Check if the instance can be migrated
196
+        return isset($allowedPreviousVersions[$currentVendor][$majorMinor]) ||
197
+            isset($allowedPreviousVersions[$currentVendor][$oldVersion]);
198
+    }
199
+
200
+    /**
201
+     * runs the update actions in maintenance mode, does not upgrade the source files
202
+     * except the main .htaccess file
203
+     *
204
+     * @param string $currentVersion current version to upgrade to
205
+     * @param string $installedVersion previous version from which to upgrade from
206
+     *
207
+     * @throws \Exception
208
+     */
209
+    private function doUpgrade($currentVersion, $installedVersion) {
210
+        // Stop update if the update is over several major versions
211
+        $allowedPreviousVersions = $this->getAllowedPreviousVersions();
212
+        if (!$this->isUpgradePossible($installedVersion, $currentVersion, $allowedPreviousVersions)) {
213
+            throw new \Exception('Updates between multiple major versions and downgrades are unsupported.');
214
+        }
215
+
216
+        // Update .htaccess files
217
+        try {
218
+            Setup::updateHtaccess();
219
+            Setup::protectDataDirectory();
220
+        } catch (\Exception $e) {
221
+            throw new \Exception($e->getMessage());
222
+        }
223
+
224
+        // create empty file in data dir, so we can later find
225
+        // out that this is indeed an ownCloud data directory
226
+        // (in case it didn't exist before)
227
+        file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
228
+
229
+        // pre-upgrade repairs
230
+        $repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
231
+        $repair->run();
232
+
233
+        $this->doCoreUpgrade();
234
+
235
+        try {
236
+            // TODO: replace with the new repair step mechanism https://github.com/owncloud/core/pull/24378
237
+            Setup::installBackgroundJobs();
238
+        } catch (\Exception $e) {
239
+            throw new \Exception($e->getMessage());
240
+        }
241
+
242
+        // update all shipped apps
243
+        $this->checkAppsRequirements();
244
+        $this->doAppUpgrade();
245
+
246
+        // Update the appfetchers version so it downloads the correct list from the appstore
247
+        \OC::$server->getAppFetcher()->setVersion($currentVersion);
248
+
249
+        // upgrade appstore apps
250
+        $this->upgradeAppStoreApps(\OC::$server->getAppManager()->getInstalledApps());
251
+
252
+        // install new shipped apps on upgrade
253
+        OC_App::loadApps(['authentication']);
254
+        $errors = Installer::installShippedApps(true);
255
+        foreach ($errors as $appId => $exception) {
256
+            /** @var \Exception $exception */
257
+            $this->log->logException($exception, ['app' => $appId]);
258
+            $this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
259
+        }
260
+
261
+        // post-upgrade repairs
262
+        $repair = new Repair(Repair::getRepairSteps(), \OC::$server->getEventDispatcher());
263
+        $repair->run();
264
+
265
+        //Invalidate update feed
266
+        $this->config->setAppValue('core', 'lastupdatedat', 0);
267
+
268
+        // Check for code integrity if not disabled
269
+        if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270
+            $this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271
+            $this->checker->runInstanceVerification();
272
+            $this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
273
+        }
274
+
275
+        // only set the final version if everything went well
276
+        $this->config->setSystemValue('version', implode('.', Util::getVersion()));
277
+        $this->config->setAppValue('core', 'vendor', $this->getVendor());
278
+    }
279
+
280
+    protected function doCoreUpgrade() {
281
+        $this->emit('\OC\Updater', 'dbUpgradeBefore');
282
+
283
+        // execute core migrations
284
+        $ms = new MigrationService('core', \OC::$server->getDatabaseConnection());
285
+        $ms->migrate();
286
+
287
+        $this->emit('\OC\Updater', 'dbUpgrade');
288
+    }
289
+
290
+    /**
291
+     * @param string $version the oc version to check app compatibility with
292
+     */
293
+    protected function checkAppUpgrade($version) {
294
+        $apps = \OC_App::getEnabledApps();
295
+        $this->emit('\OC\Updater', 'appUpgradeCheckBefore');
296
+
297
+        $appManager = \OC::$server->getAppManager();
298
+        foreach ($apps as $appId) {
299
+            $info = \OC_App::getAppInfo($appId);
300
+            $compatible = \OC_App::isAppCompatible($version, $info);
301
+            $isShipped = $appManager->isShipped($appId);
302
+
303
+            if ($compatible && $isShipped && \OC_App::shouldUpgrade($appId)) {
304
+                /**
305
+                 * FIXME: The preupdate check is performed before the database migration, otherwise database changes
306
+                 * are not possible anymore within it. - Consider this when touching the code.
307
+                 * @link https://github.com/owncloud/core/issues/10980
308
+                 * @see \OC_App::updateApp
309
+                 */
310
+                if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
311
+                    $this->includePreUpdate($appId);
312
+                }
313
+                if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
314
+                    $this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
+                    \OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
316
+                }
317
+            }
318
+        }
319
+
320
+        $this->emit('\OC\Updater', 'appUpgradeCheck');
321
+    }
322
+
323
+    /**
324
+     * Includes the pre-update file. Done here to prevent namespace mixups.
325
+     * @param string $appId
326
+     */
327
+    private function includePreUpdate($appId) {
328
+        include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
329
+    }
330
+
331
+    /**
332
+     * upgrades all apps within a major ownCloud upgrade. Also loads "priority"
333
+     * (types authentication, filesystem, logging, in that order) afterwards.
334
+     *
335
+     * @throws NeedsUpdateException
336
+     */
337
+    protected function doAppUpgrade() {
338
+        $apps = \OC_App::getEnabledApps();
339
+        $priorityTypes = array('authentication', 'filesystem', 'logging');
340
+        $pseudoOtherType = 'other';
341
+        $stacks = array($pseudoOtherType => array());
342
+
343
+        foreach ($apps as $appId) {
344
+            $priorityType = false;
345
+            foreach ($priorityTypes as $type) {
346
+                if(!isset($stacks[$type])) {
347
+                    $stacks[$type] = array();
348
+                }
349
+                if (\OC_App::isType($appId, [$type])) {
350
+                    $stacks[$type][] = $appId;
351
+                    $priorityType = true;
352
+                    break;
353
+                }
354
+            }
355
+            if (!$priorityType) {
356
+                $stacks[$pseudoOtherType][] = $appId;
357
+            }
358
+        }
359
+        foreach ($stacks as $type => $stack) {
360
+            foreach ($stack as $appId) {
361
+                if (\OC_App::shouldUpgrade($appId)) {
362
+                    $this->emit('\OC\Updater', 'appUpgradeStarted', [$appId, \OC_App::getAppVersion($appId)]);
363
+                    \OC_App::updateApp($appId);
364
+                    $this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365
+                }
366
+                if($type !== $pseudoOtherType) {
367
+                    // load authentication, filesystem and logging apps after
368
+                    // upgrading them. Other apps my need to rely on modifying
369
+                    // user and/or filesystem aspects.
370
+                    \OC_App::loadApp($appId);
371
+                }
372
+            }
373
+        }
374
+    }
375
+
376
+    /**
377
+     * check if the current enabled apps are compatible with the current
378
+     * ownCloud version. disable them if not.
379
+     * This is important if you upgrade ownCloud and have non ported 3rd
380
+     * party apps installed.
381
+     *
382
+     * @return array
383
+     * @throws \Exception
384
+     */
385
+    private function checkAppsRequirements() {
386
+        $isCoreUpgrade = $this->isCodeUpgrade();
387
+        $apps = OC_App::getEnabledApps();
388
+        $version = implode('.', Util::getVersion());
389
+        $disabledApps = [];
390
+        $appManager = \OC::$server->getAppManager();
391
+        foreach ($apps as $app) {
392
+            // check if the app is compatible with this version of ownCloud
393
+            $info = OC_App::getAppInfo($app);
394
+            if($info === null || !OC_App::isAppCompatible($version, $info)) {
395
+                if ($appManager->isShipped($app)) {
396
+                    throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
397
+                }
398
+                \OC::$server->getAppManager()->disableApp($app);
399
+                $this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
400
+            }
401
+            // no need to disable any app in case this is a non-core upgrade
402
+            if (!$isCoreUpgrade) {
403
+                continue;
404
+            }
405
+            // shipped apps will remain enabled
406
+            if ($appManager->isShipped($app)) {
407
+                continue;
408
+            }
409
+            // authentication and session apps will remain enabled as well
410
+            if (OC_App::isType($app, ['session', 'authentication'])) {
411
+                continue;
412
+            }
413
+        }
414
+        return $disabledApps;
415
+    }
416
+
417
+    /**
418
+     * @return bool
419
+     */
420
+    private function isCodeUpgrade() {
421
+        $installedVersion = $this->config->getSystemValue('version', '0.0.0');
422
+        $currentVersion = implode('.', Util::getVersion());
423
+        if (version_compare($currentVersion, $installedVersion, '>')) {
424
+            return true;
425
+        }
426
+        return false;
427
+    }
428
+
429
+    /**
430
+     * @param array $disabledApps
431
+     * @throws \Exception
432
+     */
433
+    private function upgradeAppStoreApps(array $disabledApps) {
434
+        foreach($disabledApps as $app) {
435
+            try {
436
+                $this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437
+                if ($this->installer->isUpdateAvailable($app)) {
438
+                    $this->emit('\OC\Updater', 'upgradeAppStoreApp', [$app]);
439
+                    $this->installer->updateAppstoreApp($app);
440
+                }
441
+                $this->emit('\OC\Updater', 'checkAppStoreApp', [$app]);
442
+            } catch (\Exception $ex) {
443
+                $this->log->logException($ex, ['app' => 'core']);
444
+            }
445
+        }
446
+    }
447
+
448
+    /**
449
+     * Forward messages emitted by the repair routine
450
+     */
451
+    private function emitRepairEvents() {
452
+        $dispatcher = \OC::$server->getEventDispatcher();
453
+        $dispatcher->addListener('\OC\Repair::warning', function ($event) {
454
+            if ($event instanceof GenericEvent) {
455
+                $this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456
+            }
457
+        });
458
+        $dispatcher->addListener('\OC\Repair::error', function ($event) {
459
+            if ($event instanceof GenericEvent) {
460
+                $this->emit('\OC\Updater', 'repairError', $event->getArguments());
461
+            }
462
+        });
463
+        $dispatcher->addListener('\OC\Repair::info', function ($event) {
464
+            if ($event instanceof GenericEvent) {
465
+                $this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466
+            }
467
+        });
468
+        $dispatcher->addListener('\OC\Repair::step', function ($event) {
469
+            if ($event instanceof GenericEvent) {
470
+                $this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471
+            }
472
+        });
473
+    }
474
+
475
+    private function logAllEvents() {
476
+        $log = $this->log;
477
+
478
+        $dispatcher = \OC::$server->getEventDispatcher();
479
+        $dispatcher->addListener('\OC\DB\Migrator::executeSql', function($event) use ($log) {
480
+            if (!$event instanceof GenericEvent) {
481
+                return;
482
+            }
483
+            $log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
484
+        });
485
+        $dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486
+            if (!$event instanceof GenericEvent) {
487
+                return;
488
+            }
489
+            $log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
490
+        });
491
+
492
+        $repairListener = function($event) use ($log) {
493
+            if (!$event instanceof GenericEvent) {
494
+                return;
495
+            }
496
+            switch ($event->getSubject()) {
497
+                case '\OC\Repair::startProgress':
498
+                    $log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
499
+                    break;
500
+                case '\OC\Repair::advance':
501
+                    $desc = $event->getArgument(1);
502
+                    if (empty($desc)) {
503
+                        $desc = '';
504
+                    }
505
+                    $log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
506
+
507
+                    break;
508
+                case '\OC\Repair::finishProgress':
509
+                    $log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510
+                    break;
511
+                case '\OC\Repair::step':
512
+                    $log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
513
+                    break;
514
+                case '\OC\Repair::info':
515
+                    $log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
516
+                    break;
517
+                case '\OC\Repair::warning':
518
+                    $log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
519
+                    break;
520
+                case '\OC\Repair::error':
521
+                    $log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
522
+                    break;
523
+            }
524
+        };
525
+
526
+        $dispatcher->addListener('\OC\Repair::startProgress', $repairListener);
527
+        $dispatcher->addListener('\OC\Repair::advance', $repairListener);
528
+        $dispatcher->addListener('\OC\Repair::finishProgress', $repairListener);
529
+        $dispatcher->addListener('\OC\Repair::step', $repairListener);
530
+        $dispatcher->addListener('\OC\Repair::info', $repairListener);
531
+        $dispatcher->addListener('\OC\Repair::warning', $repairListener);
532
+        $dispatcher->addListener('\OC\Repair::error', $repairListener);
533
+
534
+
535
+        $this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
536
+            $log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537
+        });
538
+        $this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
539
+            $log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540
+        });
541
+        $this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
542
+            $log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543
+        });
544
+        $this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
545
+            if ($success) {
546
+                $log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547
+            } else {
548
+                $log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549
+            }
550
+        });
551
+        $this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
552
+            $log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553
+        });
554
+        $this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
555
+            $log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556
+        });
557
+        $this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
558
+            $log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559
+        });
560
+        $this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
561
+            $log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562
+        });
563
+        $this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
+            $log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
565
+        });
566
+        $this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
+            $log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
568
+        });
569
+        $this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
+            $log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
571
+        });
572
+        $this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
+            $log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
574
+        });
575
+        $this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
576
+            $log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577
+        });
578
+        $this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
+            $log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580
+        });
581
+        $this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
582
+            $log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583
+        });
584
+        $this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
+            $log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
586
+        });
587
+        $this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
+            $log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
589
+        });
590
+        $this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
+            $log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
592
+        });
593
+        $this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
594
+            $log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595
+        });
596
+        $this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
+            $log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
598
+        });
599
+        $this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
600
+            $log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601
+        });
602
+        $this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
603
+            $log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604
+        });
605
+
606
+    }
607 607
 
608 608
 }
609 609
 
Please login to merge, or discard this patch.
Spacing   +65 added lines, -65 removed lines patch added patch discarded remove patch
@@ -101,43 +101,43 @@  discard block
 block discarded – undo
101 101
 		$this->logAllEvents();
102 102
 
103 103
 		$logLevel = $this->config->getSystemValue('loglevel', Util::WARN);
104
-		$this->emit('\OC\Updater', 'setDebugLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
104
+		$this->emit('\OC\Updater', 'setDebugLogLevel', [$logLevel, $this->logLevelNames[$logLevel]]);
105 105
 		$this->config->setSystemValue('loglevel', Util::DEBUG);
106 106
 
107 107
 		$wasMaintenanceModeEnabled = $this->config->getSystemValue('maintenance', false);
108 108
 
109
-		if(!$wasMaintenanceModeEnabled) {
109
+		if (!$wasMaintenanceModeEnabled) {
110 110
 			$this->config->setSystemValue('maintenance', true);
111 111
 			$this->emit('\OC\Updater', 'maintenanceEnabled');
112 112
 		}
113 113
 
114 114
 		$installedVersion = $this->config->getSystemValue('version', '0.0.0');
115 115
 		$currentVersion = implode('.', \OCP\Util::getVersion());
116
-		$this->log->debug('starting upgrade from ' . $installedVersion . ' to ' . $currentVersion, array('app' => 'core'));
116
+		$this->log->debug('starting upgrade from '.$installedVersion.' to '.$currentVersion, array('app' => 'core'));
117 117
 
118 118
 		$success = true;
119 119
 		try {
120 120
 			$this->doUpgrade($currentVersion, $installedVersion);
121 121
 		} catch (HintException $exception) {
122 122
 			$this->log->logException($exception, ['app' => 'core']);
123
-			$this->emit('\OC\Updater', 'failure', array($exception->getMessage() . ': ' .$exception->getHint()));
123
+			$this->emit('\OC\Updater', 'failure', array($exception->getMessage().': '.$exception->getHint()));
124 124
 			$success = false;
125 125
 		} catch (\Exception $exception) {
126 126
 			$this->log->logException($exception, ['app' => 'core']);
127
-			$this->emit('\OC\Updater', 'failure', array(get_class($exception) . ': ' .$exception->getMessage()));
127
+			$this->emit('\OC\Updater', 'failure', array(get_class($exception).': '.$exception->getMessage()));
128 128
 			$success = false;
129 129
 		}
130 130
 
131 131
 		$this->emit('\OC\Updater', 'updateEnd', array($success));
132 132
 
133
-		if(!$wasMaintenanceModeEnabled && $success) {
133
+		if (!$wasMaintenanceModeEnabled && $success) {
134 134
 			$this->config->setSystemValue('maintenance', false);
135 135
 			$this->emit('\OC\Updater', 'maintenanceDisabled');
136 136
 		} else {
137 137
 			$this->emit('\OC\Updater', 'maintenanceActive');
138 138
 		}
139 139
 
140
-		$this->emit('\OC\Updater', 'resetLogLevel', [ $logLevel, $this->logLevelNames[$logLevel] ]);
140
+		$this->emit('\OC\Updater', 'resetLogLevel', [$logLevel, $this->logLevelNames[$logLevel]]);
141 141
 		$this->config->setSystemValue('loglevel', $logLevel);
142 142
 		$this->config->setSystemValue('installed', true);
143 143
 
@@ -151,7 +151,7 @@  discard block
 block discarded – undo
151 151
 	 */
152 152
 	private function getAllowedPreviousVersions() {
153 153
 		// this should really be a JSON file
154
-		require \OC::$SERVERROOT . '/version.php';
154
+		require \OC::$SERVERROOT.'/version.php';
155 155
 		/** @var array $OC_VersionCanBeUpgradedFrom */
156 156
 		return $OC_VersionCanBeUpgradedFrom;
157 157
 	}
@@ -163,7 +163,7 @@  discard block
 block discarded – undo
163 163
 	 */
164 164
 	private function getVendor() {
165 165
 		// this should really be a JSON file
166
-		require \OC::$SERVERROOT . '/version.php';
166
+		require \OC::$SERVERROOT.'/version.php';
167 167
 		/** @var string $vendor */
168 168
 		return (string) $vendor;
169 169
 	}
@@ -177,7 +177,7 @@  discard block
 block discarded – undo
177 177
 	 */
178 178
 	public function isUpgradePossible($oldVersion, $newVersion, array $allowedPreviousVersions) {
179 179
 		$version = explode('.', $oldVersion);
180
-		$majorMinor = $version[0] . '.' . $version[1];
180
+		$majorMinor = $version[0].'.'.$version[1];
181 181
 
182 182
 		$currentVendor = $this->config->getAppValue('core', 'vendor', '');
183 183
 
@@ -224,7 +224,7 @@  discard block
 block discarded – undo
224 224
 		// create empty file in data dir, so we can later find
225 225
 		// out that this is indeed an ownCloud data directory
226 226
 		// (in case it didn't exist before)
227
-		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT . '/data') . '/.ocdata', '');
227
+		file_put_contents($this->config->getSystemValue('datadirectory', \OC::$SERVERROOT.'/data').'/.ocdata', '');
228 228
 
229 229
 		// pre-upgrade repairs
230 230
 		$repair = new Repair(Repair::getBeforeUpgradeRepairSteps(), \OC::$server->getEventDispatcher());
@@ -255,7 +255,7 @@  discard block
 block discarded – undo
255 255
 		foreach ($errors as $appId => $exception) {
256 256
 			/** @var \Exception $exception */
257 257
 			$this->log->logException($exception, ['app' => $appId]);
258
-			$this->emit('\OC\Updater', 'failure', [$appId . ': ' . $exception->getMessage()]);
258
+			$this->emit('\OC\Updater', 'failure', [$appId.': '.$exception->getMessage()]);
259 259
 		}
260 260
 
261 261
 		// post-upgrade repairs
@@ -266,7 +266,7 @@  discard block
 block discarded – undo
266 266
 		$this->config->setAppValue('core', 'lastupdatedat', 0);
267 267
 
268 268
 		// Check for code integrity if not disabled
269
-		if(\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
269
+		if (\OC::$server->getIntegrityCodeChecker()->isCodeCheckEnforced()) {
270 270
 			$this->emit('\OC\Updater', 'startCheckCodeIntegrity');
271 271
 			$this->checker->runInstanceVerification();
272 272
 			$this->emit('\OC\Updater', 'finishedCheckCodeIntegrity');
@@ -307,12 +307,12 @@  discard block
 block discarded – undo
307 307
 				 * @link https://github.com/owncloud/core/issues/10980
308 308
 				 * @see \OC_App::updateApp
309 309
 				 */
310
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/preupdate.php')) {
310
+				if (file_exists(\OC_App::getAppPath($appId).'/appinfo/preupdate.php')) {
311 311
 					$this->includePreUpdate($appId);
312 312
 				}
313
-				if (file_exists(\OC_App::getAppPath($appId) . '/appinfo/database.xml')) {
313
+				if (file_exists(\OC_App::getAppPath($appId).'/appinfo/database.xml')) {
314 314
 					$this->emit('\OC\Updater', 'appSimulateUpdate', array($appId));
315
-					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId) . '/appinfo/database.xml');
315
+					\OC_DB::simulateUpdateDbFromStructure(\OC_App::getAppPath($appId).'/appinfo/database.xml');
316 316
 				}
317 317
 			}
318 318
 		}
@@ -325,7 +325,7 @@  discard block
 block discarded – undo
325 325
 	 * @param string $appId
326 326
 	 */
327 327
 	private function includePreUpdate($appId) {
328
-		include \OC_App::getAppPath($appId) . '/appinfo/preupdate.php';
328
+		include \OC_App::getAppPath($appId).'/appinfo/preupdate.php';
329 329
 	}
330 330
 
331 331
 	/**
@@ -343,7 +343,7 @@  discard block
 block discarded – undo
343 343
 		foreach ($apps as $appId) {
344 344
 			$priorityType = false;
345 345
 			foreach ($priorityTypes as $type) {
346
-				if(!isset($stacks[$type])) {
346
+				if (!isset($stacks[$type])) {
347 347
 					$stacks[$type] = array();
348 348
 				}
349 349
 				if (\OC_App::isType($appId, [$type])) {
@@ -363,7 +363,7 @@  discard block
 block discarded – undo
363 363
 					\OC_App::updateApp($appId);
364 364
 					$this->emit('\OC\Updater', 'appUpgrade', [$appId, \OC_App::getAppVersion($appId)]);
365 365
 				}
366
-				if($type !== $pseudoOtherType) {
366
+				if ($type !== $pseudoOtherType) {
367 367
 					// load authentication, filesystem and logging apps after
368 368
 					// upgrading them. Other apps my need to rely on modifying
369 369
 					// user and/or filesystem aspects.
@@ -391,9 +391,9 @@  discard block
 block discarded – undo
391 391
 		foreach ($apps as $app) {
392 392
 			// check if the app is compatible with this version of ownCloud
393 393
 			$info = OC_App::getAppInfo($app);
394
-			if($info === null || !OC_App::isAppCompatible($version, $info)) {
394
+			if ($info === null || !OC_App::isAppCompatible($version, $info)) {
395 395
 				if ($appManager->isShipped($app)) {
396
-					throw new \UnexpectedValueException('The files of the app "' . $app . '" were not correctly replaced before running the update');
396
+					throw new \UnexpectedValueException('The files of the app "'.$app.'" were not correctly replaced before running the update');
397 397
 				}
398 398
 				\OC::$server->getAppManager()->disableApp($app);
399 399
 				$this->emit('\OC\Updater', 'incompatibleAppDisabled', array($app));
@@ -431,7 +431,7 @@  discard block
 block discarded – undo
431 431
 	 * @throws \Exception
432 432
 	 */
433 433
 	private function upgradeAppStoreApps(array $disabledApps) {
434
-		foreach($disabledApps as $app) {
434
+		foreach ($disabledApps as $app) {
435 435
 			try {
436 436
 				$this->emit('\OC\Updater', 'checkAppStoreAppBefore', [$app]);
437 437
 				if ($this->installer->isUpdateAvailable($app)) {
@@ -450,22 +450,22 @@  discard block
 block discarded – undo
450 450
 	 */
451 451
 	private function emitRepairEvents() {
452 452
 		$dispatcher = \OC::$server->getEventDispatcher();
453
-		$dispatcher->addListener('\OC\Repair::warning', function ($event) {
453
+		$dispatcher->addListener('\OC\Repair::warning', function($event) {
454 454
 			if ($event instanceof GenericEvent) {
455 455
 				$this->emit('\OC\Updater', 'repairWarning', $event->getArguments());
456 456
 			}
457 457
 		});
458
-		$dispatcher->addListener('\OC\Repair::error', function ($event) {
458
+		$dispatcher->addListener('\OC\Repair::error', function($event) {
459 459
 			if ($event instanceof GenericEvent) {
460 460
 				$this->emit('\OC\Updater', 'repairError', $event->getArguments());
461 461
 			}
462 462
 		});
463
-		$dispatcher->addListener('\OC\Repair::info', function ($event) {
463
+		$dispatcher->addListener('\OC\Repair::info', function($event) {
464 464
 			if ($event instanceof GenericEvent) {
465 465
 				$this->emit('\OC\Updater', 'repairInfo', $event->getArguments());
466 466
 			}
467 467
 		});
468
-		$dispatcher->addListener('\OC\Repair::step', function ($event) {
468
+		$dispatcher->addListener('\OC\Repair::step', function($event) {
469 469
 			if ($event instanceof GenericEvent) {
470 470
 				$this->emit('\OC\Updater', 'repairStep', $event->getArguments());
471 471
 			}
@@ -480,13 +480,13 @@  discard block
 block discarded – undo
480 480
 			if (!$event instanceof GenericEvent) {
481 481
 				return;
482 482
 			}
483
-			$log->info('\OC\DB\Migrator::executeSql: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
483
+			$log->info('\OC\DB\Migrator::executeSql: '.$event->getSubject().' ('.$event->getArgument(0).' of '.$event->getArgument(1).')', ['app' => 'updater']);
484 484
 		});
485 485
 		$dispatcher->addListener('\OC\DB\Migrator::checkTable', function($event) use ($log) {
486 486
 			if (!$event instanceof GenericEvent) {
487 487
 				return;
488 488
 			}
489
-			$log->info('\OC\DB\Migrator::checkTable: ' . $event->getSubject() . ' (' . $event->getArgument(0) . ' of ' . $event->getArgument(1) . ')', ['app' => 'updater']);
489
+			$log->info('\OC\DB\Migrator::checkTable: '.$event->getSubject().' ('.$event->getArgument(0).' of '.$event->getArgument(1).')', ['app' => 'updater']);
490 490
 		});
491 491
 
492 492
 		$repairListener = function($event) use ($log) {
@@ -495,30 +495,30 @@  discard block
 block discarded – undo
495 495
 			}
496 496
 			switch ($event->getSubject()) {
497 497
 				case '\OC\Repair::startProgress':
498
-					$log->info('\OC\Repair::startProgress: Starting ... ' . $event->getArgument(1) .  ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
498
+					$log->info('\OC\Repair::startProgress: Starting ... '.$event->getArgument(1).' ('.$event->getArgument(0).')', ['app' => 'updater']);
499 499
 					break;
500 500
 				case '\OC\Repair::advance':
501 501
 					$desc = $event->getArgument(1);
502 502
 					if (empty($desc)) {
503 503
 						$desc = '';
504 504
 					}
505
-					$log->info('\OC\Repair::advance: ' . $desc . ' (' . $event->getArgument(0) . ')', ['app' => 'updater']);
505
+					$log->info('\OC\Repair::advance: '.$desc.' ('.$event->getArgument(0).')', ['app' => 'updater']);
506 506
 
507 507
 					break;
508 508
 				case '\OC\Repair::finishProgress':
509 509
 					$log->info('\OC\Repair::finishProgress', ['app' => 'updater']);
510 510
 					break;
511 511
 				case '\OC\Repair::step':
512
-					$log->info('\OC\Repair::step: Repair step: ' . $event->getArgument(0), ['app' => 'updater']);
512
+					$log->info('\OC\Repair::step: Repair step: '.$event->getArgument(0), ['app' => 'updater']);
513 513
 					break;
514 514
 				case '\OC\Repair::info':
515
-					$log->info('\OC\Repair::info: Repair info: ' . $event->getArgument(0), ['app' => 'updater']);
515
+					$log->info('\OC\Repair::info: Repair info: '.$event->getArgument(0), ['app' => 'updater']);
516 516
 					break;
517 517
 				case '\OC\Repair::warning':
518
-					$log->warning('\OC\Repair::warning: Repair warning: ' . $event->getArgument(0), ['app' => 'updater']);
518
+					$log->warning('\OC\Repair::warning: Repair warning: '.$event->getArgument(0), ['app' => 'updater']);
519 519
 					break;
520 520
 				case '\OC\Repair::error':
521
-					$log->error('\OC\Repair::error: Repair error: ' . $event->getArgument(0), ['app' => 'updater']);
521
+					$log->error('\OC\Repair::error: Repair error: '.$event->getArgument(0), ['app' => 'updater']);
522 522
 					break;
523 523
 			}
524 524
 		};
@@ -532,74 +532,74 @@  discard block
 block discarded – undo
532 532
 		$dispatcher->addListener('\OC\Repair::error', $repairListener);
533 533
 
534 534
 
535
-		$this->listen('\OC\Updater', 'maintenanceEnabled', function () use($log) {
535
+		$this->listen('\OC\Updater', 'maintenanceEnabled', function() use($log) {
536 536
 			$log->info('\OC\Updater::maintenanceEnabled: Turned on maintenance mode', ['app' => 'updater']);
537 537
 		});
538
-		$this->listen('\OC\Updater', 'maintenanceDisabled', function () use($log) {
538
+		$this->listen('\OC\Updater', 'maintenanceDisabled', function() use($log) {
539 539
 			$log->info('\OC\Updater::maintenanceDisabled: Turned off maintenance mode', ['app' => 'updater']);
540 540
 		});
541
-		$this->listen('\OC\Updater', 'maintenanceActive', function () use($log) {
541
+		$this->listen('\OC\Updater', 'maintenanceActive', function() use($log) {
542 542
 			$log->info('\OC\Updater::maintenanceActive: Maintenance mode is kept active', ['app' => 'updater']);
543 543
 		});
544
-		$this->listen('\OC\Updater', 'updateEnd', function ($success) use($log) {
544
+		$this->listen('\OC\Updater', 'updateEnd', function($success) use($log) {
545 545
 			if ($success) {
546 546
 				$log->info('\OC\Updater::updateEnd: Update successful', ['app' => 'updater']);
547 547
 			} else {
548 548
 				$log->error('\OC\Updater::updateEnd: Update failed', ['app' => 'updater']);
549 549
 			}
550 550
 		});
551
-		$this->listen('\OC\Updater', 'dbUpgradeBefore', function () use($log) {
551
+		$this->listen('\OC\Updater', 'dbUpgradeBefore', function() use($log) {
552 552
 			$log->info('\OC\Updater::dbUpgradeBefore: Updating database schema', ['app' => 'updater']);
553 553
 		});
554
-		$this->listen('\OC\Updater', 'dbUpgrade', function () use($log) {
554
+		$this->listen('\OC\Updater', 'dbUpgrade', function() use($log) {
555 555
 			$log->info('\OC\Updater::dbUpgrade: Updated database', ['app' => 'updater']);
556 556
 		});
557
-		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function () use($log) {
557
+		$this->listen('\OC\Updater', 'dbSimulateUpgradeBefore', function() use($log) {
558 558
 			$log->info('\OC\Updater::dbSimulateUpgradeBefore: Checking whether the database schema can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
559 559
 		});
560
-		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function () use($log) {
560
+		$this->listen('\OC\Updater', 'dbSimulateUpgrade', function() use($log) {
561 561
 			$log->info('\OC\Updater::dbSimulateUpgrade: Checked database schema update', ['app' => 'updater']);
562 562
 		});
563
-		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function ($app) use($log) {
564
-			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: ' . $app, ['app' => 'updater']);
563
+		$this->listen('\OC\Updater', 'incompatibleAppDisabled', function($app) use($log) {
564
+			$log->info('\OC\Updater::incompatibleAppDisabled: Disabled incompatible app: '.$app, ['app' => 'updater']);
565 565
 		});
566
-		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function ($app) use($log) {
567
-			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "' . $app . '" in appstore', ['app' => 'updater']);
566
+		$this->listen('\OC\Updater', 'checkAppStoreAppBefore', function($app) use($log) {
567
+			$log->info('\OC\Updater::checkAppStoreAppBefore: Checking for update of app "'.$app.'" in appstore', ['app' => 'updater']);
568 568
 		});
569
-		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function ($app) use($log) {
570
-			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "' . $app . '" from appstore', ['app' => 'updater']);
569
+		$this->listen('\OC\Updater', 'upgradeAppStoreApp', function($app) use($log) {
570
+			$log->info('\OC\Updater::upgradeAppStoreApp: Update app "'.$app.'" from appstore', ['app' => 'updater']);
571 571
 		});
572
-		$this->listen('\OC\Updater', 'checkAppStoreApp', function ($app) use($log) {
573
-			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "' . $app . '" in appstore', ['app' => 'updater']);
572
+		$this->listen('\OC\Updater', 'checkAppStoreApp', function($app) use($log) {
573
+			$log->info('\OC\Updater::checkAppStoreApp: Checked for update of app "'.$app.'" in appstore', ['app' => 'updater']);
574 574
 		});
575
-		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function () use ($log) {
575
+		$this->listen('\OC\Updater', 'appUpgradeCheckBefore', function() use ($log) {
576 576
 			$log->info('\OC\Updater::appUpgradeCheckBefore: Checking updates of apps', ['app' => 'updater']);
577 577
 		});
578
-		$this->listen('\OC\Updater', 'appSimulateUpdate', function ($app) use ($log) {
579
-			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <' . $app . '> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
578
+		$this->listen('\OC\Updater', 'appSimulateUpdate', function($app) use ($log) {
579
+			$log->info('\OC\Updater::appSimulateUpdate: Checking whether the database schema for <'.$app.'> can be updated (this can take a long time depending on the database size)', ['app' => 'updater']);
580 580
 		});
581
-		$this->listen('\OC\Updater', 'appUpgradeCheck', function () use ($log) {
581
+		$this->listen('\OC\Updater', 'appUpgradeCheck', function() use ($log) {
582 582
 			$log->info('\OC\Updater::appUpgradeCheck: Checked database schema update for apps', ['app' => 'updater']);
583 583
 		});
584
-		$this->listen('\OC\Updater', 'appUpgradeStarted', function ($app) use ($log) {
585
-			$log->info('\OC\Updater::appUpgradeStarted: Updating <' . $app . '> ...', ['app' => 'updater']);
584
+		$this->listen('\OC\Updater', 'appUpgradeStarted', function($app) use ($log) {
585
+			$log->info('\OC\Updater::appUpgradeStarted: Updating <'.$app.'> ...', ['app' => 'updater']);
586 586
 		});
587
-		$this->listen('\OC\Updater', 'appUpgrade', function ($app, $version) use ($log) {
588
-			$log->info('\OC\Updater::appUpgrade: Updated <' . $app . '> to ' . $version, ['app' => 'updater']);
587
+		$this->listen('\OC\Updater', 'appUpgrade', function($app, $version) use ($log) {
588
+			$log->info('\OC\Updater::appUpgrade: Updated <'.$app.'> to '.$version, ['app' => 'updater']);
589 589
 		});
590
-		$this->listen('\OC\Updater', 'failure', function ($message) use($log) {
591
-			$log->error('\OC\Updater::failure: ' . $message, ['app' => 'updater']);
590
+		$this->listen('\OC\Updater', 'failure', function($message) use($log) {
591
+			$log->error('\OC\Updater::failure: '.$message, ['app' => 'updater']);
592 592
 		});
593
-		$this->listen('\OC\Updater', 'setDebugLogLevel', function () use($log) {
593
+		$this->listen('\OC\Updater', 'setDebugLogLevel', function() use($log) {
594 594
 			$log->info('\OC\Updater::setDebugLogLevel: Set log level to debug', ['app' => 'updater']);
595 595
 		});
596
-		$this->listen('\OC\Updater', 'resetLogLevel', function ($logLevel, $logLevelName) use($log) {
597
-			$log->info('\OC\Updater::resetLogLevel: Reset log level to ' . $logLevelName . '(' . $logLevel . ')', ['app' => 'updater']);
596
+		$this->listen('\OC\Updater', 'resetLogLevel', function($logLevel, $logLevelName) use($log) {
597
+			$log->info('\OC\Updater::resetLogLevel: Reset log level to '.$logLevelName.'('.$logLevel.')', ['app' => 'updater']);
598 598
 		});
599
-		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function () use($log) {
599
+		$this->listen('\OC\Updater', 'startCheckCodeIntegrity', function() use($log) {
600 600
 			$log->info('\OC\Updater::startCheckCodeIntegrity: Starting code integrity check...', ['app' => 'updater']);
601 601
 		});
602
-		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function () use($log) {
602
+		$this->listen('\OC\Updater', 'finishedCheckCodeIntegrity', function() use($log) {
603 603
 			$log->info('\OC\Updater::finishedCheckCodeIntegrity: Finished code integrity check', ['app' => 'updater']);
604 604
 		});
605 605
 
Please login to merge, or discard this patch.
lib/private/App/AppManager.php 1 patch
Indentation   +413 added lines, -413 removed lines patch added patch discarded remove patch
@@ -44,417 +44,417 @@
 block discarded – undo
44 44
 
45 45
 class AppManager implements IAppManager {
46 46
 
47
-	/**
48
-	 * Apps with these types can not be enabled for certain groups only
49
-	 * @var string[]
50
-	 */
51
-	protected $protectedAppTypes = [
52
-		'filesystem',
53
-		'prelogin',
54
-		'authentication',
55
-		'logging',
56
-		'prevent_group_restriction',
57
-	];
58
-
59
-	/** @var IUserSession */
60
-	private $userSession;
61
-
62
-	/** @var AppConfig */
63
-	private $appConfig;
64
-
65
-	/** @var IGroupManager */
66
-	private $groupManager;
67
-
68
-	/** @var ICacheFactory */
69
-	private $memCacheFactory;
70
-
71
-	/** @var EventDispatcherInterface */
72
-	private $dispatcher;
73
-
74
-	/** @var string[] $appId => $enabled */
75
-	private $installedAppsCache;
76
-
77
-	/** @var string[] */
78
-	private $shippedApps;
79
-
80
-	/** @var string[] */
81
-	private $alwaysEnabled;
82
-
83
-	/** @var array */
84
-	private $appInfos = [];
85
-
86
-	/** @var array */
87
-	private $appVersions = [];
88
-
89
-	/**
90
-	 * @param IUserSession $userSession
91
-	 * @param AppConfig $appConfig
92
-	 * @param IGroupManager $groupManager
93
-	 * @param ICacheFactory $memCacheFactory
94
-	 * @param EventDispatcherInterface $dispatcher
95
-	 */
96
-	public function __construct(IUserSession $userSession,
97
-								AppConfig $appConfig,
98
-								IGroupManager $groupManager,
99
-								ICacheFactory $memCacheFactory,
100
-								EventDispatcherInterface $dispatcher) {
101
-		$this->userSession = $userSession;
102
-		$this->appConfig = $appConfig;
103
-		$this->groupManager = $groupManager;
104
-		$this->memCacheFactory = $memCacheFactory;
105
-		$this->dispatcher = $dispatcher;
106
-	}
107
-
108
-	/**
109
-	 * @return string[] $appId => $enabled
110
-	 */
111
-	private function getInstalledAppsValues() {
112
-		if (!$this->installedAppsCache) {
113
-			$values = $this->appConfig->getValues(false, 'enabled');
114
-
115
-			$alwaysEnabledApps = $this->getAlwaysEnabledApps();
116
-			foreach($alwaysEnabledApps as $appId) {
117
-				$values[$appId] = 'yes';
118
-			}
119
-
120
-			$this->installedAppsCache = array_filter($values, function ($value) {
121
-				return $value !== 'no';
122
-			});
123
-			ksort($this->installedAppsCache);
124
-		}
125
-		return $this->installedAppsCache;
126
-	}
127
-
128
-	/**
129
-	 * List all installed apps
130
-	 *
131
-	 * @return string[]
132
-	 */
133
-	public function getInstalledApps() {
134
-		return array_keys($this->getInstalledAppsValues());
135
-	}
136
-
137
-	/**
138
-	 * List all apps enabled for a user
139
-	 *
140
-	 * @param \OCP\IUser $user
141
-	 * @return string[]
142
-	 */
143
-	public function getEnabledAppsForUser(IUser $user) {
144
-		$apps = $this->getInstalledAppsValues();
145
-		$appsForUser = array_filter($apps, function ($enabled) use ($user) {
146
-			return $this->checkAppForUser($enabled, $user);
147
-		});
148
-		return array_keys($appsForUser);
149
-	}
150
-
151
-	/**
152
-	 * Check if an app is enabled for user
153
-	 *
154
-	 * @param string $appId
155
-	 * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
156
-	 * @return bool
157
-	 */
158
-	public function isEnabledForUser($appId, $user = null) {
159
-		if ($this->isAlwaysEnabled($appId)) {
160
-			return true;
161
-		}
162
-		if ($user === null) {
163
-			$user = $this->userSession->getUser();
164
-		}
165
-		$installedApps = $this->getInstalledAppsValues();
166
-		if (isset($installedApps[$appId])) {
167
-			return $this->checkAppForUser($installedApps[$appId], $user);
168
-		} else {
169
-			return false;
170
-		}
171
-	}
172
-
173
-	/**
174
-	 * @param string $enabled
175
-	 * @param IUser $user
176
-	 * @return bool
177
-	 */
178
-	private function checkAppForUser($enabled, $user) {
179
-		if ($enabled === 'yes') {
180
-			return true;
181
-		} elseif ($user === null) {
182
-			return false;
183
-		} else {
184
-			if(empty($enabled)){
185
-				return false;
186
-			}
187
-
188
-			$groupIds = json_decode($enabled);
189
-
190
-			if (!is_array($groupIds)) {
191
-				$jsonError = json_last_error();
192
-				\OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
193
-				return false;
194
-			}
195
-
196
-			$userGroups = $this->groupManager->getUserGroupIds($user);
197
-			foreach ($userGroups as $groupId) {
198
-				if (in_array($groupId, $groupIds, true)) {
199
-					return true;
200
-				}
201
-			}
202
-			return false;
203
-		}
204
-	}
205
-
206
-	/**
207
-	 * Check if an app is enabled in the instance
208
-	 *
209
-	 * Notice: This actually checks if the app is enabled and not only if it is installed.
210
-	 *
211
-	 * @param string $appId
212
-	 * @return bool
213
-	 */
214
-	public function isInstalled($appId) {
215
-		$installedApps = $this->getInstalledAppsValues();
216
-		return isset($installedApps[$appId]);
217
-	}
218
-
219
-	/**
220
-	 * Enable an app for every user
221
-	 *
222
-	 * @param string $appId
223
-	 * @throws AppPathNotFoundException
224
-	 */
225
-	public function enableApp($appId) {
226
-		// Check if app exists
227
-		$this->getAppPath($appId);
228
-
229
-		$this->installedAppsCache[$appId] = 'yes';
230
-		$this->appConfig->setValue($appId, 'enabled', 'yes');
231
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
232
-			ManagerEvent::EVENT_APP_ENABLE, $appId
233
-		));
234
-		$this->clearAppsCache();
235
-	}
236
-
237
-	/**
238
-	 * Whether a list of types contains a protected app type
239
-	 *
240
-	 * @param string[] $types
241
-	 * @return bool
242
-	 */
243
-	public function hasProtectedAppType($types) {
244
-		if (empty($types)) {
245
-			return false;
246
-		}
247
-
248
-		$protectedTypes = array_intersect($this->protectedAppTypes, $types);
249
-		return !empty($protectedTypes);
250
-	}
251
-
252
-	/**
253
-	 * Enable an app only for specific groups
254
-	 *
255
-	 * @param string $appId
256
-	 * @param \OCP\IGroup[] $groups
257
-	 * @throws \Exception if app can't be enabled for groups
258
-	 */
259
-	public function enableAppForGroups($appId, $groups) {
260
-		$info = $this->getAppInfo($appId);
261
-		if (!empty($info['types'])) {
262
-			$protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
263
-			if (!empty($protectedTypes)) {
264
-				throw new \Exception("$appId can't be enabled for groups.");
265
-			}
266
-		}
267
-
268
-		$groupIds = array_map(function ($group) {
269
-			/** @var \OCP\IGroup $group */
270
-			return $group->getGID();
271
-		}, $groups);
272
-		$this->installedAppsCache[$appId] = json_encode($groupIds);
273
-		$this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
274
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
275
-			ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
276
-		));
277
-		$this->clearAppsCache();
278
-	}
279
-
280
-	/**
281
-	 * Disable an app for every user
282
-	 *
283
-	 * @param string $appId
284
-	 * @throws \Exception if app can't be disabled
285
-	 */
286
-	public function disableApp($appId) {
287
-		if ($this->isAlwaysEnabled($appId)) {
288
-			throw new \Exception("$appId can't be disabled.");
289
-		}
290
-		unset($this->installedAppsCache[$appId]);
291
-		$this->appConfig->setValue($appId, 'enabled', 'no');
292
-
293
-		// run uninstall steps
294
-		$appData = $this->getAppInfo($appId);
295
-		if (!is_null($appData)) {
296
-			\OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
297
-		}
298
-
299
-		$this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
300
-			ManagerEvent::EVENT_APP_DISABLE, $appId
301
-		));
302
-		$this->clearAppsCache();
303
-	}
304
-
305
-	/**
306
-	 * Get the directory for the given app.
307
-	 *
308
-	 * @param string $appId
309
-	 * @return string
310
-	 * @throws AppPathNotFoundException if app folder can't be found
311
-	 */
312
-	public function getAppPath($appId) {
313
-		$appPath = \OC_App::getAppPath($appId);
314
-		if($appPath === false) {
315
-			throw new AppPathNotFoundException('Could not find path for ' . $appId);
316
-		}
317
-		return $appPath;
318
-	}
319
-
320
-	/**
321
-	 * Clear the cached list of apps when enabling/disabling an app
322
-	 */
323
-	public function clearAppsCache() {
324
-		$settingsMemCache = $this->memCacheFactory->createDistributed('settings');
325
-		$settingsMemCache->clear('listApps');
326
-	}
327
-
328
-	/**
329
-	 * Returns a list of apps that need upgrade
330
-	 *
331
-	 * @param string $version Nextcloud version as array of version components
332
-	 * @return array list of app info from apps that need an upgrade
333
-	 *
334
-	 * @internal
335
-	 */
336
-	public function getAppsNeedingUpgrade($version) {
337
-		$appsToUpgrade = [];
338
-		$apps = $this->getInstalledApps();
339
-		foreach ($apps as $appId) {
340
-			$appInfo = $this->getAppInfo($appId);
341
-			$appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
342
-			if ($appDbVersion
343
-				&& isset($appInfo['version'])
344
-				&& version_compare($appInfo['version'], $appDbVersion, '>')
345
-				&& \OC_App::isAppCompatible($version, $appInfo)
346
-			) {
347
-				$appsToUpgrade[] = $appInfo;
348
-			}
349
-		}
350
-
351
-		return $appsToUpgrade;
352
-	}
353
-
354
-	/**
355
-	 * Returns the app information from "appinfo/info.xml".
356
-	 *
357
-	 * @param string $appId app id
358
-	 *
359
-	 * @param bool $path
360
-	 * @param null $lang
361
-	 * @return array|null app info
362
-	 */
363
-	public function getAppInfo(string $appId, bool $path = false, $lang = null) {
364
-		if ($path) {
365
-			$file = $appId;
366
-		} else {
367
-			if ($lang === null && isset($this->appInfos[$appId])) {
368
-				return $this->appInfos[$appId];
369
-			}
370
-			try {
371
-				$appPath = $this->getAppPath($appId);
372
-			} catch (AppPathNotFoundException $e) {
373
-				return null;
374
-			}
375
-			$file = $appPath . '/appinfo/info.xml';
376
-		}
377
-
378
-		$parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
379
-		$data = $parser->parse($file);
380
-
381
-		if (is_array($data)) {
382
-			$data = \OC_App::parseAppInfo($data, $lang);
383
-		}
384
-
385
-		if ($lang === null) {
386
-			$this->appInfos[$appId] = $data;
387
-		}
388
-
389
-		return $data;
390
-	}
391
-
392
-	public function getAppVersion(string $appId, bool $useCache = true): string {
393
-		if(!$useCache || !isset($this->appVersions[$appId])) {
394
-			$appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
395
-			$this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
396
-		}
397
-		return $this->appVersions[$appId];
398
-	}
399
-
400
-	/**
401
-	 * Returns a list of apps incompatible with the given version
402
-	 *
403
-	 * @param string $version Nextcloud version as array of version components
404
-	 *
405
-	 * @return array list of app info from incompatible apps
406
-	 *
407
-	 * @internal
408
-	 */
409
-	public function getIncompatibleApps(string $version): array {
410
-		$apps = $this->getInstalledApps();
411
-		$incompatibleApps = array();
412
-		foreach ($apps as $appId) {
413
-			$info = $this->getAppInfo($appId);
414
-			if ($info === null) {
415
-				$incompatibleApps[] = ['id' => $appId];
416
-			} else if (!\OC_App::isAppCompatible($version, $info)) {
417
-				$incompatibleApps[] = $info;
418
-			}
419
-		}
420
-		return $incompatibleApps;
421
-	}
422
-
423
-	/**
424
-	 * @inheritdoc
425
-	 * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped()
426
-	 */
427
-	public function isShipped($appId) {
428
-		$this->loadShippedJson();
429
-		return in_array($appId, $this->shippedApps, true);
430
-	}
431
-
432
-	private function isAlwaysEnabled($appId) {
433
-		$alwaysEnabled = $this->getAlwaysEnabledApps();
434
-		return in_array($appId, $alwaysEnabled, true);
435
-	}
436
-
437
-	/**
438
-	 * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson()
439
-	 * @throws \Exception
440
-	 */
441
-	private function loadShippedJson() {
442
-		if ($this->shippedApps === null) {
443
-			$shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
444
-			if (!file_exists($shippedJson)) {
445
-				throw new \Exception("File not found: $shippedJson");
446
-			}
447
-			$content = json_decode(file_get_contents($shippedJson), true);
448
-			$this->shippedApps = $content['shippedApps'];
449
-			$this->alwaysEnabled = $content['alwaysEnabled'];
450
-		}
451
-	}
452
-
453
-	/**
454
-	 * @inheritdoc
455
-	 */
456
-	public function getAlwaysEnabledApps() {
457
-		$this->loadShippedJson();
458
-		return $this->alwaysEnabled;
459
-	}
47
+    /**
48
+     * Apps with these types can not be enabled for certain groups only
49
+     * @var string[]
50
+     */
51
+    protected $protectedAppTypes = [
52
+        'filesystem',
53
+        'prelogin',
54
+        'authentication',
55
+        'logging',
56
+        'prevent_group_restriction',
57
+    ];
58
+
59
+    /** @var IUserSession */
60
+    private $userSession;
61
+
62
+    /** @var AppConfig */
63
+    private $appConfig;
64
+
65
+    /** @var IGroupManager */
66
+    private $groupManager;
67
+
68
+    /** @var ICacheFactory */
69
+    private $memCacheFactory;
70
+
71
+    /** @var EventDispatcherInterface */
72
+    private $dispatcher;
73
+
74
+    /** @var string[] $appId => $enabled */
75
+    private $installedAppsCache;
76
+
77
+    /** @var string[] */
78
+    private $shippedApps;
79
+
80
+    /** @var string[] */
81
+    private $alwaysEnabled;
82
+
83
+    /** @var array */
84
+    private $appInfos = [];
85
+
86
+    /** @var array */
87
+    private $appVersions = [];
88
+
89
+    /**
90
+     * @param IUserSession $userSession
91
+     * @param AppConfig $appConfig
92
+     * @param IGroupManager $groupManager
93
+     * @param ICacheFactory $memCacheFactory
94
+     * @param EventDispatcherInterface $dispatcher
95
+     */
96
+    public function __construct(IUserSession $userSession,
97
+                                AppConfig $appConfig,
98
+                                IGroupManager $groupManager,
99
+                                ICacheFactory $memCacheFactory,
100
+                                EventDispatcherInterface $dispatcher) {
101
+        $this->userSession = $userSession;
102
+        $this->appConfig = $appConfig;
103
+        $this->groupManager = $groupManager;
104
+        $this->memCacheFactory = $memCacheFactory;
105
+        $this->dispatcher = $dispatcher;
106
+    }
107
+
108
+    /**
109
+     * @return string[] $appId => $enabled
110
+     */
111
+    private function getInstalledAppsValues() {
112
+        if (!$this->installedAppsCache) {
113
+            $values = $this->appConfig->getValues(false, 'enabled');
114
+
115
+            $alwaysEnabledApps = $this->getAlwaysEnabledApps();
116
+            foreach($alwaysEnabledApps as $appId) {
117
+                $values[$appId] = 'yes';
118
+            }
119
+
120
+            $this->installedAppsCache = array_filter($values, function ($value) {
121
+                return $value !== 'no';
122
+            });
123
+            ksort($this->installedAppsCache);
124
+        }
125
+        return $this->installedAppsCache;
126
+    }
127
+
128
+    /**
129
+     * List all installed apps
130
+     *
131
+     * @return string[]
132
+     */
133
+    public function getInstalledApps() {
134
+        return array_keys($this->getInstalledAppsValues());
135
+    }
136
+
137
+    /**
138
+     * List all apps enabled for a user
139
+     *
140
+     * @param \OCP\IUser $user
141
+     * @return string[]
142
+     */
143
+    public function getEnabledAppsForUser(IUser $user) {
144
+        $apps = $this->getInstalledAppsValues();
145
+        $appsForUser = array_filter($apps, function ($enabled) use ($user) {
146
+            return $this->checkAppForUser($enabled, $user);
147
+        });
148
+        return array_keys($appsForUser);
149
+    }
150
+
151
+    /**
152
+     * Check if an app is enabled for user
153
+     *
154
+     * @param string $appId
155
+     * @param \OCP\IUser $user (optional) if not defined, the currently logged in user will be used
156
+     * @return bool
157
+     */
158
+    public function isEnabledForUser($appId, $user = null) {
159
+        if ($this->isAlwaysEnabled($appId)) {
160
+            return true;
161
+        }
162
+        if ($user === null) {
163
+            $user = $this->userSession->getUser();
164
+        }
165
+        $installedApps = $this->getInstalledAppsValues();
166
+        if (isset($installedApps[$appId])) {
167
+            return $this->checkAppForUser($installedApps[$appId], $user);
168
+        } else {
169
+            return false;
170
+        }
171
+    }
172
+
173
+    /**
174
+     * @param string $enabled
175
+     * @param IUser $user
176
+     * @return bool
177
+     */
178
+    private function checkAppForUser($enabled, $user) {
179
+        if ($enabled === 'yes') {
180
+            return true;
181
+        } elseif ($user === null) {
182
+            return false;
183
+        } else {
184
+            if(empty($enabled)){
185
+                return false;
186
+            }
187
+
188
+            $groupIds = json_decode($enabled);
189
+
190
+            if (!is_array($groupIds)) {
191
+                $jsonError = json_last_error();
192
+                \OC::$server->getLogger()->warning('AppManger::checkAppForUser - can\'t decode group IDs: ' . print_r($enabled, true) . ' - json error code: ' . $jsonError, ['app' => 'lib']);
193
+                return false;
194
+            }
195
+
196
+            $userGroups = $this->groupManager->getUserGroupIds($user);
197
+            foreach ($userGroups as $groupId) {
198
+                if (in_array($groupId, $groupIds, true)) {
199
+                    return true;
200
+                }
201
+            }
202
+            return false;
203
+        }
204
+    }
205
+
206
+    /**
207
+     * Check if an app is enabled in the instance
208
+     *
209
+     * Notice: This actually checks if the app is enabled and not only if it is installed.
210
+     *
211
+     * @param string $appId
212
+     * @return bool
213
+     */
214
+    public function isInstalled($appId) {
215
+        $installedApps = $this->getInstalledAppsValues();
216
+        return isset($installedApps[$appId]);
217
+    }
218
+
219
+    /**
220
+     * Enable an app for every user
221
+     *
222
+     * @param string $appId
223
+     * @throws AppPathNotFoundException
224
+     */
225
+    public function enableApp($appId) {
226
+        // Check if app exists
227
+        $this->getAppPath($appId);
228
+
229
+        $this->installedAppsCache[$appId] = 'yes';
230
+        $this->appConfig->setValue($appId, 'enabled', 'yes');
231
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE, new ManagerEvent(
232
+            ManagerEvent::EVENT_APP_ENABLE, $appId
233
+        ));
234
+        $this->clearAppsCache();
235
+    }
236
+
237
+    /**
238
+     * Whether a list of types contains a protected app type
239
+     *
240
+     * @param string[] $types
241
+     * @return bool
242
+     */
243
+    public function hasProtectedAppType($types) {
244
+        if (empty($types)) {
245
+            return false;
246
+        }
247
+
248
+        $protectedTypes = array_intersect($this->protectedAppTypes, $types);
249
+        return !empty($protectedTypes);
250
+    }
251
+
252
+    /**
253
+     * Enable an app only for specific groups
254
+     *
255
+     * @param string $appId
256
+     * @param \OCP\IGroup[] $groups
257
+     * @throws \Exception if app can't be enabled for groups
258
+     */
259
+    public function enableAppForGroups($appId, $groups) {
260
+        $info = $this->getAppInfo($appId);
261
+        if (!empty($info['types'])) {
262
+            $protectedTypes = array_intersect($this->protectedAppTypes, $info['types']);
263
+            if (!empty($protectedTypes)) {
264
+                throw new \Exception("$appId can't be enabled for groups.");
265
+            }
266
+        }
267
+
268
+        $groupIds = array_map(function ($group) {
269
+            /** @var \OCP\IGroup $group */
270
+            return $group->getGID();
271
+        }, $groups);
272
+        $this->installedAppsCache[$appId] = json_encode($groupIds);
273
+        $this->appConfig->setValue($appId, 'enabled', json_encode($groupIds));
274
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, new ManagerEvent(
275
+            ManagerEvent::EVENT_APP_ENABLE_FOR_GROUPS, $appId, $groups
276
+        ));
277
+        $this->clearAppsCache();
278
+    }
279
+
280
+    /**
281
+     * Disable an app for every user
282
+     *
283
+     * @param string $appId
284
+     * @throws \Exception if app can't be disabled
285
+     */
286
+    public function disableApp($appId) {
287
+        if ($this->isAlwaysEnabled($appId)) {
288
+            throw new \Exception("$appId can't be disabled.");
289
+        }
290
+        unset($this->installedAppsCache[$appId]);
291
+        $this->appConfig->setValue($appId, 'enabled', 'no');
292
+
293
+        // run uninstall steps
294
+        $appData = $this->getAppInfo($appId);
295
+        if (!is_null($appData)) {
296
+            \OC_App::executeRepairSteps($appId, $appData['repair-steps']['uninstall']);
297
+        }
298
+
299
+        $this->dispatcher->dispatch(ManagerEvent::EVENT_APP_DISABLE, new ManagerEvent(
300
+            ManagerEvent::EVENT_APP_DISABLE, $appId
301
+        ));
302
+        $this->clearAppsCache();
303
+    }
304
+
305
+    /**
306
+     * Get the directory for the given app.
307
+     *
308
+     * @param string $appId
309
+     * @return string
310
+     * @throws AppPathNotFoundException if app folder can't be found
311
+     */
312
+    public function getAppPath($appId) {
313
+        $appPath = \OC_App::getAppPath($appId);
314
+        if($appPath === false) {
315
+            throw new AppPathNotFoundException('Could not find path for ' . $appId);
316
+        }
317
+        return $appPath;
318
+    }
319
+
320
+    /**
321
+     * Clear the cached list of apps when enabling/disabling an app
322
+     */
323
+    public function clearAppsCache() {
324
+        $settingsMemCache = $this->memCacheFactory->createDistributed('settings');
325
+        $settingsMemCache->clear('listApps');
326
+    }
327
+
328
+    /**
329
+     * Returns a list of apps that need upgrade
330
+     *
331
+     * @param string $version Nextcloud version as array of version components
332
+     * @return array list of app info from apps that need an upgrade
333
+     *
334
+     * @internal
335
+     */
336
+    public function getAppsNeedingUpgrade($version) {
337
+        $appsToUpgrade = [];
338
+        $apps = $this->getInstalledApps();
339
+        foreach ($apps as $appId) {
340
+            $appInfo = $this->getAppInfo($appId);
341
+            $appDbVersion = $this->appConfig->getValue($appId, 'installed_version');
342
+            if ($appDbVersion
343
+                && isset($appInfo['version'])
344
+                && version_compare($appInfo['version'], $appDbVersion, '>')
345
+                && \OC_App::isAppCompatible($version, $appInfo)
346
+            ) {
347
+                $appsToUpgrade[] = $appInfo;
348
+            }
349
+        }
350
+
351
+        return $appsToUpgrade;
352
+    }
353
+
354
+    /**
355
+     * Returns the app information from "appinfo/info.xml".
356
+     *
357
+     * @param string $appId app id
358
+     *
359
+     * @param bool $path
360
+     * @param null $lang
361
+     * @return array|null app info
362
+     */
363
+    public function getAppInfo(string $appId, bool $path = false, $lang = null) {
364
+        if ($path) {
365
+            $file = $appId;
366
+        } else {
367
+            if ($lang === null && isset($this->appInfos[$appId])) {
368
+                return $this->appInfos[$appId];
369
+            }
370
+            try {
371
+                $appPath = $this->getAppPath($appId);
372
+            } catch (AppPathNotFoundException $e) {
373
+                return null;
374
+            }
375
+            $file = $appPath . '/appinfo/info.xml';
376
+        }
377
+
378
+        $parser = new InfoParser($this->memCacheFactory->createLocal('core.appinfo'));
379
+        $data = $parser->parse($file);
380
+
381
+        if (is_array($data)) {
382
+            $data = \OC_App::parseAppInfo($data, $lang);
383
+        }
384
+
385
+        if ($lang === null) {
386
+            $this->appInfos[$appId] = $data;
387
+        }
388
+
389
+        return $data;
390
+    }
391
+
392
+    public function getAppVersion(string $appId, bool $useCache = true): string {
393
+        if(!$useCache || !isset($this->appVersions[$appId])) {
394
+            $appInfo = \OC::$server->getAppManager()->getAppInfo($appId);
395
+            $this->appVersions[$appId] = ($appInfo !== null && isset($appInfo['version'])) ? $appInfo['version'] : '0';
396
+        }
397
+        return $this->appVersions[$appId];
398
+    }
399
+
400
+    /**
401
+     * Returns a list of apps incompatible with the given version
402
+     *
403
+     * @param string $version Nextcloud version as array of version components
404
+     *
405
+     * @return array list of app info from incompatible apps
406
+     *
407
+     * @internal
408
+     */
409
+    public function getIncompatibleApps(string $version): array {
410
+        $apps = $this->getInstalledApps();
411
+        $incompatibleApps = array();
412
+        foreach ($apps as $appId) {
413
+            $info = $this->getAppInfo($appId);
414
+            if ($info === null) {
415
+                $incompatibleApps[] = ['id' => $appId];
416
+            } else if (!\OC_App::isAppCompatible($version, $info)) {
417
+                $incompatibleApps[] = $info;
418
+            }
419
+        }
420
+        return $incompatibleApps;
421
+    }
422
+
423
+    /**
424
+     * @inheritdoc
425
+     * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::isShipped()
426
+     */
427
+    public function isShipped($appId) {
428
+        $this->loadShippedJson();
429
+        return in_array($appId, $this->shippedApps, true);
430
+    }
431
+
432
+    private function isAlwaysEnabled($appId) {
433
+        $alwaysEnabled = $this->getAlwaysEnabledApps();
434
+        return in_array($appId, $alwaysEnabled, true);
435
+    }
436
+
437
+    /**
438
+     * In case you change this method, also change \OC\App\CodeChecker\InfoChecker::loadShippedJson()
439
+     * @throws \Exception
440
+     */
441
+    private function loadShippedJson() {
442
+        if ($this->shippedApps === null) {
443
+            $shippedJson = \OC::$SERVERROOT . '/core/shipped.json';
444
+            if (!file_exists($shippedJson)) {
445
+                throw new \Exception("File not found: $shippedJson");
446
+            }
447
+            $content = json_decode(file_get_contents($shippedJson), true);
448
+            $this->shippedApps = $content['shippedApps'];
449
+            $this->alwaysEnabled = $content['alwaysEnabled'];
450
+        }
451
+    }
452
+
453
+    /**
454
+     * @inheritdoc
455
+     */
456
+    public function getAlwaysEnabledApps() {
457
+        $this->loadShippedJson();
458
+        return $this->alwaysEnabled;
459
+    }
460 460
 }
Please login to merge, or discard this patch.