1 | <?php |
||
39 | class ImportOwncloudCustomGroups implements IRepairStep { |
||
40 | |||
41 | /** @var IDBConnection */ |
||
42 | protected $connection; |
||
43 | |||
44 | /** @var IConfig */ |
||
45 | protected $config; |
||
46 | |||
47 | /** @var array */ |
||
48 | protected $circlesById = []; |
||
49 | /** @var array */ |
||
50 | protected $circlesByUri = []; |
||
51 | /** @var array */ |
||
52 | protected $circleHasAdmin = []; |
||
53 | |||
54 | public function __construct(IDBConnection $connection, IConfig $config) { |
||
58 | |||
59 | /** |
||
60 | * Returns the step's name |
||
61 | * |
||
62 | * @return string |
||
63 | * @since 9.1.0 |
||
64 | */ |
||
65 | public function getName() { |
||
68 | |||
69 | /** |
||
70 | * @param IOutput $output |
||
71 | */ |
||
72 | public function run(IOutput $output) { |
||
73 | if (!$this->shouldRun()) { |
||
74 | return; |
||
75 | } |
||
76 | |||
77 | $this->createCircles($output); |
||
78 | $this->createMemberships($output); |
||
79 | $this->updateShares($output); |
||
80 | |||
81 | $this->config->setAppValue('circles', 'imported_custom_groups', 'true'); |
||
82 | } |
||
83 | |||
84 | /** |
||
85 | * @param IOutput $output |
||
86 | */ |
||
87 | public function createCircles(IOutput $output) { |
||
88 | $output->info('Creating circles'); |
||
89 | |||
90 | $select = $this->connection->getQueryBuilder(); |
||
91 | $select->select('*') |
||
92 | ->from('custom_group') |
||
93 | ->orderBy('group_id'); |
||
94 | |||
95 | $insert = $this->connection->getQueryBuilder(); |
||
96 | $insert->insert('circles_circles') |
||
97 | ->values([ |
||
98 | 'name' => $insert->createParameter('name'), |
||
99 | 'type' => $insert->createParameter('type'), |
||
100 | 'creation' => $insert->createFunction('NOW()'), |
||
101 | ]); |
||
102 | |||
103 | $output->startProgress(); |
||
104 | $result = $select->execute(); |
||
105 | |||
106 | while ($row = $result->fetch()) { |
||
107 | $insert->setParameter('name', $row['display_name']) |
||
108 | ->setParameter('type', Circle::CIRCLES_PERSONAL); |
||
109 | |||
110 | $insert->execute(); |
||
111 | $output->advance(); |
||
112 | |||
113 | $this->circlesById[$row['groud_id']] = $insert->getLastInsertId(); |
||
114 | $this->circlesByUri[$row['uri']] = $this->circlesById[$row['groud_id']]; |
||
115 | } |
||
116 | |||
117 | $result->closeCursor(); |
||
118 | $output->finishProgress(); |
||
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param IOutput $output |
||
123 | */ |
||
124 | public function createMemberships(IOutput $output) { |
||
173 | |||
174 | /** |
||
175 | * Update shares |
||
176 | * - type 7 instead of 1 |
||
177 | * - with circle ID instead of `customgroup_` + group URI |
||
178 | * |
||
179 | * @param IOutput $output |
||
180 | */ |
||
181 | public function updateShares(IOutput $output) { |
||
182 | $output->info('Update shares from custom groups to circles'); |
||
183 | |||
184 | $select = $this->connection->getQueryBuilder(); |
||
185 | $select->select('*') |
||
186 | ->from('share') |
||
187 | ->where($select->expr()->eq('share_type', $select->createNamedParameter(Share::SHARE_TYPE_GROUP))); |
||
|
|||
188 | |||
189 | $update = $this->connection->getQueryBuilder(); |
||
190 | $update->update('share') |
||
191 | ->set('share_type', $update->createParameter('type')) |
||
192 | ->set('share_with', $update->createParameter('with')) |
||
193 | ->where($update->expr()->eq('id', $update->createParameter('id'))); |
||
194 | |||
195 | $output->startProgress(); |
||
196 | $result = $select->execute(); |
||
197 | |||
198 | while ($row = $result->fetch()) { |
||
199 | $with = $row['share_with']; |
||
200 | if (strpos($with, 'customgroup_') !== 0) { |
||
201 | // Stray membership |
||
202 | continue; |
||
203 | } |
||
204 | |||
205 | $groupUri = substr($with, strlen('customgroup_')); |
||
206 | if ($groupUri === '' || !isset($this->circlesByUri[$groupUri])) { |
||
207 | // Not a customgroup |
||
208 | continue; |
||
209 | } |
||
210 | |||
211 | $update->setParameter('type', Share::SHARE_TYPE_CIRCLE) |
||
212 | ->setParameter('with', $this->circlesByUri[$groupUri]) |
||
213 | ->setParameter('id', $row['id']); |
||
214 | |||
215 | $update->execute(); |
||
216 | $output->advance(); |
||
217 | } |
||
218 | |||
219 | $result->closeCursor(); |
||
220 | $output->finishProgress(); |
||
221 | } |
||
222 | |||
223 | protected function shouldRun() { |
||
227 | |||
228 | } |
||
229 |
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.