Repair.php 9.9 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\NC11\FixMountStorages;
  50. use OC\Repair\Owncloud\MoveAvatars;
  51. use OC\Repair\Owncloud\InstallCoreBundle;
  52. use OC\Repair\Owncloud\UpdateLanguageCodes;
  53. use OC\Repair\NC13\AddLogRotateJob;
  54. use OC\Repair\NC14\AddPreviewBackgroundCleanupJob;
  55. use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob;
  56. use OC\Repair\NC16\CleanupCardDAVPhotoCache;
  57. use OC\Repair\NC16\ClearCollectionsAccessCache;
  58. use OC\Repair\NC18\ResetGeneratedAvatarFlag;
  59. use OC\Repair\NC20\EncryptionLegacyCipher;
  60. use OC\Repair\NC20\EncryptionMigration;
  61. use OC\Repair\NC20\ShippedDashboardEnable;
  62. use OC\Repair\NC21\AddCheckForUserCertificatesJob;
  63. use OC\Repair\NC21\ValidatePhoneNumber;
  64. use OC\Repair\OldGroupMembershipShares;
  65. use OC\Repair\Owncloud\DropAccountTermsTable;
  66. use OC\Repair\Owncloud\SaveAccountsTableData;
  67. use OC\Repair\RemoveLinkShares;
  68. use OC\Repair\RepairDavShares;
  69. use OC\Repair\RepairInvalidShares;
  70. use OC\Repair\RepairMimeTypes;
  71. use OC\Repair\SqliteAutoincrement;
  72. use OC\Template\JSCombiner;
  73. use OC\Template\SCSSCacher;
  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 $logger;
  91. /**
  92. * Creates a new repair step runner
  93. *
  94. * @param IRepairStep[] $repairSteps array of RepairStep instances
  95. * @param EventDispatcherInterface $dispatcher
  96. */
  97. public function __construct(array $repairSteps, EventDispatcherInterface $dispatcher, LoggerInterface $logger) {
  98. $this->repairSteps = $repairSteps;
  99. $this->dispatcher = $dispatcher;
  100. $this->logger = $logger;
  101. }
  102. /**
  103. * Run a series of repair steps for common problems
  104. */
  105. public function run() {
  106. if (count($this->repairSteps) === 0) {
  107. $this->emit('\OC\Repair', 'info', ['No repair steps available']);
  108. return;
  109. }
  110. // run each repair step
  111. foreach ($this->repairSteps as $step) {
  112. $this->currentStep = $step->getName();
  113. $this->emit('\OC\Repair', 'step', [$this->currentStep]);
  114. try {
  115. $step->run($this);
  116. } catch (\Exception $e) {
  117. $this->logger->error("Exception while executing repair step " . $step->getName(), ['exception' => $e]);
  118. $this->emit('\OC\Repair', 'error', [$e->getMessage()]);
  119. }
  120. }
  121. }
  122. /**
  123. * Add repair step
  124. *
  125. * @param IRepairStep|string $repairStep repair step
  126. * @throws \Exception
  127. */
  128. public function addStep($repairStep) {
  129. if (is_string($repairStep)) {
  130. try {
  131. $s = \OC::$server->query($repairStep);
  132. } catch (QueryException $e) {
  133. if (class_exists($repairStep)) {
  134. try {
  135. // Last resort: hope there are no constructor arguments
  136. $s = new $repairStep();
  137. } catch (Throwable $inner) {
  138. // Well, it was worth a try
  139. throw new \Exception("Repair step '$repairStep' can't be instantiated: " . $e->getMessage(), 0, $e);
  140. }
  141. } else {
  142. throw new \Exception("Repair step '$repairStep' is unknown", 0, $e);
  143. }
  144. }
  145. if ($s instanceof IRepairStep) {
  146. $this->repairSteps[] = $s;
  147. } else {
  148. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  149. }
  150. } else {
  151. $this->repairSteps[] = $repairStep;
  152. }
  153. }
  154. /**
  155. * Returns the default repair steps to be run on the
  156. * command line or after an upgrade.
  157. *
  158. * @return IRepairStep[]
  159. */
  160. public static function getRepairSteps() {
  161. return [
  162. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
  163. new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  164. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  165. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  166. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  167. new MoveAvatars(
  168. \OC::$server->getJobList(),
  169. \OC::$server->getConfig()
  170. ),
  171. new CleanPreviews(
  172. \OC::$server->getJobList(),
  173. \OC::$server->getUserManager(),
  174. \OC::$server->getConfig()
  175. ),
  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(SCSSCacher::class), \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->getLogger()),
  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->getLogger(), $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. }