1
0

Repair.php 9.9 KB

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