Repair.php 10.0 KB

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