Completed
Push — master ( e33512...01466a )
by Morris
44s
created

RepairInvalidPaths::getId()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 15
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 10
c 1
b 0
f 0
nc 2
nop 2
dl 15
loc 15
rs 9.4285
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\DB\QueryBuilder\IQueryBuilder;
26
use OCP\IConfig;
27
use OCP\IDBConnection;
28
use OCP\Migration\IOutput;
29
use OCP\Migration\IRepairStep;
30
31
class RepairInvalidPaths implements IRepairStep {
32
	const MAX_ROWS = 1000;
33
34
	/** @var IDBConnection */
35
	private $connection;
36
	/** @var IConfig */
37
	private $config;
38
39
	private $getIdQuery;
40
	private $updateQuery;
41
	private $reparentQuery;
42
	private $deleteQuery;
43
44
	public function __construct(IDBConnection $connection, IConfig $config) {
45
		$this->connection = $connection;
46
		$this->config = $config;
47
	}
48
49
50
	public function getName() {
51
		return 'Repair invalid paths in file cache';
52
	}
53
54
	private function getInvalidEntries() {
55
		$builder = $this->connection->getQueryBuilder();
56
57
		$computedPath = $builder->func()->concat(
58
			'p.path',
59
			$builder->func()->concat($builder->createNamedParameter('/'), 'f.name')
60
		);
61
62
		//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;
63
		$query = $builder->select('f.fileid', 'f.path', 'p.path AS parent_path', 'f.name', 'f.parent', 'f.storage')
64
			->from('filecache', 'f')
65
			->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...
66
				$builder->expr()->eq('f.parent', 'p.fileid'),
67
				$builder->expr()->neq('p.name', $builder->createNamedParameter(''))
68
			))
69
			->where($builder->expr()->neq('f.path', $computedPath))
70
			->setMaxResults(self::MAX_ROWS);
71
72
		do {
73
			$result = $query->execute();
74
			$rows = $result->fetchAll();
75
			foreach ($rows as $row) {
76
				yield $row;
77
			}
78
			$result->closeCursor();
79
		} while (count($rows) >= self::MAX_ROWS);
80
	}
81
82 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...
83
		if (!$this->getIdQuery) {
84
			$builder = $this->connection->getQueryBuilder();
85
86
			$this->getIdQuery = $builder->select('fileid')
87
				->from('filecache')
88
				->where($builder->expr()->eq('storage', $builder->createParameter('storage')))
89
				->andWhere($builder->expr()->eq('path', $builder->createParameter('path')));
90
		}
91
92
		$this->getIdQuery->setParameter('storage', $storage, IQueryBuilder::PARAM_INT);
93
		$this->getIdQuery->setParameter('path', $path);
94
95
		return $this->getIdQuery->execute()->fetchColumn();
96
	}
97
98 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...
99
		if (!$this->updateQuery) {
100
			$builder = $this->connection->getQueryBuilder();
101
102
			$this->updateQuery = $builder->update('filecache')
103
				->set('path', $builder->createParameter('newpath'))
104
				->set('path_hash', $builder->func()->md5($builder->createParameter('newpath')))
105
				->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
106
		}
107
108
		$this->updateQuery->setParameter('newpath', $newPath);
109
		$this->updateQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
110
111
		$this->updateQuery->execute();
112
	}
113
114
	private function reparent($from, $to) {
115 View Code Duplication
		if (!$this->reparentQuery) {
116
			$builder = $this->connection->getQueryBuilder();
117
118
			$this->reparentQuery = $builder->update('filecache')
119
				->set('parent', $builder->createParameter('to'))
120
				->where($builder->expr()->eq('fileid', $builder->createParameter('from')));
121
		}
122
123
		$this->reparentQuery->setParameter('from', $from);
124
		$this->reparentQuery->setParameter('to', $to);
125
126
		$this->reparentQuery->execute();
127
	}
128
129
	private function delete($fileid) {
130 View Code Duplication
		if (!$this->deleteQuery) {
131
			$builder = $this->connection->getQueryBuilder();
132
133
			$this->deleteQuery = $builder->delete('filecache')
134
				->where($builder->expr()->eq('fileid', $builder->createParameter('fileid')));
135
		}
136
137
		$this->deleteQuery->setParameter('fileid', $fileid, IQueryBuilder::PARAM_INT);
138
139
		$this->deleteQuery->execute();
140
	}
141
142
	private function repair() {
143
		$this->connection->beginTransaction();
144
		$entries = $this->getInvalidEntries();
145
		$count = 0;
146
		foreach ($entries as $entry) {
147
			$count++;
148
			$calculatedPath = $entry['parent_path'] . '/' . $entry['name'];
149
			if ($newId = $this->getId($entry['storage'], $calculatedPath)) {
150
				// a new entry with the correct path has already been created, reuse that one and delete the incorrect entry
151
				$this->reparent($entry['fileid'], $newId);
152
				$this->delete($entry['fileid']);
153
			} else {
154
				$this->update($entry['fileid'], $calculatedPath);
155
			}
156
		}
157
		$this->connection->commit();
158
		return $count;
159
	}
160
161
	public function run(IOutput $output) {
162
		$versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0');
163
		// 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...
164
		if (version_compare($versionFromBeforeUpdate, '12.0.0.30', '<') || version_compare($versionFromBeforeUpdate, '13.0.0.0', '==')) {
165
			$count = $this->repair();
166
167
			$output->info('Repaired ' . $count . ' paths');
168
		}
169
	}
170
}
171