|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Circles - Bring cloud-users closer together. |
|
4
|
|
|
* |
|
5
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
6
|
|
|
* later. See the COPYING file. |
|
7
|
|
|
* |
|
8
|
|
|
* @author Maxence Lange <[email protected]> |
|
9
|
|
|
* @copyright 2017 |
|
10
|
|
|
* @license GNU AGPL version 3 or any later version |
|
11
|
|
|
* |
|
12
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
13
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
14
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
15
|
|
|
* License, or (at your option) any later version. |
|
16
|
|
|
* |
|
17
|
|
|
* This program is distributed in the hope that it will be useful, |
|
18
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
19
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
20
|
|
|
* GNU Affero General Public License for more details. |
|
21
|
|
|
* |
|
22
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
23
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
24
|
|
|
* |
|
25
|
|
|
*/ |
|
26
|
|
|
|
|
27
|
|
|
namespace OCA\Circles\Migration; |
|
28
|
|
|
|
|
29
|
|
|
use OC\Share\Share; |
|
30
|
|
|
use OCA\Circles\AppInfo\Application; |
|
31
|
|
|
use OCA\Circles\Db\CoreRequestBuilder; |
|
32
|
|
|
use OCA\Circles\Model\Circle; |
|
33
|
|
|
use OCP\IConfig; |
|
34
|
|
|
use OCP\IDBConnection; |
|
35
|
|
|
use OCP\Migration\IOutput; |
|
36
|
|
|
use OCP\Migration\IRepairStep; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Class UsingShortenUniqueIdInsteadOfCircleId |
|
40
|
|
|
* |
|
41
|
|
|
* @package OCA\Circles\Migration |
|
42
|
|
|
*/ |
|
43
|
|
|
class UsingShortenUniqueIdInsteadOfCircleId implements IRepairStep { |
|
44
|
|
|
|
|
45
|
|
|
/** @var IDBConnection */ |
|
46
|
|
|
protected $connection; |
|
47
|
|
|
|
|
48
|
|
|
/** @var IConfig */ |
|
49
|
|
|
protected $config; |
|
50
|
|
|
|
|
51
|
|
|
public function __construct(IDBConnection $connection, IConfig $config) { |
|
52
|
|
|
$this->connection = $connection; |
|
53
|
|
|
$this->config = $config; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Returns the step's name |
|
58
|
|
|
* |
|
59
|
|
|
* @return string |
|
60
|
|
|
* @since 9.1.0 |
|
61
|
|
|
*/ |
|
62
|
|
|
public function getName() { |
|
63
|
|
|
return 'Using shorten unique id instead of circle id'; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param IOutput $output |
|
68
|
|
|
*/ |
|
69
|
|
|
public function run(IOutput $output) { |
|
70
|
|
|
$oldVersion = explode( |
|
71
|
|
|
'.', \OC::$server->getConfig() |
|
72
|
|
|
->getAppValue(Application::APP_NAME, 'installed_version', '') |
|
73
|
|
|
); |
|
74
|
|
|
|
|
75
|
|
|
if ((int)$oldVersion[0] === 0 |
|
76
|
|
|
&& (int)$oldVersion[1] < 13) { |
|
77
|
|
|
$this->swapToShortenUniqueId(); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
private function swapToShortenUniqueId() { |
|
83
|
|
|
|
|
84
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
85
|
|
|
|
|
86
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
87
|
|
|
$qb->select('id', 'unique_id') |
|
88
|
|
|
->from(CoreRequestBuilder::TABLE_CIRCLES); |
|
89
|
|
|
|
|
90
|
|
|
$cursor = $qb->execute(); |
|
91
|
|
|
while ($data = $cursor->fetch()) { |
|
92
|
|
|
$circleId = $data['id']; |
|
93
|
|
|
$shortenUniqueId = substr($data['unique_id'], 0, Circle::UNIQUEID_SHORT_LENGTH); |
|
94
|
|
|
|
|
95
|
|
|
$this->swapToShortenUniqueIdInTable( |
|
96
|
|
|
$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_GROUPS |
|
97
|
|
|
); |
|
98
|
|
|
$this->swapToShortenUniqueIdInTable( |
|
99
|
|
|
$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS |
|
100
|
|
|
); |
|
101
|
|
|
|
|
102
|
|
|
// $this->cleanBuggyDuplicateEntries( |
|
|
|
|
|
|
103
|
|
|
// $circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS, 'user_id' |
|
104
|
|
|
// ); |
|
105
|
|
|
$this->swapToShortenUniqueIdInTable( |
|
106
|
|
|
$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_MEMBERS |
|
107
|
|
|
); |
|
108
|
|
|
|
|
109
|
|
|
$this->swapToShortenUniqueIdInTable( |
|
110
|
|
|
$circleId, $shortenUniqueId, CoreRequestBuilder::TABLE_LINKS |
|
111
|
|
|
); |
|
112
|
|
|
$this->swapToShortenUniqueIdInShares($circleId, $shortenUniqueId); |
|
113
|
|
|
} |
|
114
|
|
|
$cursor->closeCursor(); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
private function swapToShortenUniqueIdInTable($circleId, $shortenUniqueId, $table) { |
|
119
|
|
|
|
|
120
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
121
|
|
|
$qb->update($table) |
|
122
|
|
|
->where( |
|
123
|
|
|
$qb->expr() |
|
124
|
|
|
->eq('circle_id', $qb->createNamedParameter($circleId)) |
|
125
|
|
|
); |
|
126
|
|
|
|
|
127
|
|
|
$qb->set('circle_id', $qb->createNamedParameter($shortenUniqueId)); |
|
128
|
|
|
$qb->execute(); |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
|
|
132
|
|
View Code Duplication |
private function swapToShortenUniqueIdInShares($circleId, $shortenUniqueId) { |
|
|
|
|
|
|
133
|
|
|
$qb = $this->connection->getQueryBuilder(); |
|
134
|
|
|
$expr = $qb->expr(); |
|
135
|
|
|
|
|
136
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
137
|
|
|
$qb->update('share') |
|
138
|
|
|
->where( |
|
139
|
|
|
$expr->andX( |
|
140
|
|
|
$expr->eq( |
|
141
|
|
|
'share_type', $qb->createNamedParameter(Share::SHARE_TYPE_CIRCLE) |
|
142
|
|
|
), |
|
143
|
|
|
$expr->eq('share_with', $qb->createNamedParameter($circleId)) |
|
144
|
|
|
) |
|
145
|
|
|
); |
|
146
|
|
|
|
|
147
|
|
|
$qb->set('share_with', $qb->createNamedParameter($shortenUniqueId)); |
|
148
|
|
|
$qb->execute(); |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
|
|
152
|
|
|
// private function cleanBuggyDuplicateEntries($circleId, $shortenUniqueId, $table, $field) { |
|
|
|
|
|
|
153
|
|
|
// |
|
154
|
|
|
// $qb = $this->connection->getQueryBuilder(); |
|
155
|
|
|
// $expr = $qb->expr(); |
|
156
|
|
|
// |
|
157
|
|
|
// $qb->select($field) |
|
158
|
|
|
// ->from($table) |
|
159
|
|
|
// ->where( |
|
160
|
|
|
// $expr->eq('circle_id', $qb->createNamedParameter($circleId)) |
|
161
|
|
|
// ); |
|
162
|
|
|
// |
|
163
|
|
|
// $cursor = $qb->execute(); |
|
164
|
|
|
// while ($data = $cursor->fetch()) { |
|
165
|
|
|
// $val = $data[$field]; |
|
166
|
|
|
// if ($val !== '') { |
|
167
|
|
|
// $qb2 = $this->connection->getQueryBuilder(); |
|
168
|
|
|
// $expr2 = $qb2->expr(); |
|
169
|
|
|
// /** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
170
|
|
|
// $qb2->delete($table) |
|
171
|
|
|
// ->where( |
|
172
|
|
|
// $expr2->andX( |
|
173
|
|
|
// $expr2->eq('circle_id', $qb2->createNamedParameter($shortenUniqueId)), |
|
174
|
|
|
// $expr2->eq($field, $qb2->createNamedParameter($val)) |
|
175
|
|
|
// ) |
|
176
|
|
|
// ); |
|
177
|
|
|
// $qb2->execute(); |
|
178
|
|
|
// } |
|
179
|
|
|
// } |
|
180
|
|
|
// $cursor->closeCursor(); |
|
181
|
|
|
// } |
|
182
|
|
|
|
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
|
|
186
|
|
|
|
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.