Completed
Push — master ( ca3aef...5b2d6b )
by Morris
15:49
created

Repair::getRepairSteps()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 22
nc 1
nop 0
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
/**
3
 * @copyright Copyright (c) 2016, ownCloud, Inc.
4
 *
5
 * @author Arthur Schiwon <[email protected]>
6
 * @author Georg Ehrke <[email protected]>
7
 * @author Joas Schilling <[email protected]>
8
 * @author Lukas Reschke <[email protected]>
9
 * @author Morris Jobke <[email protected]>
10
 * @author Robin Appelman <[email protected]>
11
 * @author Roeland Jago Douma <[email protected]>
12
 * @author Thomas Müller <[email protected]>
13
 * @author Vincent Petry <[email protected]>
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
31
namespace OC;
32
33
use OC\App\AppStore\Bundles\BundleFetcher;
34
use OC\Repair\CleanTags;
35
use OC\Repair\Collation;
36
use OC\Repair\MoveUpdaterStepFile;
37
use OC\Repair\NC11\CleanPreviews;
38
use OC\Repair\NC11\FixMountStorages;
39
use OC\Repair\NC11\MoveAvatars;
40
use OC\Repair\NC12\InstallCoreBundle;
41
use OC\Repair\NC12\UpdateLanguageCodes;
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
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
	/**
65
	 * Creates a new repair step runner
66
	 *
67
	 * @param IRepairStep[] $repairSteps array of RepairStep instances
68
	 * @param EventDispatcher $dispatcher
69
	 */
70
	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
71
		$this->repairSteps = $repairSteps;
72
		$this->dispatcher = $dispatcher;
73
	}
74
75
	/**
76
	 * Run a series of repair steps for common problems
77
	 */
78
	public function run() {
79
		if (count($this->repairSteps) === 0) {
80
			$this->emit('\OC\Repair', 'info', array('No repair steps available'));
81
			return;
82
		}
83
		// run each repair step
84
		foreach ($this->repairSteps as $step) {
85
			$this->currentStep = $step->getName();
86
			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
87
			$step->run($this);
88
		}
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
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
	/**
120
	 * Returns the default repair steps to be run on the
121
	 * command line or after an upgrade.
122
	 *
123
	 * @return IRepairStep[]
124
	 */
125
	public static function getRepairSteps() {
126
		return [
127
			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), \OC::$server->getDatabaseConnection(), false),
128
			new RepairMimeTypes(\OC::$server->getConfig()),
129
			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
130
			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
131
			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
132
			new MoveUpdaterStepFile(\OC::$server->getConfig()),
133
			new MoveAvatars(
134
				\OC::$server->getJobList(),
135
				\OC::$server->getConfig()
136
			),
137
			new CleanPreviews(
138
				\OC::$server->getJobList(),
139
				\OC::$server->getUserManager(),
140
				\OC::$server->getConfig()
141
			),
142
			new FixMountStorages(\OC::$server->getDatabaseConnection()),
143
			new UpdateLanguageCodes(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig()),
144
			new InstallCoreBundle(
145
				\OC::$server->query(BundleFetcher::class),
146
				\OC::$server->getConfig(),
147
				\OC::$server->query(Installer::class)
148
			),
149
			new RepairInvalidPaths(\OC::$server->getDatabaseConnection(), \OC::$server->getConfig())
150
		];
151
	}
152
153
	/**
154
	 * Returns expensive repair steps to be run on the
155
	 * command line with a special option.
156
	 *
157
	 * @return IRepairStep[]
158
	 */
159
	public static function getExpensiveRepairSteps() {
160
		return [
161
			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager())
162
		];
163
	}
164
165
	/**
166
	 * Returns the repair steps to be run before an
167
	 * upgrade.
168
	 *
169
	 * @return IRepairStep[]
170
	 */
171
	public static function getBeforeUpgradeRepairSteps() {
172
		$connection = \OC::$server->getDatabaseConnection();
173
		$config = \OC::$server->getConfig();
174
		$steps = [
175
			new Collation(\OC::$server->getConfig(), \OC::$server->getLogger(), $connection, true),
176
			new SqliteAutoincrement($connection),
0 ignored issues
show
Compatibility introduced by
$connection of type object<OCP\IDBConnection> is not a sub-type of object<OC\DB\Connection>. It seems like you assume a concrete implementation of the interface OCP\IDBConnection to be always present.

This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.

Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.

Loading history...
177
			new SaveAccountsTableData($connection, $config),
178
			new DropAccountTermsTable($connection),
179
		];
180
181
		return $steps;
182
	}
183
184
	/**
185
	 * @param string $scope
186
	 * @param string $method
187
	 * @param array $arguments
188
	 */
189
	public function emit($scope, $method, array $arguments = []) {
190
		if (!is_null($this->dispatcher)) {
191
			$this->dispatcher->dispatch("$scope::$method",
192
				new GenericEvent("$scope::$method", $arguments));
193
		}
194
	}
195
196
	public function info($string) {
197
		// for now just emit as we did in the past
198
		$this->emit('\OC\Repair', 'info', array($string));
199
	}
200
201
	/**
202
	 * @param string $message
203
	 */
204
	public function warning($message) {
205
		// for now just emit as we did in the past
206
		$this->emit('\OC\Repair', 'warning', [$message]);
207
	}
208
209
	/**
210
	 * @param int $max
211
	 */
212
	public function startProgress($max = 0) {
213
		// for now just emit as we did in the past
214
		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
215
	}
216
217
	/**
218
	 * @param int $step
219
	 * @param string $description
220
	 */
221
	public function advance($step = 1, $description = '') {
222
		// for now just emit as we did in the past
223
		$this->emit('\OC\Repair', 'advance', [$step, $description]);
224
	}
225
226
	/**
227
	 * @param int $max
0 ignored issues
show
Bug introduced by
There is no parameter named $max. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
228
	 */
229
	public function finishProgress() {
230
		// for now just emit as we did in the past
231
		$this->emit('\OC\Repair', 'finishProgress', []);
232
	}
233
}
234