Completed
Pull Request — master (#5628)
by Joas
17:04
created

RepairInvalidPaths::run()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 5
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 9
rs 9.6666
1
<?php
2
/**
3
 * @copyright Copyright (c) 2017 Robin Appelman <[email protected]>
4
 *
5
 * @license GNU AGPL version 3 or any later version
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as
9
 * published by the Free Software Foundation, either version 3 of the
10
 * License, or (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 *
20
 */
21
22
namespace OC\Repair\NC13;
23
24
25
use OCP\IConfig;
26
use OCP\IDBConnection;
27
use OCP\Migration\IOutput;
28
use OCP\Migration\IRepairStep;
29
30
class RepairInvalidPaths implements IRepairStep {
31
	/** @var IDBConnection */
32
	private $connection;
33
	/** @var IConfig */
34
	private $config;
35
36
	public function __construct(IDBConnection $connection, IConfig $config) {
37
		$this->connection = $connection;
38
		$this->config = $config;
39
	}
40
41
42
	public function getName() {
43
		return 'Repair invalid paths in file cache';
44
	}
45
46
	private function getInvalidEntries() {
47
		$builder = $this->connection->getQueryBuilder();
48
49
		$computedPath = $builder->func()->concat(
50
			'p.path',
51
			$builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
52
		);
53
54
		//select f.path, f.parent,p.path from oc_filecache f inner join oc_filecache p on f.parent=p.fileid and p.path!='' where f.path != p.path || '/' || f.name;
55
		$query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage')
56
			->from('filecache', 'f')
57
			->innerJoin('f', 'filecache', 'p', $builder->expr()->andX(
0 ignored issues
show
Documentation introduced by
$builder->expr()->andX($...ateNamedParameter(''))) is of type object<OCP\DB\QueryBuilder\ICompositeExpression>, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
58
				$builder->expr()->eq('f.parent', 'p.fileid'),
59
				$builder->expr()->neq('p.name', $builder->createNamedParameter(''))
60
			))
61
			->where($builder->expr()->neq('f.path', $computedPath));
62
63
		return $query->execute()->fetchAll();
64
	}
65
66 View Code Duplication
	private function getId($storage, $path) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
67
		$builder = $this->connection->getQueryBuilder();
68
69
		$query = $builder->select('fileid')
70
			->from('filecache')
71
			->where($builder->expr()->eq('storage', $builder->createNamedParameter($storage)))
72
			->andWhere($builder->expr()->eq('path', $builder->createNamedParameter($path)));
73
74
		return $query->execute()->fetchColumn();
75
	}
76
77 View Code Duplication
	private function update($fileid, $newPath) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
78
		$builder = $this->connection->getQueryBuilder();
79
80
		$query = $builder->update('filecache')
81
			->set('path', $builder->createNamedParameter($newPath))
82
			->set('path_hash', $builder->createNamedParameter(md5($newPath)))
83
			->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileid)));
84
85
		$query->execute();
86
	}
87
88 View Code Duplication
	private function reparent($from, $to) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
89
		$builder = $this->connection->getQueryBuilder();
90
91
		$query = $builder->update('filecache')
92
			->set('parent', $builder->createNamedParameter($to))
93
			->where($builder->expr()->eq('fileid', $builder->createNamedParameter($from)));
94
		$query->execute();
95
	}
96
97 View Code Duplication
	private function delete($fileid) {
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
98
		$builder = $this->connection->getQueryBuilder();
99
100
		$query = $builder->delete('filecache')
101
			->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileid)));
102
		$query->execute();
103
	}
104
105
	private function repair() {
106
		$entries = $this->getInvalidEntries();
107
		foreach ($entries as $entry) {
108
			$calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
109
			if ($newId = $this->getId($entry['storage'], $calculatedPath)) {
110
				// a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
111
				$this->reparent($entry['fileid'], $newId);
112
				$this->delete($entry['fileid']);
113
			} else {
114
				$this->update($entry['fileid'], $calculatedPath);
115
			}
116
		}
117
		return count($entries);
118
	}
119
120
	public function run(IOutput $output) {
121
		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
122
		// was added to 12.0.0.30 and 13.0.0.1
0 ignored issues
show
Unused Code Comprehensibility introduced by
44% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
123
		if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) {
124
			$count = $this->repair();
125
126
			$output->info('Repaired ' . $count . ' paths');
127
		}
128
	}
129
}
130