Repair.php 6.7 KB

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