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

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