Repair.php 9.2 KB

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