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