Repair.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OC;
  8. use OC\DB\Connection;
  9. use OC\DB\ConnectionAdapter;
  10. use OC\Repair\AddAppConfigLazyMigration;
  11. use OC\Repair\AddBruteForceCleanupJob;
  12. use OC\Repair\AddCleanupUpdaterBackupsJob;
  13. use OC\Repair\AddMetadataGenerationJob;
  14. use OC\Repair\AddRemoveOldTasksBackgroundJob;
  15. use OC\Repair\CleanTags;
  16. use OC\Repair\CleanUpAbandonedApps;
  17. use OC\Repair\ClearFrontendCaches;
  18. use OC\Repair\ClearGeneratedAvatarCache;
  19. use OC\Repair\Collation;
  20. use OC\Repair\Events\RepairAdvanceEvent;
  21. use OC\Repair\Events\RepairErrorEvent;
  22. use OC\Repair\Events\RepairFinishEvent;
  23. use OC\Repair\Events\RepairInfoEvent;
  24. use OC\Repair\Events\RepairStartEvent;
  25. use OC\Repair\Events\RepairStepEvent;
  26. use OC\Repair\Events\RepairWarningEvent;
  27. use OC\Repair\MoveUpdaterStepFile;
  28. use OC\Repair\NC11\FixMountStorages;
  29. use OC\Repair\NC13\AddLogRotateJob;
  30. use OC\Repair\NC14\AddPreviewBackgroundCleanupJob;
  31. use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob;
  32. use OC\Repair\NC16\CleanupCardDAVPhotoCache;
  33. use OC\Repair\NC16\ClearCollectionsAccessCache;
  34. use OC\Repair\NC18\ResetGeneratedAvatarFlag;
  35. use OC\Repair\NC20\EncryptionLegacyCipher;
  36. use OC\Repair\NC20\EncryptionMigration;
  37. use OC\Repair\NC20\ShippedDashboardEnable;
  38. use OC\Repair\NC21\AddCheckForUserCertificatesJob;
  39. use OC\Repair\NC21\ValidatePhoneNumber;
  40. use OC\Repair\NC22\LookupServerSendCheck;
  41. use OC\Repair\NC24\AddTokenCleanupJob;
  42. use OC\Repair\NC25\AddMissingSecretJob;
  43. use OC\Repair\OldGroupMembershipShares;
  44. use OC\Repair\Owncloud\CleanPreviews;
  45. use OC\Repair\Owncloud\DropAccountTermsTable;
  46. use OC\Repair\Owncloud\MigrateOauthTables;
  47. use OC\Repair\Owncloud\MoveAvatars;
  48. use OC\Repair\Owncloud\SaveAccountsTableData;
  49. use OC\Repair\Owncloud\UpdateLanguageCodes;
  50. use OC\Repair\RemoveLinkShares;
  51. use OC\Repair\RepairDavShares;
  52. use OC\Repair\RepairInvalidShares;
  53. use OC\Repair\RepairMimeTypes;
  54. use OC\Template\JSCombiner;
  55. use OCP\AppFramework\QueryException;
  56. use OCP\AppFramework\Utility\ITimeFactory;
  57. use OCP\Collaboration\Resources\IManager;
  58. use OCP\EventDispatcher\IEventDispatcher;
  59. use OCP\Migration\IOutput;
  60. use OCP\Migration\IRepairStep;
  61. use Psr\Log\LoggerInterface;
  62. use Throwable;
  63. class Repair implements IOutput {
  64. /** @var IRepairStep[] */
  65. private array $repairSteps = [];
  66. private string $currentStep;
  67. public function __construct(
  68. private IEventDispatcher $dispatcher,
  69. private LoggerInterface $logger
  70. ) {
  71. }
  72. /** @param IRepairStep[] $repairSteps */
  73. public function setRepairSteps(array $repairSteps): void {
  74. $this->repairSteps = $repairSteps;
  75. }
  76. /**
  77. * Run a series of repair steps for common problems
  78. */
  79. public function run() {
  80. if (count($this->repairSteps) === 0) {
  81. $this->dispatcher->dispatchTyped(new RepairInfoEvent('No repair steps available'));
  82. return;
  83. }
  84. // run each repair step
  85. foreach ($this->repairSteps as $step) {
  86. $this->currentStep = $step->getName();
  87. $this->dispatcher->dispatchTyped(new RepairStepEvent($this->currentStep));
  88. try {
  89. $step->run($this);
  90. } catch (\Exception $e) {
  91. $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]);
  92. $this->dispatcher->dispatchTyped(new RepairErrorEvent($e->getMessage()));
  93. }
  94. }
  95. }
  96. /**
  97. * Add repair step
  98. *
  99. * @param IRepairStep|string $repairStep repair step
  100. * @throws \Exception
  101. */
  102. public function addStep($repairStep) {
  103. if (is_string($repairStep)) {
  104. try {
  105. $s = \OC::$server->get($repairStep);
  106. } catch (QueryException $e) {
  107. if (class_exists($repairStep)) {
  108. try {
  109. // Last resort: hope there are no constructor arguments
  110. $s = new $repairStep();
  111. } catch (Throwable $inner) {
  112. // Well, it was worth a try
  113. throw new \Exception("Repair step '$repairStep' can't be instantiated: " . $e->getMessage(), 0, $e);
  114. }
  115. } else {
  116. throw new \Exception("Repair step '$repairStep' is unknown", 0, $e);
  117. }
  118. }
  119. if ($s instanceof IRepairStep) {
  120. $this->repairSteps[] = $s;
  121. } else {
  122. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  123. }
  124. } else {
  125. $this->repairSteps[] = $repairStep;
  126. }
  127. }
  128. /**
  129. * Returns the default repair steps to be run on the
  130. * command line or after an upgrade.
  131. *
  132. * @return IRepairStep[]
  133. */
  134. public static function getRepairSteps(): array {
  135. return [
  136. new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->getDatabaseConnection(), false),
  137. new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  138. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  139. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  140. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  141. new MoveAvatars(
  142. \OC::$server->getJobList(),
  143. \OC::$server->getConfig()
  144. ),
  145. new CleanPreviews(
  146. \OC::$server->getJobList(),
  147. \OC::$server->getUserManager(),
  148. \OC::$server->getConfig()
  149. ),
  150. new MigrateOauthTables(\OC::$server->get(Connection::class)),
  151. new FixMountStorages(\OC::$server->getDatabaseConnection()),
  152. new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
  153. new AddLogRotateJob(\OC::$server->getJobList()),
  154. new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OCP\Server::get(JSCombiner::class)),
  155. \OCP\Server::get(ClearGeneratedAvatarCache::class),
  156. new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
  157. new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
  158. new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
  159. new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
  160. new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OCP\Server::get(ITimeFactory::class)),
  161. new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OCP\Server::get(IManager::class)),
  162. \OCP\Server::get(ResetGeneratedAvatarFlag::class),
  163. \OCP\Server::get(EncryptionLegacyCipher::class),
  164. \OCP\Server::get(EncryptionMigration::class),
  165. \OCP\Server::get(ShippedDashboardEnable::class),
  166. \OCP\Server::get(AddBruteForceCleanupJob::class),
  167. \OCP\Server::get(AddCheckForUserCertificatesJob::class),
  168. \OCP\Server::get(RepairDavShares::class),
  169. \OCP\Server::get(LookupServerSendCheck::class),
  170. \OCP\Server::get(AddTokenCleanupJob::class),
  171. \OCP\Server::get(CleanUpAbandonedApps::class),
  172. \OCP\Server::get(AddMissingSecretJob::class),
  173. \OCP\Server::get(AddRemoveOldTasksBackgroundJob::class),
  174. \OCP\Server::get(AddMetadataGenerationJob::class),
  175. \OCP\Server::get(AddAppConfigLazyMigration::class),
  176. ];
  177. }
  178. /**
  179. * Returns expensive repair steps to be run on the
  180. * command line with a special option.
  181. *
  182. * @return IRepairStep[]
  183. */
  184. public static function getExpensiveRepairSteps() {
  185. return [
  186. new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
  187. \OC::$server->get(ValidatePhoneNumber::class),
  188. ];
  189. }
  190. /**
  191. * Returns the repair steps to be run before an
  192. * upgrade.
  193. *
  194. * @return IRepairStep[]
  195. */
  196. public static function getBeforeUpgradeRepairSteps() {
  197. /** @var ConnectionAdapter $connectionAdapter */
  198. $connectionAdapter = \OC::$server->get(ConnectionAdapter::class);
  199. $config = \OC::$server->getConfig();
  200. $steps = [
  201. new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), $connectionAdapter, true),
  202. new SaveAccountsTableData($connectionAdapter, $config),
  203. new DropAccountTermsTable($connectionAdapter),
  204. ];
  205. return $steps;
  206. }
  207. public function debug(string $message): void {
  208. }
  209. /**
  210. * @param string $message
  211. */
  212. public function info($message) {
  213. // for now just emit as we did in the past
  214. $this->dispatcher->dispatchTyped(new RepairInfoEvent($message));
  215. }
  216. /**
  217. * @param string $message
  218. */
  219. public function warning($message) {
  220. // for now just emit as we did in the past
  221. $this->dispatcher->dispatchTyped(new RepairWarningEvent($message));
  222. }
  223. /**
  224. * @param int $max
  225. */
  226. public function startProgress($max = 0) {
  227. // for now just emit as we did in the past
  228. $this->dispatcher->dispatchTyped(new RepairStartEvent($max, $this->currentStep));
  229. }
  230. /**
  231. * @param int $step number of step to advance
  232. * @param string $description
  233. */
  234. public function advance($step = 1, $description = '') {
  235. // for now just emit as we did in the past
  236. $this->dispatcher->dispatchTyped(new RepairAdvanceEvent($step, $description));
  237. }
  238. /**
  239. * @param int $max
  240. */
  241. public function finishProgress() {
  242. // for now just emit as we did in the past
  243. $this->dispatcher->dispatchTyped(new RepairFinishEvent());
  244. }
  245. }