Repair.php 6.2 KB

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