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.4KB

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