Repair.php 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  6. * @author Daniel Kesselberg <mail@danielkesselberg.de>
  7. * @author Georg Ehrke <oc.list@georgehrke.com>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author John Molakvoæ (skjnldsv) <skjnldsv@protonmail.com>
  10. * @author Julius Härtl <jus@bitgrid.net>
  11. * @author Lukas Reschke <lukas@statuscode.ch>
  12. * @author Michael Weimann <mail@michael-weimann.eu>
  13. * @author Morris Jobke <hey@morrisjobke.de>
  14. * @author Robin Appelman <robin@icewind.nl>
  15. * @author Roeland Jago Douma <roeland@famdouma.nl>
  16. * @author Thomas Müller <thomas.mueller@tmit.eu>
  17. * @author Vincent Petry <pvince81@owncloud.com>
  18. *
  19. * @license AGPL-3.0
  20. *
  21. * This code is free software: you can redistribute it and/or modify
  22. * it under the terms of the GNU Affero General Public License, version 3,
  23. * as published by the Free Software Foundation.
  24. *
  25. * This program is distributed in the hope that it will be useful,
  26. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  27. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  28. * GNU Affero General Public License for more details.
  29. *
  30. * You should have received a copy of the GNU Affero General Public License, version 3,
  31. * along with this program. If not, see <http://www.gnu.org/licenses/>
  32. *
  33. */
  34. namespace OC;
  35. use OC\App\AppStore\Bundles\BundleFetcher;
  36. use OC\Avatar\AvatarManager;
  37. use OC\Repair\AddCleanupUpdaterBackupsJob;
  38. use OC\Repair\CleanTags;
  39. use OC\Repair\ClearFrontendCaches;
  40. use OC\Repair\ClearGeneratedAvatarCache;
  41. use OC\Repair\Collation;
  42. use OC\Repair\MoveUpdaterStepFile;
  43. use OC\Repair\NC22\LookupServerSendCheck;
  44. use OC\Repair\Owncloud\CleanPreviews;
  45. use OC\Repair\NC11\FixMountStorages;
  46. use OC\Repair\Owncloud\MoveAvatars;
  47. use OC\Repair\Owncloud\InstallCoreBundle;
  48. use OC\Repair\Owncloud\UpdateLanguageCodes;
  49. use OC\Repair\NC13\AddLogRotateJob;
  50. use OC\Repair\NC14\AddPreviewBackgroundCleanupJob;
  51. use OC\Repair\NC16\AddClenupLoginFlowV2BackgroundJob;
  52. use OC\Repair\NC16\CleanupCardDAVPhotoCache;
  53. use OC\Repair\NC16\ClearCollectionsAccessCache;
  54. use OC\Repair\NC18\ResetGeneratedAvatarFlag;
  55. use OC\Repair\NC20\EncryptionLegacyCipher;
  56. use OC\Repair\NC20\EncryptionMigration;
  57. use OC\Repair\NC20\ShippedDashboardEnable;
  58. use OC\Repair\OldGroupMembershipShares;
  59. use OC\Repair\Owncloud\DropAccountTermsTable;
  60. use OC\Repair\Owncloud\SaveAccountsTableData;
  61. use OC\Repair\RemoveLinkShares;
  62. use OC\Repair\RepairDavShares;
  63. use OC\Repair\RepairInvalidShares;
  64. use OC\Repair\RepairMimeTypes;
  65. use OC\Repair\SqliteAutoincrement;
  66. use OC\Template\JSCombiner;
  67. use OC\Template\SCSSCacher;
  68. use OCP\AppFramework\QueryException;
  69. use OCP\AppFramework\Utility\ITimeFactory;
  70. use OCP\Collaboration\Resources\IManager;
  71. use OCP\Migration\IOutput;
  72. use OCP\Migration\IRepairStep;
  73. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  74. use Symfony\Component\EventDispatcher\GenericEvent;
  75. class Repair implements IOutput {
  76. /** @var IRepairStep[] */
  77. private $repairSteps;
  78. /** @var EventDispatcherInterface */
  79. private $dispatcher;
  80. /** @var string */
  81. private $currentStep;
  82. /**
  83. * Creates a new repair step runner
  84. *
  85. * @param IRepairStep[] $repairSteps array of RepairStep instances
  86. * @param EventDispatcherInterface $dispatcher
  87. */
  88. public function __construct(array $repairSteps, EventDispatcherInterface $dispatcher) {
  89. $this->repairSteps = $repairSteps;
  90. $this->dispatcher = $dispatcher;
  91. }
  92. /**
  93. * Run a series of repair steps for common problems
  94. */
  95. public function run() {
  96. if (count($this->repairSteps) === 0) {
  97. $this->emit('\OC\Repair', 'info', ['No repair steps available']);
  98. return;
  99. }
  100. // run each repair step
  101. foreach ($this->repairSteps as $step) {
  102. $this->currentStep = $step->getName();
  103. $this->emit('\OC\Repair', 'step', [$this->currentStep]);
  104. $step->run($this);
  105. }
  106. }
  107. /**
  108. * Add repair step
  109. *
  110. * @param IRepairStep|string $repairStep repair step
  111. * @throws \Exception
  112. */
  113. public function addStep($repairStep) {
  114. if (is_string($repairStep)) {
  115. try {
  116. $s = \OC::$server->query($repairStep);
  117. } catch (QueryException $e) {
  118. if (class_exists($repairStep)) {
  119. $s = new $repairStep();
  120. } else {
  121. throw new \Exception("Repair step '$repairStep' is unknown");
  122. }
  123. }
  124. if ($s instanceof IRepairStep) {
  125. $this->repairSteps[] = $s;
  126. } else {
  127. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  128. }
  129. } else {
  130. $this->repairSteps[] = $repairStep;
  131. }
  132. }
  133. /**
  134. * Returns the default repair steps to be run on the
  135. * command line or after an upgrade.
  136. *
  137. * @return IRepairStep[]
  138. */
  139. public static function getRepairSteps() {
  140. return [
  141. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
  142. new RepairMimeTypes(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  143. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  144. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  145. new MoveUpdaterStepFile(\OC::$server->getConfig()),
  146. new MoveAvatars(
  147. \OC::$server->getJobList(),
  148. \OC::$server->getConfig()
  149. ),
  150. new CleanPreviews(
  151. \OC::$server->getJobList(),
  152. \OC::$server->getUserManager(),
  153. \OC::$server->getConfig()
  154. ),
  155. new FixMountStorages(\OC::$server->getDatabaseConnection()),
  156. new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
  157. new InstallCoreBundle(
  158. \OC::$server->query(BundleFetcher::class),
  159. \OC::$server->getConfig(),
  160. \OC::$server->query(Installer::class)
  161. ),
  162. new AddLogRotateJob(\OC::$server->getJobList()),
  163. new ClearFrontendCaches(\OC::$server->getMemCacheFactory(), \OC::$server->query(SCSSCacher::class), \OC::$server->query(JSCombiner::class)),
  164. new ClearGeneratedAvatarCache(\OC::$server->getConfig(), \OC::$server->query(AvatarManager::class)),
  165. new AddPreviewBackgroundCleanupJob(\OC::$server->getJobList()),
  166. new AddCleanupUpdaterBackupsJob(\OC::$server->getJobList()),
  167. new CleanupCardDAVPhotoCache(\OC::$server->getConfig(), \OC::$server->getAppDataDir('dav-photocache'), \OC::$server->getLogger()),
  168. new AddClenupLoginFlowV2BackgroundJob(\OC::$server->getJobList()),
  169. new RemoveLinkShares(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig(), \OC::$server->getGroupManager(), \OC::$server->getNotificationManager(), \OC::$server->query(ITimeFactory::class)),
  170. new ClearCollectionsAccessCache(\OC::$server->getConfig(), \OC::$server->query(IManager::class)),
  171. \OC::$server->query(ResetGeneratedAvatarFlag::class),
  172. \OC::$server->query(EncryptionLegacyCipher::class),
  173. \OC::$server->query(EncryptionMigration::class),
  174. \OC::$server->get(ShippedDashboardEnable::class),
  175. \OC::$server->get(RepairDavShares::class),
  176. \OC::$server->get(LookupServerSendCheck::class),
  177. ];
  178. }
  179. /**
  180. * Returns expensive repair steps to be run on the
  181. * command line with a special option.
  182. *
  183. * @return IRepairStep[]
  184. */
  185. public static function getExpensiveRepairSteps() {
  186. return [
  187. new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
  188. ];
  189. }
  190. /**
  191. * Returns the repair steps to be run before an
  192. * upgrade.
  193. *
  194. * @return IRepairStep[]
  195. */
  196. public static function getBeforeUpgradeRepairSteps() {
  197. $connection = \OC::$server->getDatabaseConnection();
  198. $config = \OC::$server->getConfig();
  199. $steps = [
  200. new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
  201. new SqliteAutoincrement($connection),
  202. new SaveAccountsTableData($connection, $config),
  203. new DropAccountTermsTable($connection),
  204. ];
  205. return $steps;
  206. }
  207. /**
  208. * @param string $scope
  209. * @param string $method
  210. * @param array $arguments
  211. */
  212. public function emit($scope, $method, array $arguments = []) {
  213. if (!is_null($this->dispatcher)) {
  214. $this->dispatcher->dispatch("$scope::$method",
  215. new GenericEvent("$scope::$method", $arguments));
  216. }
  217. }
  218. public function info($string) {
  219. // for now just emit as we did in the past
  220. $this->emit('\OC\Repair', 'info', [$string]);
  221. }
  222. /**
  223. * @param string $message
  224. */
  225. public function warning($message) {
  226. // for now just emit as we did in the past
  227. $this->emit('\OC\Repair', 'warning', [$message]);
  228. }
  229. /**
  230. * @param int $max
  231. */
  232. public function startProgress($max = 0) {
  233. // for now just emit as we did in the past
  234. $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
  235. }
  236. /**
  237. * @param int $step
  238. * @param string $description
  239. */
  240. public function advance($step = 1, $description = '') {
  241. // for now just emit as we did in the past
  242. $this->emit('\OC\Repair', 'advance', [$step, $description]);
  243. }
  244. /**
  245. * @param int $max
  246. */
  247. public function finishProgress() {
  248. // for now just emit as we did in the past
  249. $this->emit('\OC\Repair', 'finishProgress', []);
  250. }
  251. }