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 7.8KB

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