You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Repair.php 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Weimann <mail@michael-weimann.eu>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <pvince81@owncloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC;
  35. use OC\Avatar\AvatarManager;
  36. use OC\Repair\AddBruteForceCleanupJob;
  37. use OC\Repair\AddCleanupUpdaterBackupsJob;
  38. use OC\Repair\CleanTags;
  39. use OC\Repair\ClearFrontendCaches;
  40. use OC\Repair\ClearGeneratedAvatarCache;
  41. use OC\Repair\Collation;
  42. use OC\Repair\MoveUpdaterStepFile;
  43. use OC\Repair\NC11\FixMountStorages;
  44. use OC\Repair\NC13\AddLogRotateJob;
  45. use OC\Repair\NC14\AddPreviewBackgroundCleanupJob;
  46. use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob;
  47. use OC\Repair\NC16\CleanupCardDAVPhotoCache;
  48. use OC\Repair\NC16\ClearCollectionsAccessCache;
  49. use OC\Repair\NC18\ResetGeneratedAvatarFlag;
  50. use OC\Repair\NC20\EncryptionLegacyCipher;
  51. use OC\Repair\NC20\EncryptionMigration;
  52. use OC\Repair\NC20\ShippedDashboardEnable;
  53. use OC\Repair\NC21\AddCheckForUserCertificatesJob;
  54. use OC\Repair\OldGroupMembershipShares;
  55. use OC\Repair\Owncloud\DropAccountTermsTable;
  56. use OC\Repair\Owncloud\SaveAccountsTableData;
  57. use OC\Repair\RemoveLinkShares;
  58. use OC\Repair\RepairInvalidShares;
  59. use OC\Repair\RepairMimeTypes;
  60. use OC\Repair\SqliteAutoincrement;
  61. use OC\Template\JSCombiner;
  62. use OC\Template\SCSSCacher;
  63. use OCP\AppFramework\QueryException;
  64. use OCP\AppFramework\Utility\ITimeFactory;
  65. use OCP\Collaboration\Resources\IManager;
  66. use OCP\Migration\IOutput;
  67. use OCP\Migration\IRepairStep;
  68. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  69. use Symfony\Component\EventDispatcher\GenericEvent;
  70. class Repair implements IOutput {
  71. /** @var IRepairStep[] */
  72. private $repairSteps;
  73. /** @var EventDispatcherInterface */
  74. private $dispatcher;
  75. /** @var string */
  76. private $currentStep;
  77. /**
  78. * Creates a new repair step runner
  79. *
  80. * @param IRepairStep[] $repairSteps array of RepairStep instances
  81. * @param EventDispatcherInterface $dispatcher
  82. */
  83. public function __construct(array $repairSteps, EventDispatcherInterface $dispatcher) {
  84. $this->repairSteps = $repairSteps;
  85. $this->dispatcher = $dispatcher;
  86. }
  87. /**
  88. * Run a series of repair steps for common problems
  89. */
  90. public function run() {
  91. if (count($this->repairSteps) === 0) {
  92. $this->emit('\OC\Repair', 'info', ['No repair steps available']);
  93. return;
  94. }
  95. // run each repair step
  96. foreach ($this->repairSteps as $step) {
  97. $this->currentStep = $step->getName();
  98. $this->emit('\OC\Repair', 'step', [$this->currentStep]);
  99. $step->run($this);
  100. }
  101. }
  102. /**
  103. * Add repair step
  104. *
  105. * @param IRepairStep|string $repairStep repair step
  106. * @throws \Exception
  107. */
  108. public function addStep($repairStep) {
  109. if (is_string($repairStep)) {
  110. try {
  111. $s = \OC::$server->query($repairStep);
  112. } catch (QueryException $e) {
  113. if (class_exists($repairStep)) {
  114. $s = new $repairStep();
  115. } else {
  116. throw new \Exception("Repair step '$repairStep' is unknown");
  117. }
  118. }
  119. if ($s instanceof IRepairStep) {
  120. $this->repairSteps[] = $s;
  121. } else {
  122. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  123. }
  124. } else {
  125. $this->repairSteps[] = $repairStep;
  126. }
  127. }
  128. /**
  129. * Returns the default repair steps to be run on the
  130. * command line or after an upgrade.
  131. *
  132. * @return IRepairStep[]
  133. */
  134. public static function getRepairSteps() {
  135. return [
  136. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
  137. new RepairMimeTypes(\OC::$server->getConfig()),
  138. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  139. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  140. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  141. new FixMountStorages(\OC::$server->getDatabaseConnection()),
  142. new AddLogRotateJob(\OC::$server->getJobList()),
  143. new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
  144. new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
  145. new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
  146. new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
  147. new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->getLogger()),
  148. new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
  149. new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OC::$server->query(ITimeFactory::class)),
  150. new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
  151. \OC::$server->query(ResetGeneratedAvatarFlag::class),
  152. \OC::$server->query(EncryptionLegacyCipher::class),
  153. \OC::$server->query(EncryptionMigration::class),
  154. \OC::$server->get(ShippedDashboardEnable::class),
  155. \OC::$server->get(AddBruteForceCleanupJob::class),
  156. \OC::$server->get(AddCheckForUserCertificatesJob::class),
  157. ];
  158. }
  159. /**
  160. * Returns expensive repair steps to be run on the
  161. * command line with a special option.
  162. *
  163. * @return IRepairStep[]
  164. */
  165. public static function getExpensiveRepairSteps() {
  166. return [
  167. new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
  168. ];
  169. }
  170. /**
  171. * Returns the repair steps to be run before an
  172. * upgrade.
  173. *
  174. * @return IRepairStep[]
  175. */
  176. public static function getBeforeUpgradeRepairSteps() {
  177. $connection = \OC::$server->getDatabaseConnection();
  178. $config = \OC::$server->getConfig();
  179. $steps = [
  180. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
  181. new SqliteAutoincrement($connection),
  182. new SaveAccountsTableData($connection, $config),
  183. new DropAccountTermsTable($connection)
  184. ];
  185. return $steps;
  186. }
  187. /**
  188. * @param string $scope
  189. * @param string $method
  190. * @param array $arguments
  191. */
  192. public function emit($scope, $method, array $arguments = []) {
  193. if (!is_null($this->dispatcher)) {
  194. $this->dispatcher->dispatch("$scope::$method",
  195. new GenericEvent("$scope::$method", $arguments));
  196. }
  197. }
  198. public function info($string) {
  199. // for now just emit as we did in the past
  200. $this->emit('\OC\Repair', 'info', [$string]);
  201. }
  202. /**
  203. * @param string $message
  204. */
  205. public function warning($message) {
  206. // for now just emit as we did in the past
  207. $this->emit('\OC\Repair', 'warning', [$message]);
  208. }
  209. /**
  210. * @param int $max
  211. */
  212. public function startProgress($max = 0) {
  213. // for now just emit as we did in the past
  214. $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
  215. }
  216. /**
  217. * @param int $step
  218. * @param string $description
  219. */
  220. public function advance($step = 1, $description = '') {
  221. // for now just emit as we did in the past
  222. $this->emit('\OC\Repair', 'advance', [$step, $description]);
  223. }
  224. /**
  225. * @param int $max
  226. */
  227. public function finishProgress() {
  228. // for now just emit as we did in the past
  229. $this->emit('\OC\Repair', 'finishProgress', []);
  230. }
  231. }