Repair.php 9.4 KB

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