|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Circles - Bring cloud-users closer together. |
|
5
|
|
|
* |
|
6
|
|
|
* This file is licensed under the Affero General Public License version 3 or |
|
7
|
|
|
* later. See the COPYING file. |
|
8
|
|
|
* |
|
9
|
|
|
* @author Maxence Lange <[email protected]> |
|
10
|
|
|
* @copyright 2017 |
|
11
|
|
|
* @license GNU AGPL version 3 or any later version |
|
12
|
|
|
* |
|
13
|
|
|
* This program is free software: you can redistribute it and/or modify |
|
14
|
|
|
* it under the terms of the GNU Affero General Public License as |
|
15
|
|
|
* published by the Free Software Foundation, either version 3 of the |
|
16
|
|
|
* License, or (at your option) any later version. |
|
17
|
|
|
* |
|
18
|
|
|
* This program is distributed in the hope that it will be useful, |
|
19
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
20
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
21
|
|
|
* GNU Affero General Public License for more details. |
|
22
|
|
|
* |
|
23
|
|
|
* You should have received a copy of the GNU Affero General Public License |
|
24
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25
|
|
|
* |
|
26
|
|
|
*/ |
|
27
|
|
|
|
|
28
|
|
|
namespace OCA\Circles\Db; |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
use Doctrine\DBAL\Query\QueryBuilder; |
|
32
|
|
|
use OC\L10N\L10N; |
|
33
|
|
|
use OCA\Circles\Model\Circle; |
|
34
|
|
|
use OCA\Circles\Model\FederatedLink; |
|
35
|
|
|
use OCA\Circles\Model\Member; |
|
36
|
|
|
use OCA\Circles\Model\SharingFrame; |
|
37
|
|
|
use OCP\DB\QueryBuilder\IQueryBuilder; |
|
38
|
|
|
use OCP\IDBConnection; |
|
39
|
|
|
|
|
40
|
|
|
class CirclesRequestBuilder { |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
const TABLE_CIRCLES = 'circles_circles'; |
|
43
|
|
|
const TABLE_MEMBERS = 'circles_members'; |
|
44
|
|
|
|
|
45
|
|
|
/** @var IDBConnection */ |
|
46
|
|
|
protected $dbConnection; |
|
47
|
|
|
|
|
48
|
|
|
/** @var L10N */ |
|
49
|
|
|
protected $l10n; |
|
50
|
|
|
|
|
51
|
|
|
private $default_select_alias; |
|
52
|
|
|
|
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Join the Circles table |
|
56
|
|
|
* |
|
57
|
|
|
* @param IQueryBuilder $qb |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function joinCircles(& $qb, $field) { |
|
60
|
|
|
$expr = $qb->expr(); |
|
61
|
|
|
|
|
62
|
|
|
$qb->from(self::TABLE_CIRCLES, 'c') |
|
63
|
|
|
->andWhere($expr->eq('c.id', $field)); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Limit the request to the Share by its Id. |
|
69
|
|
|
* |
|
70
|
|
|
* @param IQueryBuilder $qb |
|
71
|
|
|
* @param int $circleId |
|
72
|
|
|
*/ |
|
73
|
|
|
protected function limitToCircleId(IQueryBuilder &$qb, $circleId) { |
|
74
|
|
|
$expr = $qb->expr(); |
|
75
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
|
76
|
|
|
|
|
77
|
|
|
$qb->andWhere($expr->eq($pf . 'circle_id', $qb->createNamedParameter($circleId))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Limit the request by its Id. |
|
83
|
|
|
* |
|
84
|
|
|
* @param IQueryBuilder $qb |
|
85
|
|
|
* @param int $id |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function limitToId(IQueryBuilder &$qb, $id) { |
|
88
|
|
|
$expr = $qb->expr(); |
|
89
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
|
90
|
|
|
|
|
91
|
|
|
$qb->andWhere($expr->eq($pf . 'id', $qb->createNamedParameter($id))); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Limit the request by its UniqueId. |
|
97
|
|
|
* |
|
98
|
|
|
* @param IQueryBuilder $qb |
|
99
|
|
|
* @param int $uniqueId |
|
100
|
|
|
*/ |
|
101
|
|
|
protected function limitToUniqueId(IQueryBuilder &$qb, $uniqueId) { |
|
102
|
|
|
$expr = $qb->expr(); |
|
103
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
|
104
|
|
|
|
|
105
|
|
|
$qb->andWhere($expr->eq($pf . 'unique_id', $qb->createNamedParameter($uniqueId))); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* Limit the request by its Token. |
|
111
|
|
|
* |
|
112
|
|
|
* @param IQueryBuilder $qb |
|
113
|
|
|
* @param $token |
|
114
|
|
|
*/ |
|
115
|
|
|
protected function limitToToken(IQueryBuilder &$qb, $token) { |
|
116
|
|
|
$expr = $qb->expr(); |
|
117
|
|
|
$pf = ($qb->getType() === QueryBuilder::SELECT) ? $this->default_select_alias . '.' : ''; |
|
118
|
|
|
|
|
119
|
|
|
$qb->andWhere($expr->eq($pf . 'token', $qb->createNamedParameter($token))); |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Limit the request to a minimum member level. |
|
125
|
|
|
* |
|
126
|
|
|
* @param IQueryBuilder $qb |
|
127
|
|
|
* @param $level |
|
128
|
|
|
*/ |
|
129
|
|
|
protected function limitToMemberLevel(IQueryBuilder &$qb, $level) { |
|
130
|
|
|
$qb->where( |
|
131
|
|
|
$qb->expr() |
|
132
|
|
|
->gte('m.level', $qb->createNamedParameter($level)) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* add a request to the members list, using the current user ID. |
|
139
|
|
|
* will returns level and stuff. |
|
140
|
|
|
* |
|
141
|
|
|
* @param IQueryBuilder $qb |
|
142
|
|
|
* @param string $userId |
|
143
|
|
|
*/ |
|
144
|
|
|
protected function leftJoinUserIdAsMember(IQueryBuilder &$qb, $userId) { |
|
145
|
|
|
|
|
146
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
|
147
|
|
|
return; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
$expr = $qb->expr(); |
|
151
|
|
|
$pf = $this->default_select_alias . '.'; |
|
152
|
|
|
|
|
153
|
|
|
$qb->selectAlias('u.level', 'user_level'); |
|
154
|
|
|
$qb->leftJoin( |
|
155
|
|
|
$this->default_select_alias, MembersMapper::TABLENAME, 'u', |
|
156
|
|
|
$expr->andX( |
|
157
|
|
|
$expr->eq($pf . 'id', 'u.circle_id'), |
|
158
|
|
|
$expr->eq('u.user_id', $qb->createNamedParameter($userId)) |
|
159
|
|
|
) |
|
160
|
|
|
); |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* @param IQueryBuilder $qb |
|
165
|
|
|
* |
|
166
|
|
|
* @deprecated |
|
167
|
|
|
* never used in fact. |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function leftJoinOwner(IQueryBuilder &$qb) { |
|
170
|
|
|
|
|
171
|
|
|
if ($qb->getType() !== QueryBuilder::SELECT) { |
|
172
|
|
|
return; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
$expr = $qb->expr(); |
|
176
|
|
|
$pf = $this->default_select_alias . '.'; |
|
177
|
|
|
|
|
178
|
|
|
$qb->leftJoin( |
|
179
|
|
|
$this->default_select_alias, MembersMapper::TABLENAME, 'o', |
|
180
|
|
|
$expr->andX( |
|
181
|
|
|
$expr->eq($pf . 'id', 'o.circle_id'), |
|
182
|
|
|
$expr->eq('o.level', $qb->createNamedParameter(Member::LEVEL_OWNER)) |
|
183
|
|
|
) |
|
184
|
|
|
); |
|
185
|
|
|
} |
|
186
|
|
|
|
|
187
|
|
|
|
|
188
|
|
|
/** |
|
189
|
|
|
* Base of the Sql Select request for Shares |
|
190
|
|
|
* |
|
191
|
|
|
* @return IQueryBuilder |
|
192
|
|
|
*/ |
|
193
|
|
|
protected function getLinksSelectSql() { |
|
194
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
195
|
|
|
|
|
196
|
|
|
$qb->select('id', 'status', 'address', 'token', 'circle_id', 'unique_id', 'creation') |
|
197
|
|
|
->from('circles_links', 's'); |
|
198
|
|
|
|
|
199
|
|
|
$this->default_select_alias = 's'; |
|
200
|
|
|
|
|
201
|
|
|
return $qb; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Base of the Sql Select request for Shares |
|
207
|
|
|
* |
|
208
|
|
|
* @return IQueryBuilder |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function getSharesSelectSql() { |
|
211
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
212
|
|
|
|
|
213
|
|
|
$qb->select( |
|
214
|
|
|
'circle_id', 'source', 'type', 'author', 'cloud_id', 'payload', 'creation', 'headers', |
|
215
|
|
|
'unique_id' |
|
216
|
|
|
) |
|
217
|
|
|
->from('circles_shares', 's'); |
|
218
|
|
|
|
|
219
|
|
|
$this->default_select_alias = 's'; |
|
220
|
|
|
|
|
221
|
|
|
return $qb; |
|
222
|
|
|
} |
|
223
|
|
|
|
|
224
|
|
|
/** |
|
225
|
|
|
* Base of the Sql Insert request for Shares |
|
226
|
|
|
* |
|
227
|
|
|
* @return IQueryBuilder |
|
228
|
|
|
*/ |
|
229
|
|
|
protected function getSharesInsertSql() { |
|
230
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
231
|
|
|
$qb->insert('circles_shares') |
|
232
|
|
|
->setValue('creation', $qb->createFunction('NOW()')); |
|
233
|
|
|
|
|
234
|
|
|
return $qb; |
|
235
|
|
|
} |
|
236
|
|
|
|
|
237
|
|
|
|
|
238
|
|
|
/** |
|
239
|
|
|
* Base of the Sql Update request for Shares |
|
240
|
|
|
* |
|
241
|
|
|
* @param string $uniqueId |
|
242
|
|
|
* |
|
243
|
|
|
* @return IQueryBuilder |
|
244
|
|
|
*/ |
|
245
|
|
|
protected function getSharesUpdateSql(string $uniqueId) { |
|
246
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
247
|
|
|
$qb->update('circles_shares') |
|
248
|
|
|
->where( |
|
249
|
|
|
$qb->expr() |
|
250
|
|
|
->eq('unique_id', $qb->createNamedParameter($uniqueId)) |
|
251
|
|
|
); |
|
252
|
|
|
|
|
253
|
|
|
return $qb; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
|
|
257
|
|
|
/** |
|
258
|
|
|
* @return IQueryBuilder |
|
259
|
|
|
*/ |
|
260
|
|
|
protected function getMembersSelectSql() { |
|
261
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
262
|
|
|
|
|
263
|
|
|
$qb->select('user_id', 'circle_id', 'level', 'status', 'joined') |
|
264
|
|
|
->from('circles_members', 'm'); |
|
265
|
|
|
|
|
266
|
|
|
$this->default_select_alias = 'm'; |
|
267
|
|
|
|
|
268
|
|
|
return $qb; |
|
269
|
|
|
} |
|
270
|
|
|
|
|
271
|
|
|
|
|
272
|
|
|
/** |
|
273
|
|
|
* @return IQueryBuilder |
|
274
|
|
|
*/ |
|
275
|
|
|
protected function getCirclesSelectSql() { |
|
276
|
|
|
$qb = $this->dbConnection->getQueryBuilder(); |
|
277
|
|
|
|
|
278
|
|
|
$qb->select('c.id', 'c.unique_id', 'c.name', 'c.description', 'c.type', 'c.creation') |
|
279
|
|
|
->from('circles_circles', 'c'); |
|
280
|
|
|
$this->default_select_alias = 'c'; |
|
281
|
|
|
|
|
282
|
|
|
return $qb; |
|
283
|
|
|
} |
|
284
|
|
|
|
|
285
|
|
|
/** |
|
286
|
|
|
* @param array $data |
|
287
|
|
|
* |
|
288
|
|
|
* @return Member |
|
289
|
|
|
*/ |
|
290
|
|
|
protected function parseMembersSelectSql(array $data) { |
|
291
|
|
|
$member = new Member($this->l10n); |
|
292
|
|
|
$member->setUserId($data['user_id']); |
|
293
|
|
|
$member->setCircleId($data['circle_id']); |
|
294
|
|
|
$member->setLevel($data['level']); |
|
295
|
|
|
$member->setStatus($data['status']); |
|
296
|
|
|
$member->setJoined($data['joined']); |
|
297
|
|
|
|
|
298
|
|
|
return $member; |
|
299
|
|
|
} |
|
300
|
|
|
|
|
301
|
|
|
|
|
302
|
|
|
/** |
|
303
|
|
|
* @param array $data |
|
304
|
|
|
* |
|
305
|
|
|
* @return Circle |
|
|
|
|
|
|
306
|
|
|
*/ |
|
307
|
|
|
protected function parseCirclesSelectSql($data) { |
|
308
|
|
|
if ($data === false || $data === null) { |
|
309
|
|
|
return null; |
|
310
|
|
|
} |
|
311
|
|
|
|
|
312
|
|
|
$circle = new Circle($this->l10n); |
|
313
|
|
|
$circle->setId($data['id']); |
|
314
|
|
|
$circle->setUniqueId($data['unique_id']); |
|
315
|
|
|
$circle->setName($data['name']); |
|
316
|
|
|
$circle->setDescription($data['description']); |
|
317
|
|
|
$circle->setType($data['type']); |
|
318
|
|
|
$circle->setCreation($data['creation']); |
|
319
|
|
|
|
|
320
|
|
|
if (key_exists('user_level', $data)) { |
|
321
|
|
|
$user = new Member($this->l10n); |
|
322
|
|
|
$user->setLevel($data['user_level']); |
|
323
|
|
|
$circle->setUser($user); |
|
324
|
|
|
} |
|
325
|
|
|
|
|
326
|
|
|
return $circle; |
|
327
|
|
|
} |
|
328
|
|
|
|
|
329
|
|
|
|
|
330
|
|
|
/** |
|
331
|
|
|
* @param array $data |
|
332
|
|
|
* |
|
333
|
|
|
* @return SharingFrame |
|
|
|
|
|
|
334
|
|
|
*/ |
|
335
|
|
|
protected function parseSharesSelectSql($data) { |
|
336
|
|
|
if ($data === false || $data === null) { |
|
337
|
|
|
return null; |
|
338
|
|
|
} |
|
339
|
|
|
|
|
340
|
|
|
$frame = new SharingFrame($data['source'], $data['type']); |
|
341
|
|
|
$frame->setCircleId($data['circle_id']); |
|
342
|
|
|
$frame->setAuthor($data['author']); |
|
343
|
|
|
$frame->setCloudId($data['cloud_id']); |
|
344
|
|
|
$frame->setPayload(json_decode($data['payload'], true)); |
|
345
|
|
|
$frame->setCreation($data['creation']); |
|
346
|
|
|
$frame->setHeaders(json_decode($data['headers'], true)); |
|
347
|
|
|
$frame->setUniqueId($data['unique_id']); |
|
348
|
|
|
|
|
349
|
|
|
return $frame; |
|
350
|
|
|
} |
|
351
|
|
|
|
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* @param array $data |
|
355
|
|
|
* |
|
356
|
|
|
* @return FederatedLink |
|
|
|
|
|
|
357
|
|
|
*/ |
|
358
|
|
View Code Duplication |
public function parseLinksSelectSql($data) { |
|
|
|
|
|
|
359
|
|
|
if ($data === false || $data === null) { |
|
360
|
|
|
return null; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
$link = new FederatedLink(); |
|
364
|
|
|
$link->setId($data['id']) |
|
365
|
|
|
->setUniqueId($data['unique_id']) |
|
366
|
|
|
->setStatus($data['status']) |
|
367
|
|
|
->setAddress($data['address']) |
|
368
|
|
|
->setToken($data['token']) |
|
369
|
|
|
->setCircleId($data['circle_id']); |
|
370
|
|
|
|
|
371
|
|
|
return $link; |
|
372
|
|
|
} |
|
373
|
|
|
|
|
374
|
|
|
|
|
375
|
|
|
} |
This check marks property names that have not been written in camelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection string becomes
databaseConnectionString.