Repair.php 7.0 KB

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