Repair.php 6.6 KB

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