Repair.php 10.0 KB

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