|
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( |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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) { |
|
|
|
|
|
|
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 |
|
|
|
|
|
|
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
|
|
|
|
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: