Repair.php 7.2 KB

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