Completed
Push — master ( 0241f7...6d62a7 )
by Morris
07:58
created

Repair::finishProgress()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 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 Thomas Müller <[email protected]>
10
 * @author Vincent Petry <[email protected]>
11
 *
12
 * @copyright Copyright (c) 2016, ownCloud, Inc.
13
 * @license AGPL-3.0
14
 *
15
 * This code is free software: you can redistribute it and/or modify
16
 * it under the terms of the GNU Affero General Public License, version 3,
17
 * as published by the Free Software Foundation.
18
 *
19
 * This program is distributed in the hope that it will be useful,
20
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22
 * GNU Affero General Public License for more details.
23
 *
24
 * You should have received a copy of the GNU Affero General Public License, version 3,
25
 * along with this program.  If not, see <http://www.gnu.org/licenses/>
26
 *
27
 */
28
29
namespace OC;
30
31
use OC\Hooks\BasicEmitter;
32
use OC\Hooks\Emitter;
33
use OC\Repair\AssetCache;
34
use OC\Repair\CleanTags;
35
use OC\Repair\Collation;
36
use OC\Repair\DropOldJobs;
37
use OC\Repair\OldGroupMembershipShares;
38
use OC\Repair\RemoveGetETagEntries;
39
use OC\Repair\SharePropagation;
40
use OC\Repair\SqliteAutoincrement;
41
use OC\Repair\DropOldTables;
42
use OC\Repair\FillETags;
43
use OC\Repair\InnoDB;
44
use OC\Repair\RepairLegacyStorages;
45
use OC\Repair\RepairMimeTypes;
46
use OC\Repair\SearchLuceneTables;
47
use OC\Repair\UpdateOutdatedOcsIds;
48
use OC\Repair\RepairInvalidShares;
49
use OCP\Migration\IOutput;
50
use OCP\Migration\IRepairStep;
51
use Symfony\Component\EventDispatcher\EventDispatcher;
52
use Symfony\Component\EventDispatcher\GenericEvent;
53
54
class Repair extends BasicEmitter implements IOutput{
55
	/* @var IRepairStep[] */
56
	private $repairSteps;
57
	/** @var EventDispatcher */
58
	private $dispatcher;
59
60
	/**
61
	 * Creates a new repair step runner
62
	 *
63
	 * @param IRepairStep[] $repairSteps array of RepairStep instances
64
	 * @param EventDispatcher $dispatcher
65
	 */
66
	public function __construct($repairSteps = [], EventDispatcher $dispatcher = null) {
67
		$this->repairSteps = $repairSteps;
68
		$this->dispatcher = $dispatcher;
69
	}
70
71
	/**
72
	 * Run a series of repair steps for common problems
73
	 */
74
	public function run() {
75
		$self = $this;
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->emit('\OC\Repair', 'step', array($step->getName()));
83
84
			if ($step instanceof Emitter) {
85
				$step->listen('\OC\Repair', 'warning', function ($description) use ($self) {
86
					$self->emit('\OC\Repair', 'warning', array($description));
87
				});
88
				$step->listen('\OC\Repair', 'info', function ($description) use ($self) {
89
					$self->emit('\OC\Repair', 'info', array($description));
90
				});
91
			}
92
93
			$step->run($this);
94
		}
95
	}
96
97
	/**
98
	 * Add repair step
99
	 *
100
	 * @param IRepairStep|string $repairStep repair step
101
	 * @throws \Exception
102
	 */
103
	public function addStep($repairStep) {
104
		if (is_string($repairStep)) {
105
			if (class_exists($repairStep)) {
106
				$s = new $repairStep();
107
				if ($s instanceof IRepairStep) {
108
					$this->repairSteps[] = $s;
109
				} else {
110
					throw new \Exception("Repair step '$repairStep' is not of type \\OCP\\Migration\\IRepairStep");
111
				}
112
			} else {
113
				throw new \Exception("Repair step '$repairStep' is unknown");
114
			}
115
		} else {
116
			$this->repairSteps[] = $repairStep;
117
		}
118
	}
119
120
	/**
121
	 * Returns the default repair steps to be run on the
122
	 * command line or after an upgrade.
123
	 *
124
	 * @return IRepairStep[]
125
	 */
126
	public static function getRepairSteps() {
127
		return [
128
			new RepairMimeTypes(\OC::$server->getConfig()),
129
			new RepairLegacyStorages(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
130
			new AssetCache(),
131
			new FillETags(\OC::$server->getDatabaseConnection()),
132
			new CleanTags(\OC::$server->getDatabaseConnection()),
133
			new DropOldTables(\OC::$server->getDatabaseConnection()),
134
			new DropOldJobs(\OC::$server->getJobList()),
135
			new RemoveGetETagEntries(\OC::$server->getDatabaseConnection()),
136
			new UpdateOutdatedOcsIds(\OC::$server->getConfig()),
137
			new RepairInvalidShares(\OC::$server->getConfig(), \OC::$server->getDatabaseConnection()),
138
			new SharePropagation(\OC::$server->getConfig()),
139
		];
140
	}
141
142
	/**
143
	 * Returns expensive repair steps to be run on the
144
	 * command line with a special option.
145
	 *
146
	 * @return IRepairStep[]
147
	 */
148
	public static function getExpensiveRepairSteps() {
149
		return [
150
			new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
151
		];
152
	}
153
154
	/**
155
	 * Returns the repair steps to be run before an
156
	 * upgrade.
157
	 *
158
	 * @return IRepairStep[]
159
	 */
160
	public static function getBeforeUpgradeRepairSteps() {
161
		$connection = \OC::$server->getDatabaseConnection();
162
		$steps = [
163
			new InnoDB(),
164
			new Collation(\OC::$server->getConfig(), $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...
165
			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...
166
			new SearchLuceneTables(),
167
		];
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
177
		return $steps;
178
	}
179
180
	/**
181
	 * {@inheritDoc}
182
	 */
183
	public function emit($scope, $method, array $arguments = []) {
184
		parent::emit($scope, $method, $arguments);
185
		if (!is_null($this->dispatcher)) {
186
			$this->dispatcher->dispatch("$scope::$method",
187
				new GenericEvent("$scope::$method", $arguments));
188
		}
189
	}
190
191
	public function info($string) {
192
		// for now just emit as we did in the past
193
		$this->emit('\OC\Repair', 'info', array($string));
194
	}
195
196
	/**
197
	 * @param string $message
198
	 */
199
	public function warning($message) {
200
		// for now just emit as we did in the past
201
		$this->emit('\OC\Repair', 'warning', [$message]);
202
	}
203
204
	/**
205
	 * @param int $max
206
	 */
207
	public function startProgress($max = 0) {
208
		// for now just emit as we did in the past
209
		$this->emit('\OC\Repair', 'startProgress', [$max]);
210
	}
211
212
	/**
213
	 * @param int $step
214
	 */
215
	public function advance($step = 1) {
216
		// for now just emit as we did in the past
217
		$this->emit('\OC\Repair', 'advance', [$step]);
218
	}
219
220
	/**
221
	 * @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...
222
	 */
223
	public function finishProgress() {
224
		// for now just emit as we did in the past
225
		$this->emit('\OC\Repair', 'finishProgress', []);
226
	}
227
}
228