Repair.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  8. * @author Georg Ehrke <oc.list@georgehrke.com>
  9. * @author Joas Schilling <coding@schilljs.com>
  10. * @author John Molakvoæ <skjnldsv@protonmail.com>
  11. * @author Julius Härtl <jus@bitgrid.net>
  12. * @author Lukas Reschke <lukas@statuscode.ch>
  13. * @author Michael Weimann <mail@michael-weimann.eu>
  14. * @author Morris Jobke <hey@morrisjobke.de>
  15. * @author Robin Appelman <robin@icewind.nl>
  16. * @author Roeland Jago Douma <roeland@famdouma.nl>
  17. * @author Thomas Müller <thomas.mueller@tmit.eu>
  18. * @author Vincent Petry <vincent@nextcloud.com>
  19. *
  20. * @license AGPL-3.0
  21. *
  22. * This code is free software: you can redistribute it and/or modify
  23. * it under the terms of the GNU Affero General Public License, version 3,
  24. * as published by the Free Software Foundation.
  25. *
  26. * This program is distributed in the hope that it will be useful,
  27. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  28. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  29. * GNU Affero General Public License for more details.
  30. *
  31. * You should have received a copy of the GNU Affero General Public License, version 3,
  32. * along with this program. If not, see <http://www.gnu.org/licenses/>
  33. *
  34. */
  35. namespace OC;
  36. use OC\DB\Connection;
  37. use OC\DB\ConnectionAdapter;
  38. use OC\Repair\AddAppConfigLazyMigration;
  39. use OC\Repair\AddBruteForceCleanupJob;
  40. use OC\Repair\AddCleanupDeletedUsersBackgroundJob;
  41. use OC\Repair\AddCleanupUpdaterBackupsJob;
  42. use OC\Repair\AddMetadataGenerationJob;
  43. use OC\Repair\AddRemoveOldTasksBackgroundJob;
  44. use OC\Repair\CleanTags;
  45. use OC\Repair\CleanUpAbandonedApps;
  46. use OC\Repair\ClearFrontendCaches;
  47. use OC\Repair\ClearGeneratedAvatarCache;
  48. use OC\Repair\Collation;
  49. use OC\Repair\Events\RepairAdvanceEvent;
  50. use OC\Repair\Events\RepairErrorEvent;
  51. use OC\Repair\Events\RepairFinishEvent;
  52. use OC\Repair\Events\RepairInfoEvent;
  53. use OC\Repair\Events\RepairStartEvent;
  54. use OC\Repair\Events\RepairStepEvent;
  55. use OC\Repair\Events\RepairWarningEvent;
  56. use OC\Repair\MoveUpdaterStepFile;
  57. use OC\Repair\NC11\FixMountStorages;
  58. use OC\Repair\NC13\AddLogRotateJob;
  59. use OC\Repair\NC14\AddPreviewBackgroundCleanupJob;
  60. use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob;
  61. use OC\Repair\NC16\CleanupCardDAVPhotoCache;
  62. use OC\Repair\NC16\ClearCollectionsAccessCache;
  63. use OC\Repair\NC18\ResetGeneratedAvatarFlag;
  64. use OC\Repair\NC20\EncryptionLegacyCipher;
  65. use OC\Repair\NC20\EncryptionMigration;
  66. use OC\Repair\NC20\ShippedDashboardEnable;
  67. use OC\Repair\NC21\AddCheckForUserCertificatesJob;
  68. use OC\Repair\NC21\ValidatePhoneNumber;
  69. use OC\Repair\NC22\LookupServerSendCheck;
  70. use OC\Repair\NC24\AddTokenCleanupJob;
  71. use OC\Repair\NC25\AddMissingSecretJob;
  72. use OC\Repair\OldGroupMembershipShares;
  73. use OC\Repair\Owncloud\CleanPreviews;
  74. use OC\Repair\Owncloud\DropAccountTermsTable;
  75. use OC\Repair\Owncloud\MigrateOauthTables;
  76. use OC\Repair\Owncloud\MoveAvatars;
  77. use OC\Repair\Owncloud\SaveAccountsTableData;
  78. use OC\Repair\Owncloud\UpdateLanguageCodes;
  79. use OC\Repair\RemoveLinkShares;
  80. use OC\Repair\RepairDavShares;
  81. use OC\Repair\RepairInvalidShares;
  82. use OC\Repair\RepairLogoDimension;
  83. use OC\Repair\RepairMimeTypes;
  84. use OC\Template\JSCombiner;
  85. use OCA\DAV\Migration\DeleteSchedulingObjects;
  86. use OCP\AppFramework\QueryException;
  87. use OCP\AppFramework\Utility\ITimeFactory;
  88. use OCP\Collaboration\Resources\IManager;
  89. use OCP\EventDispatcher\IEventDispatcher;
  90. use OCP\Migration\IOutput;
  91. use OCP\Migration\IRepairStep;
  92. use Psr\Log\LoggerInterface;
  93. use Throwable;
  94. class Repair implements IOutput {
  95. /** @var IRepairStep[] */
  96. private array $repairSteps = [];
  97. private string $currentStep;
  98. public function __construct(
  99. private IEventDispatcher $dispatcher,
  100. private LoggerInterface $logger
  101. ) {
  102. }
  103. /** @param IRepairStep[] $repairSteps */
  104. public function setRepairSteps(array $repairSteps): void {
  105. $this->repairSteps = $repairSteps;
  106. }
  107. /**
  108. * Run a series of repair steps for common problems
  109. */
  110. public function run() {
  111. if (count($this->repairSteps) === 0) {
  112. $this->dispatcher->dispatchTyped(new RepairInfoEvent('No repair steps available'));
  113. return;
  114. }
  115. // run each repair step
  116. foreach ($this->repairSteps as $step) {
  117. $this->currentStep = $step->getName();
  118. $this->dispatcher->dispatchTyped(new RepairStepEvent($this->currentStep));
  119. try {
  120. $step->run($this);
  121. } catch (\Exception $e) {
  122. $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]);
  123. $this->dispatcher->dispatchTyped(new RepairErrorEvent($e->getMessage()));
  124. }
  125. }
  126. }
  127. /**
  128. * Add repair step
  129. *
  130. * @param IRepairStep|string $repairStep repair step
  131. * @throws \Exception
  132. */
  133. public function addStep($repairStep) {
  134. if (is_string($repairStep)) {
  135. try {
  136. $s = \OC::$server->get($repairStep);
  137. } catch (QueryException $e) {
  138. if (class_exists($repairStep)) {
  139. try {
  140. // Last resort: hope there are no constructor arguments
  141. $s = new $repairStep();
  142. } catch (Throwable $inner) {
  143. // Well, it was worth a try
  144. throw new \Exception("Repair step '$repairStep' can't be instantiated: " . $e->getMessage(), 0, $e);
  145. }
  146. } else {
  147. throw new \Exception("Repair step '$repairStep' is unknown", 0, $e);
  148. }
  149. }
  150. if ($s instanceof IRepairStep) {
  151. $this->repairSteps[] = $s;
  152. } else {
  153. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  154. }
  155. } else {
  156. $this->repairSteps[] = $repairStep;
  157. }
  158. }
  159. /**
  160. * Returns the default repair steps to be run on the
  161. * command line or after an upgrade.
  162. *
  163. * @return IRepairStep[]
  164. */
  165. public static function getRepairSteps(): array {
  166. return [
  167. new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), \OC::$server->getDatabaseConnection(), false),
  168. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  169. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  170. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  171. new MoveAvatars(
  172. \OC::$server->getJobList(),
  173. \OC::$server->getConfig()
  174. ),
  175. new CleanPreviews(
  176. \OC::$server->getJobList(),
  177. \OC::$server->getUserManager(),
  178. \OC::$server->getConfig()
  179. ),
  180. new MigrateOauthTables(\OC::$server->get(Connection::class)),
  181. new FixMountStorages(\OC::$server->getDatabaseConnection()),
  182. new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
  183. new AddLogRotateJob(\OC::$server->getJobList()),
  184. new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OCP\Server::get(JSCombiner::class)),
  185. \OCP\Server::get(ClearGeneratedAvatarCache::class),
  186. new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
  187. new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
  188. new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->get(LoggerInterface::class)),
  189. new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
  190. new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OCP\Server::get(ITimeFactory::class)),
  191. new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OCP\Server::get(IManager::class)),
  192. \OCP\Server::get(ResetGeneratedAvatarFlag::class),
  193. \OCP\Server::get(EncryptionLegacyCipher::class),
  194. \OCP\Server::get(EncryptionMigration::class),
  195. \OCP\Server::get(ShippedDashboardEnable::class),
  196. \OCP\Server::get(AddBruteForceCleanupJob::class),
  197. \OCP\Server::get(AddCheckForUserCertificatesJob::class),
  198. \OCP\Server::get(RepairDavShares::class),
  199. \OCP\Server::get(LookupServerSendCheck::class),
  200. \OCP\Server::get(AddTokenCleanupJob::class),
  201. \OCP\Server::get(CleanUpAbandonedApps::class),
  202. \OCP\Server::get(AddMissingSecretJob::class),
  203. \OCP\Server::get(AddRemoveOldTasksBackgroundJob::class),
  204. \OCP\Server::get(AddMetadataGenerationJob::class),
  205. \OCP\Server::get(AddAppConfigLazyMigration::class),
  206. \OCP\Server::get(RepairLogoDimension::class),
  207. \OCP\Server::get(AddCleanupDeletedUsersBackgroundJob::class),
  208. ];
  209. }
  210. /**
  211. * Returns expensive repair steps to be run on the
  212. * command line with a special option.
  213. *
  214. * @return IRepairStep[]
  215. */
  216. public static function getExpensiveRepairSteps() {
  217. return [
  218. new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
  219. new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  220. \OC::$server->get(ValidatePhoneNumber::class),
  221. \OC::$server->get(DeleteSchedulingObjects::class),
  222. ];
  223. }
  224. /**
  225. * Returns the repair steps to be run before an
  226. * upgrade.
  227. *
  228. * @return IRepairStep[]
  229. */
  230. public static function getBeforeUpgradeRepairSteps() {
  231. /** @var ConnectionAdapter $connectionAdapter */
  232. $connectionAdapter = \OC::$server->get(ConnectionAdapter::class);
  233. $config = \OC::$server->getConfig();
  234. $steps = [
  235. new Collation(\OC::$server->getConfig(), \OC::$server->get(LoggerInterface::class), $connectionAdapter, true),
  236. new SaveAccountsTableData($connectionAdapter, $config),
  237. new DropAccountTermsTable($connectionAdapter),
  238. ];
  239. return $steps;
  240. }
  241. public function debug(string $message): void {
  242. }
  243. /**
  244. * @param string $message
  245. */
  246. public function info($message) {
  247. // for now just emit as we did in the past
  248. $this->dispatcher->dispatchTyped(new RepairInfoEvent($message));
  249. }
  250. /**
  251. * @param string $message
  252. */
  253. public function warning($message) {
  254. // for now just emit as we did in the past
  255. $this->dispatcher->dispatchTyped(new RepairWarningEvent($message));
  256. }
  257. /**
  258. * @param int $max
  259. */
  260. public function startProgress($max = 0) {
  261. // for now just emit as we did in the past
  262. $this->dispatcher->dispatchTyped(new RepairStartEvent($max, $this->currentStep));
  263. }
  264. /**
  265. * @param int $step number of step to advance
  266. * @param string $description
  267. */
  268. public function advance($step = 1, $description = '') {
  269. // for now just emit as we did in the past
  270. $this->dispatcher->dispatchTyped(new RepairAdvanceEvent($step, $description));
  271. }
  272. /**
  273. * @param int $max
  274. */
  275. public function finishProgress() {
  276. // for now just emit as we did in the past
  277. $this->dispatcher->dispatchTyped(new RepairFinishEvent());
  278. }
  279. }