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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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\Repair\AssetCache;
  32. use OC\Repair\AvatarPermissions;
  33. use OC\Repair\CleanTags;
  34. use OC\Repair\Collation;
  35. use OC\Repair\DropOldJobs;
  36. use OC\Repair\OldGroupMembershipShares;
  37. use OC\Repair\RemoveGetETagEntries;
  38. use OC\Repair\RemoveOldShares;
  39. use OC\Repair\RemoveRootShares;
  40. use OC\Repair\SharePropagation;
  41. use OC\Repair\SqliteAutoincrement;
  42. use OC\Repair\DropOldTables;
  43. use OC\Repair\FillETags;
  44. use OC\Repair\InnoDB;
  45. use OC\Repair\RepairLegacyStorages;
  46. use OC\Repair\RepairMimeTypes;
  47. use OC\Repair\SearchLuceneTables;
  48. use OC\Repair\UpdateOutdatedOcsIds;
  49. use OC\Repair\RepairInvalidShares;
  50. use OC\Repair\RepairUnmergedShares;
  51. use OCP\AppFramework\QueryException;
  52. use OCP\Migration\IOutput;
  53. use OCP\Migration\IRepairStep;
  54. use Symfony\Component\EventDispatcher\EventDispatcher;
  55. use Symfony\Component\EventDispatcher\GenericEvent;
  56. class Repair implements IOutput{
  57. /* @var IRepairStep[] */
  58. private $repairSteps;
  59. /** @var EventDispatcher */
  60. private $dispatcher;
  61. /** @var string */
  62. private $currentStep;
  63. /**
  64. * Creates a new repair step runner
  65. *
  66. * @param IRepairStep[] $repairSteps array of RepairStep instances
  67. * @param EventDispatcher $dispatcher
  68. */
  69. public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
  70. $this->repairSteps = $repairSteps;
  71. $this->dispatcher = $dispatcher;
  72. }
  73. /**
  74. * Run a series of repair steps for common problems
  75. */
  76. public function run() {
  77. if (count($this->repairSteps) === 0) {
  78. $this->emit('\OC\Repair', 'info', array('No repair steps available'));
  79. return;
  80. }
  81. // run each repair step
  82. foreach ($this->repairSteps as $step) {
  83. $this->currentStep = $step->getName();
  84. $this->emit('\OC\Repair', 'step', [$this->currentStep]);
  85. $step->run($this);
  86. }
  87. }
  88. /**
  89. * Add repair step
  90. *
  91. * @param IRepairStep|string $repairStep repair step
  92. * @throws \Exception
  93. */
  94. public function addStep($repairStep) {
  95. if (is_string($repairStep)) {
  96. try {
  97. $s = \OC::$server->query($repairStep);
  98. } catch (QueryException $e) {
  99. if (class_exists($repairStep)) {
  100. $s = new $repairStep();
  101. } else {
  102. throw new \Exception("Repair step '$repairStep' is unknown");
  103. }
  104. }
  105. if ($s instanceof IRepairStep) {
  106. $this->repairSteps[] = $s;
  107. } else {
  108. throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
  109. }
  110. } else {
  111. $this->repairSteps[] = $repairStep;
  112. }
  113. }
  114. /**
  115. * Returns the default repair steps to be run on the
  116. * command line or after an upgrade.
  117. *
  118. * @return IRepairStep[]
  119. */
  120. public static function getRepairSteps() {
  121. return [
  122. new RepairMimeTypes(\OC::$server->getConfig()),
  123. new RepairLegacyStorages(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  124. new AssetCache(),
  125. new FillETags(\OC::$server->getDatabaseConnection()),
  126. new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
  127. new DropOldTables(\OC::$server->getDatabaseConnection()),
  128. new DropOldJobs(\OC::$server->getJobList()),
  129. new RemoveGetETagEntries(\OC::$server->getDatabaseConnection()),
  130. new UpdateOutdatedOcsIds(\OC::$server->getConfig()),
  131. new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
  132. new SharePropagation(\OC::$server->getConfig()),
  133. new RemoveOldShares(\OC::$server->getDatabaseConnection()),
  134. new AvatarPermissions(\OC::$server->getDatabaseConnection()),
  135. new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
  136. new RepairUnmergedShares(
  137. \OC::$server->getConfig(),
  138. \OC::$server->getDatabaseConnection(),
  139. \OC::$server->getUserManager(),
  140. \OC::$server->getGroupManager()
  141. ),
  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. $steps = [
  164. new InnoDB(),
  165. new Collation(\OC::$server->getConfig(), $connection),
  166. new SqliteAutoincrement($connection),
  167. new SearchLuceneTables(),
  168. ];
  169. //There is no need to delete all previews on every single update
  170. //only 7.0.0 through 7.0.2 generated broken previews
  171. $currentVersion = \OC::$server->getConfig()->getSystemValue('version');
  172. if (version_compare($currentVersion, '7.0.0.0', '>=') &&
  173. version_compare($currentVersion, '7.0.3.4', '<=')) {
  174. $steps[] = new \OC\Repair\Preview();
  175. }
  176. return $steps;
  177. }
  178. /**
  179. * @param string $scope
  180. * @param string $method
  181. * @param array $arguments
  182. */
  183. public function emit($scope, $method, array $arguments = []) {
  184. if (!is_null($this->dispatcher)) {
  185. $this->dispatcher->dispatch("$scope::$method",
  186. new GenericEvent("$scope::$method", $arguments));
  187. }
  188. }
  189. public function info($string) {
  190. // for now just emit as we did in the past
  191. $this->emit('\OC\Repair', 'info', array($string));
  192. }
  193. /**
  194. * @param string $message
  195. */
  196. public function warning($message) {
  197. // for now just emit as we did in the past
  198. $this->emit('\OC\Repair', 'warning', [$message]);
  199. }
  200. /**
  201. * @param int $max
  202. */
  203. public function startProgress($max = 0) {
  204. // for now just emit as we did in the past
  205. $this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
  206. }
  207. /**
  208. * @param int $step
  209. * @param string $description
  210. */
  211. public function advance($step = 1, $description = '') {
  212. // for now just emit as we did in the past
  213. $this->emit('\OC\Repair', 'advance', [$step, $description]);
  214. }
  215. /**
  216. * @param int $max
  217. */
  218. public function finishProgress() {
  219. // for now just emit as we did in the past
  220. $this->emit('\OC\Repair', 'finishProgress', []);
  221. }
  222. }