1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Viktar Dubiniuk <[email protected]> |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2018, ownCloud GmbH |
6
|
|
|
* @license AGPL-3.0 |
7
|
|
|
* |
8
|
|
|
* This code is free software: you can redistribute it and/or modify |
9
|
|
|
* it under the terms of the GNU Affero General Public License, version 3, |
10
|
|
|
* as published by the Free Software Foundation. |
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, version 3, |
18
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/> |
19
|
|
|
* |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace OCA\DAV\Migrations; |
23
|
|
|
|
24
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
25
|
|
|
use OCP\Files\Node; |
26
|
|
|
use OCP\IDBConnection; |
27
|
|
|
use OCP\IUser; |
28
|
|
|
use OCP\IUserManager; |
29
|
|
|
use OCP\Migration\ISqlMigration; |
30
|
|
|
|
31
|
|
|
/* |
32
|
|
|
* Resolve userid/propertypath into fileid |
33
|
|
|
* Update all entries with actual fileid if possible |
34
|
|
|
* Drop all entries that can't be resolved |
35
|
|
|
*/ |
36
|
|
|
class Version20170202213905 implements ISqlMigration { |
37
|
|
|
|
38
|
|
|
/** @var IUserManager */ |
39
|
|
|
private $userManager; |
40
|
|
|
|
41
|
|
|
/** @var string[] */ |
42
|
|
|
private $statements = []; |
43
|
|
|
|
44
|
|
|
public function __construct(IUserManager $userManager) { |
45
|
|
|
$this->userManager = $userManager; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @param IDBConnection $connection |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
|
|
public function sql(IDBConnection $connection) { |
53
|
|
|
$qb = $connection->getQueryBuilder(); |
54
|
|
|
$qb->select('*') |
55
|
|
|
->from('properties', 'props') |
56
|
|
|
->setMaxResults(1); |
57
|
|
|
$result = $qb->execute(); |
58
|
|
|
$row = $result->fetch(); |
59
|
|
|
|
60
|
|
|
// There is nothing to do if table is empty or has no userid field |
61
|
|
|
if (!$row || !isset($row['userid'])) { |
62
|
|
|
return $this->statements; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$qb->resetQueryParts() |
66
|
|
|
->setMaxResults(null) |
67
|
|
|
->select('userid', 'propertypath') |
|
|
|
|
68
|
|
|
->from('properties', 'props') |
69
|
|
|
->groupBy('userid') |
70
|
|
|
->addGroupBy('propertypath') |
71
|
|
|
->orderBy('userid') |
72
|
|
|
->addOrderBy('propertypath'); |
73
|
|
|
|
74
|
|
|
$selectResult = $qb->execute(); |
75
|
|
|
while ($row = $selectResult->fetch()) { |
76
|
|
|
try { |
77
|
|
|
$sql = $this->getRepairEntrySql($qb, $row); |
78
|
|
|
if ($sql !== null) { |
79
|
|
|
$this->statements[] = $sql; |
80
|
|
|
} |
81
|
|
|
} catch (\Exception $e) { |
|
|
|
|
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//Mounted FS can have side effects on further migrations |
86
|
|
|
\OC_Util::tearDownFS(); |
87
|
|
|
|
88
|
|
|
// drop entries with empty fileid |
89
|
|
|
$dropQuery = $qb->resetQueryParts() |
90
|
|
|
->delete('properties') |
91
|
|
|
->where( |
92
|
|
|
$qb->expr()->eq('fileid', $qb->expr()->literal('0')) |
93
|
|
|
) |
94
|
|
|
->orWhere( |
95
|
|
|
$qb->expr()->isNull('fileid') |
96
|
|
|
); |
97
|
|
|
$this->statements[] = $dropQuery->getSQL(); |
98
|
|
|
|
99
|
|
|
return $this->statements; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @param IQueryBuilder $qb |
104
|
|
|
* @param $entry |
105
|
|
|
* @return string|null |
106
|
|
|
*/ |
107
|
|
|
private function getRepairEntrySql(IQueryBuilder $qb, $entry) { |
108
|
|
|
$userId = $entry['userid']; |
109
|
|
|
$user = $this->userManager->get($userId); |
110
|
|
|
if (!($user instanceof IUser)) { |
111
|
|
|
return null; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
$node = \OC::$server->getUserFolder($userId)->get($entry['propertypath']); |
115
|
|
|
if ($node instanceof Node && $node->getId()) { |
116
|
|
|
$fileId = $node->getId(); |
117
|
|
|
$updateQuery = $this->getRepairQuery($qb, $fileId, $userId, $entry['propertypath']); |
118
|
|
|
return $updateQuery->getSQL(); |
119
|
|
|
} |
120
|
|
|
return null; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* @param IQueryBuilder $qb |
125
|
|
|
* @param int $fileId |
126
|
|
|
* @param string $userId |
127
|
|
|
* @param string $propertyPath |
128
|
|
|
* @return IQueryBuilder |
129
|
|
|
*/ |
130
|
|
|
private function getRepairQuery(IQueryBuilder $qb, $fileId, $userId, $propertyPath) { |
131
|
|
|
return $qb->resetQueryParts() |
132
|
|
|
->update('properties') |
133
|
|
|
->set( |
134
|
|
|
'fileid', |
135
|
|
|
$qb->expr()->literal($fileId) |
136
|
|
|
) |
137
|
|
|
->where( |
138
|
|
|
$qb->expr()->eq('userid', $qb->expr()->literal($userId)) |
139
|
|
|
) |
140
|
|
|
->andWhere( |
141
|
|
|
$qb->expr()->eq( |
142
|
|
|
'propertypath', |
143
|
|
|
$qb->expr()->literal($propertyPath) |
144
|
|
|
) |
145
|
|
|
); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.