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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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\OldGroupMembershipShares;
  54. use OC\Repair\Owncloud\DropAccountTermsTable;
  55. use OC\Repair\Owncloud\SaveAccountsTableData;
  56. use OC\Repair\RemoveLinkShares;
  57. use OC\Repair\RepairInvalidShares;
  58. use OC\Repair\RepairMimeTypes;
  59. use OC\Repair\SqliteAutoincrement;
  60. use OC\Template\JSCombiner;
  61. use OC\Template\SCSSCacher;
  62. use OCP\AppFramework\QueryException;
  63. use OCP\AppFramework\Utility\ITimeFactory;
  64. use OCP\Collaboration\Resources\IManager;
  65. use OCP\Migration\IOutput;
  66. use OCP\Migration\IRepairStep;
  67. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  68. use Symfony\Component\EventDispatcher\GenericEvent;
  69. class Repair implements IOutput {
  70. /** @var IRepairStep[] */
  71. private $repairSteps;
  72. /** @var EventDispatcherInterface */
  73. private $dispatcher;
  74. /** @var string */
  75. private $currentStep;
  76. /**
  77. * Creates a new repair step runner
  78. *
  79. * @param IRepairStep[] $repairSteps array of RepairStep instances
  80. * @param EventDispatcherInterface $dispatcher
  81. */
  82. public function __construct(array $repairSteps, EventDispatcherInterface $dispatcher) {
  83. $this->repairSteps = $repairSteps;
  84. $this->dispatcher = $dispatcher;
  85. }
  86. /**
  87. * Run a series of repair steps for common problems
  88. */
  89. public function run() {
  90. if (count($this->repairSteps) === 0) {
  91. $this->emit('\OC\Repair', 'info', ['No repair steps available']);
  92. return;
  93. }
  94. // run each repair step
  95. foreach ($this->repairSteps as $step) {
  96. $this->currentStep = $step->getName();
  97. $this->emit('\OC\Repair', 'step', [$this->currentStep]);
  98. $step->run($this);
  99. }
  100. }
  101. /**
  102. * Add repair step
  103. *
  104. * @param IRepairStep|string $repairStep repair step
  105. * @throws \Exception
  106. */
  107. public function addStep($repairStep) {
  108. if (is_string($repairStep)) {
  109. try {
  110. $s = \OC::$server->query($repairStep);
  111. } catch (QueryException $e) {
  112. if (class_exists($repairStep)) {
  113. $s = new $repairStep();
  114. } else {
  115. throw new \Exception("Repair step '$repairStep' is unknown");
  116. }
  117. }
  118. if ($s instanceof IRepairStep) {
  119. $this->repairSteps[] = $s;
  120. } else {
  121. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  122. }
  123. } else {
  124. $this->repairSteps[] = $repairStep;
  125. }
  126. }
  127. /**
  128. * Returns the default repair steps to be run on the
  129. * command line or after an upgrade.
  130. *
  131. * @return IRepairStep[]
  132. */
  133. public static function getRepairSteps() {
  134. return [
  135. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
  136. new RepairMimeTypes(\OC::$server->getConfig()),
  137. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  138. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  139. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  140. new FixMountStorages(\OC::$server->getDatabaseConnection()),
  141. new AddLogRotateJob(\OC::$server->getJobList()),
  142. new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
  143. new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
  144. new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
  145. new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
  146. new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->getLogger()),
  147. new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
  148. new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OC::$server->query(ITimeFactory::class)),
  149. new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
  150. \OC::$server->query(ResetGeneratedAvatarFlag::class),
  151. \OC::$server->query(EncryptionLegacyCipher::class),
  152. \OC::$server->query(EncryptionMigration::class),
  153. \OC::$server->get(ShippedDashboardEnable::class),
  154. \OC::$server->get(AddBruteForceCleanupJob::class),
  155. ];
  156. }
  157. /**
  158. * Returns expensive repair steps to be run on the
  159. * command line with a special option.
  160. *
  161. * @return IRepairStep[]
  162. */
  163. public static function getExpensiveRepairSteps() {
  164. return [
  165. new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
  166. ];
  167. }
  168. /**
  169. * Returns the repair steps to be run before an
  170. * upgrade.
  171. *
  172. * @return IRepairStep[]
  173. */
  174. public static function getBeforeUpgradeRepairSteps() {
  175. $connection = \OC::$server->getDatabaseConnection();
  176. $config = \OC::$server->getConfig();
  177. $steps = [
  178. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
  179. new SqliteAutoincrement($connection),
  180. new SaveAccountsTableData($connection, $config),
  181. new DropAccountTermsTable($connection)
  182. ];
  183. return $steps;
  184. }
  185. /**
  186. * @param string $scope
  187. * @param string $method
  188. * @param array $arguments
  189. */
  190. public function emit($scope, $method, array $arguments = []) {
  191. if (!is_null($this->dispatcher)) {
  192. $this->dispatcher->dispatch("$scope::$method",
  193. new GenericEvent("$scope::$method", $arguments));
  194. }
  195. }
  196. public function info($string) {
  197. // for now just emit as we did in the past
  198. $this->emit('\OC\Repair', 'info', [$string]);
  199. }
  200. /**
  201. * @param string $message
  202. */
  203. public function warning($message) {
  204. // for now just emit as we did in the past
  205. $this->emit('\OC\Repair', 'warning', [$message]);
  206. }
  207. /**
  208. * @param int $max
  209. */
  210. public function startProgress($max = 0) {
  211. // for now just emit as we did in the past
  212. $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
  213. }
  214. /**
  215. * @param int $step
  216. * @param string $description
  217. */
  218. public function advance($step = 1, $description = '') {
  219. // for now just emit as we did in the past
  220. $this->emit('\OC\Repair', 'advance', [$step, $description]);
  221. }
  222. /**
  223. * @param int $max
  224. */
  225. public function finishProgress() {
  226. // for now just emit as we did in the past
  227. $this->emit('\OC\Repair', 'finishProgress', []);
  228. }
  229. }