Completed
Push — master ( e4992c...6d0a35 )
by
unknown
10:42
created

Repair::emit()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 3
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @author Arthur Schiwon <[email protected]>
4
 * @author Georg Ehrke <[email protected]>
5
 * @author Joas Schilling <[email protected]>
6
 * @author Lukas Reschke <[email protected]>
7
 * @author Morris Jobke <[email protected]>
8
 * @author Robin Appelman <[email protected]>
9
 * @author Roeland Jago Douma <[email protected]>
10
 * @author Thomas Müller <[email protected]>
11
 * @author Vincent Petry <[email protected]>
12
 *
13
 * @copyright Copyright (c) 2018, ownCloud GmbH
14
 * @license AGPL-3.0
15
 *
16
 * This code is free software: you can redistribute it and/or modify
17
 * it under the terms of the GNU Affero General Public License, version 3,
18
 * as published by the Free Software Foundation.
19
 *
20
 * This program is distributed in the hope that it will be useful,
21
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23
 * GNU Affero General Public License for more details.
24
 *
25
 * You should have received a copy of the GNU Affero General Public License, version 3,
26
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
27
 *
28
 */
29
30
namespace OC;
31
32
use OC\Repair\Apps;
33
use OC\Repair\CleanTags;
34
use OC\Repair\Collation;
35
use OC\Repair\DisableExtraThemes;
36
use OC\Repair\DropOldJobs;
37
use OC\Repair\OldGroupMembershipShares;
38
use OC\Repair\RemoveGetETagEntries;
39
use OC\Repair\RemoveRootShares;
40
use OC\Repair\RepairOrphanedSubshare;
41
use OC\Repair\RepairSubShares;
42
use OC\Repair\SharePropagation;
43
use OC\Repair\SqliteAutoincrement;
44
use OC\Repair\DropOldTables;
45
use OC\Repair\FillETags;
46
use OC\Repair\InnoDB;
47
use OC\Repair\RepairMimeTypes;
48
use OC\Repair\SearchLuceneTables;
49
use OC\Repair\UpdateOutdatedOcsIds;
50
use OC\Repair\RepairInvalidShares;
51
use OC\Repair\RepairUnmergedShares;
52
use OCP\AppFramework\QueryException;
53
use OCP\Migration\IOutput;
54
use OCP\Migration\IRepairStep;
55
use Symfony\Component\EventDispatcher\EventDispatcher;
56
use Symfony\Component\EventDispatcher\GenericEvent;
57
use OC\Repair\MoveAvatarOutsideHome;
58
use OC\Repair\RepairMismatchFileCachePath;
59
60
class Repair implements IOutput {
61
	/* @var IRepairStep[] */
62
	private $repairSteps;
63
	/** @var EventDispatcher */
64
	private $dispatcher;
65
	/** @var string */
66
	private $currentStep;
67
68
	/**
69
	 * Creates a new repair step runner
70
	 *
71
	 * @param IRepairStep[] $repairSteps array of RepairStep instances
72
	 * @param EventDispatcher $dispatcher
73
	 */
74
	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
75
		$this->repairSteps = $repairSteps;
76
		$this->dispatcher = $dispatcher;
77
	}
78
79
	/**
80
	 * Run a series of repair steps for common problems
81
	 */
82
	public function run() {
83
		if (\count($this->repairSteps) === 0) {
84
			$this->emit('\OC\Repair', 'info', ['No repair steps available']);
85
			return;
86
		}
87
		// run each repair step
88
		foreach ($this->repairSteps as $step) {
89
			$this->currentStep = $step->getName();
90
			$this->emit('\OC\Repair', 'step', [$this->currentStep]);
91
			$step->run($this);
92
		}
93
	}
94
95
	/**
96
	 * Add repair step
97
	 *
98
	 * @param IRepairStep|string $repairStep repair step
99
	 * @throws \Exception
100
	 */
101
	public function addStep($repairStep) {
102
		if (\is_string($repairStep)) {
103
			try {
104
				$s = \OC::$server->query($repairStep);
105
			} catch (QueryException $e) {
106
				if (\class_exists($repairStep)) {
107
					$s = new $repairStep();
108
				} else {
109
					throw new \Exception("Repair step '$repairStep' is unknown");
110
				}
111
			}
112
113
			if ($s instanceof IRepairStep) {
114
				$this->repairSteps[] = $s;
115
			} else {
116
				throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
117
			}
118
		} else {
119
			$this->repairSteps[] = $repairStep;
120
		}
121
	}
122
123
	/**
124
	 * Returns the default repair steps to be run on the
125
	 * command line or after an upgrade.
126
	 *
127
	 * @return IRepairStep[]
128
	 */
129
	public static function getRepairSteps() {
130
		return [
131
			new RepairMimeTypes(\OC::$server->getConfig()),
132
			new RepairMismatchFileCachePath(
133
				\OC::$server->getDatabaseConnection(),
134
				\OC::$server->getMimeTypeLoader(),
135
				\OC::$server->getLogger(),
136
				\OC::$server->getConfig()),
137
			new FillETags(\OC::$server->getDatabaseConnection()),
138
			new CleanTags(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager()),
139
			new DropOldTables(\OC::$server->getDatabaseConnection()),
140
			new DropOldJobs(\OC::$server->getJobList()),
141
			new RemoveGetETagEntries(\OC::$server->getDatabaseConnection()),
142
			new UpdateOutdatedOcsIds(\OC::$server->getConfig()),
143
			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
144
			new SharePropagation(\OC::$server->getConfig()),
145
			new MoveAvatarOutsideHome(
146
				\OC::$server->getConfig(),
147
				\OC::$server->getDatabaseConnection(),
148
				\OC::$server->getUserManager(),
149
				\OC::$server->getAvatarManager(),
150
				\OC::$server->getLazyRootFolder(),
151
				\OC::$server->getL10N('core'),
152
				\OC::$server->getLogger()
153
			),
154
			new RemoveRootShares(\OC::$server->getDatabaseConnection(), \OC::$server->getUserManager(), \OC::$server->getLazyRootFolder()),
155
			new RepairUnmergedShares(
156
				\OC::$server->getConfig(),
157
				\OC::$server->getDatabaseConnection(),
158
				\OC::$server->getUserManager(),
159
				\OC::$server->getGroupManager()
160
			),
161
			new DisableExtraThemes(
162
				\OC::$server->getAppManager(),
163
				\OC::$server->getConfig(),
164
				\OC::$server->getAppConfig()
165
			),
166
			new RepairSubShares(
167
				\OC::$server->getDatabaseConnection()
168
			),
169
		];
170
	}
171
172
	/**
173
	 * Returns expensive repair steps to be run on the
174
	 * command line with a special option.
175
	 *
176
	 * @return IRepairStep[]
177
	 */
178
	public static function getExpensiveRepairSteps() {
179
		return [
180
			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
181
		];
182
	}
183
184
	/**
185
	 * Returns the repair steps to be run before an
186
	 * upgrade.
187
	 *
188
	 * @return IRepairStep[]
189
	 */
190
	public static function getBeforeUpgradeRepairSteps() {
191
		$connection = \OC::$server->getDatabaseConnection();
192
		$steps = [
193
			new InnoDB(),
194
			new Collation(\OC::$server->getConfig(), $connection),
195
			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...
196
			new RepairOrphanedSubshare($connection),
197
			new SearchLuceneTables(),
198
			new Apps(\OC::$server->getAppManager(), \OC::$server->getEventDispatcher(), \OC::$server->getConfig(), new \OC_Defaults()),
0 ignored issues
show
Compatibility introduced by
\OC::$server->getEventDispatcher() of type object<Symfony\Component...entDispatcherInterface> is not a sub-type of object<Symfony\Component...atcher\EventDispatcher>. It seems like you assume a concrete implementation of the interface Symfony\Component\EventD...ventDispatcherInterface 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...
199
		];
200
201
		//There is no need to delete all previews on every single update
202
		//only 7.0.0 through 7.0.2 generated broken previews
203
		$currentVersion = \OC::$server->getConfig()->getSystemValue('version');
204
		if (\version_compare($currentVersion, '7.0.0.0', '>=') &&
205
			\version_compare($currentVersion, '7.0.3.4', '<=')) {
206
			$steps[] = new \OC\Repair\Preview();
207
		}
208
209
		return $steps;
210
	}
211
212
	/**
213
	 * @param string $scope
214
	 * @param string $method
215
	 * @param array $arguments
216
	 */
217
	public function emit($scope, $method, array $arguments = []) {
218
		if ($this->dispatcher !== null) {
219
			$this->dispatcher->dispatch("$scope::$method",
220
				new GenericEvent("$scope::$method", $arguments));
221
		}
222
	}
223
224
	public function info($string) {
225
		// for now just emit as we did in the past
226
		$this->emit('\OC\Repair', 'info', [$string]);
227
	}
228
229
	/**
230
	 * @param string $message
231
	 */
232
	public function warning($message) {
233
		// for now just emit as we did in the past
234
		$this->emit('\OC\Repair', 'warning', [$message]);
235
	}
236
237
	/**
238
	 * @param int $max
239
	 */
240
	public function startProgress($max = 0) {
241
		// for now just emit as we did in the past
242
		$this->emit('\OC\Repair', 'startProgress', [$max, $this->currentStep]);
243
	}
244
245
	/**
246
	 * @param int $step
247
	 * @param string $description
248
	 */
249
	public function advance($step = 1, $description = '') {
250
		// for now just emit as we did in the past
251
		$this->emit('\OC\Repair', 'advance', [$step, $description]);
252
	}
253
254
	/**
255
	 * emit signal
256
	 */
257
	public function finishProgress() {
258
		// for now just emit as we did in the past
259
		$this->emit('\OC\Repair', 'finishProgress', []);
260
	}
261
}
262