Completed
Pull Request — master (#5816)
by Joas
26:55 queued 12:26
created
lib/private/DB/QueryBuilder/QueryBuilder.php 2 patches
Indentation   +1161 added lines, -1161 removed lines patch added patch discarded remove patch
@@ -46,1165 +46,1165 @@
 block discarded – undo
46 46
 
47 47
 class QueryBuilder implements IQueryBuilder {
48 48
 
49
-	/** @var \OCP\IDBConnection */
50
-	private $connection;
51
-
52
-	/** @var SystemConfig */
53
-	private $systemConfig;
54
-
55
-	/** @var ILogger */
56
-	private $logger;
57
-
58
-	/** @var \Doctrine\DBAL\Query\QueryBuilder */
59
-	private $queryBuilder;
60
-
61
-	/** @var QuoteHelper */
62
-	private $helper;
63
-
64
-	/** @var bool */
65
-	private $automaticTablePrefix = true;
66
-
67
-	/** @var string */
68
-	protected $lastInsertedTable;
69
-
70
-	/**
71
-	 * Initializes a new QueryBuilder.
72
-	 *
73
-	 * @param IDBConnection $connection
74
-	 * @param SystemConfig $systemConfig
75
-	 * @param ILogger $logger
76
-	 */
77
-	public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) {
78
-		$this->connection = $connection;
79
-		$this->systemConfig = $systemConfig;
80
-		$this->logger = $logger;
81
-		$this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection);
82
-		$this->helper = new QuoteHelper();
83
-	}
84
-
85
-	/**
86
-	 * Enable/disable automatic prefixing of table names with the oc_ prefix
87
-	 *
88
-	 * @param bool $enabled If set to true table names will be prefixed with the
89
-	 * owncloud database prefix automatically.
90
-	 * @since 8.2.0
91
-	 */
92
-	public function automaticTablePrefix($enabled) {
93
-		$this->automaticTablePrefix = (bool) $enabled;
94
-	}
95
-
96
-	/**
97
-	 * Gets an ExpressionBuilder used for object-oriented construction of query expressions.
98
-	 * This producer method is intended for convenient inline usage. Example:
99
-	 *
100
-	 * <code>
101
-	 *     $qb = $conn->getQueryBuilder()
102
-	 *         ->select('u')
103
-	 *         ->from('users', 'u')
104
-	 *         ->where($qb->expr()->eq('u.id', 1));
105
-	 * </code>
106
-	 *
107
-	 * For more complex expression construction, consider storing the expression
108
-	 * builder object in a local variable.
109
-	 *
110
-	 * @return \OCP\DB\QueryBuilder\IExpressionBuilder
111
-	 */
112
-	public function expr() {
113
-		if ($this->connection instanceof OracleConnection) {
114
-			return new OCIExpressionBuilder($this->connection);
115
-		} else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
116
-			return new PgSqlExpressionBuilder($this->connection);
117
-		} else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
118
-			return new MySqlExpressionBuilder($this->connection);
119
-		} else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
120
-			return new SqliteExpressionBuilder($this->connection);
121
-		} else {
122
-			return new ExpressionBuilder($this->connection);
123
-		}
124
-	}
125
-
126
-	/**
127
-	 * Gets an FunctionBuilder used for object-oriented construction of query functions.
128
-	 * This producer method is intended for convenient inline usage. Example:
129
-	 *
130
-	 * <code>
131
-	 *     $qb = $conn->getQueryBuilder()
132
-	 *         ->select('u')
133
-	 *         ->from('users', 'u')
134
-	 *         ->where($qb->fun()->md5('u.id'));
135
-	 * </code>
136
-	 *
137
-	 * For more complex function construction, consider storing the function
138
-	 * builder object in a local variable.
139
-	 *
140
-	 * @return \OCP\DB\QueryBuilder\IFunctionBuilder
141
-	 */
142
-	public function func() {
143
-		if ($this->connection instanceof OracleConnection) {
144
-			return new OCIFunctionBuilder($this->helper);
145
-		} else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
146
-			return new SqliteFunctionBuilder($this->helper);
147
-		} else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
148
-			return new PgSqlFunctionBuilder($this->helper);
149
-		} else {
150
-			return new FunctionBuilder($this->helper);
151
-		}
152
-	}
153
-
154
-	/**
155
-	 * Gets the type of the currently built query.
156
-	 *
157
-	 * @return integer
158
-	 */
159
-	public function getType() {
160
-		return $this->queryBuilder->getType();
161
-	}
162
-
163
-	/**
164
-	 * Gets the associated DBAL Connection for this query builder.
165
-	 *
166
-	 * @return \OCP\IDBConnection
167
-	 */
168
-	public function getConnection() {
169
-		return $this->connection;
170
-	}
171
-
172
-	/**
173
-	 * Gets the state of this query builder instance.
174
-	 *
175
-	 * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
176
-	 */
177
-	public function getState() {
178
-		return $this->queryBuilder->getState();
179
-	}
180
-
181
-	/**
182
-	 * Executes this query using the bound parameters and their types.
183
-	 *
184
-	 * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate}
185
-	 * for insert, update and delete statements.
186
-	 *
187
-	 * @return \Doctrine\DBAL\Driver\Statement|int
188
-	 */
189
-	public function execute() {
190
-		if ($this->systemConfig->getValue('log_query', false)) {
191
-			$params = [];
192
-			foreach ($this->getParameters() as $placeholder => $value) {
193
-				if (is_array($value)) {
194
-					$params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')';
195
-				} else {
196
-					$params[] = $placeholder . ' => \'' . $value . '\'';
197
-				}
198
-			}
199
-			if (empty($params)) {
200
-				$this->logger->debug('DB QueryBuilder: \'{query}\'', [
201
-					'query' => $this->getSQL(),
202
-					'app' => 'core',
203
-				]);
204
-			} else {
205
-				$this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [
206
-					'query' => $this->getSQL(),
207
-					'params' => implode(', ', $params),
208
-					'app' => 'core',
209
-				]);
210
-			}
211
-		}
212
-
213
-		return $this->queryBuilder->execute();
214
-	}
215
-
216
-	/**
217
-	 * Gets the complete SQL string formed by the current specifications of this QueryBuilder.
218
-	 *
219
-	 * <code>
220
-	 *     $qb = $conn->getQueryBuilder()
221
-	 *         ->select('u')
222
-	 *         ->from('User', 'u')
223
-	 *     echo $qb->getSQL(); // SELECT u FROM User u
224
-	 * </code>
225
-	 *
226
-	 * @return string The SQL query string.
227
-	 */
228
-	public function getSQL() {
229
-		return $this->queryBuilder->getSQL();
230
-	}
231
-
232
-	/**
233
-	 * Sets a query parameter for the query being constructed.
234
-	 *
235
-	 * <code>
236
-	 *     $qb = $conn->getQueryBuilder()
237
-	 *         ->select('u')
238
-	 *         ->from('users', 'u')
239
-	 *         ->where('u.id = :user_id')
240
-	 *         ->setParameter(':user_id', 1);
241
-	 * </code>
242
-	 *
243
-	 * @param string|integer $key The parameter position or name.
244
-	 * @param mixed $value The parameter value.
245
-	 * @param string|null $type One of the IQueryBuilder::PARAM_* constants.
246
-	 *
247
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
248
-	 */
249
-	public function setParameter($key, $value, $type = null) {
250
-		$this->queryBuilder->setParameter($key, $value, $type);
251
-
252
-		return $this;
253
-	}
254
-
255
-	/**
256
-	 * Sets a collection of query parameters for the query being constructed.
257
-	 *
258
-	 * <code>
259
-	 *     $qb = $conn->getQueryBuilder()
260
-	 *         ->select('u')
261
-	 *         ->from('users', 'u')
262
-	 *         ->where('u.id = :user_id1 OR u.id = :user_id2')
263
-	 *         ->setParameters(array(
264
-	 *             ':user_id1' => 1,
265
-	 *             ':user_id2' => 2
266
-	 *         ));
267
-	 * </code>
268
-	 *
269
-	 * @param array $params The query parameters to set.
270
-	 * @param array $types The query parameters types to set.
271
-	 *
272
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
273
-	 */
274
-	public function setParameters(array $params, array $types = array()) {
275
-		$this->queryBuilder->setParameters($params, $types);
276
-
277
-		return $this;
278
-	}
279
-
280
-	/**
281
-	 * Gets all defined query parameters for the query being constructed indexed by parameter index or name.
282
-	 *
283
-	 * @return array The currently defined query parameters indexed by parameter index or name.
284
-	 */
285
-	public function getParameters() {
286
-		return $this->queryBuilder->getParameters();
287
-	}
288
-
289
-	/**
290
-	 * Gets a (previously set) query parameter of the query being constructed.
291
-	 *
292
-	 * @param mixed $key The key (index or name) of the bound parameter.
293
-	 *
294
-	 * @return mixed The value of the bound parameter.
295
-	 */
296
-	public function getParameter($key) {
297
-		return $this->queryBuilder->getParameter($key);
298
-	}
299
-
300
-	/**
301
-	 * Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
302
-	 *
303
-	 * @return array The currently defined query parameter types indexed by parameter index or name.
304
-	 */
305
-	public function getParameterTypes() {
306
-		return $this->queryBuilder->getParameterTypes();
307
-	}
308
-
309
-	/**
310
-	 * Gets a (previously set) query parameter type of the query being constructed.
311
-	 *
312
-	 * @param mixed $key The key (index or name) of the bound parameter type.
313
-	 *
314
-	 * @return mixed The value of the bound parameter type.
315
-	 */
316
-	public function getParameterType($key) {
317
-		return $this->queryBuilder->getParameterType($key);
318
-	}
319
-
320
-	/**
321
-	 * Sets the position of the first result to retrieve (the "offset").
322
-	 *
323
-	 * @param integer $firstResult The first result to return.
324
-	 *
325
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
326
-	 */
327
-	public function setFirstResult($firstResult) {
328
-		$this->queryBuilder->setFirstResult($firstResult);
329
-
330
-		return $this;
331
-	}
332
-
333
-	/**
334
-	 * Gets the position of the first result the query object was set to retrieve (the "offset").
335
-	 * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder.
336
-	 *
337
-	 * @return integer The position of the first result.
338
-	 */
339
-	public function getFirstResult() {
340
-		return $this->queryBuilder->getFirstResult();
341
-	}
342
-
343
-	/**
344
-	 * Sets the maximum number of results to retrieve (the "limit").
345
-	 *
346
-	 * NOTE: Setting max results to "0" will cause mixed behaviour. While most
347
-	 * of the databases will just return an empty result set, Oracle will return
348
-	 * all entries.
349
-	 *
350
-	 * @param integer $maxResults The maximum number of results to retrieve.
351
-	 *
352
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
353
-	 */
354
-	public function setMaxResults($maxResults) {
355
-		$this->queryBuilder->setMaxResults($maxResults);
356
-
357
-		return $this;
358
-	}
359
-
360
-	/**
361
-	 * Gets the maximum number of results the query object was set to retrieve (the "limit").
362
-	 * Returns NULL if {@link setMaxResults} was not applied to this query builder.
363
-	 *
364
-	 * @return integer The maximum number of results.
365
-	 */
366
-	public function getMaxResults() {
367
-		return $this->queryBuilder->getMaxResults();
368
-	}
369
-
370
-	/**
371
-	 * Specifies an item that is to be returned in the query result.
372
-	 * Replaces any previously specified selections, if any.
373
-	 *
374
-	 * <code>
375
-	 *     $qb = $conn->getQueryBuilder()
376
-	 *         ->select('u.id', 'p.id')
377
-	 *         ->from('users', 'u')
378
-	 *         ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
379
-	 * </code>
380
-	 *
381
-	 * @param mixed $select The selection expressions.
382
-	 *
383
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
384
-	 */
385
-	public function select($select = null) {
386
-		$selects = is_array($select) ? $select : func_get_args();
387
-
388
-		$this->queryBuilder->select(
389
-			$this->helper->quoteColumnNames($selects)
390
-		);
391
-
392
-		return $this;
393
-	}
394
-
395
-	/**
396
-	 * Specifies an item that is to be returned with a different name in the query result.
397
-	 *
398
-	 * <code>
399
-	 *     $qb = $conn->getQueryBuilder()
400
-	 *         ->selectAlias('u.id', 'user_id')
401
-	 *         ->from('users', 'u')
402
-	 *         ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
403
-	 * </code>
404
-	 *
405
-	 * @param mixed $select The selection expressions.
406
-	 * @param string $alias The column alias used in the constructed query.
407
-	 *
408
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
409
-	 */
410
-	public function selectAlias($select, $alias) {
411
-
412
-		$this->queryBuilder->addSelect(
413
-			$this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias)
414
-		);
415
-
416
-		return $this;
417
-	}
418
-
419
-	/**
420
-	 * Specifies an item that is to be returned uniquely in the query result.
421
-	 *
422
-	 * <code>
423
-	 *     $qb = $conn->getQueryBuilder()
424
-	 *         ->selectDistinct('type')
425
-	 *         ->from('users');
426
-	 * </code>
427
-	 *
428
-	 * @param mixed $select The selection expressions.
429
-	 *
430
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
431
-	 */
432
-	public function selectDistinct($select) {
433
-
434
-		$this->queryBuilder->addSelect(
435
-			'DISTINCT ' . $this->helper->quoteColumnName($select)
436
-		);
437
-
438
-		return $this;
439
-	}
440
-
441
-	/**
442
-	 * Adds an item that is to be returned in the query result.
443
-	 *
444
-	 * <code>
445
-	 *     $qb = $conn->getQueryBuilder()
446
-	 *         ->select('u.id')
447
-	 *         ->addSelect('p.id')
448
-	 *         ->from('users', 'u')
449
-	 *         ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
450
-	 * </code>
451
-	 *
452
-	 * @param mixed $select The selection expression.
453
-	 *
454
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
455
-	 */
456
-	public function addSelect($select = null) {
457
-		$selects = is_array($select) ? $select : func_get_args();
458
-
459
-		$this->queryBuilder->addSelect(
460
-			$this->helper->quoteColumnNames($selects)
461
-		);
462
-
463
-		return $this;
464
-	}
465
-
466
-	/**
467
-	 * Turns the query being built into a bulk delete query that ranges over
468
-	 * a certain table.
469
-	 *
470
-	 * <code>
471
-	 *     $qb = $conn->getQueryBuilder()
472
-	 *         ->delete('users', 'u')
473
-	 *         ->where('u.id = :user_id');
474
-	 *         ->setParameter(':user_id', 1);
475
-	 * </code>
476
-	 *
477
-	 * @param string $delete The table whose rows are subject to the deletion.
478
-	 * @param string $alias The table alias used in the constructed query.
479
-	 *
480
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
481
-	 */
482
-	public function delete($delete = null, $alias = null) {
483
-		$this->queryBuilder->delete(
484
-			$this->getTableName($delete),
485
-			$alias
486
-		);
487
-
488
-		return $this;
489
-	}
490
-
491
-	/**
492
-	 * Turns the query being built into a bulk update query that ranges over
493
-	 * a certain table
494
-	 *
495
-	 * <code>
496
-	 *     $qb = $conn->getQueryBuilder()
497
-	 *         ->update('users', 'u')
498
-	 *         ->set('u.password', md5('password'))
499
-	 *         ->where('u.id = ?');
500
-	 * </code>
501
-	 *
502
-	 * @param string $update The table whose rows are subject to the update.
503
-	 * @param string $alias The table alias used in the constructed query.
504
-	 *
505
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
506
-	 */
507
-	public function update($update = null, $alias = null) {
508
-		$this->queryBuilder->update(
509
-			$this->getTableName($update),
510
-			$alias
511
-		);
512
-
513
-		return $this;
514
-	}
515
-
516
-	/**
517
-	 * Turns the query being built into an insert query that inserts into
518
-	 * a certain table
519
-	 *
520
-	 * <code>
521
-	 *     $qb = $conn->getQueryBuilder()
522
-	 *         ->insert('users')
523
-	 *         ->values(
524
-	 *             array(
525
-	 *                 'name' => '?',
526
-	 *                 'password' => '?'
527
-	 *             )
528
-	 *         );
529
-	 * </code>
530
-	 *
531
-	 * @param string $insert The table into which the rows should be inserted.
532
-	 *
533
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
534
-	 */
535
-	public function insert($insert = null) {
536
-		$this->queryBuilder->insert(
537
-			$this->getTableName($insert)
538
-		);
539
-
540
-		$this->lastInsertedTable = $insert;
541
-
542
-		return $this;
543
-	}
544
-
545
-	/**
546
-	 * Creates and adds a query root corresponding to the table identified by the
547
-	 * given alias, forming a cartesian product with any existing query roots.
548
-	 *
549
-	 * <code>
550
-	 *     $qb = $conn->getQueryBuilder()
551
-	 *         ->select('u.id')
552
-	 *         ->from('users', 'u')
553
-	 * </code>
554
-	 *
555
-	 * @param string $from The table.
556
-	 * @param string|null $alias The alias of the table.
557
-	 *
558
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
559
-	 */
560
-	public function from($from, $alias = null) {
561
-		$this->queryBuilder->from(
562
-			$this->getTableName($from),
563
-			$this->quoteAlias($alias)
564
-		);
565
-
566
-		return $this;
567
-	}
568
-
569
-	/**
570
-	 * Creates and adds a join to the query.
571
-	 *
572
-	 * <code>
573
-	 *     $qb = $conn->getQueryBuilder()
574
-	 *         ->select('u.name')
575
-	 *         ->from('users', 'u')
576
-	 *         ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
577
-	 * </code>
578
-	 *
579
-	 * @param string $fromAlias The alias that points to a from clause.
580
-	 * @param string $join The table name to join.
581
-	 * @param string $alias The alias of the join table.
582
-	 * @param string $condition The condition for the join.
583
-	 *
584
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
585
-	 */
586
-	public function join($fromAlias, $join, $alias, $condition = null) {
587
-		$this->queryBuilder->join(
588
-			$this->quoteAlias($fromAlias),
589
-			$this->getTableName($join),
590
-			$this->quoteAlias($alias),
591
-			$condition
592
-		);
593
-
594
-		return $this;
595
-	}
596
-
597
-	/**
598
-	 * Creates and adds a join to the query.
599
-	 *
600
-	 * <code>
601
-	 *     $qb = $conn->getQueryBuilder()
602
-	 *         ->select('u.name')
603
-	 *         ->from('users', 'u')
604
-	 *         ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
605
-	 * </code>
606
-	 *
607
-	 * @param string $fromAlias The alias that points to a from clause.
608
-	 * @param string $join The table name to join.
609
-	 * @param string $alias The alias of the join table.
610
-	 * @param string $condition The condition for the join.
611
-	 *
612
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
613
-	 */
614
-	public function innerJoin($fromAlias, $join, $alias, $condition = null) {
615
-		$this->queryBuilder->innerJoin(
616
-			$this->quoteAlias($fromAlias),
617
-			$this->getTableName($join),
618
-			$this->quoteAlias($alias),
619
-			$condition
620
-		);
621
-
622
-		return $this;
623
-	}
624
-
625
-	/**
626
-	 * Creates and adds a left join to the query.
627
-	 *
628
-	 * <code>
629
-	 *     $qb = $conn->getQueryBuilder()
630
-	 *         ->select('u.name')
631
-	 *         ->from('users', 'u')
632
-	 *         ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
633
-	 * </code>
634
-	 *
635
-	 * @param string $fromAlias The alias that points to a from clause.
636
-	 * @param string $join The table name to join.
637
-	 * @param string $alias The alias of the join table.
638
-	 * @param string $condition The condition for the join.
639
-	 *
640
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
641
-	 */
642
-	public function leftJoin($fromAlias, $join, $alias, $condition = null) {
643
-		$this->queryBuilder->leftJoin(
644
-			$this->quoteAlias($fromAlias),
645
-			$this->getTableName($join),
646
-			$this->quoteAlias($alias),
647
-			$condition
648
-		);
649
-
650
-		return $this;
651
-	}
652
-
653
-	/**
654
-	 * Creates and adds a right join to the query.
655
-	 *
656
-	 * <code>
657
-	 *     $qb = $conn->getQueryBuilder()
658
-	 *         ->select('u.name')
659
-	 *         ->from('users', 'u')
660
-	 *         ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
661
-	 * </code>
662
-	 *
663
-	 * @param string $fromAlias The alias that points to a from clause.
664
-	 * @param string $join The table name to join.
665
-	 * @param string $alias The alias of the join table.
666
-	 * @param string $condition The condition for the join.
667
-	 *
668
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
669
-	 */
670
-	public function rightJoin($fromAlias, $join, $alias, $condition = null) {
671
-		$this->queryBuilder->rightJoin(
672
-			$this->quoteAlias($fromAlias),
673
-			$this->getTableName($join),
674
-			$this->quoteAlias($alias),
675
-			$condition
676
-		);
677
-
678
-		return $this;
679
-	}
680
-
681
-	/**
682
-	 * Sets a new value for a column in a bulk update query.
683
-	 *
684
-	 * <code>
685
-	 *     $qb = $conn->getQueryBuilder()
686
-	 *         ->update('users', 'u')
687
-	 *         ->set('u.password', md5('password'))
688
-	 *         ->where('u.id = ?');
689
-	 * </code>
690
-	 *
691
-	 * @param string $key The column to set.
692
-	 * @param string $value The value, expression, placeholder, etc.
693
-	 *
694
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
695
-	 */
696
-	public function set($key, $value) {
697
-		$this->queryBuilder->set(
698
-			$this->helper->quoteColumnName($key),
699
-			$this->helper->quoteColumnName($value)
700
-		);
701
-
702
-		return $this;
703
-	}
704
-
705
-	/**
706
-	 * Specifies one or more restrictions to the query result.
707
-	 * Replaces any previously specified restrictions, if any.
708
-	 *
709
-	 * <code>
710
-	 *     $qb = $conn->getQueryBuilder()
711
-	 *         ->select('u.name')
712
-	 *         ->from('users', 'u')
713
-	 *         ->where('u.id = ?');
714
-	 *
715
-	 *     // You can optionally programatically build and/or expressions
716
-	 *     $qb = $conn->getQueryBuilder();
717
-	 *
718
-	 *     $or = $qb->expr()->orx();
719
-	 *     $or->add($qb->expr()->eq('u.id', 1));
720
-	 *     $or->add($qb->expr()->eq('u.id', 2));
721
-	 *
722
-	 *     $qb->update('users', 'u')
723
-	 *         ->set('u.password', md5('password'))
724
-	 *         ->where($or);
725
-	 * </code>
726
-	 *
727
-	 * @param mixed $predicates The restriction predicates.
728
-	 *
729
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
730
-	 */
731
-	public function where($predicates) {
732
-		call_user_func_array(
733
-			[$this->queryBuilder, 'where'],
734
-			func_get_args()
735
-		);
736
-
737
-		return $this;
738
-	}
739
-
740
-	/**
741
-	 * Adds one or more restrictions to the query results, forming a logical
742
-	 * conjunction with any previously specified restrictions.
743
-	 *
744
-	 * <code>
745
-	 *     $qb = $conn->getQueryBuilder()
746
-	 *         ->select('u')
747
-	 *         ->from('users', 'u')
748
-	 *         ->where('u.username LIKE ?')
749
-	 *         ->andWhere('u.is_active = 1');
750
-	 * </code>
751
-	 *
752
-	 * @param mixed $where The query restrictions.
753
-	 *
754
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
755
-	 *
756
-	 * @see where()
757
-	 */
758
-	public function andWhere($where) {
759
-		call_user_func_array(
760
-			[$this->queryBuilder, 'andWhere'],
761
-			func_get_args()
762
-		);
763
-
764
-		return $this;
765
-	}
766
-
767
-	/**
768
-	 * Adds one or more restrictions to the query results, forming a logical
769
-	 * disjunction with any previously specified restrictions.
770
-	 *
771
-	 * <code>
772
-	 *     $qb = $conn->getQueryBuilder()
773
-	 *         ->select('u.name')
774
-	 *         ->from('users', 'u')
775
-	 *         ->where('u.id = 1')
776
-	 *         ->orWhere('u.id = 2');
777
-	 * </code>
778
-	 *
779
-	 * @param mixed $where The WHERE statement.
780
-	 *
781
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
782
-	 *
783
-	 * @see where()
784
-	 */
785
-	public function orWhere($where) {
786
-		call_user_func_array(
787
-			[$this->queryBuilder, 'orWhere'],
788
-			func_get_args()
789
-		);
790
-
791
-		return $this;
792
-	}
793
-
794
-	/**
795
-	 * Specifies a grouping over the results of the query.
796
-	 * Replaces any previously specified groupings, if any.
797
-	 *
798
-	 * <code>
799
-	 *     $qb = $conn->getQueryBuilder()
800
-	 *         ->select('u.name')
801
-	 *         ->from('users', 'u')
802
-	 *         ->groupBy('u.id');
803
-	 * </code>
804
-	 *
805
-	 * @param mixed $groupBy The grouping expression.
806
-	 *
807
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
808
-	 */
809
-	public function groupBy($groupBy) {
810
-		$groupBys = is_array($groupBy) ? $groupBy : func_get_args();
811
-
812
-		call_user_func_array(
813
-			[$this->queryBuilder, 'groupBy'],
814
-			$this->helper->quoteColumnNames($groupBys)
815
-		);
816
-
817
-		return $this;
818
-	}
819
-
820
-	/**
821
-	 * Adds a grouping expression to the query.
822
-	 *
823
-	 * <code>
824
-	 *     $qb = $conn->getQueryBuilder()
825
-	 *         ->select('u.name')
826
-	 *         ->from('users', 'u')
827
-	 *         ->groupBy('u.lastLogin');
828
-	 *         ->addGroupBy('u.createdAt')
829
-	 * </code>
830
-	 *
831
-	 * @param mixed $groupBy The grouping expression.
832
-	 *
833
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
834
-	 */
835
-	public function addGroupBy($groupBy) {
836
-		$groupBys = is_array($groupBy) ? $groupBy : func_get_args();
837
-
838
-		call_user_func_array(
839
-			[$this->queryBuilder, 'addGroupBy'],
840
-			$this->helper->quoteColumnNames($groupBys)
841
-		);
842
-
843
-		return $this;
844
-	}
845
-
846
-	/**
847
-	 * Sets a value for a column in an insert query.
848
-	 *
849
-	 * <code>
850
-	 *     $qb = $conn->getQueryBuilder()
851
-	 *         ->insert('users')
852
-	 *         ->values(
853
-	 *             array(
854
-	 *                 'name' => '?'
855
-	 *             )
856
-	 *         )
857
-	 *         ->setValue('password', '?');
858
-	 * </code>
859
-	 *
860
-	 * @param string $column The column into which the value should be inserted.
861
-	 * @param string $value The value that should be inserted into the column.
862
-	 *
863
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
864
-	 */
865
-	public function setValue($column, $value) {
866
-		$this->queryBuilder->setValue(
867
-			$this->helper->quoteColumnName($column),
868
-			$value
869
-		);
870
-
871
-		return $this;
872
-	}
873
-
874
-	/**
875
-	 * Specifies values for an insert query indexed by column names.
876
-	 * Replaces any previous values, if any.
877
-	 *
878
-	 * <code>
879
-	 *     $qb = $conn->getQueryBuilder()
880
-	 *         ->insert('users')
881
-	 *         ->values(
882
-	 *             array(
883
-	 *                 'name' => '?',
884
-	 *                 'password' => '?'
885
-	 *             )
886
-	 *         );
887
-	 * </code>
888
-	 *
889
-	 * @param array $values The values to specify for the insert query indexed by column names.
890
-	 *
891
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
892
-	 */
893
-	public function values(array $values) {
894
-		$quotedValues = [];
895
-		foreach ($values as $key => $value) {
896
-			$quotedValues[$this->helper->quoteColumnName($key)] = $value;
897
-		}
898
-
899
-		$this->queryBuilder->values($quotedValues);
900
-
901
-		return $this;
902
-	}
903
-
904
-	/**
905
-	 * Specifies a restriction over the groups of the query.
906
-	 * Replaces any previous having restrictions, if any.
907
-	 *
908
-	 * @param mixed $having The restriction over the groups.
909
-	 *
910
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
911
-	 */
912
-	public function having($having) {
913
-		call_user_func_array(
914
-			[$this->queryBuilder, 'having'],
915
-			func_get_args()
916
-		);
917
-
918
-		return $this;
919
-	}
920
-
921
-	/**
922
-	 * Adds a restriction over the groups of the query, forming a logical
923
-	 * conjunction with any existing having restrictions.
924
-	 *
925
-	 * @param mixed $having The restriction to append.
926
-	 *
927
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
928
-	 */
929
-	public function andHaving($having) {
930
-		call_user_func_array(
931
-			[$this->queryBuilder, 'andHaving'],
932
-			func_get_args()
933
-		);
934
-
935
-		return $this;
936
-	}
937
-
938
-	/**
939
-	 * Adds a restriction over the groups of the query, forming a logical
940
-	 * disjunction with any existing having restrictions.
941
-	 *
942
-	 * @param mixed $having The restriction to add.
943
-	 *
944
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
945
-	 */
946
-	public function orHaving($having) {
947
-		call_user_func_array(
948
-			[$this->queryBuilder, 'orHaving'],
949
-			func_get_args()
950
-		);
951
-
952
-		return $this;
953
-	}
954
-
955
-	/**
956
-	 * Specifies an ordering for the query results.
957
-	 * Replaces any previously specified orderings, if any.
958
-	 *
959
-	 * @param string $sort The ordering expression.
960
-	 * @param string $order The ordering direction.
961
-	 *
962
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
963
-	 */
964
-	public function orderBy($sort, $order = null) {
965
-		$this->queryBuilder->orderBy(
966
-			$this->helper->quoteColumnName($sort),
967
-			$order
968
-		);
969
-
970
-		return $this;
971
-	}
972
-
973
-	/**
974
-	 * Adds an ordering to the query results.
975
-	 *
976
-	 * @param string $sort The ordering expression.
977
-	 * @param string $order The ordering direction.
978
-	 *
979
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
980
-	 */
981
-	public function addOrderBy($sort, $order = null) {
982
-		$this->queryBuilder->addOrderBy(
983
-			$this->helper->quoteColumnName($sort),
984
-			$order
985
-		);
986
-
987
-		return $this;
988
-	}
989
-
990
-	/**
991
-	 * Gets a query part by its name.
992
-	 *
993
-	 * @param string $queryPartName
994
-	 *
995
-	 * @return mixed
996
-	 */
997
-	public function getQueryPart($queryPartName) {
998
-		return $this->queryBuilder->getQueryPart($queryPartName);
999
-	}
1000
-
1001
-	/**
1002
-	 * Gets all query parts.
1003
-	 *
1004
-	 * @return array
1005
-	 */
1006
-	public function getQueryParts() {
1007
-		return $this->queryBuilder->getQueryParts();
1008
-	}
1009
-
1010
-	/**
1011
-	 * Resets SQL parts.
1012
-	 *
1013
-	 * @param array|null $queryPartNames
1014
-	 *
1015
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
1016
-	 */
1017
-	public function resetQueryParts($queryPartNames = null) {
1018
-		$this->queryBuilder->resetQueryParts($queryPartNames);
1019
-
1020
-		return $this;
1021
-	}
1022
-
1023
-	/**
1024
-	 * Resets a single SQL part.
1025
-	 *
1026
-	 * @param string $queryPartName
1027
-	 *
1028
-	 * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
1029
-	 */
1030
-	public function resetQueryPart($queryPartName) {
1031
-		$this->queryBuilder->resetQueryPart($queryPartName);
1032
-
1033
-		return $this;
1034
-	}
1035
-
1036
-	/**
1037
-	 * Creates a new named parameter and bind the value $value to it.
1038
-	 *
1039
-	 * This method provides a shortcut for PDOStatement::bindValue
1040
-	 * when using prepared statements.
1041
-	 *
1042
-	 * The parameter $value specifies the value that you want to bind. If
1043
-	 * $placeholder is not provided bindValue() will automatically create a
1044
-	 * placeholder for you. An automatic placeholder will be of the name
1045
-	 * ':dcValue1', ':dcValue2' etc.
1046
-	 *
1047
-	 * For more information see {@link http://php.net/pdostatement-bindparam}
1048
-	 *
1049
-	 * Example:
1050
-	 * <code>
1051
-	 * $value = 2;
1052
-	 * $q->eq( 'id', $q->bindValue( $value ) );
1053
-	 * $stmt = $q->executeQuery(); // executed with 'id = 2'
1054
-	 * </code>
1055
-	 *
1056
-	 * @license New BSD License
1057
-	 * @link http://www.zetacomponents.org
1058
-	 *
1059
-	 * @param mixed $value
1060
-	 * @param mixed $type
1061
-	 * @param string $placeHolder The name to bind with. The string must start with a colon ':'.
1062
-	 *
1063
-	 * @return IParameter the placeholder name used.
1064
-	 */
1065
-	public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) {
1066
-		return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder));
1067
-	}
1068
-
1069
-	/**
1070
-	 * Creates a new positional parameter and bind the given value to it.
1071
-	 *
1072
-	 * Attention: If you are using positional parameters with the query builder you have
1073
-	 * to be very careful to bind all parameters in the order they appear in the SQL
1074
-	 * statement , otherwise they get bound in the wrong order which can lead to serious
1075
-	 * bugs in your code.
1076
-	 *
1077
-	 * Example:
1078
-	 * <code>
1079
-	 *  $qb = $conn->getQueryBuilder();
1080
-	 *  $qb->select('u.*')
1081
-	 *     ->from('users', 'u')
1082
-	 *     ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR))
1083
-	 *     ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))
1084
-	 * </code>
1085
-	 *
1086
-	 * @param mixed $value
1087
-	 * @param integer $type
1088
-	 *
1089
-	 * @return IParameter
1090
-	 */
1091
-	public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) {
1092
-		return new Parameter($this->queryBuilder->createPositionalParameter($value, $type));
1093
-	}
1094
-
1095
-	/**
1096
-	 * Creates a new parameter
1097
-	 *
1098
-	 * Example:
1099
-	 * <code>
1100
-	 *  $qb = $conn->getQueryBuilder();
1101
-	 *  $qb->select('u.*')
1102
-	 *     ->from('users', 'u')
1103
-	 *     ->where('u.username = ' . $qb->createParameter('name'))
1104
-	 *     ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))
1105
-	 * </code>
1106
-	 *
1107
-	 * @param string $name
1108
-	 *
1109
-	 * @return IParameter
1110
-	 */
1111
-	public function createParameter($name) {
1112
-		return new Parameter(':' . $name);
1113
-	}
1114
-
1115
-	/**
1116
-	 * Creates a new function
1117
-	 *
1118
-	 * Attention: Column names inside the call have to be quoted before hand
1119
-	 *
1120
-	 * Example:
1121
-	 * <code>
1122
-	 *  $qb = $conn->getQueryBuilder();
1123
-	 *  $qb->select($qb->createFunction('COUNT(*)'))
1124
-	 *     ->from('users', 'u')
1125
-	 *  echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u
1126
-	 * </code>
1127
-	 * <code>
1128
-	 *  $qb = $conn->getQueryBuilder();
1129
-	 *  $qb->select($qb->createFunction('COUNT(`column`)'))
1130
-	 *     ->from('users', 'u')
1131
-	 *  echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u
1132
-	 * </code>
1133
-	 *
1134
-	 * @param string $call
1135
-	 *
1136
-	 * @return IQueryFunction
1137
-	 */
1138
-	public function createFunction($call) {
1139
-		return new QueryFunction($call);
1140
-	}
1141
-
1142
-	/**
1143
-	 * Used to get the id of the last inserted element
1144
-	 * @return int
1145
-	 * @throws \BadMethodCallException When being called before an insert query has been run.
1146
-	 */
1147
-	public function getLastInsertId() {
1148
-		if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) {
1149
-			// lastInsertId() needs the prefix but no quotes
1150
-			$table = $this->prefixTableName($this->lastInsertedTable);
1151
-			return (int) $this->connection->lastInsertId($table);
1152
-		}
1153
-
1154
-		throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.');
1155
-	}
1156
-
1157
-	/**
1158
-	 * Returns the table name quoted and with database prefix as needed by the implementation
1159
-	 *
1160
-	 * @param string $table
1161
-	 * @return string
1162
-	 */
1163
-	public function getTableName($table) {
1164
-		$table = $this->prefixTableName($table);
1165
-		return $this->helper->quoteColumnName($table);
1166
-	}
1167
-
1168
-	/**
1169
-	 * Returns the table name with database prefix as needed by the implementation
1170
-	 *
1171
-	 * @param string $table
1172
-	 * @return string
1173
-	 */
1174
-	protected function prefixTableName($table) {
1175
-		if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) {
1176
-			return $table;
1177
-		}
1178
-
1179
-		return '*PREFIX*' . $table;
1180
-	}
1181
-
1182
-	/**
1183
-	 * Returns the column name quoted and with table alias prefix as needed by the implementation
1184
-	 *
1185
-	 * @param string $column
1186
-	 * @param string $tableAlias
1187
-	 * @return string
1188
-	 */
1189
-	public function getColumnName($column, $tableAlias = '') {
1190
-		if ($tableAlias !== '') {
1191
-			$tableAlias .= '.';
1192
-		}
1193
-
1194
-		return $this->helper->quoteColumnName($tableAlias . $column);
1195
-	}
1196
-
1197
-	/**
1198
-	 * Returns the column name quoted and with table alias prefix as needed by the implementation
1199
-	 *
1200
-	 * @param string $alias
1201
-	 * @return string
1202
-	 */
1203
-	public function quoteAlias($alias) {
1204
-		if ($alias === '' || $alias === null) {
1205
-			return $alias;
1206
-		}
1207
-
1208
-		return $this->helper->quoteColumnName($alias);
1209
-	}
49
+    /** @var \OCP\IDBConnection */
50
+    private $connection;
51
+
52
+    /** @var SystemConfig */
53
+    private $systemConfig;
54
+
55
+    /** @var ILogger */
56
+    private $logger;
57
+
58
+    /** @var \Doctrine\DBAL\Query\QueryBuilder */
59
+    private $queryBuilder;
60
+
61
+    /** @var QuoteHelper */
62
+    private $helper;
63
+
64
+    /** @var bool */
65
+    private $automaticTablePrefix = true;
66
+
67
+    /** @var string */
68
+    protected $lastInsertedTable;
69
+
70
+    /**
71
+     * Initializes a new QueryBuilder.
72
+     *
73
+     * @param IDBConnection $connection
74
+     * @param SystemConfig $systemConfig
75
+     * @param ILogger $logger
76
+     */
77
+    public function __construct(IDBConnection $connection, SystemConfig $systemConfig, ILogger $logger) {
78
+        $this->connection = $connection;
79
+        $this->systemConfig = $systemConfig;
80
+        $this->logger = $logger;
81
+        $this->queryBuilder = new \Doctrine\DBAL\Query\QueryBuilder($this->connection);
82
+        $this->helper = new QuoteHelper();
83
+    }
84
+
85
+    /**
86
+     * Enable/disable automatic prefixing of table names with the oc_ prefix
87
+     *
88
+     * @param bool $enabled If set to true table names will be prefixed with the
89
+     * owncloud database prefix automatically.
90
+     * @since 8.2.0
91
+     */
92
+    public function automaticTablePrefix($enabled) {
93
+        $this->automaticTablePrefix = (bool) $enabled;
94
+    }
95
+
96
+    /**
97
+     * Gets an ExpressionBuilder used for object-oriented construction of query expressions.
98
+     * This producer method is intended for convenient inline usage. Example:
99
+     *
100
+     * <code>
101
+     *     $qb = $conn->getQueryBuilder()
102
+     *         ->select('u')
103
+     *         ->from('users', 'u')
104
+     *         ->where($qb->expr()->eq('u.id', 1));
105
+     * </code>
106
+     *
107
+     * For more complex expression construction, consider storing the expression
108
+     * builder object in a local variable.
109
+     *
110
+     * @return \OCP\DB\QueryBuilder\IExpressionBuilder
111
+     */
112
+    public function expr() {
113
+        if ($this->connection instanceof OracleConnection) {
114
+            return new OCIExpressionBuilder($this->connection);
115
+        } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
116
+            return new PgSqlExpressionBuilder($this->connection);
117
+        } else if ($this->connection->getDatabasePlatform() instanceof MySqlPlatform) {
118
+            return new MySqlExpressionBuilder($this->connection);
119
+        } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
120
+            return new SqliteExpressionBuilder($this->connection);
121
+        } else {
122
+            return new ExpressionBuilder($this->connection);
123
+        }
124
+    }
125
+
126
+    /**
127
+     * Gets an FunctionBuilder used for object-oriented construction of query functions.
128
+     * This producer method is intended for convenient inline usage. Example:
129
+     *
130
+     * <code>
131
+     *     $qb = $conn->getQueryBuilder()
132
+     *         ->select('u')
133
+     *         ->from('users', 'u')
134
+     *         ->where($qb->fun()->md5('u.id'));
135
+     * </code>
136
+     *
137
+     * For more complex function construction, consider storing the function
138
+     * builder object in a local variable.
139
+     *
140
+     * @return \OCP\DB\QueryBuilder\IFunctionBuilder
141
+     */
142
+    public function func() {
143
+        if ($this->connection instanceof OracleConnection) {
144
+            return new OCIFunctionBuilder($this->helper);
145
+        } else if ($this->connection->getDatabasePlatform() instanceof SqlitePlatform) {
146
+            return new SqliteFunctionBuilder($this->helper);
147
+        } else if ($this->connection->getDatabasePlatform() instanceof PostgreSqlPlatform) {
148
+            return new PgSqlFunctionBuilder($this->helper);
149
+        } else {
150
+            return new FunctionBuilder($this->helper);
151
+        }
152
+    }
153
+
154
+    /**
155
+     * Gets the type of the currently built query.
156
+     *
157
+     * @return integer
158
+     */
159
+    public function getType() {
160
+        return $this->queryBuilder->getType();
161
+    }
162
+
163
+    /**
164
+     * Gets the associated DBAL Connection for this query builder.
165
+     *
166
+     * @return \OCP\IDBConnection
167
+     */
168
+    public function getConnection() {
169
+        return $this->connection;
170
+    }
171
+
172
+    /**
173
+     * Gets the state of this query builder instance.
174
+     *
175
+     * @return integer Either QueryBuilder::STATE_DIRTY or QueryBuilder::STATE_CLEAN.
176
+     */
177
+    public function getState() {
178
+        return $this->queryBuilder->getState();
179
+    }
180
+
181
+    /**
182
+     * Executes this query using the bound parameters and their types.
183
+     *
184
+     * Uses {@see Connection::executeQuery} for select statements and {@see Connection::executeUpdate}
185
+     * for insert, update and delete statements.
186
+     *
187
+     * @return \Doctrine\DBAL\Driver\Statement|int
188
+     */
189
+    public function execute() {
190
+        if ($this->systemConfig->getValue('log_query', false)) {
191
+            $params = [];
192
+            foreach ($this->getParameters() as $placeholder => $value) {
193
+                if (is_array($value)) {
194
+                    $params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')';
195
+                } else {
196
+                    $params[] = $placeholder . ' => \'' . $value . '\'';
197
+                }
198
+            }
199
+            if (empty($params)) {
200
+                $this->logger->debug('DB QueryBuilder: \'{query}\'', [
201
+                    'query' => $this->getSQL(),
202
+                    'app' => 'core',
203
+                ]);
204
+            } else {
205
+                $this->logger->debug('DB QueryBuilder: \'{query}\' with parameters: {params}', [
206
+                    'query' => $this->getSQL(),
207
+                    'params' => implode(', ', $params),
208
+                    'app' => 'core',
209
+                ]);
210
+            }
211
+        }
212
+
213
+        return $this->queryBuilder->execute();
214
+    }
215
+
216
+    /**
217
+     * Gets the complete SQL string formed by the current specifications of this QueryBuilder.
218
+     *
219
+     * <code>
220
+     *     $qb = $conn->getQueryBuilder()
221
+     *         ->select('u')
222
+     *         ->from('User', 'u')
223
+     *     echo $qb->getSQL(); // SELECT u FROM User u
224
+     * </code>
225
+     *
226
+     * @return string The SQL query string.
227
+     */
228
+    public function getSQL() {
229
+        return $this->queryBuilder->getSQL();
230
+    }
231
+
232
+    /**
233
+     * Sets a query parameter for the query being constructed.
234
+     *
235
+     * <code>
236
+     *     $qb = $conn->getQueryBuilder()
237
+     *         ->select('u')
238
+     *         ->from('users', 'u')
239
+     *         ->where('u.id = :user_id')
240
+     *         ->setParameter(':user_id', 1);
241
+     * </code>
242
+     *
243
+     * @param string|integer $key The parameter position or name.
244
+     * @param mixed $value The parameter value.
245
+     * @param string|null $type One of the IQueryBuilder::PARAM_* constants.
246
+     *
247
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
248
+     */
249
+    public function setParameter($key, $value, $type = null) {
250
+        $this->queryBuilder->setParameter($key, $value, $type);
251
+
252
+        return $this;
253
+    }
254
+
255
+    /**
256
+     * Sets a collection of query parameters for the query being constructed.
257
+     *
258
+     * <code>
259
+     *     $qb = $conn->getQueryBuilder()
260
+     *         ->select('u')
261
+     *         ->from('users', 'u')
262
+     *         ->where('u.id = :user_id1 OR u.id = :user_id2')
263
+     *         ->setParameters(array(
264
+     *             ':user_id1' => 1,
265
+     *             ':user_id2' => 2
266
+     *         ));
267
+     * </code>
268
+     *
269
+     * @param array $params The query parameters to set.
270
+     * @param array $types The query parameters types to set.
271
+     *
272
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
273
+     */
274
+    public function setParameters(array $params, array $types = array()) {
275
+        $this->queryBuilder->setParameters($params, $types);
276
+
277
+        return $this;
278
+    }
279
+
280
+    /**
281
+     * Gets all defined query parameters for the query being constructed indexed by parameter index or name.
282
+     *
283
+     * @return array The currently defined query parameters indexed by parameter index or name.
284
+     */
285
+    public function getParameters() {
286
+        return $this->queryBuilder->getParameters();
287
+    }
288
+
289
+    /**
290
+     * Gets a (previously set) query parameter of the query being constructed.
291
+     *
292
+     * @param mixed $key The key (index or name) of the bound parameter.
293
+     *
294
+     * @return mixed The value of the bound parameter.
295
+     */
296
+    public function getParameter($key) {
297
+        return $this->queryBuilder->getParameter($key);
298
+    }
299
+
300
+    /**
301
+     * Gets all defined query parameter types for the query being constructed indexed by parameter index or name.
302
+     *
303
+     * @return array The currently defined query parameter types indexed by parameter index or name.
304
+     */
305
+    public function getParameterTypes() {
306
+        return $this->queryBuilder->getParameterTypes();
307
+    }
308
+
309
+    /**
310
+     * Gets a (previously set) query parameter type of the query being constructed.
311
+     *
312
+     * @param mixed $key The key (index or name) of the bound parameter type.
313
+     *
314
+     * @return mixed The value of the bound parameter type.
315
+     */
316
+    public function getParameterType($key) {
317
+        return $this->queryBuilder->getParameterType($key);
318
+    }
319
+
320
+    /**
321
+     * Sets the position of the first result to retrieve (the "offset").
322
+     *
323
+     * @param integer $firstResult The first result to return.
324
+     *
325
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
326
+     */
327
+    public function setFirstResult($firstResult) {
328
+        $this->queryBuilder->setFirstResult($firstResult);
329
+
330
+        return $this;
331
+    }
332
+
333
+    /**
334
+     * Gets the position of the first result the query object was set to retrieve (the "offset").
335
+     * Returns NULL if {@link setFirstResult} was not applied to this QueryBuilder.
336
+     *
337
+     * @return integer The position of the first result.
338
+     */
339
+    public function getFirstResult() {
340
+        return $this->queryBuilder->getFirstResult();
341
+    }
342
+
343
+    /**
344
+     * Sets the maximum number of results to retrieve (the "limit").
345
+     *
346
+     * NOTE: Setting max results to "0" will cause mixed behaviour. While most
347
+     * of the databases will just return an empty result set, Oracle will return
348
+     * all entries.
349
+     *
350
+     * @param integer $maxResults The maximum number of results to retrieve.
351
+     *
352
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
353
+     */
354
+    public function setMaxResults($maxResults) {
355
+        $this->queryBuilder->setMaxResults($maxResults);
356
+
357
+        return $this;
358
+    }
359
+
360
+    /**
361
+     * Gets the maximum number of results the query object was set to retrieve (the "limit").
362
+     * Returns NULL if {@link setMaxResults} was not applied to this query builder.
363
+     *
364
+     * @return integer The maximum number of results.
365
+     */
366
+    public function getMaxResults() {
367
+        return $this->queryBuilder->getMaxResults();
368
+    }
369
+
370
+    /**
371
+     * Specifies an item that is to be returned in the query result.
372
+     * Replaces any previously specified selections, if any.
373
+     *
374
+     * <code>
375
+     *     $qb = $conn->getQueryBuilder()
376
+     *         ->select('u.id', 'p.id')
377
+     *         ->from('users', 'u')
378
+     *         ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
379
+     * </code>
380
+     *
381
+     * @param mixed $select The selection expressions.
382
+     *
383
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
384
+     */
385
+    public function select($select = null) {
386
+        $selects = is_array($select) ? $select : func_get_args();
387
+
388
+        $this->queryBuilder->select(
389
+            $this->helper->quoteColumnNames($selects)
390
+        );
391
+
392
+        return $this;
393
+    }
394
+
395
+    /**
396
+     * Specifies an item that is to be returned with a different name in the query result.
397
+     *
398
+     * <code>
399
+     *     $qb = $conn->getQueryBuilder()
400
+     *         ->selectAlias('u.id', 'user_id')
401
+     *         ->from('users', 'u')
402
+     *         ->leftJoin('u', 'phonenumbers', 'p', 'u.id = p.user_id');
403
+     * </code>
404
+     *
405
+     * @param mixed $select The selection expressions.
406
+     * @param string $alias The column alias used in the constructed query.
407
+     *
408
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
409
+     */
410
+    public function selectAlias($select, $alias) {
411
+
412
+        $this->queryBuilder->addSelect(
413
+            $this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias)
414
+        );
415
+
416
+        return $this;
417
+    }
418
+
419
+    /**
420
+     * Specifies an item that is to be returned uniquely in the query result.
421
+     *
422
+     * <code>
423
+     *     $qb = $conn->getQueryBuilder()
424
+     *         ->selectDistinct('type')
425
+     *         ->from('users');
426
+     * </code>
427
+     *
428
+     * @param mixed $select The selection expressions.
429
+     *
430
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
431
+     */
432
+    public function selectDistinct($select) {
433
+
434
+        $this->queryBuilder->addSelect(
435
+            'DISTINCT ' . $this->helper->quoteColumnName($select)
436
+        );
437
+
438
+        return $this;
439
+    }
440
+
441
+    /**
442
+     * Adds an item that is to be returned in the query result.
443
+     *
444
+     * <code>
445
+     *     $qb = $conn->getQueryBuilder()
446
+     *         ->select('u.id')
447
+     *         ->addSelect('p.id')
448
+     *         ->from('users', 'u')
449
+     *         ->leftJoin('u', 'phonenumbers', 'u.id = p.user_id');
450
+     * </code>
451
+     *
452
+     * @param mixed $select The selection expression.
453
+     *
454
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
455
+     */
456
+    public function addSelect($select = null) {
457
+        $selects = is_array($select) ? $select : func_get_args();
458
+
459
+        $this->queryBuilder->addSelect(
460
+            $this->helper->quoteColumnNames($selects)
461
+        );
462
+
463
+        return $this;
464
+    }
465
+
466
+    /**
467
+     * Turns the query being built into a bulk delete query that ranges over
468
+     * a certain table.
469
+     *
470
+     * <code>
471
+     *     $qb = $conn->getQueryBuilder()
472
+     *         ->delete('users', 'u')
473
+     *         ->where('u.id = :user_id');
474
+     *         ->setParameter(':user_id', 1);
475
+     * </code>
476
+     *
477
+     * @param string $delete The table whose rows are subject to the deletion.
478
+     * @param string $alias The table alias used in the constructed query.
479
+     *
480
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
481
+     */
482
+    public function delete($delete = null, $alias = null) {
483
+        $this->queryBuilder->delete(
484
+            $this->getTableName($delete),
485
+            $alias
486
+        );
487
+
488
+        return $this;
489
+    }
490
+
491
+    /**
492
+     * Turns the query being built into a bulk update query that ranges over
493
+     * a certain table
494
+     *
495
+     * <code>
496
+     *     $qb = $conn->getQueryBuilder()
497
+     *         ->update('users', 'u')
498
+     *         ->set('u.password', md5('password'))
499
+     *         ->where('u.id = ?');
500
+     * </code>
501
+     *
502
+     * @param string $update The table whose rows are subject to the update.
503
+     * @param string $alias The table alias used in the constructed query.
504
+     *
505
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
506
+     */
507
+    public function update($update = null, $alias = null) {
508
+        $this->queryBuilder->update(
509
+            $this->getTableName($update),
510
+            $alias
511
+        );
512
+
513
+        return $this;
514
+    }
515
+
516
+    /**
517
+     * Turns the query being built into an insert query that inserts into
518
+     * a certain table
519
+     *
520
+     * <code>
521
+     *     $qb = $conn->getQueryBuilder()
522
+     *         ->insert('users')
523
+     *         ->values(
524
+     *             array(
525
+     *                 'name' => '?',
526
+     *                 'password' => '?'
527
+     *             )
528
+     *         );
529
+     * </code>
530
+     *
531
+     * @param string $insert The table into which the rows should be inserted.
532
+     *
533
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
534
+     */
535
+    public function insert($insert = null) {
536
+        $this->queryBuilder->insert(
537
+            $this->getTableName($insert)
538
+        );
539
+
540
+        $this->lastInsertedTable = $insert;
541
+
542
+        return $this;
543
+    }
544
+
545
+    /**
546
+     * Creates and adds a query root corresponding to the table identified by the
547
+     * given alias, forming a cartesian product with any existing query roots.
548
+     *
549
+     * <code>
550
+     *     $qb = $conn->getQueryBuilder()
551
+     *         ->select('u.id')
552
+     *         ->from('users', 'u')
553
+     * </code>
554
+     *
555
+     * @param string $from The table.
556
+     * @param string|null $alias The alias of the table.
557
+     *
558
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
559
+     */
560
+    public function from($from, $alias = null) {
561
+        $this->queryBuilder->from(
562
+            $this->getTableName($from),
563
+            $this->quoteAlias($alias)
564
+        );
565
+
566
+        return $this;
567
+    }
568
+
569
+    /**
570
+     * Creates and adds a join to the query.
571
+     *
572
+     * <code>
573
+     *     $qb = $conn->getQueryBuilder()
574
+     *         ->select('u.name')
575
+     *         ->from('users', 'u')
576
+     *         ->join('u', 'phonenumbers', 'p', 'p.is_primary = 1');
577
+     * </code>
578
+     *
579
+     * @param string $fromAlias The alias that points to a from clause.
580
+     * @param string $join The table name to join.
581
+     * @param string $alias The alias of the join table.
582
+     * @param string $condition The condition for the join.
583
+     *
584
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
585
+     */
586
+    public function join($fromAlias, $join, $alias, $condition = null) {
587
+        $this->queryBuilder->join(
588
+            $this->quoteAlias($fromAlias),
589
+            $this->getTableName($join),
590
+            $this->quoteAlias($alias),
591
+            $condition
592
+        );
593
+
594
+        return $this;
595
+    }
596
+
597
+    /**
598
+     * Creates and adds a join to the query.
599
+     *
600
+     * <code>
601
+     *     $qb = $conn->getQueryBuilder()
602
+     *         ->select('u.name')
603
+     *         ->from('users', 'u')
604
+     *         ->innerJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
605
+     * </code>
606
+     *
607
+     * @param string $fromAlias The alias that points to a from clause.
608
+     * @param string $join The table name to join.
609
+     * @param string $alias The alias of the join table.
610
+     * @param string $condition The condition for the join.
611
+     *
612
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
613
+     */
614
+    public function innerJoin($fromAlias, $join, $alias, $condition = null) {
615
+        $this->queryBuilder->innerJoin(
616
+            $this->quoteAlias($fromAlias),
617
+            $this->getTableName($join),
618
+            $this->quoteAlias($alias),
619
+            $condition
620
+        );
621
+
622
+        return $this;
623
+    }
624
+
625
+    /**
626
+     * Creates and adds a left join to the query.
627
+     *
628
+     * <code>
629
+     *     $qb = $conn->getQueryBuilder()
630
+     *         ->select('u.name')
631
+     *         ->from('users', 'u')
632
+     *         ->leftJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
633
+     * </code>
634
+     *
635
+     * @param string $fromAlias The alias that points to a from clause.
636
+     * @param string $join The table name to join.
637
+     * @param string $alias The alias of the join table.
638
+     * @param string $condition The condition for the join.
639
+     *
640
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
641
+     */
642
+    public function leftJoin($fromAlias, $join, $alias, $condition = null) {
643
+        $this->queryBuilder->leftJoin(
644
+            $this->quoteAlias($fromAlias),
645
+            $this->getTableName($join),
646
+            $this->quoteAlias($alias),
647
+            $condition
648
+        );
649
+
650
+        return $this;
651
+    }
652
+
653
+    /**
654
+     * Creates and adds a right join to the query.
655
+     *
656
+     * <code>
657
+     *     $qb = $conn->getQueryBuilder()
658
+     *         ->select('u.name')
659
+     *         ->from('users', 'u')
660
+     *         ->rightJoin('u', 'phonenumbers', 'p', 'p.is_primary = 1');
661
+     * </code>
662
+     *
663
+     * @param string $fromAlias The alias that points to a from clause.
664
+     * @param string $join The table name to join.
665
+     * @param string $alias The alias of the join table.
666
+     * @param string $condition The condition for the join.
667
+     *
668
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
669
+     */
670
+    public function rightJoin($fromAlias, $join, $alias, $condition = null) {
671
+        $this->queryBuilder->rightJoin(
672
+            $this->quoteAlias($fromAlias),
673
+            $this->getTableName($join),
674
+            $this->quoteAlias($alias),
675
+            $condition
676
+        );
677
+
678
+        return $this;
679
+    }
680
+
681
+    /**
682
+     * Sets a new value for a column in a bulk update query.
683
+     *
684
+     * <code>
685
+     *     $qb = $conn->getQueryBuilder()
686
+     *         ->update('users', 'u')
687
+     *         ->set('u.password', md5('password'))
688
+     *         ->where('u.id = ?');
689
+     * </code>
690
+     *
691
+     * @param string $key The column to set.
692
+     * @param string $value The value, expression, placeholder, etc.
693
+     *
694
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
695
+     */
696
+    public function set($key, $value) {
697
+        $this->queryBuilder->set(
698
+            $this->helper->quoteColumnName($key),
699
+            $this->helper->quoteColumnName($value)
700
+        );
701
+
702
+        return $this;
703
+    }
704
+
705
+    /**
706
+     * Specifies one or more restrictions to the query result.
707
+     * Replaces any previously specified restrictions, if any.
708
+     *
709
+     * <code>
710
+     *     $qb = $conn->getQueryBuilder()
711
+     *         ->select('u.name')
712
+     *         ->from('users', 'u')
713
+     *         ->where('u.id = ?');
714
+     *
715
+     *     // You can optionally programatically build and/or expressions
716
+     *     $qb = $conn->getQueryBuilder();
717
+     *
718
+     *     $or = $qb->expr()->orx();
719
+     *     $or->add($qb->expr()->eq('u.id', 1));
720
+     *     $or->add($qb->expr()->eq('u.id', 2));
721
+     *
722
+     *     $qb->update('users', 'u')
723
+     *         ->set('u.password', md5('password'))
724
+     *         ->where($or);
725
+     * </code>
726
+     *
727
+     * @param mixed $predicates The restriction predicates.
728
+     *
729
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
730
+     */
731
+    public function where($predicates) {
732
+        call_user_func_array(
733
+            [$this->queryBuilder, 'where'],
734
+            func_get_args()
735
+        );
736
+
737
+        return $this;
738
+    }
739
+
740
+    /**
741
+     * Adds one or more restrictions to the query results, forming a logical
742
+     * conjunction with any previously specified restrictions.
743
+     *
744
+     * <code>
745
+     *     $qb = $conn->getQueryBuilder()
746
+     *         ->select('u')
747
+     *         ->from('users', 'u')
748
+     *         ->where('u.username LIKE ?')
749
+     *         ->andWhere('u.is_active = 1');
750
+     * </code>
751
+     *
752
+     * @param mixed $where The query restrictions.
753
+     *
754
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
755
+     *
756
+     * @see where()
757
+     */
758
+    public function andWhere($where) {
759
+        call_user_func_array(
760
+            [$this->queryBuilder, 'andWhere'],
761
+            func_get_args()
762
+        );
763
+
764
+        return $this;
765
+    }
766
+
767
+    /**
768
+     * Adds one or more restrictions to the query results, forming a logical
769
+     * disjunction with any previously specified restrictions.
770
+     *
771
+     * <code>
772
+     *     $qb = $conn->getQueryBuilder()
773
+     *         ->select('u.name')
774
+     *         ->from('users', 'u')
775
+     *         ->where('u.id = 1')
776
+     *         ->orWhere('u.id = 2');
777
+     * </code>
778
+     *
779
+     * @param mixed $where The WHERE statement.
780
+     *
781
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
782
+     *
783
+     * @see where()
784
+     */
785
+    public function orWhere($where) {
786
+        call_user_func_array(
787
+            [$this->queryBuilder, 'orWhere'],
788
+            func_get_args()
789
+        );
790
+
791
+        return $this;
792
+    }
793
+
794
+    /**
795
+     * Specifies a grouping over the results of the query.
796
+     * Replaces any previously specified groupings, if any.
797
+     *
798
+     * <code>
799
+     *     $qb = $conn->getQueryBuilder()
800
+     *         ->select('u.name')
801
+     *         ->from('users', 'u')
802
+     *         ->groupBy('u.id');
803
+     * </code>
804
+     *
805
+     * @param mixed $groupBy The grouping expression.
806
+     *
807
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
808
+     */
809
+    public function groupBy($groupBy) {
810
+        $groupBys = is_array($groupBy) ? $groupBy : func_get_args();
811
+
812
+        call_user_func_array(
813
+            [$this->queryBuilder, 'groupBy'],
814
+            $this->helper->quoteColumnNames($groupBys)
815
+        );
816
+
817
+        return $this;
818
+    }
819
+
820
+    /**
821
+     * Adds a grouping expression to the query.
822
+     *
823
+     * <code>
824
+     *     $qb = $conn->getQueryBuilder()
825
+     *         ->select('u.name')
826
+     *         ->from('users', 'u')
827
+     *         ->groupBy('u.lastLogin');
828
+     *         ->addGroupBy('u.createdAt')
829
+     * </code>
830
+     *
831
+     * @param mixed $groupBy The grouping expression.
832
+     *
833
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
834
+     */
835
+    public function addGroupBy($groupBy) {
836
+        $groupBys = is_array($groupBy) ? $groupBy : func_get_args();
837
+
838
+        call_user_func_array(
839
+            [$this->queryBuilder, 'addGroupBy'],
840
+            $this->helper->quoteColumnNames($groupBys)
841
+        );
842
+
843
+        return $this;
844
+    }
845
+
846
+    /**
847
+     * Sets a value for a column in an insert query.
848
+     *
849
+     * <code>
850
+     *     $qb = $conn->getQueryBuilder()
851
+     *         ->insert('users')
852
+     *         ->values(
853
+     *             array(
854
+     *                 'name' => '?'
855
+     *             )
856
+     *         )
857
+     *         ->setValue('password', '?');
858
+     * </code>
859
+     *
860
+     * @param string $column The column into which the value should be inserted.
861
+     * @param string $value The value that should be inserted into the column.
862
+     *
863
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
864
+     */
865
+    public function setValue($column, $value) {
866
+        $this->queryBuilder->setValue(
867
+            $this->helper->quoteColumnName($column),
868
+            $value
869
+        );
870
+
871
+        return $this;
872
+    }
873
+
874
+    /**
875
+     * Specifies values for an insert query indexed by column names.
876
+     * Replaces any previous values, if any.
877
+     *
878
+     * <code>
879
+     *     $qb = $conn->getQueryBuilder()
880
+     *         ->insert('users')
881
+     *         ->values(
882
+     *             array(
883
+     *                 'name' => '?',
884
+     *                 'password' => '?'
885
+     *             )
886
+     *         );
887
+     * </code>
888
+     *
889
+     * @param array $values The values to specify for the insert query indexed by column names.
890
+     *
891
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
892
+     */
893
+    public function values(array $values) {
894
+        $quotedValues = [];
895
+        foreach ($values as $key => $value) {
896
+            $quotedValues[$this->helper->quoteColumnName($key)] = $value;
897
+        }
898
+
899
+        $this->queryBuilder->values($quotedValues);
900
+
901
+        return $this;
902
+    }
903
+
904
+    /**
905
+     * Specifies a restriction over the groups of the query.
906
+     * Replaces any previous having restrictions, if any.
907
+     *
908
+     * @param mixed $having The restriction over the groups.
909
+     *
910
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
911
+     */
912
+    public function having($having) {
913
+        call_user_func_array(
914
+            [$this->queryBuilder, 'having'],
915
+            func_get_args()
916
+        );
917
+
918
+        return $this;
919
+    }
920
+
921
+    /**
922
+     * Adds a restriction over the groups of the query, forming a logical
923
+     * conjunction with any existing having restrictions.
924
+     *
925
+     * @param mixed $having The restriction to append.
926
+     *
927
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
928
+     */
929
+    public function andHaving($having) {
930
+        call_user_func_array(
931
+            [$this->queryBuilder, 'andHaving'],
932
+            func_get_args()
933
+        );
934
+
935
+        return $this;
936
+    }
937
+
938
+    /**
939
+     * Adds a restriction over the groups of the query, forming a logical
940
+     * disjunction with any existing having restrictions.
941
+     *
942
+     * @param mixed $having The restriction to add.
943
+     *
944
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
945
+     */
946
+    public function orHaving($having) {
947
+        call_user_func_array(
948
+            [$this->queryBuilder, 'orHaving'],
949
+            func_get_args()
950
+        );
951
+
952
+        return $this;
953
+    }
954
+
955
+    /**
956
+     * Specifies an ordering for the query results.
957
+     * Replaces any previously specified orderings, if any.
958
+     *
959
+     * @param string $sort The ordering expression.
960
+     * @param string $order The ordering direction.
961
+     *
962
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
963
+     */
964
+    public function orderBy($sort, $order = null) {
965
+        $this->queryBuilder->orderBy(
966
+            $this->helper->quoteColumnName($sort),
967
+            $order
968
+        );
969
+
970
+        return $this;
971
+    }
972
+
973
+    /**
974
+     * Adds an ordering to the query results.
975
+     *
976
+     * @param string $sort The ordering expression.
977
+     * @param string $order The ordering direction.
978
+     *
979
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
980
+     */
981
+    public function addOrderBy($sort, $order = null) {
982
+        $this->queryBuilder->addOrderBy(
983
+            $this->helper->quoteColumnName($sort),
984
+            $order
985
+        );
986
+
987
+        return $this;
988
+    }
989
+
990
+    /**
991
+     * Gets a query part by its name.
992
+     *
993
+     * @param string $queryPartName
994
+     *
995
+     * @return mixed
996
+     */
997
+    public function getQueryPart($queryPartName) {
998
+        return $this->queryBuilder->getQueryPart($queryPartName);
999
+    }
1000
+
1001
+    /**
1002
+     * Gets all query parts.
1003
+     *
1004
+     * @return array
1005
+     */
1006
+    public function getQueryParts() {
1007
+        return $this->queryBuilder->getQueryParts();
1008
+    }
1009
+
1010
+    /**
1011
+     * Resets SQL parts.
1012
+     *
1013
+     * @param array|null $queryPartNames
1014
+     *
1015
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
1016
+     */
1017
+    public function resetQueryParts($queryPartNames = null) {
1018
+        $this->queryBuilder->resetQueryParts($queryPartNames);
1019
+
1020
+        return $this;
1021
+    }
1022
+
1023
+    /**
1024
+     * Resets a single SQL part.
1025
+     *
1026
+     * @param string $queryPartName
1027
+     *
1028
+     * @return \OCP\DB\QueryBuilder\IQueryBuilder This QueryBuilder instance.
1029
+     */
1030
+    public function resetQueryPart($queryPartName) {
1031
+        $this->queryBuilder->resetQueryPart($queryPartName);
1032
+
1033
+        return $this;
1034
+    }
1035
+
1036
+    /**
1037
+     * Creates a new named parameter and bind the value $value to it.
1038
+     *
1039
+     * This method provides a shortcut for PDOStatement::bindValue
1040
+     * when using prepared statements.
1041
+     *
1042
+     * The parameter $value specifies the value that you want to bind. If
1043
+     * $placeholder is not provided bindValue() will automatically create a
1044
+     * placeholder for you. An automatic placeholder will be of the name
1045
+     * ':dcValue1', ':dcValue2' etc.
1046
+     *
1047
+     * For more information see {@link http://php.net/pdostatement-bindparam}
1048
+     *
1049
+     * Example:
1050
+     * <code>
1051
+     * $value = 2;
1052
+     * $q->eq( 'id', $q->bindValue( $value ) );
1053
+     * $stmt = $q->executeQuery(); // executed with 'id = 2'
1054
+     * </code>
1055
+     *
1056
+     * @license New BSD License
1057
+     * @link http://www.zetacomponents.org
1058
+     *
1059
+     * @param mixed $value
1060
+     * @param mixed $type
1061
+     * @param string $placeHolder The name to bind with. The string must start with a colon ':'.
1062
+     *
1063
+     * @return IParameter the placeholder name used.
1064
+     */
1065
+    public function createNamedParameter($value, $type = IQueryBuilder::PARAM_STR, $placeHolder = null) {
1066
+        return new Parameter($this->queryBuilder->createNamedParameter($value, $type, $placeHolder));
1067
+    }
1068
+
1069
+    /**
1070
+     * Creates a new positional parameter and bind the given value to it.
1071
+     *
1072
+     * Attention: If you are using positional parameters with the query builder you have
1073
+     * to be very careful to bind all parameters in the order they appear in the SQL
1074
+     * statement , otherwise they get bound in the wrong order which can lead to serious
1075
+     * bugs in your code.
1076
+     *
1077
+     * Example:
1078
+     * <code>
1079
+     *  $qb = $conn->getQueryBuilder();
1080
+     *  $qb->select('u.*')
1081
+     *     ->from('users', 'u')
1082
+     *     ->where('u.username = ' . $qb->createPositionalParameter('Foo', IQueryBuilder::PARAM_STR))
1083
+     *     ->orWhere('u.username = ' . $qb->createPositionalParameter('Bar', IQueryBuilder::PARAM_STR))
1084
+     * </code>
1085
+     *
1086
+     * @param mixed $value
1087
+     * @param integer $type
1088
+     *
1089
+     * @return IParameter
1090
+     */
1091
+    public function createPositionalParameter($value, $type = IQueryBuilder::PARAM_STR) {
1092
+        return new Parameter($this->queryBuilder->createPositionalParameter($value, $type));
1093
+    }
1094
+
1095
+    /**
1096
+     * Creates a new parameter
1097
+     *
1098
+     * Example:
1099
+     * <code>
1100
+     *  $qb = $conn->getQueryBuilder();
1101
+     *  $qb->select('u.*')
1102
+     *     ->from('users', 'u')
1103
+     *     ->where('u.username = ' . $qb->createParameter('name'))
1104
+     *     ->setParameter('name', 'Bar', IQueryBuilder::PARAM_STR))
1105
+     * </code>
1106
+     *
1107
+     * @param string $name
1108
+     *
1109
+     * @return IParameter
1110
+     */
1111
+    public function createParameter($name) {
1112
+        return new Parameter(':' . $name);
1113
+    }
1114
+
1115
+    /**
1116
+     * Creates a new function
1117
+     *
1118
+     * Attention: Column names inside the call have to be quoted before hand
1119
+     *
1120
+     * Example:
1121
+     * <code>
1122
+     *  $qb = $conn->getQueryBuilder();
1123
+     *  $qb->select($qb->createFunction('COUNT(*)'))
1124
+     *     ->from('users', 'u')
1125
+     *  echo $qb->getSQL(); // SELECT COUNT(*) FROM `users` u
1126
+     * </code>
1127
+     * <code>
1128
+     *  $qb = $conn->getQueryBuilder();
1129
+     *  $qb->select($qb->createFunction('COUNT(`column`)'))
1130
+     *     ->from('users', 'u')
1131
+     *  echo $qb->getSQL(); // SELECT COUNT(`column`) FROM `users` u
1132
+     * </code>
1133
+     *
1134
+     * @param string $call
1135
+     *
1136
+     * @return IQueryFunction
1137
+     */
1138
+    public function createFunction($call) {
1139
+        return new QueryFunction($call);
1140
+    }
1141
+
1142
+    /**
1143
+     * Used to get the id of the last inserted element
1144
+     * @return int
1145
+     * @throws \BadMethodCallException When being called before an insert query has been run.
1146
+     */
1147
+    public function getLastInsertId() {
1148
+        if ($this->getType() === \Doctrine\DBAL\Query\QueryBuilder::INSERT && $this->lastInsertedTable) {
1149
+            // lastInsertId() needs the prefix but no quotes
1150
+            $table = $this->prefixTableName($this->lastInsertedTable);
1151
+            return (int) $this->connection->lastInsertId($table);
1152
+        }
1153
+
1154
+        throw new \BadMethodCallException('Invalid call to getLastInsertId without using insert() before.');
1155
+    }
1156
+
1157
+    /**
1158
+     * Returns the table name quoted and with database prefix as needed by the implementation
1159
+     *
1160
+     * @param string $table
1161
+     * @return string
1162
+     */
1163
+    public function getTableName($table) {
1164
+        $table = $this->prefixTableName($table);
1165
+        return $this->helper->quoteColumnName($table);
1166
+    }
1167
+
1168
+    /**
1169
+     * Returns the table name with database prefix as needed by the implementation
1170
+     *
1171
+     * @param string $table
1172
+     * @return string
1173
+     */
1174
+    protected function prefixTableName($table) {
1175
+        if ($this->automaticTablePrefix === false || strpos($table, '*PREFIX*') === 0) {
1176
+            return $table;
1177
+        }
1178
+
1179
+        return '*PREFIX*' . $table;
1180
+    }
1181
+
1182
+    /**
1183
+     * Returns the column name quoted and with table alias prefix as needed by the implementation
1184
+     *
1185
+     * @param string $column
1186
+     * @param string $tableAlias
1187
+     * @return string
1188
+     */
1189
+    public function getColumnName($column, $tableAlias = '') {
1190
+        if ($tableAlias !== '') {
1191
+            $tableAlias .= '.';
1192
+        }
1193
+
1194
+        return $this->helper->quoteColumnName($tableAlias . $column);
1195
+    }
1196
+
1197
+    /**
1198
+     * Returns the column name quoted and with table alias prefix as needed by the implementation
1199
+     *
1200
+     * @param string $alias
1201
+     * @return string
1202
+     */
1203
+    public function quoteAlias($alias) {
1204
+        if ($alias === '' || $alias === null) {
1205
+            return $alias;
1206
+        }
1207
+
1208
+        return $this->helper->quoteColumnName($alias);
1209
+    }
1210 1210
 }
Please login to merge, or discard this patch.
Spacing   +7 added lines, -7 removed lines patch added patch discarded remove patch
@@ -191,9 +191,9 @@  discard block
 block discarded – undo
191 191
 			$params = [];
192 192
 			foreach ($this->getParameters() as $placeholder => $value) {
193 193
 				if (is_array($value)) {
194
-					$params[] = $placeholder . ' => (\'' . implode('\', \'', $value) . '\')';
194
+					$params[] = $placeholder.' => (\''.implode('\', \'', $value).'\')';
195 195
 				} else {
196
-					$params[] = $placeholder . ' => \'' . $value . '\'';
196
+					$params[] = $placeholder.' => \''.$value.'\'';
197 197
 				}
198 198
 			}
199 199
 			if (empty($params)) {
@@ -410,7 +410,7 @@  discard block
 block discarded – undo
410 410
 	public function selectAlias($select, $alias) {
411 411
 
412 412
 		$this->queryBuilder->addSelect(
413
-			$this->helper->quoteColumnName($select) . ' AS ' . $this->helper->quoteColumnName($alias)
413
+			$this->helper->quoteColumnName($select).' AS '.$this->helper->quoteColumnName($alias)
414 414
 		);
415 415
 
416 416
 		return $this;
@@ -432,7 +432,7 @@  discard block
 block discarded – undo
432 432
 	public function selectDistinct($select) {
433 433
 
434 434
 		$this->queryBuilder->addSelect(
435
-			'DISTINCT ' . $this->helper->quoteColumnName($select)
435
+			'DISTINCT '.$this->helper->quoteColumnName($select)
436 436
 		);
437 437
 
438 438
 		return $this;
@@ -1109,7 +1109,7 @@  discard block
 block discarded – undo
1109 1109
 	 * @return IParameter
1110 1110
 	 */
1111 1111
 	public function createParameter($name) {
1112
-		return new Parameter(':' . $name);
1112
+		return new Parameter(':'.$name);
1113 1113
 	}
1114 1114
 
1115 1115
 	/**
@@ -1176,7 +1176,7 @@  discard block
 block discarded – undo
1176 1176
 			return $table;
1177 1177
 		}
1178 1178
 
1179
-		return '*PREFIX*' . $table;
1179
+		return '*PREFIX*'.$table;
1180 1180
 	}
1181 1181
 
1182 1182
 	/**
@@ -1191,7 +1191,7 @@  discard block
 block discarded – undo
1191 1191
 			$tableAlias .= '.';
1192 1192
 		}
1193 1193
 
1194
-		return $this->helper->quoteColumnName($tableAlias . $column);
1194
+		return $this->helper->quoteColumnName($tableAlias.$column);
1195 1195
 	}
1196 1196
 
1197 1197
 	/**
Please login to merge, or discard this patch.
lib/private/DB/QueryBuilder/QuoteHelper.php 2 patches
Indentation   +41 added lines, -41 removed lines patch added patch discarded remove patch
@@ -27,55 +27,55 @@
 block discarded – undo
27 27
 use OCP\DB\QueryBuilder\IQueryFunction;
28 28
 
29 29
 class QuoteHelper {
30
-	/**
31
-	 * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter
32
-	 * @return array|string
33
-	 */
34
-	public function quoteColumnNames($strings) {
35
-		if (!is_array($strings)) {
36
-			return $this->quoteColumnName($strings);
37
-		}
30
+    /**
31
+     * @param array|string|ILiteral|IParameter|IQueryFunction $strings string, Literal or Parameter
32
+     * @return array|string
33
+     */
34
+    public function quoteColumnNames($strings) {
35
+        if (!is_array($strings)) {
36
+            return $this->quoteColumnName($strings);
37
+        }
38 38
 
39
-		$return = [];
40
-		foreach ($strings as $string) {
41
-			$return[] = $this->quoteColumnName($string);
42
-		}
39
+        $return = [];
40
+        foreach ($strings as $string) {
41
+            $return[] = $this->quoteColumnName($string);
42
+        }
43 43
 
44
-		return $return;
45
-	}
44
+        return $return;
45
+    }
46 46
 
47
-	/**
48
-	 * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter
49
-	 * @return string
50
-	 */
51
-	public function quoteColumnName($string) {
52
-		if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) {
53
-			return (string) $string;
54
-		}
47
+    /**
48
+     * @param string|ILiteral|IParameter|IQueryFunction $string string, Literal or Parameter
49
+     * @return string
50
+     */
51
+    public function quoteColumnName($string) {
52
+        if ($string instanceof IParameter || $string instanceof ILiteral || $string instanceof IQueryFunction) {
53
+            return (string) $string;
54
+        }
55 55
 
56
-		if ($string === null || $string === 'null' || $string === '*') {
57
-			return $string;
58
-		}
56
+        if ($string === null || $string === 'null' || $string === '*') {
57
+            return $string;
58
+        }
59 59
 
60
-		if (!is_string($string)) {
61
-			throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
62
-		}
60
+        if (!is_string($string)) {
61
+            throw new \InvalidArgumentException('Only strings, Literals and Parameters are allowed');
62
+        }
63 63
 
64
-		$string = str_replace(' AS ', ' as ', $string);
65
-		if (substr_count($string, ' as ')) {
66
-			return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2)));
67
-		}
64
+        $string = str_replace(' AS ', ' as ', $string);
65
+        if (substr_count($string, ' as ')) {
66
+            return implode(' as ', array_map([$this, 'quoteColumnName'], explode(' as ', $string, 2)));
67
+        }
68 68
 
69
-		if (substr_count($string, '.')) {
70
-			list($alias, $columnName) = explode('.', $string, 2);
69
+        if (substr_count($string, '.')) {
70
+            list($alias, $columnName) = explode('.', $string, 2);
71 71
 
72
-			if ($columnName === '*') {
73
-				return '`' . $alias . '`.*';
74
-			}
72
+            if ($columnName === '*') {
73
+                return '`' . $alias . '`.*';
74
+            }
75 75
 
76
-			return '`' . $alias . '`.`' . $columnName . '`';
77
-		}
76
+            return '`' . $alias . '`.`' . $columnName . '`';
77
+        }
78 78
 
79
-		return '`' . $string . '`';
80
-	}
79
+        return '`' . $string . '`';
80
+    }
81 81
 }
Please login to merge, or discard this patch.
Spacing   +3 added lines, -3 removed lines patch added patch discarded remove patch
@@ -70,12 +70,12 @@
 block discarded – undo
70 70
 			list($alias, $columnName) = explode('.', $string, 2);
71 71
 
72 72
 			if ($columnName === '*') {
73
-				return '`' . $alias . '`.*';
73
+				return '`'.$alias.'`.*';
74 74
 			}
75 75
 
76
-			return '`' . $alias . '`.`' . $columnName . '`';
76
+			return '`'.$alias.'`.`'.$columnName.'`';
77 77
 		}
78 78
 
79
-		return '`' . $string . '`';
79
+		return '`'.$string.'`';
80 80
 	}
81 81
 }
Please login to merge, or discard this patch.
lib/private/Comments/Manager.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -190,7 +190,7 @@  discard block
 block discarded – undo
190 190
 			!is_string($type) || empty($type)
191 191
 			|| !is_string($id) || empty($id)
192 192
 		) {
193
-			throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
193
+			throw new \InvalidArgumentException($role.' parameters must be string and not empty');
194 194
 		}
195 195
 	}
196 196
 
@@ -413,7 +413,7 @@  discard block
 block discarded – undo
413 413
 		$qb = $this->dbConn->getQueryBuilder();
414 414
 		$query = $qb->select('fileid')
415 415
 			->selectAlias(
416
-				$qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'),
416
+				$qb->createFunction('COUNT('.$qb->getColumnName('c.id').')'),
417 417
 				'num_ids'
418 418
 			)
419 419
 			->from('comments', 'c')
@@ -546,7 +546,7 @@  discard block
 block discarded – undo
546 546
 	 * @param IComment $comment
547 547
 	 * @return bool
548 548
 	 */
549
-	protected function insert(IComment &$comment) {
549
+	protected function insert(IComment & $comment) {
550 550
 		$qb = $this->dbConn->getQueryBuilder();
551 551
 		$affectedRows = $qb
552 552
 			->insert('comments')
@@ -845,7 +845,7 @@  discard block
 block discarded – undo
845 845
 		if (!isset($this->displayNameResolvers[$type])) {
846 846
 			throw new \OutOfBoundsException('No Displayname resolver for this type registered');
847 847
 		}
848
-		return (string)$this->displayNameResolvers[$type]($id);
848
+		return (string) $this->displayNameResolvers[$type]($id);
849 849
 	}
850 850
 
851 851
 	/**
Please login to merge, or discard this patch.
Indentation   +846 added lines, -846 removed lines patch added patch discarded remove patch
@@ -38,850 +38,850 @@
 block discarded – undo
38 38
 
39 39
 class Manager implements ICommentsManager {
40 40
 
41
-	/** @var  IDBConnection */
42
-	protected $dbConn;
43
-
44
-	/** @var  ILogger */
45
-	protected $logger;
46
-
47
-	/** @var IConfig */
48
-	protected $config;
49
-
50
-	/** @var IComment[] */
51
-	protected $commentsCache = [];
52
-
53
-	/** @var  \Closure[] */
54
-	protected $eventHandlerClosures = [];
55
-
56
-	/** @var  ICommentsEventHandler[] */
57
-	protected $eventHandlers = [];
58
-
59
-	/** @var \Closure[] */
60
-	protected $displayNameResolvers = [];
61
-
62
-	/**
63
-	 * Manager constructor.
64
-	 *
65
-	 * @param IDBConnection $dbConn
66
-	 * @param ILogger $logger
67
-	 * @param IConfig $config
68
-	 */
69
-	public function __construct(
70
-		IDBConnection $dbConn,
71
-		ILogger $logger,
72
-		IConfig $config
73
-	) {
74
-		$this->dbConn = $dbConn;
75
-		$this->logger = $logger;
76
-		$this->config = $config;
77
-	}
78
-
79
-	/**
80
-	 * converts data base data into PHP native, proper types as defined by
81
-	 * IComment interface.
82
-	 *
83
-	 * @param array $data
84
-	 * @return array
85
-	 */
86
-	protected function normalizeDatabaseData(array $data) {
87
-		$data['id'] = strval($data['id']);
88
-		$data['parent_id'] = strval($data['parent_id']);
89
-		$data['topmost_parent_id'] = strval($data['topmost_parent_id']);
90
-		$data['creation_timestamp'] = new \DateTime($data['creation_timestamp']);
91
-		if (!is_null($data['latest_child_timestamp'])) {
92
-			$data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
93
-		}
94
-		$data['children_count'] = intval($data['children_count']);
95
-		return $data;
96
-	}
97
-
98
-	/**
99
-	 * prepares a comment for an insert or update operation after making sure
100
-	 * all necessary fields have a value assigned.
101
-	 *
102
-	 * @param IComment $comment
103
-	 * @return IComment returns the same updated IComment instance as provided
104
-	 *                  by parameter for convenience
105
-	 * @throws \UnexpectedValueException
106
-	 */
107
-	protected function prepareCommentForDatabaseWrite(IComment $comment) {
108
-		if (!$comment->getActorType()
109
-			|| !$comment->getActorId()
110
-			|| !$comment->getObjectType()
111
-			|| !$comment->getObjectId()
112
-			|| !$comment->getVerb()
113
-		) {
114
-			throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
115
-		}
116
-
117
-		if ($comment->getId() === '') {
118
-			$comment->setChildrenCount(0);
119
-			$comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
120
-			$comment->setLatestChildDateTime(null);
121
-		}
122
-
123
-		if (is_null($comment->getCreationDateTime())) {
124
-			$comment->setCreationDateTime(new \DateTime());
125
-		}
126
-
127
-		if ($comment->getParentId() !== '0') {
128
-			$comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId()));
129
-		} else {
130
-			$comment->setTopmostParentId('0');
131
-		}
132
-
133
-		$this->cache($comment);
134
-
135
-		return $comment;
136
-	}
137
-
138
-	/**
139
-	 * returns the topmost parent id of a given comment identified by ID
140
-	 *
141
-	 * @param string $id
142
-	 * @return string
143
-	 * @throws NotFoundException
144
-	 */
145
-	protected function determineTopmostParentId($id) {
146
-		$comment = $this->get($id);
147
-		if ($comment->getParentId() === '0') {
148
-			return $comment->getId();
149
-		} else {
150
-			return $this->determineTopmostParentId($comment->getId());
151
-		}
152
-	}
153
-
154
-	/**
155
-	 * updates child information of a comment
156
-	 *
157
-	 * @param string $id
158
-	 * @param \DateTime $cDateTime the date time of the most recent child
159
-	 * @throws NotFoundException
160
-	 */
161
-	protected function updateChildrenInformation($id, \DateTime $cDateTime) {
162
-		$qb = $this->dbConn->getQueryBuilder();
163
-		$query = $qb->select($qb->createFunction('COUNT(`id`)'))
164
-			->from('comments')
165
-			->where($qb->expr()->eq('parent_id', $qb->createParameter('id')))
166
-			->setParameter('id', $id);
167
-
168
-		$resultStatement = $query->execute();
169
-		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
170
-		$resultStatement->closeCursor();
171
-		$children = intval($data[0]);
172
-
173
-		$comment = $this->get($id);
174
-		$comment->setChildrenCount($children);
175
-		$comment->setLatestChildDateTime($cDateTime);
176
-		$this->save($comment);
177
-	}
178
-
179
-	/**
180
-	 * Tests whether actor or object type and id parameters are acceptable.
181
-	 * Throws exception if not.
182
-	 *
183
-	 * @param string $role
184
-	 * @param string $type
185
-	 * @param string $id
186
-	 * @throws \InvalidArgumentException
187
-	 */
188
-	protected function checkRoleParameters($role, $type, $id) {
189
-		if (
190
-			!is_string($type) || empty($type)
191
-			|| !is_string($id) || empty($id)
192
-		) {
193
-			throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
194
-		}
195
-	}
196
-
197
-	/**
198
-	 * run-time caches a comment
199
-	 *
200
-	 * @param IComment $comment
201
-	 */
202
-	protected function cache(IComment $comment) {
203
-		$id = $comment->getId();
204
-		if (empty($id)) {
205
-			return;
206
-		}
207
-		$this->commentsCache[strval($id)] = $comment;
208
-	}
209
-
210
-	/**
211
-	 * removes an entry from the comments run time cache
212
-	 *
213
-	 * @param mixed $id the comment's id
214
-	 */
215
-	protected function uncache($id) {
216
-		$id = strval($id);
217
-		if (isset($this->commentsCache[$id])) {
218
-			unset($this->commentsCache[$id]);
219
-		}
220
-	}
221
-
222
-	/**
223
-	 * returns a comment instance
224
-	 *
225
-	 * @param string $id the ID of the comment
226
-	 * @return IComment
227
-	 * @throws NotFoundException
228
-	 * @throws \InvalidArgumentException
229
-	 * @since 9.0.0
230
-	 */
231
-	public function get($id) {
232
-		if (intval($id) === 0) {
233
-			throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.');
234
-		}
235
-
236
-		if (isset($this->commentsCache[$id])) {
237
-			return $this->commentsCache[$id];
238
-		}
239
-
240
-		$qb = $this->dbConn->getQueryBuilder();
241
-		$resultStatement = $qb->select('*')
242
-			->from('comments')
243
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
244
-			->setParameter('id', $id, IQueryBuilder::PARAM_INT)
245
-			->execute();
246
-
247
-		$data = $resultStatement->fetch();
248
-		$resultStatement->closeCursor();
249
-		if (!$data) {
250
-			throw new NotFoundException();
251
-		}
252
-
253
-		$comment = new Comment($this->normalizeDatabaseData($data));
254
-		$this->cache($comment);
255
-		return $comment;
256
-	}
257
-
258
-	/**
259
-	 * returns the comment specified by the id and all it's child comments.
260
-	 * At this point of time, we do only support one level depth.
261
-	 *
262
-	 * @param string $id
263
-	 * @param int $limit max number of entries to return, 0 returns all
264
-	 * @param int $offset the start entry
265
-	 * @return array
266
-	 * @since 9.0.0
267
-	 *
268
-	 * The return array looks like this
269
-	 * [
270
-	 *   'comment' => IComment, // root comment
271
-	 *   'replies' =>
272
-	 *   [
273
-	 *     0 =>
274
-	 *     [
275
-	 *       'comment' => IComment,
276
-	 *       'replies' => []
277
-	 *     ]
278
-	 *     1 =>
279
-	 *     [
280
-	 *       'comment' => IComment,
281
-	 *       'replies'=> []
282
-	 *     ],
283
-	 *     …
284
-	 *   ]
285
-	 * ]
286
-	 */
287
-	public function getTree($id, $limit = 0, $offset = 0) {
288
-		$tree = [];
289
-		$tree['comment'] = $this->get($id);
290
-		$tree['replies'] = [];
291
-
292
-		$qb = $this->dbConn->getQueryBuilder();
293
-		$query = $qb->select('*')
294
-			->from('comments')
295
-			->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id')))
296
-			->orderBy('creation_timestamp', 'DESC')
297
-			->setParameter('id', $id);
298
-
299
-		if ($limit > 0) {
300
-			$query->setMaxResults($limit);
301
-		}
302
-		if ($offset > 0) {
303
-			$query->setFirstResult($offset);
304
-		}
305
-
306
-		$resultStatement = $query->execute();
307
-		while ($data = $resultStatement->fetch()) {
308
-			$comment = new Comment($this->normalizeDatabaseData($data));
309
-			$this->cache($comment);
310
-			$tree['replies'][] = [
311
-				'comment' => $comment,
312
-				'replies' => []
313
-			];
314
-		}
315
-		$resultStatement->closeCursor();
316
-
317
-		return $tree;
318
-	}
319
-
320
-	/**
321
-	 * returns comments for a specific object (e.g. a file).
322
-	 *
323
-	 * The sort order is always newest to oldest.
324
-	 *
325
-	 * @param string $objectType the object type, e.g. 'files'
326
-	 * @param string $objectId the id of the object
327
-	 * @param int $limit optional, number of maximum comments to be returned. if
328
-	 * not specified, all comments are returned.
329
-	 * @param int $offset optional, starting point
330
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
331
-	 * that may be returned
332
-	 * @return IComment[]
333
-	 * @since 9.0.0
334
-	 */
335
-	public function getForObject(
336
-		$objectType,
337
-		$objectId,
338
-		$limit = 0,
339
-		$offset = 0,
340
-		\DateTime $notOlderThan = null
341
-	) {
342
-		$comments = [];
343
-
344
-		$qb = $this->dbConn->getQueryBuilder();
345
-		$query = $qb->select('*')
346
-			->from('comments')
347
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
348
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
349
-			->orderBy('creation_timestamp', 'DESC')
350
-			->setParameter('type', $objectType)
351
-			->setParameter('id', $objectId);
352
-
353
-		if ($limit > 0) {
354
-			$query->setMaxResults($limit);
355
-		}
356
-		if ($offset > 0) {
357
-			$query->setFirstResult($offset);
358
-		}
359
-		if (!is_null($notOlderThan)) {
360
-			$query
361
-				->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
362
-				->setParameter('notOlderThan', $notOlderThan, 'datetime');
363
-		}
364
-
365
-		$resultStatement = $query->execute();
366
-		while ($data = $resultStatement->fetch()) {
367
-			$comment = new Comment($this->normalizeDatabaseData($data));
368
-			$this->cache($comment);
369
-			$comments[] = $comment;
370
-		}
371
-		$resultStatement->closeCursor();
372
-
373
-		return $comments;
374
-	}
375
-
376
-	/**
377
-	 * @param $objectType string the object type, e.g. 'files'
378
-	 * @param $objectId string the id of the object
379
-	 * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
380
-	 * that may be returned
381
-	 * @return Int
382
-	 * @since 9.0.0
383
-	 */
384
-	public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {
385
-		$qb = $this->dbConn->getQueryBuilder();
386
-		$query = $qb->select($qb->createFunction('COUNT(`id`)'))
387
-			->from('comments')
388
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
389
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
390
-			->setParameter('type', $objectType)
391
-			->setParameter('id', $objectId);
392
-
393
-		if (!is_null($notOlderThan)) {
394
-			$query
395
-				->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
396
-				->setParameter('notOlderThan', $notOlderThan, 'datetime');
397
-		}
398
-
399
-		$resultStatement = $query->execute();
400
-		$data = $resultStatement->fetch(\PDO::FETCH_NUM);
401
-		$resultStatement->closeCursor();
402
-		return intval($data[0]);
403
-	}
404
-
405
-	/**
406
-	 * Get the number of unread comments for all files in a folder
407
-	 *
408
-	 * @param int $folderId
409
-	 * @param IUser $user
410
-	 * @return array [$fileId => $unreadCount]
411
-	 */
412
-	public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
413
-		$qb = $this->dbConn->getQueryBuilder();
414
-		$query = $qb->select('fileid')
415
-			->selectAlias(
416
-				$qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'),
417
-				'num_ids'
418
-			)
419
-			->from('comments', 'c')
420
-			->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
421
-				$qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
422
-				$qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT))
423
-			))
424
-			->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
425
-				$qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
426
-				$qb->expr()->eq('m.object_id', 'c.object_id'),
427
-				$qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID()))
428
-			))
429
-			->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)))
430
-			->andWhere($qb->expr()->orX(
431
-				$qb->expr()->gt('c.creation_timestamp', 'marker_datetime'),
432
-				$qb->expr()->isNull('marker_datetime')
433
-			))
434
-			->groupBy('f.fileid');
435
-
436
-		$resultStatement = $query->execute();
437
-
438
-		$results = [];
439
-		while ($row = $resultStatement->fetch()) {
440
-			$results[$row['fileid']] = (int) $row['num_ids'];
441
-		}
442
-		$resultStatement->closeCursor();
443
-		return $results;
444
-	}
445
-
446
-	/**
447
-	 * creates a new comment and returns it. At this point of time, it is not
448
-	 * saved in the used data storage. Use save() after setting other fields
449
-	 * of the comment (e.g. message or verb).
450
-	 *
451
-	 * @param string $actorType the actor type (e.g. 'users')
452
-	 * @param string $actorId a user id
453
-	 * @param string $objectType the object type the comment is attached to
454
-	 * @param string $objectId the object id the comment is attached to
455
-	 * @return IComment
456
-	 * @since 9.0.0
457
-	 */
458
-	public function create($actorType, $actorId, $objectType, $objectId) {
459
-		$comment = new Comment();
460
-		$comment
461
-			->setActor($actorType, $actorId)
462
-			->setObject($objectType, $objectId);
463
-		return $comment;
464
-	}
465
-
466
-	/**
467
-	 * permanently deletes the comment specified by the ID
468
-	 *
469
-	 * When the comment has child comments, their parent ID will be changed to
470
-	 * the parent ID of the item that is to be deleted.
471
-	 *
472
-	 * @param string $id
473
-	 * @return bool
474
-	 * @throws \InvalidArgumentException
475
-	 * @since 9.0.0
476
-	 */
477
-	public function delete($id) {
478
-		if (!is_string($id)) {
479
-			throw new \InvalidArgumentException('Parameter must be string');
480
-		}
481
-
482
-		try {
483
-			$comment = $this->get($id);
484
-		} catch (\Exception $e) {
485
-			// Ignore exceptions, we just don't fire a hook then
486
-			$comment = null;
487
-		}
488
-
489
-		$qb = $this->dbConn->getQueryBuilder();
490
-		$query = $qb->delete('comments')
491
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
492
-			->setParameter('id', $id);
493
-
494
-		try {
495
-			$affectedRows = $query->execute();
496
-			$this->uncache($id);
497
-		} catch (DriverException $e) {
498
-			$this->logger->logException($e, ['app' => 'core_comments']);
499
-			return false;
500
-		}
501
-
502
-		if ($affectedRows > 0 && $comment instanceof IComment) {
503
-			$this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
504
-		}
505
-
506
-		return ($affectedRows > 0);
507
-	}
508
-
509
-	/**
510
-	 * saves the comment permanently
511
-	 *
512
-	 * if the supplied comment has an empty ID, a new entry comment will be
513
-	 * saved and the instance updated with the new ID.
514
-	 *
515
-	 * Otherwise, an existing comment will be updated.
516
-	 *
517
-	 * Throws NotFoundException when a comment that is to be updated does not
518
-	 * exist anymore at this point of time.
519
-	 *
520
-	 * @param IComment $comment
521
-	 * @return bool
522
-	 * @throws NotFoundException
523
-	 * @since 9.0.0
524
-	 */
525
-	public function save(IComment $comment) {
526
-		if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
527
-			$result = $this->insert($comment);
528
-		} else {
529
-			$result = $this->update($comment);
530
-		}
531
-
532
-		if ($result && !!$comment->getParentId()) {
533
-			$this->updateChildrenInformation(
534
-				$comment->getParentId(),
535
-				$comment->getCreationDateTime()
536
-			);
537
-			$this->cache($comment);
538
-		}
539
-
540
-		return $result;
541
-	}
542
-
543
-	/**
544
-	 * inserts the provided comment in the database
545
-	 *
546
-	 * @param IComment $comment
547
-	 * @return bool
548
-	 */
549
-	protected function insert(IComment &$comment) {
550
-		$qb = $this->dbConn->getQueryBuilder();
551
-		$affectedRows = $qb
552
-			->insert('comments')
553
-			->values([
554
-				'parent_id' => $qb->createNamedParameter($comment->getParentId()),
555
-				'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
556
-				'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
557
-				'actor_type' => $qb->createNamedParameter($comment->getActorType()),
558
-				'actor_id' => $qb->createNamedParameter($comment->getActorId()),
559
-				'message' => $qb->createNamedParameter($comment->getMessage()),
560
-				'verb' => $qb->createNamedParameter($comment->getVerb()),
561
-				'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
562
-				'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
563
-				'object_type' => $qb->createNamedParameter($comment->getObjectType()),
564
-				'object_id' => $qb->createNamedParameter($comment->getObjectId()),
565
-			])
566
-			->execute();
567
-
568
-		if ($affectedRows > 0) {
569
-			$comment->setId(strval($qb->getLastInsertId()));
570
-			$this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
571
-		}
572
-
573
-		return $affectedRows > 0;
574
-	}
575
-
576
-	/**
577
-	 * updates a Comment data row
578
-	 *
579
-	 * @param IComment $comment
580
-	 * @return bool
581
-	 * @throws NotFoundException
582
-	 */
583
-	protected function update(IComment $comment) {
584
-		// for properly working preUpdate Events we need the old comments as is
585
-		// in the DB and overcome caching. Also avoid that outdated information stays.
586
-		$this->uncache($comment->getId());
587
-		$this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
588
-		$this->uncache($comment->getId());
589
-
590
-		$qb = $this->dbConn->getQueryBuilder();
591
-		$affectedRows = $qb
592
-			->update('comments')
593
-			->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
594
-			->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
595
-			->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
596
-			->set('actor_type', $qb->createNamedParameter($comment->getActorType()))
597
-			->set('actor_id', $qb->createNamedParameter($comment->getActorId()))
598
-			->set('message', $qb->createNamedParameter($comment->getMessage()))
599
-			->set('verb', $qb->createNamedParameter($comment->getVerb()))
600
-			->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'))
601
-			->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
602
-			->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
603
-			->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
604
-			->where($qb->expr()->eq('id', $qb->createParameter('id')))
605
-			->setParameter('id', $comment->getId())
606
-			->execute();
607
-
608
-		if ($affectedRows === 0) {
609
-			throw new NotFoundException('Comment to update does ceased to exist');
610
-		}
611
-
612
-		$this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
613
-
614
-		return $affectedRows > 0;
615
-	}
616
-
617
-	/**
618
-	 * removes references to specific actor (e.g. on user delete) of a comment.
619
-	 * The comment itself must not get lost/deleted.
620
-	 *
621
-	 * @param string $actorType the actor type (e.g. 'users')
622
-	 * @param string $actorId a user id
623
-	 * @return boolean
624
-	 * @since 9.0.0
625
-	 */
626
-	public function deleteReferencesOfActor($actorType, $actorId) {
627
-		$this->checkRoleParameters('Actor', $actorType, $actorId);
628
-
629
-		$qb = $this->dbConn->getQueryBuilder();
630
-		$affectedRows = $qb
631
-			->update('comments')
632
-			->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
633
-			->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
634
-			->where($qb->expr()->eq('actor_type', $qb->createParameter('type')))
635
-			->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id')))
636
-			->setParameter('type', $actorType)
637
-			->setParameter('id', $actorId)
638
-			->execute();
639
-
640
-		$this->commentsCache = [];
641
-
642
-		return is_int($affectedRows);
643
-	}
644
-
645
-	/**
646
-	 * deletes all comments made of a specific object (e.g. on file delete)
647
-	 *
648
-	 * @param string $objectType the object type (e.g. 'files')
649
-	 * @param string $objectId e.g. the file id
650
-	 * @return boolean
651
-	 * @since 9.0.0
652
-	 */
653
-	public function deleteCommentsAtObject($objectType, $objectId) {
654
-		$this->checkRoleParameters('Object', $objectType, $objectId);
655
-
656
-		$qb = $this->dbConn->getQueryBuilder();
657
-		$affectedRows = $qb
658
-			->delete('comments')
659
-			->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
660
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
661
-			->setParameter('type', $objectType)
662
-			->setParameter('id', $objectId)
663
-			->execute();
664
-
665
-		$this->commentsCache = [];
666
-
667
-		return is_int($affectedRows);
668
-	}
669
-
670
-	/**
671
-	 * deletes the read markers for the specified user
672
-	 *
673
-	 * @param \OCP\IUser $user
674
-	 * @return bool
675
-	 * @since 9.0.0
676
-	 */
677
-	public function deleteReadMarksFromUser(IUser $user) {
678
-		$qb = $this->dbConn->getQueryBuilder();
679
-		$query = $qb->delete('comments_read_markers')
680
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
681
-			->setParameter('user_id', $user->getUID());
682
-
683
-		try {
684
-			$affectedRows = $query->execute();
685
-		} catch (DriverException $e) {
686
-			$this->logger->logException($e, ['app' => 'core_comments']);
687
-			return false;
688
-		}
689
-		return ($affectedRows > 0);
690
-	}
691
-
692
-	/**
693
-	 * sets the read marker for a given file to the specified date for the
694
-	 * provided user
695
-	 *
696
-	 * @param string $objectType
697
-	 * @param string $objectId
698
-	 * @param \DateTime $dateTime
699
-	 * @param IUser $user
700
-	 * @since 9.0.0
701
-	 * @suppress SqlInjectionChecker
702
-	 */
703
-	public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
704
-		$this->checkRoleParameters('Object', $objectType, $objectId);
705
-
706
-		$qb = $this->dbConn->getQueryBuilder();
707
-		$values = [
708
-			'user_id' => $qb->createNamedParameter($user->getUID()),
709
-			'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
710
-			'object_type' => $qb->createNamedParameter($objectType),
711
-			'object_id' => $qb->createNamedParameter($objectId),
712
-		];
713
-
714
-		// Strategy: try to update, if this does not return affected rows, do an insert.
715
-		$affectedRows = $qb
716
-			->update('comments_read_markers')
717
-			->set('user_id', $values['user_id'])
718
-			->set('marker_datetime', $values['marker_datetime'])
719
-			->set('object_type', $values['object_type'])
720
-			->set('object_id', $values['object_id'])
721
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
722
-			->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
723
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
724
-			->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
725
-			->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
726
-			->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
727
-			->execute();
728
-
729
-		if ($affectedRows > 0) {
730
-			return;
731
-		}
732
-
733
-		$qb->insert('comments_read_markers')
734
-			->values($values)
735
-			->execute();
736
-	}
737
-
738
-	/**
739
-	 * returns the read marker for a given file to the specified date for the
740
-	 * provided user. It returns null, when the marker is not present, i.e.
741
-	 * no comments were marked as read.
742
-	 *
743
-	 * @param string $objectType
744
-	 * @param string $objectId
745
-	 * @param IUser $user
746
-	 * @return \DateTime|null
747
-	 * @since 9.0.0
748
-	 */
749
-	public function getReadMark($objectType, $objectId, IUser $user) {
750
-		$qb = $this->dbConn->getQueryBuilder();
751
-		$resultStatement = $qb->select('marker_datetime')
752
-			->from('comments_read_markers')
753
-			->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
754
-			->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
755
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
756
-			->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
757
-			->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
758
-			->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
759
-			->execute();
760
-
761
-		$data = $resultStatement->fetch();
762
-		$resultStatement->closeCursor();
763
-		if (!$data || is_null($data['marker_datetime'])) {
764
-			return null;
765
-		}
766
-
767
-		return new \DateTime($data['marker_datetime']);
768
-	}
769
-
770
-	/**
771
-	 * deletes the read markers on the specified object
772
-	 *
773
-	 * @param string $objectType
774
-	 * @param string $objectId
775
-	 * @return bool
776
-	 * @since 9.0.0
777
-	 */
778
-	public function deleteReadMarksOnObject($objectType, $objectId) {
779
-		$this->checkRoleParameters('Object', $objectType, $objectId);
780
-
781
-		$qb = $this->dbConn->getQueryBuilder();
782
-		$query = $qb->delete('comments_read_markers')
783
-			->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
784
-			->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
785
-			->setParameter('object_type', $objectType)
786
-			->setParameter('object_id', $objectId);
787
-
788
-		try {
789
-			$affectedRows = $query->execute();
790
-		} catch (DriverException $e) {
791
-			$this->logger->logException($e, ['app' => 'core_comments']);
792
-			return false;
793
-		}
794
-		return ($affectedRows > 0);
795
-	}
796
-
797
-	/**
798
-	 * registers an Entity to the manager, so event notifications can be send
799
-	 * to consumers of the comments infrastructure
800
-	 *
801
-	 * @param \Closure $closure
802
-	 */
803
-	public function registerEventHandler(\Closure $closure) {
804
-		$this->eventHandlerClosures[] = $closure;
805
-		$this->eventHandlers = [];
806
-	}
807
-
808
-	/**
809
-	 * registers a method that resolves an ID to a display name for a given type
810
-	 *
811
-	 * @param string $type
812
-	 * @param \Closure $closure
813
-	 * @throws \OutOfBoundsException
814
-	 * @since 11.0.0
815
-	 *
816
-	 * Only one resolver shall be registered per type. Otherwise a
817
-	 * \OutOfBoundsException has to thrown.
818
-	 */
819
-	public function registerDisplayNameResolver($type, \Closure $closure) {
820
-		if (!is_string($type)) {
821
-			throw new \InvalidArgumentException('String expected.');
822
-		}
823
-		if (isset($this->displayNameResolvers[$type])) {
824
-			throw new \OutOfBoundsException('Displayname resolver for this type already registered');
825
-		}
826
-		$this->displayNameResolvers[$type] = $closure;
827
-	}
828
-
829
-	/**
830
-	 * resolves a given ID of a given Type to a display name.
831
-	 *
832
-	 * @param string $type
833
-	 * @param string $id
834
-	 * @return string
835
-	 * @throws \OutOfBoundsException
836
-	 * @since 11.0.0
837
-	 *
838
-	 * If a provided type was not registered, an \OutOfBoundsException shall
839
-	 * be thrown. It is upon the resolver discretion what to return of the
840
-	 * provided ID is unknown. It must be ensured that a string is returned.
841
-	 */
842
-	public function resolveDisplayName($type, $id) {
843
-		if (!is_string($type)) {
844
-			throw new \InvalidArgumentException('String expected.');
845
-		}
846
-		if (!isset($this->displayNameResolvers[$type])) {
847
-			throw new \OutOfBoundsException('No Displayname resolver for this type registered');
848
-		}
849
-		return (string)$this->displayNameResolvers[$type]($id);
850
-	}
851
-
852
-	/**
853
-	 * returns valid, registered entities
854
-	 *
855
-	 * @return \OCP\Comments\ICommentsEventHandler[]
856
-	 */
857
-	private function getEventHandlers() {
858
-		if (!empty($this->eventHandlers)) {
859
-			return $this->eventHandlers;
860
-		}
861
-
862
-		$this->eventHandlers = [];
863
-		foreach ($this->eventHandlerClosures as $name => $closure) {
864
-			$entity = $closure();
865
-			if (!($entity instanceof ICommentsEventHandler)) {
866
-				throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface');
867
-			}
868
-			$this->eventHandlers[$name] = $entity;
869
-		}
870
-
871
-		return $this->eventHandlers;
872
-	}
873
-
874
-	/**
875
-	 * sends notifications to the registered entities
876
-	 *
877
-	 * @param $eventType
878
-	 * @param IComment $comment
879
-	 */
880
-	private function sendEvent($eventType, IComment $comment) {
881
-		$entities = $this->getEventHandlers();
882
-		$event = new CommentsEvent($eventType, $comment);
883
-		foreach ($entities as $entity) {
884
-			$entity->handle($event);
885
-		}
886
-	}
41
+    /** @var  IDBConnection */
42
+    protected $dbConn;
43
+
44
+    /** @var  ILogger */
45
+    protected $logger;
46
+
47
+    /** @var IConfig */
48
+    protected $config;
49
+
50
+    /** @var IComment[] */
51
+    protected $commentsCache = [];
52
+
53
+    /** @var  \Closure[] */
54
+    protected $eventHandlerClosures = [];
55
+
56
+    /** @var  ICommentsEventHandler[] */
57
+    protected $eventHandlers = [];
58
+
59
+    /** @var \Closure[] */
60
+    protected $displayNameResolvers = [];
61
+
62
+    /**
63
+     * Manager constructor.
64
+     *
65
+     * @param IDBConnection $dbConn
66
+     * @param ILogger $logger
67
+     * @param IConfig $config
68
+     */
69
+    public function __construct(
70
+        IDBConnection $dbConn,
71
+        ILogger $logger,
72
+        IConfig $config
73
+    ) {
74
+        $this->dbConn = $dbConn;
75
+        $this->logger = $logger;
76
+        $this->config = $config;
77
+    }
78
+
79
+    /**
80
+     * converts data base data into PHP native, proper types as defined by
81
+     * IComment interface.
82
+     *
83
+     * @param array $data
84
+     * @return array
85
+     */
86
+    protected function normalizeDatabaseData(array $data) {
87
+        $data['id'] = strval($data['id']);
88
+        $data['parent_id'] = strval($data['parent_id']);
89
+        $data['topmost_parent_id'] = strval($data['topmost_parent_id']);
90
+        $data['creation_timestamp'] = new \DateTime($data['creation_timestamp']);
91
+        if (!is_null($data['latest_child_timestamp'])) {
92
+            $data['latest_child_timestamp'] = new \DateTime($data['latest_child_timestamp']);
93
+        }
94
+        $data['children_count'] = intval($data['children_count']);
95
+        return $data;
96
+    }
97
+
98
+    /**
99
+     * prepares a comment for an insert or update operation after making sure
100
+     * all necessary fields have a value assigned.
101
+     *
102
+     * @param IComment $comment
103
+     * @return IComment returns the same updated IComment instance as provided
104
+     *                  by parameter for convenience
105
+     * @throws \UnexpectedValueException
106
+     */
107
+    protected function prepareCommentForDatabaseWrite(IComment $comment) {
108
+        if (!$comment->getActorType()
109
+            || !$comment->getActorId()
110
+            || !$comment->getObjectType()
111
+            || !$comment->getObjectId()
112
+            || !$comment->getVerb()
113
+        ) {
114
+            throw new \UnexpectedValueException('Actor, Object and Verb information must be provided for saving');
115
+        }
116
+
117
+        if ($comment->getId() === '') {
118
+            $comment->setChildrenCount(0);
119
+            $comment->setLatestChildDateTime(new \DateTime('0000-00-00 00:00:00', new \DateTimeZone('UTC')));
120
+            $comment->setLatestChildDateTime(null);
121
+        }
122
+
123
+        if (is_null($comment->getCreationDateTime())) {
124
+            $comment->setCreationDateTime(new \DateTime());
125
+        }
126
+
127
+        if ($comment->getParentId() !== '0') {
128
+            $comment->setTopmostParentId($this->determineTopmostParentId($comment->getParentId()));
129
+        } else {
130
+            $comment->setTopmostParentId('0');
131
+        }
132
+
133
+        $this->cache($comment);
134
+
135
+        return $comment;
136
+    }
137
+
138
+    /**
139
+     * returns the topmost parent id of a given comment identified by ID
140
+     *
141
+     * @param string $id
142
+     * @return string
143
+     * @throws NotFoundException
144
+     */
145
+    protected function determineTopmostParentId($id) {
146
+        $comment = $this->get($id);
147
+        if ($comment->getParentId() === '0') {
148
+            return $comment->getId();
149
+        } else {
150
+            return $this->determineTopmostParentId($comment->getId());
151
+        }
152
+    }
153
+
154
+    /**
155
+     * updates child information of a comment
156
+     *
157
+     * @param string $id
158
+     * @param \DateTime $cDateTime the date time of the most recent child
159
+     * @throws NotFoundException
160
+     */
161
+    protected function updateChildrenInformation($id, \DateTime $cDateTime) {
162
+        $qb = $this->dbConn->getQueryBuilder();
163
+        $query = $qb->select($qb->createFunction('COUNT(`id`)'))
164
+            ->from('comments')
165
+            ->where($qb->expr()->eq('parent_id', $qb->createParameter('id')))
166
+            ->setParameter('id', $id);
167
+
168
+        $resultStatement = $query->execute();
169
+        $data = $resultStatement->fetch(\PDO::FETCH_NUM);
170
+        $resultStatement->closeCursor();
171
+        $children = intval($data[0]);
172
+
173
+        $comment = $this->get($id);
174
+        $comment->setChildrenCount($children);
175
+        $comment->setLatestChildDateTime($cDateTime);
176
+        $this->save($comment);
177
+    }
178
+
179
+    /**
180
+     * Tests whether actor or object type and id parameters are acceptable.
181
+     * Throws exception if not.
182
+     *
183
+     * @param string $role
184
+     * @param string $type
185
+     * @param string $id
186
+     * @throws \InvalidArgumentException
187
+     */
188
+    protected function checkRoleParameters($role, $type, $id) {
189
+        if (
190
+            !is_string($type) || empty($type)
191
+            || !is_string($id) || empty($id)
192
+        ) {
193
+            throw new \InvalidArgumentException($role . ' parameters must be string and not empty');
194
+        }
195
+    }
196
+
197
+    /**
198
+     * run-time caches a comment
199
+     *
200
+     * @param IComment $comment
201
+     */
202
+    protected function cache(IComment $comment) {
203
+        $id = $comment->getId();
204
+        if (empty($id)) {
205
+            return;
206
+        }
207
+        $this->commentsCache[strval($id)] = $comment;
208
+    }
209
+
210
+    /**
211
+     * removes an entry from the comments run time cache
212
+     *
213
+     * @param mixed $id the comment's id
214
+     */
215
+    protected function uncache($id) {
216
+        $id = strval($id);
217
+        if (isset($this->commentsCache[$id])) {
218
+            unset($this->commentsCache[$id]);
219
+        }
220
+    }
221
+
222
+    /**
223
+     * returns a comment instance
224
+     *
225
+     * @param string $id the ID of the comment
226
+     * @return IComment
227
+     * @throws NotFoundException
228
+     * @throws \InvalidArgumentException
229
+     * @since 9.0.0
230
+     */
231
+    public function get($id) {
232
+        if (intval($id) === 0) {
233
+            throw new \InvalidArgumentException('IDs must be translatable to a number in this implementation.');
234
+        }
235
+
236
+        if (isset($this->commentsCache[$id])) {
237
+            return $this->commentsCache[$id];
238
+        }
239
+
240
+        $qb = $this->dbConn->getQueryBuilder();
241
+        $resultStatement = $qb->select('*')
242
+            ->from('comments')
243
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
244
+            ->setParameter('id', $id, IQueryBuilder::PARAM_INT)
245
+            ->execute();
246
+
247
+        $data = $resultStatement->fetch();
248
+        $resultStatement->closeCursor();
249
+        if (!$data) {
250
+            throw new NotFoundException();
251
+        }
252
+
253
+        $comment = new Comment($this->normalizeDatabaseData($data));
254
+        $this->cache($comment);
255
+        return $comment;
256
+    }
257
+
258
+    /**
259
+     * returns the comment specified by the id and all it's child comments.
260
+     * At this point of time, we do only support one level depth.
261
+     *
262
+     * @param string $id
263
+     * @param int $limit max number of entries to return, 0 returns all
264
+     * @param int $offset the start entry
265
+     * @return array
266
+     * @since 9.0.0
267
+     *
268
+     * The return array looks like this
269
+     * [
270
+     *   'comment' => IComment, // root comment
271
+     *   'replies' =>
272
+     *   [
273
+     *     0 =>
274
+     *     [
275
+     *       'comment' => IComment,
276
+     *       'replies' => []
277
+     *     ]
278
+     *     1 =>
279
+     *     [
280
+     *       'comment' => IComment,
281
+     *       'replies'=> []
282
+     *     ],
283
+     *     …
284
+     *   ]
285
+     * ]
286
+     */
287
+    public function getTree($id, $limit = 0, $offset = 0) {
288
+        $tree = [];
289
+        $tree['comment'] = $this->get($id);
290
+        $tree['replies'] = [];
291
+
292
+        $qb = $this->dbConn->getQueryBuilder();
293
+        $query = $qb->select('*')
294
+            ->from('comments')
295
+            ->where($qb->expr()->eq('topmost_parent_id', $qb->createParameter('id')))
296
+            ->orderBy('creation_timestamp', 'DESC')
297
+            ->setParameter('id', $id);
298
+
299
+        if ($limit > 0) {
300
+            $query->setMaxResults($limit);
301
+        }
302
+        if ($offset > 0) {
303
+            $query->setFirstResult($offset);
304
+        }
305
+
306
+        $resultStatement = $query->execute();
307
+        while ($data = $resultStatement->fetch()) {
308
+            $comment = new Comment($this->normalizeDatabaseData($data));
309
+            $this->cache($comment);
310
+            $tree['replies'][] = [
311
+                'comment' => $comment,
312
+                'replies' => []
313
+            ];
314
+        }
315
+        $resultStatement->closeCursor();
316
+
317
+        return $tree;
318
+    }
319
+
320
+    /**
321
+     * returns comments for a specific object (e.g. a file).
322
+     *
323
+     * The sort order is always newest to oldest.
324
+     *
325
+     * @param string $objectType the object type, e.g. 'files'
326
+     * @param string $objectId the id of the object
327
+     * @param int $limit optional, number of maximum comments to be returned. if
328
+     * not specified, all comments are returned.
329
+     * @param int $offset optional, starting point
330
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
331
+     * that may be returned
332
+     * @return IComment[]
333
+     * @since 9.0.0
334
+     */
335
+    public function getForObject(
336
+        $objectType,
337
+        $objectId,
338
+        $limit = 0,
339
+        $offset = 0,
340
+        \DateTime $notOlderThan = null
341
+    ) {
342
+        $comments = [];
343
+
344
+        $qb = $this->dbConn->getQueryBuilder();
345
+        $query = $qb->select('*')
346
+            ->from('comments')
347
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
348
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
349
+            ->orderBy('creation_timestamp', 'DESC')
350
+            ->setParameter('type', $objectType)
351
+            ->setParameter('id', $objectId);
352
+
353
+        if ($limit > 0) {
354
+            $query->setMaxResults($limit);
355
+        }
356
+        if ($offset > 0) {
357
+            $query->setFirstResult($offset);
358
+        }
359
+        if (!is_null($notOlderThan)) {
360
+            $query
361
+                ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
362
+                ->setParameter('notOlderThan', $notOlderThan, 'datetime');
363
+        }
364
+
365
+        $resultStatement = $query->execute();
366
+        while ($data = $resultStatement->fetch()) {
367
+            $comment = new Comment($this->normalizeDatabaseData($data));
368
+            $this->cache($comment);
369
+            $comments[] = $comment;
370
+        }
371
+        $resultStatement->closeCursor();
372
+
373
+        return $comments;
374
+    }
375
+
376
+    /**
377
+     * @param $objectType string the object type, e.g. 'files'
378
+     * @param $objectId string the id of the object
379
+     * @param \DateTime $notOlderThan optional, timestamp of the oldest comments
380
+     * that may be returned
381
+     * @return Int
382
+     * @since 9.0.0
383
+     */
384
+    public function getNumberOfCommentsForObject($objectType, $objectId, \DateTime $notOlderThan = null) {
385
+        $qb = $this->dbConn->getQueryBuilder();
386
+        $query = $qb->select($qb->createFunction('COUNT(`id`)'))
387
+            ->from('comments')
388
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
389
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
390
+            ->setParameter('type', $objectType)
391
+            ->setParameter('id', $objectId);
392
+
393
+        if (!is_null($notOlderThan)) {
394
+            $query
395
+                ->andWhere($qb->expr()->gt('creation_timestamp', $qb->createParameter('notOlderThan')))
396
+                ->setParameter('notOlderThan', $notOlderThan, 'datetime');
397
+        }
398
+
399
+        $resultStatement = $query->execute();
400
+        $data = $resultStatement->fetch(\PDO::FETCH_NUM);
401
+        $resultStatement->closeCursor();
402
+        return intval($data[0]);
403
+    }
404
+
405
+    /**
406
+     * Get the number of unread comments for all files in a folder
407
+     *
408
+     * @param int $folderId
409
+     * @param IUser $user
410
+     * @return array [$fileId => $unreadCount]
411
+     */
412
+    public function getNumberOfUnreadCommentsForFolder($folderId, IUser $user) {
413
+        $qb = $this->dbConn->getQueryBuilder();
414
+        $query = $qb->select('fileid')
415
+            ->selectAlias(
416
+                $qb->createFunction('COUNT(' . $qb->getColumnName('c.id') . ')'),
417
+                'num_ids'
418
+            )
419
+            ->from('comments', 'c')
420
+            ->innerJoin('c', 'filecache', 'f', $qb->expr()->andX(
421
+                $qb->expr()->eq('c.object_type', $qb->createNamedParameter('files')),
422
+                $qb->expr()->eq('f.fileid', $qb->expr()->castColumn('c.object_id', IQueryBuilder::PARAM_INT))
423
+            ))
424
+            ->leftJoin('c', 'comments_read_markers', 'm', $qb->expr()->andX(
425
+                $qb->expr()->eq('m.object_type', $qb->createNamedParameter('files')),
426
+                $qb->expr()->eq('m.object_id', 'c.object_id'),
427
+                $qb->expr()->eq('m.user_id', $qb->createNamedParameter($user->getUID()))
428
+            ))
429
+            ->andWhere($qb->expr()->eq('f.parent', $qb->createNamedParameter($folderId)))
430
+            ->andWhere($qb->expr()->orX(
431
+                $qb->expr()->gt('c.creation_timestamp', 'marker_datetime'),
432
+                $qb->expr()->isNull('marker_datetime')
433
+            ))
434
+            ->groupBy('f.fileid');
435
+
436
+        $resultStatement = $query->execute();
437
+
438
+        $results = [];
439
+        while ($row = $resultStatement->fetch()) {
440
+            $results[$row['fileid']] = (int) $row['num_ids'];
441
+        }
442
+        $resultStatement->closeCursor();
443
+        return $results;
444
+    }
445
+
446
+    /**
447
+     * creates a new comment and returns it. At this point of time, it is not
448
+     * saved in the used data storage. Use save() after setting other fields
449
+     * of the comment (e.g. message or verb).
450
+     *
451
+     * @param string $actorType the actor type (e.g. 'users')
452
+     * @param string $actorId a user id
453
+     * @param string $objectType the object type the comment is attached to
454
+     * @param string $objectId the object id the comment is attached to
455
+     * @return IComment
456
+     * @since 9.0.0
457
+     */
458
+    public function create($actorType, $actorId, $objectType, $objectId) {
459
+        $comment = new Comment();
460
+        $comment
461
+            ->setActor($actorType, $actorId)
462
+            ->setObject($objectType, $objectId);
463
+        return $comment;
464
+    }
465
+
466
+    /**
467
+     * permanently deletes the comment specified by the ID
468
+     *
469
+     * When the comment has child comments, their parent ID will be changed to
470
+     * the parent ID of the item that is to be deleted.
471
+     *
472
+     * @param string $id
473
+     * @return bool
474
+     * @throws \InvalidArgumentException
475
+     * @since 9.0.0
476
+     */
477
+    public function delete($id) {
478
+        if (!is_string($id)) {
479
+            throw new \InvalidArgumentException('Parameter must be string');
480
+        }
481
+
482
+        try {
483
+            $comment = $this->get($id);
484
+        } catch (\Exception $e) {
485
+            // Ignore exceptions, we just don't fire a hook then
486
+            $comment = null;
487
+        }
488
+
489
+        $qb = $this->dbConn->getQueryBuilder();
490
+        $query = $qb->delete('comments')
491
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
492
+            ->setParameter('id', $id);
493
+
494
+        try {
495
+            $affectedRows = $query->execute();
496
+            $this->uncache($id);
497
+        } catch (DriverException $e) {
498
+            $this->logger->logException($e, ['app' => 'core_comments']);
499
+            return false;
500
+        }
501
+
502
+        if ($affectedRows > 0 && $comment instanceof IComment) {
503
+            $this->sendEvent(CommentsEvent::EVENT_DELETE, $comment);
504
+        }
505
+
506
+        return ($affectedRows > 0);
507
+    }
508
+
509
+    /**
510
+     * saves the comment permanently
511
+     *
512
+     * if the supplied comment has an empty ID, a new entry comment will be
513
+     * saved and the instance updated with the new ID.
514
+     *
515
+     * Otherwise, an existing comment will be updated.
516
+     *
517
+     * Throws NotFoundException when a comment that is to be updated does not
518
+     * exist anymore at this point of time.
519
+     *
520
+     * @param IComment $comment
521
+     * @return bool
522
+     * @throws NotFoundException
523
+     * @since 9.0.0
524
+     */
525
+    public function save(IComment $comment) {
526
+        if ($this->prepareCommentForDatabaseWrite($comment)->getId() === '') {
527
+            $result = $this->insert($comment);
528
+        } else {
529
+            $result = $this->update($comment);
530
+        }
531
+
532
+        if ($result && !!$comment->getParentId()) {
533
+            $this->updateChildrenInformation(
534
+                $comment->getParentId(),
535
+                $comment->getCreationDateTime()
536
+            );
537
+            $this->cache($comment);
538
+        }
539
+
540
+        return $result;
541
+    }
542
+
543
+    /**
544
+     * inserts the provided comment in the database
545
+     *
546
+     * @param IComment $comment
547
+     * @return bool
548
+     */
549
+    protected function insert(IComment &$comment) {
550
+        $qb = $this->dbConn->getQueryBuilder();
551
+        $affectedRows = $qb
552
+            ->insert('comments')
553
+            ->values([
554
+                'parent_id' => $qb->createNamedParameter($comment->getParentId()),
555
+                'topmost_parent_id' => $qb->createNamedParameter($comment->getTopmostParentId()),
556
+                'children_count' => $qb->createNamedParameter($comment->getChildrenCount()),
557
+                'actor_type' => $qb->createNamedParameter($comment->getActorType()),
558
+                'actor_id' => $qb->createNamedParameter($comment->getActorId()),
559
+                'message' => $qb->createNamedParameter($comment->getMessage()),
560
+                'verb' => $qb->createNamedParameter($comment->getVerb()),
561
+                'creation_timestamp' => $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'),
562
+                'latest_child_timestamp' => $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'),
563
+                'object_type' => $qb->createNamedParameter($comment->getObjectType()),
564
+                'object_id' => $qb->createNamedParameter($comment->getObjectId()),
565
+            ])
566
+            ->execute();
567
+
568
+        if ($affectedRows > 0) {
569
+            $comment->setId(strval($qb->getLastInsertId()));
570
+            $this->sendEvent(CommentsEvent::EVENT_ADD, $comment);
571
+        }
572
+
573
+        return $affectedRows > 0;
574
+    }
575
+
576
+    /**
577
+     * updates a Comment data row
578
+     *
579
+     * @param IComment $comment
580
+     * @return bool
581
+     * @throws NotFoundException
582
+     */
583
+    protected function update(IComment $comment) {
584
+        // for properly working preUpdate Events we need the old comments as is
585
+        // in the DB and overcome caching. Also avoid that outdated information stays.
586
+        $this->uncache($comment->getId());
587
+        $this->sendEvent(CommentsEvent::EVENT_PRE_UPDATE, $this->get($comment->getId()));
588
+        $this->uncache($comment->getId());
589
+
590
+        $qb = $this->dbConn->getQueryBuilder();
591
+        $affectedRows = $qb
592
+            ->update('comments')
593
+            ->set('parent_id', $qb->createNamedParameter($comment->getParentId()))
594
+            ->set('topmost_parent_id', $qb->createNamedParameter($comment->getTopmostParentId()))
595
+            ->set('children_count', $qb->createNamedParameter($comment->getChildrenCount()))
596
+            ->set('actor_type', $qb->createNamedParameter($comment->getActorType()))
597
+            ->set('actor_id', $qb->createNamedParameter($comment->getActorId()))
598
+            ->set('message', $qb->createNamedParameter($comment->getMessage()))
599
+            ->set('verb', $qb->createNamedParameter($comment->getVerb()))
600
+            ->set('creation_timestamp', $qb->createNamedParameter($comment->getCreationDateTime(), 'datetime'))
601
+            ->set('latest_child_timestamp', $qb->createNamedParameter($comment->getLatestChildDateTime(), 'datetime'))
602
+            ->set('object_type', $qb->createNamedParameter($comment->getObjectType()))
603
+            ->set('object_id', $qb->createNamedParameter($comment->getObjectId()))
604
+            ->where($qb->expr()->eq('id', $qb->createParameter('id')))
605
+            ->setParameter('id', $comment->getId())
606
+            ->execute();
607
+
608
+        if ($affectedRows === 0) {
609
+            throw new NotFoundException('Comment to update does ceased to exist');
610
+        }
611
+
612
+        $this->sendEvent(CommentsEvent::EVENT_UPDATE, $comment);
613
+
614
+        return $affectedRows > 0;
615
+    }
616
+
617
+    /**
618
+     * removes references to specific actor (e.g. on user delete) of a comment.
619
+     * The comment itself must not get lost/deleted.
620
+     *
621
+     * @param string $actorType the actor type (e.g. 'users')
622
+     * @param string $actorId a user id
623
+     * @return boolean
624
+     * @since 9.0.0
625
+     */
626
+    public function deleteReferencesOfActor($actorType, $actorId) {
627
+        $this->checkRoleParameters('Actor', $actorType, $actorId);
628
+
629
+        $qb = $this->dbConn->getQueryBuilder();
630
+        $affectedRows = $qb
631
+            ->update('comments')
632
+            ->set('actor_type', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
633
+            ->set('actor_id', $qb->createNamedParameter(ICommentsManager::DELETED_USER))
634
+            ->where($qb->expr()->eq('actor_type', $qb->createParameter('type')))
635
+            ->andWhere($qb->expr()->eq('actor_id', $qb->createParameter('id')))
636
+            ->setParameter('type', $actorType)
637
+            ->setParameter('id', $actorId)
638
+            ->execute();
639
+
640
+        $this->commentsCache = [];
641
+
642
+        return is_int($affectedRows);
643
+    }
644
+
645
+    /**
646
+     * deletes all comments made of a specific object (e.g. on file delete)
647
+     *
648
+     * @param string $objectType the object type (e.g. 'files')
649
+     * @param string $objectId e.g. the file id
650
+     * @return boolean
651
+     * @since 9.0.0
652
+     */
653
+    public function deleteCommentsAtObject($objectType, $objectId) {
654
+        $this->checkRoleParameters('Object', $objectType, $objectId);
655
+
656
+        $qb = $this->dbConn->getQueryBuilder();
657
+        $affectedRows = $qb
658
+            ->delete('comments')
659
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('type')))
660
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('id')))
661
+            ->setParameter('type', $objectType)
662
+            ->setParameter('id', $objectId)
663
+            ->execute();
664
+
665
+        $this->commentsCache = [];
666
+
667
+        return is_int($affectedRows);
668
+    }
669
+
670
+    /**
671
+     * deletes the read markers for the specified user
672
+     *
673
+     * @param \OCP\IUser $user
674
+     * @return bool
675
+     * @since 9.0.0
676
+     */
677
+    public function deleteReadMarksFromUser(IUser $user) {
678
+        $qb = $this->dbConn->getQueryBuilder();
679
+        $query = $qb->delete('comments_read_markers')
680
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
681
+            ->setParameter('user_id', $user->getUID());
682
+
683
+        try {
684
+            $affectedRows = $query->execute();
685
+        } catch (DriverException $e) {
686
+            $this->logger->logException($e, ['app' => 'core_comments']);
687
+            return false;
688
+        }
689
+        return ($affectedRows > 0);
690
+    }
691
+
692
+    /**
693
+     * sets the read marker for a given file to the specified date for the
694
+     * provided user
695
+     *
696
+     * @param string $objectType
697
+     * @param string $objectId
698
+     * @param \DateTime $dateTime
699
+     * @param IUser $user
700
+     * @since 9.0.0
701
+     * @suppress SqlInjectionChecker
702
+     */
703
+    public function setReadMark($objectType, $objectId, \DateTime $dateTime, IUser $user) {
704
+        $this->checkRoleParameters('Object', $objectType, $objectId);
705
+
706
+        $qb = $this->dbConn->getQueryBuilder();
707
+        $values = [
708
+            'user_id' => $qb->createNamedParameter($user->getUID()),
709
+            'marker_datetime' => $qb->createNamedParameter($dateTime, 'datetime'),
710
+            'object_type' => $qb->createNamedParameter($objectType),
711
+            'object_id' => $qb->createNamedParameter($objectId),
712
+        ];
713
+
714
+        // Strategy: try to update, if this does not return affected rows, do an insert.
715
+        $affectedRows = $qb
716
+            ->update('comments_read_markers')
717
+            ->set('user_id', $values['user_id'])
718
+            ->set('marker_datetime', $values['marker_datetime'])
719
+            ->set('object_type', $values['object_type'])
720
+            ->set('object_id', $values['object_id'])
721
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
722
+            ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
723
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
724
+            ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
725
+            ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
726
+            ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
727
+            ->execute();
728
+
729
+        if ($affectedRows > 0) {
730
+            return;
731
+        }
732
+
733
+        $qb->insert('comments_read_markers')
734
+            ->values($values)
735
+            ->execute();
736
+    }
737
+
738
+    /**
739
+     * returns the read marker for a given file to the specified date for the
740
+     * provided user. It returns null, when the marker is not present, i.e.
741
+     * no comments were marked as read.
742
+     *
743
+     * @param string $objectType
744
+     * @param string $objectId
745
+     * @param IUser $user
746
+     * @return \DateTime|null
747
+     * @since 9.0.0
748
+     */
749
+    public function getReadMark($objectType, $objectId, IUser $user) {
750
+        $qb = $this->dbConn->getQueryBuilder();
751
+        $resultStatement = $qb->select('marker_datetime')
752
+            ->from('comments_read_markers')
753
+            ->where($qb->expr()->eq('user_id', $qb->createParameter('user_id')))
754
+            ->andWhere($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
755
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
756
+            ->setParameter('user_id', $user->getUID(), IQueryBuilder::PARAM_STR)
757
+            ->setParameter('object_type', $objectType, IQueryBuilder::PARAM_STR)
758
+            ->setParameter('object_id', $objectId, IQueryBuilder::PARAM_STR)
759
+            ->execute();
760
+
761
+        $data = $resultStatement->fetch();
762
+        $resultStatement->closeCursor();
763
+        if (!$data || is_null($data['marker_datetime'])) {
764
+            return null;
765
+        }
766
+
767
+        return new \DateTime($data['marker_datetime']);
768
+    }
769
+
770
+    /**
771
+     * deletes the read markers on the specified object
772
+     *
773
+     * @param string $objectType
774
+     * @param string $objectId
775
+     * @return bool
776
+     * @since 9.0.0
777
+     */
778
+    public function deleteReadMarksOnObject($objectType, $objectId) {
779
+        $this->checkRoleParameters('Object', $objectType, $objectId);
780
+
781
+        $qb = $this->dbConn->getQueryBuilder();
782
+        $query = $qb->delete('comments_read_markers')
783
+            ->where($qb->expr()->eq('object_type', $qb->createParameter('object_type')))
784
+            ->andWhere($qb->expr()->eq('object_id', $qb->createParameter('object_id')))
785
+            ->setParameter('object_type', $objectType)
786
+            ->setParameter('object_id', $objectId);
787
+
788
+        try {
789
+            $affectedRows = $query->execute();
790
+        } catch (DriverException $e) {
791
+            $this->logger->logException($e, ['app' => 'core_comments']);
792
+            return false;
793
+        }
794
+        return ($affectedRows > 0);
795
+    }
796
+
797
+    /**
798
+     * registers an Entity to the manager, so event notifications can be send
799
+     * to consumers of the comments infrastructure
800
+     *
801
+     * @param \Closure $closure
802
+     */
803
+    public function registerEventHandler(\Closure $closure) {
804
+        $this->eventHandlerClosures[] = $closure;
805
+        $this->eventHandlers = [];
806
+    }
807
+
808
+    /**
809
+     * registers a method that resolves an ID to a display name for a given type
810
+     *
811
+     * @param string $type
812
+     * @param \Closure $closure
813
+     * @throws \OutOfBoundsException
814
+     * @since 11.0.0
815
+     *
816
+     * Only one resolver shall be registered per type. Otherwise a
817
+     * \OutOfBoundsException has to thrown.
818
+     */
819
+    public function registerDisplayNameResolver($type, \Closure $closure) {
820
+        if (!is_string($type)) {
821
+            throw new \InvalidArgumentException('String expected.');
822
+        }
823
+        if (isset($this->displayNameResolvers[$type])) {
824
+            throw new \OutOfBoundsException('Displayname resolver for this type already registered');
825
+        }
826
+        $this->displayNameResolvers[$type] = $closure;
827
+    }
828
+
829
+    /**
830
+     * resolves a given ID of a given Type to a display name.
831
+     *
832
+     * @param string $type
833
+     * @param string $id
834
+     * @return string
835
+     * @throws \OutOfBoundsException
836
+     * @since 11.0.0
837
+     *
838
+     * If a provided type was not registered, an \OutOfBoundsException shall
839
+     * be thrown. It is upon the resolver discretion what to return of the
840
+     * provided ID is unknown. It must be ensured that a string is returned.
841
+     */
842
+    public function resolveDisplayName($type, $id) {
843
+        if (!is_string($type)) {
844
+            throw new \InvalidArgumentException('String expected.');
845
+        }
846
+        if (!isset($this->displayNameResolvers[$type])) {
847
+            throw new \OutOfBoundsException('No Displayname resolver for this type registered');
848
+        }
849
+        return (string)$this->displayNameResolvers[$type]($id);
850
+    }
851
+
852
+    /**
853
+     * returns valid, registered entities
854
+     *
855
+     * @return \OCP\Comments\ICommentsEventHandler[]
856
+     */
857
+    private function getEventHandlers() {
858
+        if (!empty($this->eventHandlers)) {
859
+            return $this->eventHandlers;
860
+        }
861
+
862
+        $this->eventHandlers = [];
863
+        foreach ($this->eventHandlerClosures as $name => $closure) {
864
+            $entity = $closure();
865
+            if (!($entity instanceof ICommentsEventHandler)) {
866
+                throw new \InvalidArgumentException('The given entity does not implement the ICommentsEntity interface');
867
+            }
868
+            $this->eventHandlers[$name] = $entity;
869
+        }
870
+
871
+        return $this->eventHandlers;
872
+    }
873
+
874
+    /**
875
+     * sends notifications to the registered entities
876
+     *
877
+     * @param $eventType
878
+     * @param IComment $comment
879
+     */
880
+    private function sendEvent($eventType, IComment $comment) {
881
+        $entities = $this->getEventHandlers();
882
+        $event = new CommentsEvent($eventType, $comment);
883
+        foreach ($entities as $entity) {
884
+            $entity->handle($event);
885
+        }
886
+    }
887 887
 }
Please login to merge, or discard this patch.
lib/private/Server.php 1 patch
Indentation   +1659 added lines, -1659 removed lines patch added patch discarded remove patch
@@ -127,1668 +127,1668 @@
 block discarded – undo
127 127
  * TODO: hookup all manager classes
128 128
  */
129 129
 class Server extends ServerContainer implements IServerContainer {
130
-	/** @var string */
131
-	private $webRoot;
132
-
133
-	/**
134
-	 * @param string $webRoot
135
-	 * @param \OC\Config $config
136
-	 */
137
-	public function __construct($webRoot, \OC\Config $config) {
138
-		parent::__construct();
139
-		$this->webRoot = $webRoot;
140
-
141
-		$this->registerService(\OCP\IServerContainer::class, function(IServerContainer $c) {
142
-			return $c;
143
-		});
144
-
145
-		$this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class);
146
-		$this->registerAlias('ContactsManager', \OCP\Contacts\IManager::class);
147
-
148
-		$this->registerAlias(IActionFactory::class, ActionFactory::class);
149
-
150
-
151
-
152
-		$this->registerService(\OCP\IPreview::class, function (Server $c) {
153
-			return new PreviewManager(
154
-				$c->getConfig(),
155
-				$c->getRootFolder(),
156
-				$c->getAppDataDir('preview'),
157
-				$c->getEventDispatcher(),
158
-				$c->getSession()->get('user_id')
159
-			);
160
-		});
161
-		$this->registerAlias('PreviewManager', \OCP\IPreview::class);
162
-
163
-		$this->registerService(\OC\Preview\Watcher::class, function (Server $c) {
164
-			return new \OC\Preview\Watcher(
165
-				$c->getAppDataDir('preview')
166
-			);
167
-		});
168
-
169
-		$this->registerService('EncryptionManager', function (Server $c) {
170
-			$view = new View();
171
-			$util = new Encryption\Util(
172
-				$view,
173
-				$c->getUserManager(),
174
-				$c->getGroupManager(),
175
-				$c->getConfig()
176
-			);
177
-			return new Encryption\Manager(
178
-				$c->getConfig(),
179
-				$c->getLogger(),
180
-				$c->getL10N('core'),
181
-				new View(),
182
-				$util,
183
-				new ArrayCache()
184
-			);
185
-		});
186
-
187
-		$this->registerService('EncryptionFileHelper', function (Server $c) {
188
-			$util = new Encryption\Util(
189
-				new View(),
190
-				$c->getUserManager(),
191
-				$c->getGroupManager(),
192
-				$c->getConfig()
193
-			);
194
-			return new Encryption\File(
195
-				$util,
196
-				$c->getRootFolder(),
197
-				$c->getShareManager()
198
-			);
199
-		});
200
-
201
-		$this->registerService('EncryptionKeyStorage', function (Server $c) {
202
-			$view = new View();
203
-			$util = new Encryption\Util(
204
-				$view,
205
-				$c->getUserManager(),
206
-				$c->getGroupManager(),
207
-				$c->getConfig()
208
-			);
209
-
210
-			return new Encryption\Keys\Storage($view, $util);
211
-		});
212
-		$this->registerService('TagMapper', function (Server $c) {
213
-			return new TagMapper($c->getDatabaseConnection());
214
-		});
215
-
216
-		$this->registerService(\OCP\ITagManager::class, function (Server $c) {
217
-			$tagMapper = $c->query('TagMapper');
218
-			return new TagManager($tagMapper, $c->getUserSession());
219
-		});
220
-		$this->registerAlias('TagManager', \OCP\ITagManager::class);
221
-
222
-		$this->registerService('SystemTagManagerFactory', function (Server $c) {
223
-			$config = $c->getConfig();
224
-			$factoryClass = $config->getSystemValue('systemtags.managerFactory', '\OC\SystemTag\ManagerFactory');
225
-			/** @var \OC\SystemTag\ManagerFactory $factory */
226
-			$factory = new $factoryClass($this);
227
-			return $factory;
228
-		});
229
-		$this->registerService(\OCP\SystemTag\ISystemTagManager::class, function (Server $c) {
230
-			return $c->query('SystemTagManagerFactory')->getManager();
231
-		});
232
-		$this->registerAlias('SystemTagManager', \OCP\SystemTag\ISystemTagManager::class);
233
-
234
-		$this->registerService(\OCP\SystemTag\ISystemTagObjectMapper::class, function (Server $c) {
235
-			return $c->query('SystemTagManagerFactory')->getObjectMapper();
236
-		});
237
-		$this->registerService('RootFolder', function (Server $c) {
238
-			$manager = \OC\Files\Filesystem::getMountManager(null);
239
-			$view = new View();
240
-			$root = new Root(
241
-				$manager,
242
-				$view,
243
-				null,
244
-				$c->getUserMountCache(),
245
-				$this->getLogger(),
246
-				$this->getUserManager()
247
-			);
248
-			$connector = new HookConnector($root, $view);
249
-			$connector->viewToNode();
250
-
251
-			$previewConnector = new \OC\Preview\WatcherConnector($root, $c->getSystemConfig());
252
-			$previewConnector->connectWatcher();
253
-
254
-			return $root;
255
-		});
256
-		$this->registerAlias('SystemTagObjectMapper', \OCP\SystemTag\ISystemTagObjectMapper::class);
257
-
258
-		$this->registerService(\OCP\Files\IRootFolder::class, function(Server $c) {
259
-			return new LazyRoot(function() use ($c) {
260
-				return $c->query('RootFolder');
261
-			});
262
-		});
263
-		$this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class);
264
-
265
-		$this->registerService(\OCP\IUserManager::class, function (Server $c) {
266
-			$config = $c->getConfig();
267
-			return new \OC\User\Manager($config);
268
-		});
269
-		$this->registerAlias('UserManager', \OCP\IUserManager::class);
270
-
271
-		$this->registerService(\OCP\IGroupManager::class, function (Server $c) {
272
-			$groupManager = new \OC\Group\Manager($this->getUserManager(), $this->getLogger());
273
-			$groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
274
-				\OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid));
275
-			});
276
-			$groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $gid) {
277
-				\OC_Hook::emit('OC_User', 'post_createGroup', array('gid' => $gid->getGID()));
278
-			});
279
-			$groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) {
280
-				\OC_Hook::emit('OC_Group', 'pre_deleteGroup', array('run' => true, 'gid' => $group->getGID()));
281
-			});
282
-			$groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) {
283
-				\OC_Hook::emit('OC_User', 'post_deleteGroup', array('gid' => $group->getGID()));
284
-			});
285
-			$groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
286
-				\OC_Hook::emit('OC_Group', 'pre_addToGroup', array('run' => true, 'uid' => $user->getUID(), 'gid' => $group->getGID()));
287
-			});
288
-			$groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
289
-				\OC_Hook::emit('OC_Group', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
290
-				//Minimal fix to keep it backward compatible TODO: clean up all the GroupManager hooks
291
-				\OC_Hook::emit('OC_User', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
292
-			});
293
-			return $groupManager;
294
-		});
295
-		$this->registerAlias('GroupManager', \OCP\IGroupManager::class);
296
-
297
-		$this->registerService(Store::class, function(Server $c) {
298
-			$session = $c->getSession();
299
-			if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
300
-				$tokenProvider = $c->query('OC\Authentication\Token\IProvider');
301
-			} else {
302
-				$tokenProvider = null;
303
-			}
304
-			$logger = $c->getLogger();
305
-			return new Store($session, $logger, $tokenProvider);
306
-		});
307
-		$this->registerAlias(IStore::class, Store::class);
308
-		$this->registerService('OC\Authentication\Token\DefaultTokenMapper', function (Server $c) {
309
-			$dbConnection = $c->getDatabaseConnection();
310
-			return new Authentication\Token\DefaultTokenMapper($dbConnection);
311
-		});
312
-		$this->registerService('OC\Authentication\Token\DefaultTokenProvider', function (Server $c) {
313
-			$mapper = $c->query('OC\Authentication\Token\DefaultTokenMapper');
314
-			$crypto = $c->getCrypto();
315
-			$config = $c->getConfig();
316
-			$logger = $c->getLogger();
317
-			$timeFactory = new TimeFactory();
318
-			return new \OC\Authentication\Token\DefaultTokenProvider($mapper, $crypto, $config, $logger, $timeFactory);
319
-		});
320
-		$this->registerAlias('OC\Authentication\Token\IProvider', 'OC\Authentication\Token\DefaultTokenProvider');
321
-
322
-		$this->registerService(\OCP\IUserSession::class, function (Server $c) {
323
-			$manager = $c->getUserManager();
324
-			$session = new \OC\Session\Memory('');
325
-			$timeFactory = new TimeFactory();
326
-			// Token providers might require a working database. This code
327
-			// might however be called when ownCloud is not yet setup.
328
-			if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
329
-				$defaultTokenProvider = $c->query('OC\Authentication\Token\IProvider');
330
-			} else {
331
-				$defaultTokenProvider = null;
332
-			}
333
-
334
-			$userSession = new \OC\User\Session($manager, $session, $timeFactory, $defaultTokenProvider, $c->getConfig(), $c->getSecureRandom(), $c->getLockdownManager());
335
-			$userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
336
-				\OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
337
-			});
338
-			$userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
339
-				/** @var $user \OC\User\User */
340
-				\OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
341
-			});
342
-			$userSession->listen('\OC\User', 'preDelete', function ($user) {
343
-				/** @var $user \OC\User\User */
344
-				\OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
345
-			});
346
-			$userSession->listen('\OC\User', 'postDelete', function ($user) {
347
-				/** @var $user \OC\User\User */
348
-				\OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
349
-			});
350
-			$userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
351
-				/** @var $user \OC\User\User */
352
-				\OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
353
-			});
354
-			$userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
355
-				/** @var $user \OC\User\User */
356
-				\OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
357
-			});
358
-			$userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
359
-				\OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
360
-			});
361
-			$userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
362
-				/** @var $user \OC\User\User */
363
-				\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
364
-			});
365
-			$userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
366
-				/** @var $user \OC\User\User */
367
-				\OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
368
-			});
369
-			$userSession->listen('\OC\User', 'logout', function () {
370
-				\OC_Hook::emit('OC_User', 'logout', array());
371
-			});
372
-			$userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
373
-				/** @var $user \OC\User\User */
374
-				\OC_Hook::emit('OC_User', 'changeUser', array('run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue));
375
-			});
376
-			return $userSession;
377
-		});
378
-		$this->registerAlias('UserSession', \OCP\IUserSession::class);
379
-
380
-		$this->registerService(\OC\Authentication\TwoFactorAuth\Manager::class, function (Server $c) {
381
-			return new \OC\Authentication\TwoFactorAuth\Manager($c->getAppManager(), $c->getSession(), $c->getConfig(), $c->getActivityManager(), $c->getLogger());
382
-		});
383
-
384
-		$this->registerAlias(\OCP\INavigationManager::class, \OC\NavigationManager::class);
385
-		$this->registerAlias('NavigationManager', \OCP\INavigationManager::class);
386
-
387
-		$this->registerService(\OC\AllConfig::class, function (Server $c) {
388
-			return new \OC\AllConfig(
389
-				$c->getSystemConfig()
390
-			);
391
-		});
392
-		$this->registerAlias('AllConfig', \OC\AllConfig::class);
393
-		$this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class);
394
-
395
-		$this->registerService('SystemConfig', function ($c) use ($config) {
396
-			return new \OC\SystemConfig($config);
397
-		});
398
-
399
-		$this->registerService(\OC\AppConfig::class, function (Server $c) {
400
-			return new \OC\AppConfig($c->getDatabaseConnection());
401
-		});
402
-		$this->registerAlias('AppConfig', \OC\AppConfig::class);
403
-		$this->registerAlias(\OCP\IAppConfig::class, \OC\AppConfig::class);
404
-
405
-		$this->registerService(\OCP\L10N\IFactory::class, function (Server $c) {
406
-			return new \OC\L10N\Factory(
407
-				$c->getConfig(),
408
-				$c->getRequest(),
409
-				$c->getUserSession(),
410
-				\OC::$SERVERROOT
411
-			);
412
-		});
413
-		$this->registerAlias('L10NFactory', \OCP\L10N\IFactory::class);
414
-
415
-		$this->registerService(\OCP\IURLGenerator::class, function (Server $c) {
416
-			$config = $c->getConfig();
417
-			$cacheFactory = $c->getMemCacheFactory();
418
-			$request = $c->getRequest();
419
-			return new \OC\URLGenerator(
420
-				$config,
421
-				$cacheFactory,
422
-				$request
423
-			);
424
-		});
425
-		$this->registerAlias('URLGenerator', \OCP\IURLGenerator::class);
426
-
427
-		$this->registerService('AppHelper', function ($c) {
428
-			return new \OC\AppHelper();
429
-		});
430
-		$this->registerAlias('AppFetcher', AppFetcher::class);
431
-		$this->registerAlias('CategoryFetcher', CategoryFetcher::class);
432
-
433
-		$this->registerService(\OCP\ICache::class, function ($c) {
434
-			return new Cache\File();
435
-		});
436
-		$this->registerAlias('UserCache', \OCP\ICache::class);
437
-
438
-		$this->registerService(Factory::class, function (Server $c) {
439
-
440
-			$arrayCacheFactory = new \OC\Memcache\Factory('', $c->getLogger(),
441
-				'\\OC\\Memcache\\ArrayCache',
442
-				'\\OC\\Memcache\\ArrayCache',
443
-				'\\OC\\Memcache\\ArrayCache'
444
-			);
445
-			$config = $c->getConfig();
446
-			$request = $c->getRequest();
447
-			$urlGenerator = new URLGenerator($config, $arrayCacheFactory, $request);
448
-
449
-			if ($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
450
-				$v = \OC_App::getAppVersions();
451
-				$v['core'] = implode(',', \OC_Util::getVersion());
452
-				$version = implode(',', $v);
453
-				$instanceId = \OC_Util::getInstanceId();
454
-				$path = \OC::$SERVERROOT;
455
-				$prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . $urlGenerator->getBaseUrl());
456
-				return new \OC\Memcache\Factory($prefix, $c->getLogger(),
457
-					$config->getSystemValue('memcache.local', null),
458
-					$config->getSystemValue('memcache.distributed', null),
459
-					$config->getSystemValue('memcache.locking', null)
460
-				);
461
-			}
462
-			return $arrayCacheFactory;
463
-
464
-		});
465
-		$this->registerAlias('MemCacheFactory', Factory::class);
466
-		$this->registerAlias(ICacheFactory::class, Factory::class);
467
-
468
-		$this->registerService('RedisFactory', function (Server $c) {
469
-			$systemConfig = $c->getSystemConfig();
470
-			return new RedisFactory($systemConfig);
471
-		});
472
-
473
-		$this->registerService(\OCP\Activity\IManager::class, function (Server $c) {
474
-			return new \OC\Activity\Manager(
475
-				$c->getRequest(),
476
-				$c->getUserSession(),
477
-				$c->getConfig(),
478
-				$c->query(IValidator::class)
479
-			);
480
-		});
481
-		$this->registerAlias('ActivityManager', \OCP\Activity\IManager::class);
482
-
483
-		$this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) {
484
-			return new \OC\Activity\EventMerger(
485
-				$c->getL10N('lib')
486
-			);
487
-		});
488
-		$this->registerAlias(IValidator::class, Validator::class);
489
-
490
-		$this->registerService(\OCP\IAvatarManager::class, function (Server $c) {
491
-			return new AvatarManager(
492
-				$c->getUserManager(),
493
-				$c->getAppDataDir('avatar'),
494
-				$c->getL10N('lib'),
495
-				$c->getLogger(),
496
-				$c->getConfig()
497
-			);
498
-		});
499
-		$this->registerAlias('AvatarManager', \OCP\IAvatarManager::class);
500
-
501
-		$this->registerService(\OCP\ILogger::class, function (Server $c) {
502
-			$logType = $c->query('AllConfig')->getSystemValue('log_type', 'file');
503
-			$logger = Log::getLogClass($logType);
504
-			call_user_func(array($logger, 'init'));
505
-
506
-			return new Log($logger);
507
-		});
508
-		$this->registerAlias('Logger', \OCP\ILogger::class);
509
-
510
-		$this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) {
511
-			$config = $c->getConfig();
512
-			return new \OC\BackgroundJob\JobList(
513
-				$c->getDatabaseConnection(),
514
-				$config,
515
-				new TimeFactory()
516
-			);
517
-		});
518
-		$this->registerAlias('JobList', \OCP\BackgroundJob\IJobList::class);
519
-
520
-		$this->registerService(\OCP\Route\IRouter::class, function (Server $c) {
521
-			$cacheFactory = $c->getMemCacheFactory();
522
-			$logger = $c->getLogger();
523
-			if ($cacheFactory->isAvailable()) {
524
-				$router = new \OC\Route\CachingRouter($cacheFactory->create('route'), $logger);
525
-			} else {
526
-				$router = new \OC\Route\Router($logger);
527
-			}
528
-			return $router;
529
-		});
530
-		$this->registerAlias('Router', \OCP\Route\IRouter::class);
531
-
532
-		$this->registerService(\OCP\ISearch::class, function ($c) {
533
-			return new Search();
534
-		});
535
-		$this->registerAlias('Search', \OCP\ISearch::class);
536
-
537
-		$this->registerService(\OC\Security\RateLimiting\Limiter::class, function($c) {
538
-			return new \OC\Security\RateLimiting\Limiter(
539
-				$this->getUserSession(),
540
-				$this->getRequest(),
541
-				new \OC\AppFramework\Utility\TimeFactory(),
542
-				$c->query(\OC\Security\RateLimiting\Backend\IBackend::class)
543
-			);
544
-		});
545
-		$this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function($c) {
546
-			return new \OC\Security\RateLimiting\Backend\MemoryCache(
547
-				$this->getMemCacheFactory(),
548
-				new \OC\AppFramework\Utility\TimeFactory()
549
-			);
550
-		});
551
-
552
-		$this->registerService(\OCP\Security\ISecureRandom::class, function ($c) {
553
-			return new SecureRandom();
554
-		});
555
-		$this->registerAlias('SecureRandom', \OCP\Security\ISecureRandom::class);
556
-
557
-		$this->registerService(\OCP\Security\ICrypto::class, function (Server $c) {
558
-			return new Crypto($c->getConfig(), $c->getSecureRandom());
559
-		});
560
-		$this->registerAlias('Crypto', \OCP\Security\ICrypto::class);
561
-
562
-		$this->registerService(\OCP\Security\IHasher::class, function (Server $c) {
563
-			return new Hasher($c->getConfig());
564
-		});
565
-		$this->registerAlias('Hasher', \OCP\Security\IHasher::class);
566
-
567
-		$this->registerService(\OCP\Security\ICredentialsManager::class, function (Server $c) {
568
-			return new CredentialsManager($c->getCrypto(), $c->getDatabaseConnection());
569
-		});
570
-		$this->registerAlias('CredentialsManager', \OCP\Security\ICredentialsManager::class);
571
-
572
-		$this->registerService(IDBConnection::class, function (Server $c) {
573
-			$systemConfig = $c->getSystemConfig();
574
-			$factory = new \OC\DB\ConnectionFactory($systemConfig);
575
-			$type = $systemConfig->getValue('dbtype', 'sqlite');
576
-			if (!$factory->isValidType($type)) {
577
-				throw new \OC\DatabaseException('Invalid database type');
578
-			}
579
-			$connectionParams = $factory->createConnectionParams();
580
-			$connection = $factory->getConnection($type, $connectionParams);
581
-			$connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
582
-			return $connection;
583
-		});
584
-		$this->registerAlias('DatabaseConnection', IDBConnection::class);
585
-
586
-		$this->registerService('HTTPHelper', function (Server $c) {
587
-			$config = $c->getConfig();
588
-			return new HTTPHelper(
589
-				$config,
590
-				$c->getHTTPClientService()
591
-			);
592
-		});
593
-
594
-		$this->registerService(\OCP\Http\Client\IClientService::class, function (Server $c) {
595
-			$user = \OC_User::getUser();
596
-			$uid = $user ? $user : null;
597
-			return new ClientService(
598
-				$c->getConfig(),
599
-				new \OC\Security\CertificateManager(
600
-					$uid,
601
-					new View(),
602
-					$c->getConfig(),
603
-					$c->getLogger(),
604
-					$c->getSecureRandom()
605
-				)
606
-			);
607
-		});
608
-		$this->registerAlias('HttpClientService', \OCP\Http\Client\IClientService::class);
609
-		$this->registerService(\OCP\Diagnostics\IEventLogger::class, function (Server $c) {
610
-			$eventLogger = new EventLogger();
611
-			if ($c->getSystemConfig()->getValue('debug', false)) {
612
-				// In debug mode, module is being activated by default
613
-				$eventLogger->activate();
614
-			}
615
-			return $eventLogger;
616
-		});
617
-		$this->registerAlias('EventLogger', \OCP\Diagnostics\IEventLogger::class);
618
-
619
-		$this->registerService(\OCP\Diagnostics\IQueryLogger::class, function (Server $c) {
620
-			$queryLogger = new QueryLogger();
621
-			if ($c->getSystemConfig()->getValue('debug', false)) {
622
-				// In debug mode, module is being activated by default
623
-				$queryLogger->activate();
624
-			}
625
-			return $queryLogger;
626
-		});
627
-		$this->registerAlias('QueryLogger', \OCP\Diagnostics\IQueryLogger::class);
628
-
629
-		$this->registerService(TempManager::class, function (Server $c) {
630
-			return new TempManager(
631
-				$c->getLogger(),
632
-				$c->getConfig()
633
-			);
634
-		});
635
-		$this->registerAlias('TempManager', TempManager::class);
636
-		$this->registerAlias(ITempManager::class, TempManager::class);
637
-
638
-		$this->registerService(AppManager::class, function (Server $c) {
639
-			return new \OC\App\AppManager(
640
-				$c->getUserSession(),
641
-				$c->getAppConfig(),
642
-				$c->getGroupManager(),
643
-				$c->getMemCacheFactory(),
644
-				$c->getEventDispatcher()
645
-			);
646
-		});
647
-		$this->registerAlias('AppManager', AppManager::class);
648
-		$this->registerAlias(IAppManager::class, AppManager::class);
649
-
650
-		$this->registerService(\OCP\IDateTimeZone::class, function (Server $c) {
651
-			return new DateTimeZone(
652
-				$c->getConfig(),
653
-				$c->getSession()
654
-			);
655
-		});
656
-		$this->registerAlias('DateTimeZone', \OCP\IDateTimeZone::class);
657
-
658
-		$this->registerService(\OCP\IDateTimeFormatter::class, function (Server $c) {
659
-			$language = $c->getConfig()->getUserValue($c->getSession()->get('user_id'), 'core', 'lang', null);
660
-
661
-			return new DateTimeFormatter(
662
-				$c->getDateTimeZone()->getTimeZone(),
663
-				$c->getL10N('lib', $language)
664
-			);
665
-		});
666
-		$this->registerAlias('DateTimeFormatter', \OCP\IDateTimeFormatter::class);
667
-
668
-		$this->registerService(\OCP\Files\Config\IUserMountCache::class, function (Server $c) {
669
-			$mountCache = new UserMountCache($c->getDatabaseConnection(), $c->getUserManager(), $c->getLogger());
670
-			$listener = new UserMountCacheListener($mountCache);
671
-			$listener->listen($c->getUserManager());
672
-			return $mountCache;
673
-		});
674
-		$this->registerAlias('UserMountCache', \OCP\Files\Config\IUserMountCache::class);
675
-
676
-		$this->registerService(\OCP\Files\Config\IMountProviderCollection::class, function (Server $c) {
677
-			$loader = \OC\Files\Filesystem::getLoader();
678
-			$mountCache = $c->query('UserMountCache');
679
-			$manager =  new \OC\Files\Config\MountProviderCollection($loader, $mountCache);
680
-
681
-			// builtin providers
682
-
683
-			$config = $c->getConfig();
684
-			$manager->registerProvider(new CacheMountProvider($config));
685
-			$manager->registerHomeProvider(new LocalHomeMountProvider());
686
-			$manager->registerHomeProvider(new ObjectHomeMountProvider($config));
687
-
688
-			return $manager;
689
-		});
690
-		$this->registerAlias('MountConfigManager', \OCP\Files\Config\IMountProviderCollection::class);
691
-
692
-		$this->registerService('IniWrapper', function ($c) {
693
-			return new IniGetWrapper();
694
-		});
695
-		$this->registerService('AsyncCommandBus', function (Server $c) {
696
-			$jobList = $c->getJobList();
697
-			return new AsyncBus($jobList);
698
-		});
699
-		$this->registerService('TrustedDomainHelper', function ($c) {
700
-			return new TrustedDomainHelper($this->getConfig());
701
-		});
702
-		$this->registerService('Throttler', function(Server $c) {
703
-			return new Throttler(
704
-				$c->getDatabaseConnection(),
705
-				new TimeFactory(),
706
-				$c->getLogger(),
707
-				$c->getConfig()
708
-			);
709
-		});
710
-		$this->registerService('IntegrityCodeChecker', function (Server $c) {
711
-			// IConfig and IAppManager requires a working database. This code
712
-			// might however be called when ownCloud is not yet setup.
713
-			if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
714
-				$config = $c->getConfig();
715
-				$appManager = $c->getAppManager();
716
-			} else {
717
-				$config = null;
718
-				$appManager = null;
719
-			}
720
-
721
-			return new Checker(
722
-					new EnvironmentHelper(),
723
-					new FileAccessHelper(),
724
-					new AppLocator(),
725
-					$config,
726
-					$c->getMemCacheFactory(),
727
-					$appManager,
728
-					$c->getTempManager()
729
-			);
730
-		});
731
-		$this->registerService(\OCP\IRequest::class, function ($c) {
732
-			if (isset($this['urlParams'])) {
733
-				$urlParams = $this['urlParams'];
734
-			} else {
735
-				$urlParams = [];
736
-			}
737
-
738
-			if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
739
-				&& in_array('fakeinput', stream_get_wrappers())
740
-			) {
741
-				$stream = 'fakeinput://data';
742
-			} else {
743
-				$stream = 'php://input';
744
-			}
745
-
746
-			return new Request(
747
-				[
748
-					'get' => $_GET,
749
-					'post' => $_POST,
750
-					'files' => $_FILES,
751
-					'server' => $_SERVER,
752
-					'env' => $_ENV,
753
-					'cookies' => $_COOKIE,
754
-					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
755
-						? $_SERVER['REQUEST_METHOD']
756
-						: null,
757
-					'urlParams' => $urlParams,
758
-				],
759
-				$this->getSecureRandom(),
760
-				$this->getConfig(),
761
-				$this->getCsrfTokenManager(),
762
-				$stream
763
-			);
764
-		});
765
-		$this->registerAlias('Request', \OCP\IRequest::class);
766
-
767
-		$this->registerService(\OCP\Mail\IMailer::class, function (Server $c) {
768
-			return new Mailer(
769
-				$c->getConfig(),
770
-				$c->getLogger(),
771
-				$c->query(Defaults::class),
772
-				$c->getURLGenerator(),
773
-				$c->getL10N('lib')
774
-			);
775
-		});
776
-		$this->registerAlias('Mailer', \OCP\Mail\IMailer::class);
777
-
778
-		$this->registerService('LDAPProvider', function(Server $c) {
779
-			$config = $c->getConfig();
780
-			$factoryClass = $config->getSystemValue('ldapProviderFactory', null);
781
-			if(is_null($factoryClass)) {
782
-				throw new \Exception('ldapProviderFactory not set');
783
-			}
784
-			/** @var \OCP\LDAP\ILDAPProviderFactory $factory */
785
-			$factory = new $factoryClass($this);
786
-			return $factory->getLDAPProvider();
787
-		});
788
-		$this->registerService('LockingProvider', function (Server $c) {
789
-			$ini = $c->getIniWrapper();
790
-			$config = $c->getConfig();
791
-			$ttl = $config->getSystemValue('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time')));
792
-			if ($config->getSystemValue('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
793
-				/** @var \OC\Memcache\Factory $memcacheFactory */
794
-				$memcacheFactory = $c->getMemCacheFactory();
795
-				$memcache = $memcacheFactory->createLocking('lock');
796
-				if (!($memcache instanceof \OC\Memcache\NullCache)) {
797
-					return new MemcacheLockingProvider($memcache, $ttl);
798
-				}
799
-				return new DBLockingProvider($c->getDatabaseConnection(), $c->getLogger(), new TimeFactory(), $ttl);
800
-			}
801
-			return new NoopLockingProvider();
802
-		});
803
-
804
-		$this->registerService(\OCP\Files\Mount\IMountManager::class, function () {
805
-			return new \OC\Files\Mount\Manager();
806
-		});
807
-		$this->registerAlias('MountManager', \OCP\Files\Mount\IMountManager::class);
808
-
809
-		$this->registerService(\OCP\Files\IMimeTypeDetector::class, function (Server $c) {
810
-			return new \OC\Files\Type\Detection(
811
-				$c->getURLGenerator(),
812
-				\OC::$configDir,
813
-				\OC::$SERVERROOT . '/resources/config/'
814
-			);
815
-		});
816
-		$this->registerAlias('MimeTypeDetector', \OCP\Files\IMimeTypeDetector::class);
817
-
818
-		$this->registerService(\OCP\Files\IMimeTypeLoader::class, function (Server $c) {
819
-			return new \OC\Files\Type\Loader(
820
-				$c->getDatabaseConnection()
821
-			);
822
-		});
823
-		$this->registerAlias('MimeTypeLoader', \OCP\Files\IMimeTypeLoader::class);
824
-		$this->registerService(BundleFetcher::class, function () {
825
-			return new BundleFetcher($this->getL10N('lib'));
826
-		});
827
-		$this->registerService(\OCP\Notification\IManager::class, function (Server $c) {
828
-			return new Manager(
829
-				$c->query(IValidator::class)
830
-			);
831
-		});
832
-		$this->registerAlias('NotificationManager', \OCP\Notification\IManager::class);
833
-
834
-		$this->registerService(\OC\CapabilitiesManager::class, function (Server $c) {
835
-			$manager = new \OC\CapabilitiesManager($c->getLogger());
836
-			$manager->registerCapability(function () use ($c) {
837
-				return new \OC\OCS\CoreCapabilities($c->getConfig());
838
-			});
839
-			$manager->registerCapability(function () use ($c) {
840
-				return $c->query(\OC\Security\Bruteforce\Capabilities::class);
841
-			});
842
-			return $manager;
843
-		});
844
-		$this->registerAlias('CapabilitiesManager', \OC\CapabilitiesManager::class);
845
-
846
-		$this->registerService(\OCP\Comments\ICommentsManager::class, function(Server $c) {
847
-			$config = $c->getConfig();
848
-			$factoryClass = $config->getSystemValue('comments.managerFactory', '\OC\Comments\ManagerFactory');
849
-			/** @var \OCP\Comments\ICommentsManagerFactory $factory */
850
-			$factory = new $factoryClass($this);
851
-			return $factory->getManager();
852
-		});
853
-		$this->registerAlias('CommentsManager', \OCP\Comments\ICommentsManager::class);
854
-
855
-		$this->registerService('ThemingDefaults', function(Server $c) {
856
-			/*
130
+    /** @var string */
131
+    private $webRoot;
132
+
133
+    /**
134
+     * @param string $webRoot
135
+     * @param \OC\Config $config
136
+     */
137
+    public function __construct($webRoot, \OC\Config $config) {
138
+        parent::__construct();
139
+        $this->webRoot = $webRoot;
140
+
141
+        $this->registerService(\OCP\IServerContainer::class, function(IServerContainer $c) {
142
+            return $c;
143
+        });
144
+
145
+        $this->registerAlias(\OCP\Contacts\IManager::class, \OC\ContactsManager::class);
146
+        $this->registerAlias('ContactsManager', \OCP\Contacts\IManager::class);
147
+
148
+        $this->registerAlias(IActionFactory::class, ActionFactory::class);
149
+
150
+
151
+
152
+        $this->registerService(\OCP\IPreview::class, function (Server $c) {
153
+            return new PreviewManager(
154
+                $c->getConfig(),
155
+                $c->getRootFolder(),
156
+                $c->getAppDataDir('preview'),
157
+                $c->getEventDispatcher(),
158
+                $c->getSession()->get('user_id')
159
+            );
160
+        });
161
+        $this->registerAlias('PreviewManager', \OCP\IPreview::class);
162
+
163
+        $this->registerService(\OC\Preview\Watcher::class, function (Server $c) {
164
+            return new \OC\Preview\Watcher(
165
+                $c->getAppDataDir('preview')
166
+            );
167
+        });
168
+
169
+        $this->registerService('EncryptionManager', function (Server $c) {
170
+            $view = new View();
171
+            $util = new Encryption\Util(
172
+                $view,
173
+                $c->getUserManager(),
174
+                $c->getGroupManager(),
175
+                $c->getConfig()
176
+            );
177
+            return new Encryption\Manager(
178
+                $c->getConfig(),
179
+                $c->getLogger(),
180
+                $c->getL10N('core'),
181
+                new View(),
182
+                $util,
183
+                new ArrayCache()
184
+            );
185
+        });
186
+
187
+        $this->registerService('EncryptionFileHelper', function (Server $c) {
188
+            $util = new Encryption\Util(
189
+                new View(),
190
+                $c->getUserManager(),
191
+                $c->getGroupManager(),
192
+                $c->getConfig()
193
+            );
194
+            return new Encryption\File(
195
+                $util,
196
+                $c->getRootFolder(),
197
+                $c->getShareManager()
198
+            );
199
+        });
200
+
201
+        $this->registerService('EncryptionKeyStorage', function (Server $c) {
202
+            $view = new View();
203
+            $util = new Encryption\Util(
204
+                $view,
205
+                $c->getUserManager(),
206
+                $c->getGroupManager(),
207
+                $c->getConfig()
208
+            );
209
+
210
+            return new Encryption\Keys\Storage($view, $util);
211
+        });
212
+        $this->registerService('TagMapper', function (Server $c) {
213
+            return new TagMapper($c->getDatabaseConnection());
214
+        });
215
+
216
+        $this->registerService(\OCP\ITagManager::class, function (Server $c) {
217
+            $tagMapper = $c->query('TagMapper');
218
+            return new TagManager($tagMapper, $c->getUserSession());
219
+        });
220
+        $this->registerAlias('TagManager', \OCP\ITagManager::class);
221
+
222
+        $this->registerService('SystemTagManagerFactory', function (Server $c) {
223
+            $config = $c->getConfig();
224
+            $factoryClass = $config->getSystemValue('systemtags.managerFactory', '\OC\SystemTag\ManagerFactory');
225
+            /** @var \OC\SystemTag\ManagerFactory $factory */
226
+            $factory = new $factoryClass($this);
227
+            return $factory;
228
+        });
229
+        $this->registerService(\OCP\SystemTag\ISystemTagManager::class, function (Server $c) {
230
+            return $c->query('SystemTagManagerFactory')->getManager();
231
+        });
232
+        $this->registerAlias('SystemTagManager', \OCP\SystemTag\ISystemTagManager::class);
233
+
234
+        $this->registerService(\OCP\SystemTag\ISystemTagObjectMapper::class, function (Server $c) {
235
+            return $c->query('SystemTagManagerFactory')->getObjectMapper();
236
+        });
237
+        $this->registerService('RootFolder', function (Server $c) {
238
+            $manager = \OC\Files\Filesystem::getMountManager(null);
239
+            $view = new View();
240
+            $root = new Root(
241
+                $manager,
242
+                $view,
243
+                null,
244
+                $c->getUserMountCache(),
245
+                $this->getLogger(),
246
+                $this->getUserManager()
247
+            );
248
+            $connector = new HookConnector($root, $view);
249
+            $connector->viewToNode();
250
+
251
+            $previewConnector = new \OC\Preview\WatcherConnector($root, $c->getSystemConfig());
252
+            $previewConnector->connectWatcher();
253
+
254
+            return $root;
255
+        });
256
+        $this->registerAlias('SystemTagObjectMapper', \OCP\SystemTag\ISystemTagObjectMapper::class);
257
+
258
+        $this->registerService(\OCP\Files\IRootFolder::class, function(Server $c) {
259
+            return new LazyRoot(function() use ($c) {
260
+                return $c->query('RootFolder');
261
+            });
262
+        });
263
+        $this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class);
264
+
265
+        $this->registerService(\OCP\IUserManager::class, function (Server $c) {
266
+            $config = $c->getConfig();
267
+            return new \OC\User\Manager($config);
268
+        });
269
+        $this->registerAlias('UserManager', \OCP\IUserManager::class);
270
+
271
+        $this->registerService(\OCP\IGroupManager::class, function (Server $c) {
272
+            $groupManager = new \OC\Group\Manager($this->getUserManager(), $this->getLogger());
273
+            $groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
274
+                \OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid));
275
+            });
276
+            $groupManager->listen('\OC\Group', 'postCreate', function (\OC\Group\Group $gid) {
277
+                \OC_Hook::emit('OC_User', 'post_createGroup', array('gid' => $gid->getGID()));
278
+            });
279
+            $groupManager->listen('\OC\Group', 'preDelete', function (\OC\Group\Group $group) {
280
+                \OC_Hook::emit('OC_Group', 'pre_deleteGroup', array('run' => true, 'gid' => $group->getGID()));
281
+            });
282
+            $groupManager->listen('\OC\Group', 'postDelete', function (\OC\Group\Group $group) {
283
+                \OC_Hook::emit('OC_User', 'post_deleteGroup', array('gid' => $group->getGID()));
284
+            });
285
+            $groupManager->listen('\OC\Group', 'preAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
286
+                \OC_Hook::emit('OC_Group', 'pre_addToGroup', array('run' => true, 'uid' => $user->getUID(), 'gid' => $group->getGID()));
287
+            });
288
+            $groupManager->listen('\OC\Group', 'postAddUser', function (\OC\Group\Group $group, \OC\User\User $user) {
289
+                \OC_Hook::emit('OC_Group', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
290
+                //Minimal fix to keep it backward compatible TODO: clean up all the GroupManager hooks
291
+                \OC_Hook::emit('OC_User', 'post_addToGroup', array('uid' => $user->getUID(), 'gid' => $group->getGID()));
292
+            });
293
+            return $groupManager;
294
+        });
295
+        $this->registerAlias('GroupManager', \OCP\IGroupManager::class);
296
+
297
+        $this->registerService(Store::class, function(Server $c) {
298
+            $session = $c->getSession();
299
+            if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
300
+                $tokenProvider = $c->query('OC\Authentication\Token\IProvider');
301
+            } else {
302
+                $tokenProvider = null;
303
+            }
304
+            $logger = $c->getLogger();
305
+            return new Store($session, $logger, $tokenProvider);
306
+        });
307
+        $this->registerAlias(IStore::class, Store::class);
308
+        $this->registerService('OC\Authentication\Token\DefaultTokenMapper', function (Server $c) {
309
+            $dbConnection = $c->getDatabaseConnection();
310
+            return new Authentication\Token\DefaultTokenMapper($dbConnection);
311
+        });
312
+        $this->registerService('OC\Authentication\Token\DefaultTokenProvider', function (Server $c) {
313
+            $mapper = $c->query('OC\Authentication\Token\DefaultTokenMapper');
314
+            $crypto = $c->getCrypto();
315
+            $config = $c->getConfig();
316
+            $logger = $c->getLogger();
317
+            $timeFactory = new TimeFactory();
318
+            return new \OC\Authentication\Token\DefaultTokenProvider($mapper, $crypto, $config, $logger, $timeFactory);
319
+        });
320
+        $this->registerAlias('OC\Authentication\Token\IProvider', 'OC\Authentication\Token\DefaultTokenProvider');
321
+
322
+        $this->registerService(\OCP\IUserSession::class, function (Server $c) {
323
+            $manager = $c->getUserManager();
324
+            $session = new \OC\Session\Memory('');
325
+            $timeFactory = new TimeFactory();
326
+            // Token providers might require a working database. This code
327
+            // might however be called when ownCloud is not yet setup.
328
+            if (\OC::$server->getSystemConfig()->getValue('installed', false)) {
329
+                $defaultTokenProvider = $c->query('OC\Authentication\Token\IProvider');
330
+            } else {
331
+                $defaultTokenProvider = null;
332
+            }
333
+
334
+            $userSession = new \OC\User\Session($manager, $session, $timeFactory, $defaultTokenProvider, $c->getConfig(), $c->getSecureRandom(), $c->getLockdownManager());
335
+            $userSession->listen('\OC\User', 'preCreateUser', function ($uid, $password) {
336
+                \OC_Hook::emit('OC_User', 'pre_createUser', array('run' => true, 'uid' => $uid, 'password' => $password));
337
+            });
338
+            $userSession->listen('\OC\User', 'postCreateUser', function ($user, $password) {
339
+                /** @var $user \OC\User\User */
340
+                \OC_Hook::emit('OC_User', 'post_createUser', array('uid' => $user->getUID(), 'password' => $password));
341
+            });
342
+            $userSession->listen('\OC\User', 'preDelete', function ($user) {
343
+                /** @var $user \OC\User\User */
344
+                \OC_Hook::emit('OC_User', 'pre_deleteUser', array('run' => true, 'uid' => $user->getUID()));
345
+            });
346
+            $userSession->listen('\OC\User', 'postDelete', function ($user) {
347
+                /** @var $user \OC\User\User */
348
+                \OC_Hook::emit('OC_User', 'post_deleteUser', array('uid' => $user->getUID()));
349
+            });
350
+            $userSession->listen('\OC\User', 'preSetPassword', function ($user, $password, $recoveryPassword) {
351
+                /** @var $user \OC\User\User */
352
+                \OC_Hook::emit('OC_User', 'pre_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
353
+            });
354
+            $userSession->listen('\OC\User', 'postSetPassword', function ($user, $password, $recoveryPassword) {
355
+                /** @var $user \OC\User\User */
356
+                \OC_Hook::emit('OC_User', 'post_setPassword', array('run' => true, 'uid' => $user->getUID(), 'password' => $password, 'recoveryPassword' => $recoveryPassword));
357
+            });
358
+            $userSession->listen('\OC\User', 'preLogin', function ($uid, $password) {
359
+                \OC_Hook::emit('OC_User', 'pre_login', array('run' => true, 'uid' => $uid, 'password' => $password));
360
+            });
361
+            $userSession->listen('\OC\User', 'postLogin', function ($user, $password) {
362
+                /** @var $user \OC\User\User */
363
+                \OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
364
+            });
365
+            $userSession->listen('\OC\User', 'postRememberedLogin', function ($user, $password) {
366
+                /** @var $user \OC\User\User */
367
+                \OC_Hook::emit('OC_User', 'post_login', array('run' => true, 'uid' => $user->getUID(), 'password' => $password));
368
+            });
369
+            $userSession->listen('\OC\User', 'logout', function () {
370
+                \OC_Hook::emit('OC_User', 'logout', array());
371
+            });
372
+            $userSession->listen('\OC\User', 'changeUser', function ($user, $feature, $value, $oldValue) {
373
+                /** @var $user \OC\User\User */
374
+                \OC_Hook::emit('OC_User', 'changeUser', array('run' => true, 'user' => $user, 'feature' => $feature, 'value' => $value, 'old_value' => $oldValue));
375
+            });
376
+            return $userSession;
377
+        });
378
+        $this->registerAlias('UserSession', \OCP\IUserSession::class);
379
+
380
+        $this->registerService(\OC\Authentication\TwoFactorAuth\Manager::class, function (Server $c) {
381
+            return new \OC\Authentication\TwoFactorAuth\Manager($c->getAppManager(), $c->getSession(), $c->getConfig(), $c->getActivityManager(), $c->getLogger());
382
+        });
383
+
384
+        $this->registerAlias(\OCP\INavigationManager::class, \OC\NavigationManager::class);
385
+        $this->registerAlias('NavigationManager', \OCP\INavigationManager::class);
386
+
387
+        $this->registerService(\OC\AllConfig::class, function (Server $c) {
388
+            return new \OC\AllConfig(
389
+                $c->getSystemConfig()
390
+            );
391
+        });
392
+        $this->registerAlias('AllConfig', \OC\AllConfig::class);
393
+        $this->registerAlias(\OCP\IConfig::class, \OC\AllConfig::class);
394
+
395
+        $this->registerService('SystemConfig', function ($c) use ($config) {
396
+            return new \OC\SystemConfig($config);
397
+        });
398
+
399
+        $this->registerService(\OC\AppConfig::class, function (Server $c) {
400
+            return new \OC\AppConfig($c->getDatabaseConnection());
401
+        });
402
+        $this->registerAlias('AppConfig', \OC\AppConfig::class);
403
+        $this->registerAlias(\OCP\IAppConfig::class, \OC\AppConfig::class);
404
+
405
+        $this->registerService(\OCP\L10N\IFactory::class, function (Server $c) {
406
+            return new \OC\L10N\Factory(
407
+                $c->getConfig(),
408
+                $c->getRequest(),
409
+                $c->getUserSession(),
410
+                \OC::$SERVERROOT
411
+            );
412
+        });
413
+        $this->registerAlias('L10NFactory', \OCP\L10N\IFactory::class);
414
+
415
+        $this->registerService(\OCP\IURLGenerator::class, function (Server $c) {
416
+            $config = $c->getConfig();
417
+            $cacheFactory = $c->getMemCacheFactory();
418
+            $request = $c->getRequest();
419
+            return new \OC\URLGenerator(
420
+                $config,
421
+                $cacheFactory,
422
+                $request
423
+            );
424
+        });
425
+        $this->registerAlias('URLGenerator', \OCP\IURLGenerator::class);
426
+
427
+        $this->registerService('AppHelper', function ($c) {
428
+            return new \OC\AppHelper();
429
+        });
430
+        $this->registerAlias('AppFetcher', AppFetcher::class);
431
+        $this->registerAlias('CategoryFetcher', CategoryFetcher::class);
432
+
433
+        $this->registerService(\OCP\ICache::class, function ($c) {
434
+            return new Cache\File();
435
+        });
436
+        $this->registerAlias('UserCache', \OCP\ICache::class);
437
+
438
+        $this->registerService(Factory::class, function (Server $c) {
439
+
440
+            $arrayCacheFactory = new \OC\Memcache\Factory('', $c->getLogger(),
441
+                '\\OC\\Memcache\\ArrayCache',
442
+                '\\OC\\Memcache\\ArrayCache',
443
+                '\\OC\\Memcache\\ArrayCache'
444
+            );
445
+            $config = $c->getConfig();
446
+            $request = $c->getRequest();
447
+            $urlGenerator = new URLGenerator($config, $arrayCacheFactory, $request);
448
+
449
+            if ($config->getSystemValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
450
+                $v = \OC_App::getAppVersions();
451
+                $v['core'] = implode(',', \OC_Util::getVersion());
452
+                $version = implode(',', $v);
453
+                $instanceId = \OC_Util::getInstanceId();
454
+                $path = \OC::$SERVERROOT;
455
+                $prefix = md5($instanceId . '-' . $version . '-' . $path . '-' . $urlGenerator->getBaseUrl());
456
+                return new \OC\Memcache\Factory($prefix, $c->getLogger(),
457
+                    $config->getSystemValue('memcache.local', null),
458
+                    $config->getSystemValue('memcache.distributed', null),
459
+                    $config->getSystemValue('memcache.locking', null)
460
+                );
461
+            }
462
+            return $arrayCacheFactory;
463
+
464
+        });
465
+        $this->registerAlias('MemCacheFactory', Factory::class);
466
+        $this->registerAlias(ICacheFactory::class, Factory::class);
467
+
468
+        $this->registerService('RedisFactory', function (Server $c) {
469
+            $systemConfig = $c->getSystemConfig();
470
+            return new RedisFactory($systemConfig);
471
+        });
472
+
473
+        $this->registerService(\OCP\Activity\IManager::class, function (Server $c) {
474
+            return new \OC\Activity\Manager(
475
+                $c->getRequest(),
476
+                $c->getUserSession(),
477
+                $c->getConfig(),
478
+                $c->query(IValidator::class)
479
+            );
480
+        });
481
+        $this->registerAlias('ActivityManager', \OCP\Activity\IManager::class);
482
+
483
+        $this->registerService(\OCP\Activity\IEventMerger::class, function (Server $c) {
484
+            return new \OC\Activity\EventMerger(
485
+                $c->getL10N('lib')
486
+            );
487
+        });
488
+        $this->registerAlias(IValidator::class, Validator::class);
489
+
490
+        $this->registerService(\OCP\IAvatarManager::class, function (Server $c) {
491
+            return new AvatarManager(
492
+                $c->getUserManager(),
493
+                $c->getAppDataDir('avatar'),
494
+                $c->getL10N('lib'),
495
+                $c->getLogger(),
496
+                $c->getConfig()
497
+            );
498
+        });
499
+        $this->registerAlias('AvatarManager', \OCP\IAvatarManager::class);
500
+
501
+        $this->registerService(\OCP\ILogger::class, function (Server $c) {
502
+            $logType = $c->query('AllConfig')->getSystemValue('log_type', 'file');
503
+            $logger = Log::getLogClass($logType);
504
+            call_user_func(array($logger, 'init'));
505
+
506
+            return new Log($logger);
507
+        });
508
+        $this->registerAlias('Logger', \OCP\ILogger::class);
509
+
510
+        $this->registerService(\OCP\BackgroundJob\IJobList::class, function (Server $c) {
511
+            $config = $c->getConfig();
512
+            return new \OC\BackgroundJob\JobList(
513
+                $c->getDatabaseConnection(),
514
+                $config,
515
+                new TimeFactory()
516
+            );
517
+        });
518
+        $this->registerAlias('JobList', \OCP\BackgroundJob\IJobList::class);
519
+
520
+        $this->registerService(\OCP\Route\IRouter::class, function (Server $c) {
521
+            $cacheFactory = $c->getMemCacheFactory();
522
+            $logger = $c->getLogger();
523
+            if ($cacheFactory->isAvailable()) {
524
+                $router = new \OC\Route\CachingRouter($cacheFactory->create('route'), $logger);
525
+            } else {
526
+                $router = new \OC\Route\Router($logger);
527
+            }
528
+            return $router;
529
+        });
530
+        $this->registerAlias('Router', \OCP\Route\IRouter::class);
531
+
532
+        $this->registerService(\OCP\ISearch::class, function ($c) {
533
+            return new Search();
534
+        });
535
+        $this->registerAlias('Search', \OCP\ISearch::class);
536
+
537
+        $this->registerService(\OC\Security\RateLimiting\Limiter::class, function($c) {
538
+            return new \OC\Security\RateLimiting\Limiter(
539
+                $this->getUserSession(),
540
+                $this->getRequest(),
541
+                new \OC\AppFramework\Utility\TimeFactory(),
542
+                $c->query(\OC\Security\RateLimiting\Backend\IBackend::class)
543
+            );
544
+        });
545
+        $this->registerService(\OC\Security\RateLimiting\Backend\IBackend::class, function($c) {
546
+            return new \OC\Security\RateLimiting\Backend\MemoryCache(
547
+                $this->getMemCacheFactory(),
548
+                new \OC\AppFramework\Utility\TimeFactory()
549
+            );
550
+        });
551
+
552
+        $this->registerService(\OCP\Security\ISecureRandom::class, function ($c) {
553
+            return new SecureRandom();
554
+        });
555
+        $this->registerAlias('SecureRandom', \OCP\Security\ISecureRandom::class);
556
+
557
+        $this->registerService(\OCP\Security\ICrypto::class, function (Server $c) {
558
+            return new Crypto($c->getConfig(), $c->getSecureRandom());
559
+        });
560
+        $this->registerAlias('Crypto', \OCP\Security\ICrypto::class);
561
+
562
+        $this->registerService(\OCP\Security\IHasher::class, function (Server $c) {
563
+            return new Hasher($c->getConfig());
564
+        });
565
+        $this->registerAlias('Hasher', \OCP\Security\IHasher::class);
566
+
567
+        $this->registerService(\OCP\Security\ICredentialsManager::class, function (Server $c) {
568
+            return new CredentialsManager($c->getCrypto(), $c->getDatabaseConnection());
569
+        });
570
+        $this->registerAlias('CredentialsManager', \OCP\Security\ICredentialsManager::class);
571
+
572
+        $this->registerService(IDBConnection::class, function (Server $c) {
573
+            $systemConfig = $c->getSystemConfig();
574
+            $factory = new \OC\DB\ConnectionFactory($systemConfig);
575
+            $type = $systemConfig->getValue('dbtype', 'sqlite');
576
+            if (!$factory->isValidType($type)) {
577
+                throw new \OC\DatabaseException('Invalid database type');
578
+            }
579
+            $connectionParams = $factory->createConnectionParams();
580
+            $connection = $factory->getConnection($type, $connectionParams);
581
+            $connection->getConfiguration()->setSQLLogger($c->getQueryLogger());
582
+            return $connection;
583
+        });
584
+        $this->registerAlias('DatabaseConnection', IDBConnection::class);
585
+
586
+        $this->registerService('HTTPHelper', function (Server $c) {
587
+            $config = $c->getConfig();
588
+            return new HTTPHelper(
589
+                $config,
590
+                $c->getHTTPClientService()
591
+            );
592
+        });
593
+
594
+        $this->registerService(\OCP\Http\Client\IClientService::class, function (Server $c) {
595
+            $user = \OC_User::getUser();
596
+            $uid = $user ? $user : null;
597
+            return new ClientService(
598
+                $c->getConfig(),
599
+                new \OC\Security\CertificateManager(
600
+                    $uid,
601
+                    new View(),
602
+                    $c->getConfig(),
603
+                    $c->getLogger(),
604
+                    $c->getSecureRandom()
605
+                )
606
+            );
607
+        });
608
+        $this->registerAlias('HttpClientService', \OCP\Http\Client\IClientService::class);
609
+        $this->registerService(\OCP\Diagnostics\IEventLogger::class, function (Server $c) {
610
+            $eventLogger = new EventLogger();
611
+            if ($c->getSystemConfig()->getValue('debug', false)) {
612
+                // In debug mode, module is being activated by default
613
+                $eventLogger->activate();
614
+            }
615
+            return $eventLogger;
616
+        });
617
+        $this->registerAlias('EventLogger', \OCP\Diagnostics\IEventLogger::class);
618
+
619
+        $this->registerService(\OCP\Diagnostics\IQueryLogger::class, function (Server $c) {
620
+            $queryLogger = new QueryLogger();
621
+            if ($c->getSystemConfig()->getValue('debug', false)) {
622
+                // In debug mode, module is being activated by default
623
+                $queryLogger->activate();
624
+            }
625
+            return $queryLogger;
626
+        });
627
+        $this->registerAlias('QueryLogger', \OCP\Diagnostics\IQueryLogger::class);
628
+
629
+        $this->registerService(TempManager::class, function (Server $c) {
630
+            return new TempManager(
631
+                $c->getLogger(),
632
+                $c->getConfig()
633
+            );
634
+        });
635
+        $this->registerAlias('TempManager', TempManager::class);
636
+        $this->registerAlias(ITempManager::class, TempManager::class);
637
+
638
+        $this->registerService(AppManager::class, function (Server $c) {
639
+            return new \OC\App\AppManager(
640
+                $c->getUserSession(),
641
+                $c->getAppConfig(),
642
+                $c->getGroupManager(),
643
+                $c->getMemCacheFactory(),
644
+                $c->getEventDispatcher()
645
+            );
646
+        });
647
+        $this->registerAlias('AppManager', AppManager::class);
648
+        $this->registerAlias(IAppManager::class, AppManager::class);
649
+
650
+        $this->registerService(\OCP\IDateTimeZone::class, function (Server $c) {
651
+            return new DateTimeZone(
652
+                $c->getConfig(),
653
+                $c->getSession()
654
+            );
655
+        });
656
+        $this->registerAlias('DateTimeZone', \OCP\IDateTimeZone::class);
657
+
658
+        $this->registerService(\OCP\IDateTimeFormatter::class, function (Server $c) {
659
+            $language = $c->getConfig()->getUserValue($c->getSession()->get('user_id'), 'core', 'lang', null);
660
+
661
+            return new DateTimeFormatter(
662
+                $c->getDateTimeZone()->getTimeZone(),
663
+                $c->getL10N('lib', $language)
664
+            );
665
+        });
666
+        $this->registerAlias('DateTimeFormatter', \OCP\IDateTimeFormatter::class);
667
+
668
+        $this->registerService(\OCP\Files\Config\IUserMountCache::class, function (Server $c) {
669
+            $mountCache = new UserMountCache($c->getDatabaseConnection(), $c->getUserManager(), $c->getLogger());
670
+            $listener = new UserMountCacheListener($mountCache);
671
+            $listener->listen($c->getUserManager());
672
+            return $mountCache;
673
+        });
674
+        $this->registerAlias('UserMountCache', \OCP\Files\Config\IUserMountCache::class);
675
+
676
+        $this->registerService(\OCP\Files\Config\IMountProviderCollection::class, function (Server $c) {
677
+            $loader = \OC\Files\Filesystem::getLoader();
678
+            $mountCache = $c->query('UserMountCache');
679
+            $manager =  new \OC\Files\Config\MountProviderCollection($loader, $mountCache);
680
+
681
+            // builtin providers
682
+
683
+            $config = $c->getConfig();
684
+            $manager->registerProvider(new CacheMountProvider($config));
685
+            $manager->registerHomeProvider(new LocalHomeMountProvider());
686
+            $manager->registerHomeProvider(new ObjectHomeMountProvider($config));
687
+
688
+            return $manager;
689
+        });
690
+        $this->registerAlias('MountConfigManager', \OCP\Files\Config\IMountProviderCollection::class);
691
+
692
+        $this->registerService('IniWrapper', function ($c) {
693
+            return new IniGetWrapper();
694
+        });
695
+        $this->registerService('AsyncCommandBus', function (Server $c) {
696
+            $jobList = $c->getJobList();
697
+            return new AsyncBus($jobList);
698
+        });
699
+        $this->registerService('TrustedDomainHelper', function ($c) {
700
+            return new TrustedDomainHelper($this->getConfig());
701
+        });
702
+        $this->registerService('Throttler', function(Server $c) {
703
+            return new Throttler(
704
+                $c->getDatabaseConnection(),
705
+                new TimeFactory(),
706
+                $c->getLogger(),
707
+                $c->getConfig()
708
+            );
709
+        });
710
+        $this->registerService('IntegrityCodeChecker', function (Server $c) {
711
+            // IConfig and IAppManager requires a working database. This code
712
+            // might however be called when ownCloud is not yet setup.
713
+            if(\OC::$server->getSystemConfig()->getValue('installed', false)) {
714
+                $config = $c->getConfig();
715
+                $appManager = $c->getAppManager();
716
+            } else {
717
+                $config = null;
718
+                $appManager = null;
719
+            }
720
+
721
+            return new Checker(
722
+                    new EnvironmentHelper(),
723
+                    new FileAccessHelper(),
724
+                    new AppLocator(),
725
+                    $config,
726
+                    $c->getMemCacheFactory(),
727
+                    $appManager,
728
+                    $c->getTempManager()
729
+            );
730
+        });
731
+        $this->registerService(\OCP\IRequest::class, function ($c) {
732
+            if (isset($this['urlParams'])) {
733
+                $urlParams = $this['urlParams'];
734
+            } else {
735
+                $urlParams = [];
736
+            }
737
+
738
+            if (defined('PHPUNIT_RUN') && PHPUNIT_RUN
739
+                && in_array('fakeinput', stream_get_wrappers())
740
+            ) {
741
+                $stream = 'fakeinput://data';
742
+            } else {
743
+                $stream = 'php://input';
744
+            }
745
+
746
+            return new Request(
747
+                [
748
+                    'get' => $_GET,
749
+                    'post' => $_POST,
750
+                    'files' => $_FILES,
751
+                    'server' => $_SERVER,
752
+                    'env' => $_ENV,
753
+                    'cookies' => $_COOKIE,
754
+                    'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
755
+                        ? $_SERVER['REQUEST_METHOD']
756
+                        : null,
757
+                    'urlParams' => $urlParams,
758
+                ],
759
+                $this->getSecureRandom(),
760
+                $this->getConfig(),
761
+                $this->getCsrfTokenManager(),
762
+                $stream
763
+            );
764
+        });
765
+        $this->registerAlias('Request', \OCP\IRequest::class);
766
+
767
+        $this->registerService(\OCP\Mail\IMailer::class, function (Server $c) {
768
+            return new Mailer(
769
+                $c->getConfig(),
770
+                $c->getLogger(),
771
+                $c->query(Defaults::class),
772
+                $c->getURLGenerator(),
773
+                $c->getL10N('lib')
774
+            );
775
+        });
776
+        $this->registerAlias('Mailer', \OCP\Mail\IMailer::class);
777
+
778
+        $this->registerService('LDAPProvider', function(Server $c) {
779
+            $config = $c->getConfig();
780
+            $factoryClass = $config->getSystemValue('ldapProviderFactory', null);
781
+            if(is_null($factoryClass)) {
782
+                throw new \Exception('ldapProviderFactory not set');
783
+            }
784
+            /** @var \OCP\LDAP\ILDAPProviderFactory $factory */
785
+            $factory = new $factoryClass($this);
786
+            return $factory->getLDAPProvider();
787
+        });
788
+        $this->registerService('LockingProvider', function (Server $c) {
789
+            $ini = $c->getIniWrapper();
790
+            $config = $c->getConfig();
791
+            $ttl = $config->getSystemValue('filelocking.ttl', max(3600, $ini->getNumeric('max_execution_time')));
792
+            if ($config->getSystemValue('filelocking.enabled', true) or (defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
793
+                /** @var \OC\Memcache\Factory $memcacheFactory */
794
+                $memcacheFactory = $c->getMemCacheFactory();
795
+                $memcache = $memcacheFactory->createLocking('lock');
796
+                if (!($memcache instanceof \OC\Memcache\NullCache)) {
797
+                    return new MemcacheLockingProvider($memcache, $ttl);
798
+                }
799
+                return new DBLockingProvider($c->getDatabaseConnection(), $c->getLogger(), new TimeFactory(), $ttl);
800
+            }
801
+            return new NoopLockingProvider();
802
+        });
803
+
804
+        $this->registerService(\OCP\Files\Mount\IMountManager::class, function () {
805
+            return new \OC\Files\Mount\Manager();
806
+        });
807
+        $this->registerAlias('MountManager', \OCP\Files\Mount\IMountManager::class);
808
+
809
+        $this->registerService(\OCP\Files\IMimeTypeDetector::class, function (Server $c) {
810
+            return new \OC\Files\Type\Detection(
811
+                $c->getURLGenerator(),
812
+                \OC::$configDir,
813
+                \OC::$SERVERROOT . '/resources/config/'
814
+            );
815
+        });
816
+        $this->registerAlias('MimeTypeDetector', \OCP\Files\IMimeTypeDetector::class);
817
+
818
+        $this->registerService(\OCP\Files\IMimeTypeLoader::class, function (Server $c) {
819
+            return new \OC\Files\Type\Loader(
820
+                $c->getDatabaseConnection()
821
+            );
822
+        });
823
+        $this->registerAlias('MimeTypeLoader', \OCP\Files\IMimeTypeLoader::class);
824
+        $this->registerService(BundleFetcher::class, function () {
825
+            return new BundleFetcher($this->getL10N('lib'));
826
+        });
827
+        $this->registerService(\OCP\Notification\IManager::class, function (Server $c) {
828
+            return new Manager(
829
+                $c->query(IValidator::class)
830
+            );
831
+        });
832
+        $this->registerAlias('NotificationManager', \OCP\Notification\IManager::class);
833
+
834
+        $this->registerService(\OC\CapabilitiesManager::class, function (Server $c) {
835
+            $manager = new \OC\CapabilitiesManager($c->getLogger());
836
+            $manager->registerCapability(function () use ($c) {
837
+                return new \OC\OCS\CoreCapabilities($c->getConfig());
838
+            });
839
+            $manager->registerCapability(function () use ($c) {
840
+                return $c->query(\OC\Security\Bruteforce\Capabilities::class);
841
+            });
842
+            return $manager;
843
+        });
844
+        $this->registerAlias('CapabilitiesManager', \OC\CapabilitiesManager::class);
845
+
846
+        $this->registerService(\OCP\Comments\ICommentsManager::class, function(Server $c) {
847
+            $config = $c->getConfig();
848
+            $factoryClass = $config->getSystemValue('comments.managerFactory', '\OC\Comments\ManagerFactory');
849
+            /** @var \OCP\Comments\ICommentsManagerFactory $factory */
850
+            $factory = new $factoryClass($this);
851
+            return $factory->getManager();
852
+        });
853
+        $this->registerAlias('CommentsManager', \OCP\Comments\ICommentsManager::class);
854
+
855
+        $this->registerService('ThemingDefaults', function(Server $c) {
856
+            /*
857 857
 			 * Dark magic for autoloader.
858 858
 			 * If we do a class_exists it will try to load the class which will
859 859
 			 * make composer cache the result. Resulting in errors when enabling
860 860
 			 * the theming app.
861 861
 			 */
862
-			$prefixes = \OC::$composerAutoloader->getPrefixesPsr4();
863
-			if (isset($prefixes['OCA\\Theming\\'])) {
864
-				$classExists = true;
865
-			} else {
866
-				$classExists = false;
867
-			}
868
-
869
-			if ($classExists && $c->getConfig()->getSystemValue('installed', false) && $c->getAppManager()->isInstalled('theming') && $c->getTrustedDomainHelper()->isTrustedDomain($c->getRequest()->getInsecureServerHost())) {
870
-				return new ThemingDefaults(
871
-					$c->getConfig(),
872
-					$c->getL10N('theming'),
873
-					$c->getURLGenerator(),
874
-					$c->getAppDataDir('theming'),
875
-					$c->getMemCacheFactory(),
876
-					new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming'))
877
-				);
878
-			}
879
-			return new \OC_Defaults();
880
-		});
881
-		$this->registerService(SCSSCacher::class, function(Server $c) {
882
-			/** @var Factory $cacheFactory */
883
-			$cacheFactory = $c->query(Factory::class);
884
-			return new SCSSCacher(
885
-				$c->getLogger(),
886
-				$c->query(\OC\Files\AppData\Factory::class),
887
-				$c->getURLGenerator(),
888
-				$c->getConfig(),
889
-				$c->getThemingDefaults(),
890
-				\OC::$SERVERROOT,
891
-				$cacheFactory->create('SCSS')
892
-			);
893
-		});
894
-		$this->registerService(EventDispatcher::class, function () {
895
-			return new EventDispatcher();
896
-		});
897
-		$this->registerAlias('EventDispatcher', EventDispatcher::class);
898
-		$this->registerAlias(EventDispatcherInterface::class, EventDispatcher::class);
899
-
900
-		$this->registerService('CryptoWrapper', function (Server $c) {
901
-			// FIXME: Instantiiated here due to cyclic dependency
902
-			$request = new Request(
903
-				[
904
-					'get' => $_GET,
905
-					'post' => $_POST,
906
-					'files' => $_FILES,
907
-					'server' => $_SERVER,
908
-					'env' => $_ENV,
909
-					'cookies' => $_COOKIE,
910
-					'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
911
-						? $_SERVER['REQUEST_METHOD']
912
-						: null,
913
-				],
914
-				$c->getSecureRandom(),
915
-				$c->getConfig()
916
-			);
917
-
918
-			return new CryptoWrapper(
919
-				$c->getConfig(),
920
-				$c->getCrypto(),
921
-				$c->getSecureRandom(),
922
-				$request
923
-			);
924
-		});
925
-		$this->registerService('CsrfTokenManager', function (Server $c) {
926
-			$tokenGenerator = new CsrfTokenGenerator($c->getSecureRandom());
927
-
928
-			return new CsrfTokenManager(
929
-				$tokenGenerator,
930
-				$c->query(SessionStorage::class)
931
-			);
932
-		});
933
-		$this->registerService(SessionStorage::class, function (Server $c) {
934
-			return new SessionStorage($c->getSession());
935
-		});
936
-		$this->registerService(\OCP\Security\IContentSecurityPolicyManager::class, function (Server $c) {
937
-			return new ContentSecurityPolicyManager();
938
-		});
939
-		$this->registerAlias('ContentSecurityPolicyManager', \OCP\Security\IContentSecurityPolicyManager::class);
940
-
941
-		$this->registerService('ContentSecurityPolicyNonceManager', function(Server $c) {
942
-			return new ContentSecurityPolicyNonceManager(
943
-				$c->getCsrfTokenManager(),
944
-				$c->getRequest()
945
-			);
946
-		});
947
-
948
-		$this->registerService(\OCP\Share\IManager::class, function(Server $c) {
949
-			$config = $c->getConfig();
950
-			$factoryClass = $config->getSystemValue('sharing.managerFactory', '\OC\Share20\ProviderFactory');
951
-			/** @var \OCP\Share\IProviderFactory $factory */
952
-			$factory = new $factoryClass($this);
953
-
954
-			$manager = new \OC\Share20\Manager(
955
-				$c->getLogger(),
956
-				$c->getConfig(),
957
-				$c->getSecureRandom(),
958
-				$c->getHasher(),
959
-				$c->getMountManager(),
960
-				$c->getGroupManager(),
961
-				$c->getL10N('core'),
962
-				$factory,
963
-				$c->getUserManager(),
964
-				$c->getLazyRootFolder(),
965
-				$c->getEventDispatcher()
966
-			);
967
-
968
-			return $manager;
969
-		});
970
-		$this->registerAlias('ShareManager', \OCP\Share\IManager::class);
971
-
972
-		$this->registerService('SettingsManager', function(Server $c) {
973
-			$manager = new \OC\Settings\Manager(
974
-				$c->getLogger(),
975
-				$c->getDatabaseConnection(),
976
-				$c->getL10N('lib'),
977
-				$c->getConfig(),
978
-				$c->getEncryptionManager(),
979
-				$c->getUserManager(),
980
-				$c->getLockingProvider(),
981
-				$c->getRequest(),
982
-				new \OC\Settings\Mapper($c->getDatabaseConnection()),
983
-				$c->getURLGenerator(),
984
-				$c->query(AccountManager::class),
985
-				$c->getGroupManager(),
986
-				$c->getL10NFactory(),
987
-				$c->getThemingDefaults(),
988
-				$c->getAppManager()
989
-			);
990
-			return $manager;
991
-		});
992
-		$this->registerService(\OC\Files\AppData\Factory::class, function (Server $c) {
993
-			return new \OC\Files\AppData\Factory(
994
-				$c->getRootFolder(),
995
-				$c->getSystemConfig()
996
-			);
997
-		});
998
-
999
-		$this->registerService('LockdownManager', function (Server $c) {
1000
-			return new LockdownManager(function() use ($c) {
1001
-				return $c->getSession();
1002
-			});
1003
-		});
1004
-
1005
-		$this->registerService(\OCP\OCS\IDiscoveryService::class, function (Server $c) {
1006
-			return new DiscoveryService($c->getMemCacheFactory(), $c->getHTTPClientService());
1007
-		});
1008
-
1009
-		$this->registerService(ICloudIdManager::class, function (Server $c) {
1010
-			return new CloudIdManager();
1011
-		});
1012
-
1013
-		/* To trick DI since we don't extend the DIContainer here */
1014
-		$this->registerService(CleanPreviewsBackgroundJob::class, function (Server $c) {
1015
-			return new CleanPreviewsBackgroundJob(
1016
-				$c->getRootFolder(),
1017
-				$c->getLogger(),
1018
-				$c->getJobList(),
1019
-				new TimeFactory()
1020
-			);
1021
-		});
1022
-
1023
-		$this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
1024
-		$this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
1025
-
1026
-		$this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
1027
-		$this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
1028
-
1029
-		$this->registerService(Defaults::class, function (Server $c) {
1030
-			return new Defaults(
1031
-				$c->getThemingDefaults()
1032
-			);
1033
-		});
1034
-		$this->registerAlias('Defaults', \OCP\Defaults::class);
1035
-
1036
-		$this->registerService(\OCP\ISession::class, function(SimpleContainer $c) {
1037
-			return $c->query(\OCP\IUserSession::class)->getSession();
1038
-		});
1039
-
1040
-		$this->registerService(IShareHelper::class, function(Server $c) {
1041
-			return new ShareHelper(
1042
-				$c->query(\OCP\Share\IManager::class)
1043
-			);
1044
-		});
1045
-	}
1046
-
1047
-	/**
1048
-	 * @return \OCP\Contacts\IManager
1049
-	 */
1050
-	public function getContactsManager() {
1051
-		return $this->query('ContactsManager');
1052
-	}
1053
-
1054
-	/**
1055
-	 * @return \OC\Encryption\Manager
1056
-	 */
1057
-	public function getEncryptionManager() {
1058
-		return $this->query('EncryptionManager');
1059
-	}
1060
-
1061
-	/**
1062
-	 * @return \OC\Encryption\File
1063
-	 */
1064
-	public function getEncryptionFilesHelper() {
1065
-		return $this->query('EncryptionFileHelper');
1066
-	}
1067
-
1068
-	/**
1069
-	 * @return \OCP\Encryption\Keys\IStorage
1070
-	 */
1071
-	public function getEncryptionKeyStorage() {
1072
-		return $this->query('EncryptionKeyStorage');
1073
-	}
1074
-
1075
-	/**
1076
-	 * The current request object holding all information about the request
1077
-	 * currently being processed is returned from this method.
1078
-	 * In case the current execution was not initiated by a web request null is returned
1079
-	 *
1080
-	 * @return \OCP\IRequest
1081
-	 */
1082
-	public function getRequest() {
1083
-		return $this->query('Request');
1084
-	}
1085
-
1086
-	/**
1087
-	 * Returns the preview manager which can create preview images for a given file
1088
-	 *
1089
-	 * @return \OCP\IPreview
1090
-	 */
1091
-	public function getPreviewManager() {
1092
-		return $this->query('PreviewManager');
1093
-	}
1094
-
1095
-	/**
1096
-	 * Returns the tag manager which can get and set tags for different object types
1097
-	 *
1098
-	 * @see \OCP\ITagManager::load()
1099
-	 * @return \OCP\ITagManager
1100
-	 */
1101
-	public function getTagManager() {
1102
-		return $this->query('TagManager');
1103
-	}
1104
-
1105
-	/**
1106
-	 * Returns the system-tag manager
1107
-	 *
1108
-	 * @return \OCP\SystemTag\ISystemTagManager
1109
-	 *
1110
-	 * @since 9.0.0
1111
-	 */
1112
-	public function getSystemTagManager() {
1113
-		return $this->query('SystemTagManager');
1114
-	}
1115
-
1116
-	/**
1117
-	 * Returns the system-tag object mapper
1118
-	 *
1119
-	 * @return \OCP\SystemTag\ISystemTagObjectMapper
1120
-	 *
1121
-	 * @since 9.0.0
1122
-	 */
1123
-	public function getSystemTagObjectMapper() {
1124
-		return $this->query('SystemTagObjectMapper');
1125
-	}
1126
-
1127
-	/**
1128
-	 * Returns the avatar manager, used for avatar functionality
1129
-	 *
1130
-	 * @return \OCP\IAvatarManager
1131
-	 */
1132
-	public function getAvatarManager() {
1133
-		return $this->query('AvatarManager');
1134
-	}
1135
-
1136
-	/**
1137
-	 * Returns the root folder of ownCloud's data directory
1138
-	 *
1139
-	 * @return \OCP\Files\IRootFolder
1140
-	 */
1141
-	public function getRootFolder() {
1142
-		return $this->query('LazyRootFolder');
1143
-	}
1144
-
1145
-	/**
1146
-	 * Returns the root folder of ownCloud's data directory
1147
-	 * This is the lazy variant so this gets only initialized once it
1148
-	 * is actually used.
1149
-	 *
1150
-	 * @return \OCP\Files\IRootFolder
1151
-	 */
1152
-	public function getLazyRootFolder() {
1153
-		return $this->query('LazyRootFolder');
1154
-	}
1155
-
1156
-	/**
1157
-	 * Returns a view to ownCloud's files folder
1158
-	 *
1159
-	 * @param string $userId user ID
1160
-	 * @return \OCP\Files\Folder|null
1161
-	 */
1162
-	public function getUserFolder($userId = null) {
1163
-		if ($userId === null) {
1164
-			$user = $this->getUserSession()->getUser();
1165
-			if (!$user) {
1166
-				return null;
1167
-			}
1168
-			$userId = $user->getUID();
1169
-		}
1170
-		$root = $this->getRootFolder();
1171
-		return $root->getUserFolder($userId);
1172
-	}
1173
-
1174
-	/**
1175
-	 * Returns an app-specific view in ownClouds data directory
1176
-	 *
1177
-	 * @return \OCP\Files\Folder
1178
-	 * @deprecated since 9.2.0 use IAppData
1179
-	 */
1180
-	public function getAppFolder() {
1181
-		$dir = '/' . \OC_App::getCurrentApp();
1182
-		$root = $this->getRootFolder();
1183
-		if (!$root->nodeExists($dir)) {
1184
-			$folder = $root->newFolder($dir);
1185
-		} else {
1186
-			$folder = $root->get($dir);
1187
-		}
1188
-		return $folder;
1189
-	}
1190
-
1191
-	/**
1192
-	 * @return \OC\User\Manager
1193
-	 */
1194
-	public function getUserManager() {
1195
-		return $this->query('UserManager');
1196
-	}
1197
-
1198
-	/**
1199
-	 * @return \OC\Group\Manager
1200
-	 */
1201
-	public function getGroupManager() {
1202
-		return $this->query('GroupManager');
1203
-	}
1204
-
1205
-	/**
1206
-	 * @return \OC\User\Session
1207
-	 */
1208
-	public function getUserSession() {
1209
-		return $this->query('UserSession');
1210
-	}
1211
-
1212
-	/**
1213
-	 * @return \OCP\ISession
1214
-	 */
1215
-	public function getSession() {
1216
-		return $this->query('UserSession')->getSession();
1217
-	}
1218
-
1219
-	/**
1220
-	 * @param \OCP\ISession $session
1221
-	 */
1222
-	public function setSession(\OCP\ISession $session) {
1223
-		$this->query(SessionStorage::class)->setSession($session);
1224
-		$this->query('UserSession')->setSession($session);
1225
-		$this->query(Store::class)->setSession($session);
1226
-	}
1227
-
1228
-	/**
1229
-	 * @return \OC\Authentication\TwoFactorAuth\Manager
1230
-	 */
1231
-	public function getTwoFactorAuthManager() {
1232
-		return $this->query('\OC\Authentication\TwoFactorAuth\Manager');
1233
-	}
1234
-
1235
-	/**
1236
-	 * @return \OC\NavigationManager
1237
-	 */
1238
-	public function getNavigationManager() {
1239
-		return $this->query('NavigationManager');
1240
-	}
1241
-
1242
-	/**
1243
-	 * @return \OCP\IConfig
1244
-	 */
1245
-	public function getConfig() {
1246
-		return $this->query('AllConfig');
1247
-	}
1248
-
1249
-	/**
1250
-	 * @internal For internal use only
1251
-	 * @return \OC\SystemConfig
1252
-	 */
1253
-	public function getSystemConfig() {
1254
-		return $this->query('SystemConfig');
1255
-	}
1256
-
1257
-	/**
1258
-	 * Returns the app config manager
1259
-	 *
1260
-	 * @return \OCP\IAppConfig
1261
-	 */
1262
-	public function getAppConfig() {
1263
-		return $this->query('AppConfig');
1264
-	}
1265
-
1266
-	/**
1267
-	 * @return \OCP\L10N\IFactory
1268
-	 */
1269
-	public function getL10NFactory() {
1270
-		return $this->query('L10NFactory');
1271
-	}
1272
-
1273
-	/**
1274
-	 * get an L10N instance
1275
-	 *
1276
-	 * @param string $app appid
1277
-	 * @param string $lang
1278
-	 * @return IL10N
1279
-	 */
1280
-	public function getL10N($app, $lang = null) {
1281
-		return $this->getL10NFactory()->get($app, $lang);
1282
-	}
1283
-
1284
-	/**
1285
-	 * @return \OCP\IURLGenerator
1286
-	 */
1287
-	public function getURLGenerator() {
1288
-		return $this->query('URLGenerator');
1289
-	}
1290
-
1291
-	/**
1292
-	 * @return \OCP\IHelper
1293
-	 */
1294
-	public function getHelper() {
1295
-		return $this->query('AppHelper');
1296
-	}
1297
-
1298
-	/**
1299
-	 * @return AppFetcher
1300
-	 */
1301
-	public function getAppFetcher() {
1302
-		return $this->query(AppFetcher::class);
1303
-	}
1304
-
1305
-	/**
1306
-	 * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
1307
-	 * getMemCacheFactory() instead.
1308
-	 *
1309
-	 * @return \OCP\ICache
1310
-	 * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
1311
-	 */
1312
-	public function getCache() {
1313
-		return $this->query('UserCache');
1314
-	}
1315
-
1316
-	/**
1317
-	 * Returns an \OCP\CacheFactory instance
1318
-	 *
1319
-	 * @return \OCP\ICacheFactory
1320
-	 */
1321
-	public function getMemCacheFactory() {
1322
-		return $this->query('MemCacheFactory');
1323
-	}
1324
-
1325
-	/**
1326
-	 * Returns an \OC\RedisFactory instance
1327
-	 *
1328
-	 * @return \OC\RedisFactory
1329
-	 */
1330
-	public function getGetRedisFactory() {
1331
-		return $this->query('RedisFactory');
1332
-	}
1333
-
1334
-
1335
-	/**
1336
-	 * Returns the current session
1337
-	 *
1338
-	 * @return \OCP\IDBConnection
1339
-	 */
1340
-	public function getDatabaseConnection() {
1341
-		return $this->query('DatabaseConnection');
1342
-	}
1343
-
1344
-	/**
1345
-	 * Returns the activity manager
1346
-	 *
1347
-	 * @return \OCP\Activity\IManager
1348
-	 */
1349
-	public function getActivityManager() {
1350
-		return $this->query('ActivityManager');
1351
-	}
1352
-
1353
-	/**
1354
-	 * Returns an job list for controlling background jobs
1355
-	 *
1356
-	 * @return \OCP\BackgroundJob\IJobList
1357
-	 */
1358
-	public function getJobList() {
1359
-		return $this->query('JobList');
1360
-	}
1361
-
1362
-	/**
1363
-	 * Returns a logger instance
1364
-	 *
1365
-	 * @return \OCP\ILogger
1366
-	 */
1367
-	public function getLogger() {
1368
-		return $this->query('Logger');
1369
-	}
1370
-
1371
-	/**
1372
-	 * Returns a router for generating and matching urls
1373
-	 *
1374
-	 * @return \OCP\Route\IRouter
1375
-	 */
1376
-	public function getRouter() {
1377
-		return $this->query('Router');
1378
-	}
1379
-
1380
-	/**
1381
-	 * Returns a search instance
1382
-	 *
1383
-	 * @return \OCP\ISearch
1384
-	 */
1385
-	public function getSearch() {
1386
-		return $this->query('Search');
1387
-	}
1388
-
1389
-	/**
1390
-	 * Returns a SecureRandom instance
1391
-	 *
1392
-	 * @return \OCP\Security\ISecureRandom
1393
-	 */
1394
-	public function getSecureRandom() {
1395
-		return $this->query('SecureRandom');
1396
-	}
1397
-
1398
-	/**
1399
-	 * Returns a Crypto instance
1400
-	 *
1401
-	 * @return \OCP\Security\ICrypto
1402
-	 */
1403
-	public function getCrypto() {
1404
-		return $this->query('Crypto');
1405
-	}
1406
-
1407
-	/**
1408
-	 * Returns a Hasher instance
1409
-	 *
1410
-	 * @return \OCP\Security\IHasher
1411
-	 */
1412
-	public function getHasher() {
1413
-		return $this->query('Hasher');
1414
-	}
1415
-
1416
-	/**
1417
-	 * Returns a CredentialsManager instance
1418
-	 *
1419
-	 * @return \OCP\Security\ICredentialsManager
1420
-	 */
1421
-	public function getCredentialsManager() {
1422
-		return $this->query('CredentialsManager');
1423
-	}
1424
-
1425
-	/**
1426
-	 * Returns an instance of the HTTP helper class
1427
-	 *
1428
-	 * @deprecated Use getHTTPClientService()
1429
-	 * @return \OC\HTTPHelper
1430
-	 */
1431
-	public function getHTTPHelper() {
1432
-		return $this->query('HTTPHelper');
1433
-	}
1434
-
1435
-	/**
1436
-	 * Get the certificate manager for the user
1437
-	 *
1438
-	 * @param string $userId (optional) if not specified the current loggedin user is used, use null to get the system certificate manager
1439
-	 * @return \OCP\ICertificateManager | null if $uid is null and no user is logged in
1440
-	 */
1441
-	public function getCertificateManager($userId = '') {
1442
-		if ($userId === '') {
1443
-			$userSession = $this->getUserSession();
1444
-			$user = $userSession->getUser();
1445
-			if (is_null($user)) {
1446
-				return null;
1447
-			}
1448
-			$userId = $user->getUID();
1449
-		}
1450
-		return new CertificateManager(
1451
-			$userId,
1452
-			new View(),
1453
-			$this->getConfig(),
1454
-			$this->getLogger(),
1455
-			$this->getSecureRandom()
1456
-		);
1457
-	}
1458
-
1459
-	/**
1460
-	 * Returns an instance of the HTTP client service
1461
-	 *
1462
-	 * @return \OCP\Http\Client\IClientService
1463
-	 */
1464
-	public function getHTTPClientService() {
1465
-		return $this->query('HttpClientService');
1466
-	}
1467
-
1468
-	/**
1469
-	 * Create a new event source
1470
-	 *
1471
-	 * @return \OCP\IEventSource
1472
-	 */
1473
-	public function createEventSource() {
1474
-		return new \OC_EventSource();
1475
-	}
1476
-
1477
-	/**
1478
-	 * Get the active event logger
1479
-	 *
1480
-	 * The returned logger only logs data when debug mode is enabled
1481
-	 *
1482
-	 * @return \OCP\Diagnostics\IEventLogger
1483
-	 */
1484
-	public function getEventLogger() {
1485
-		return $this->query('EventLogger');
1486
-	}
1487
-
1488
-	/**
1489
-	 * Get the active query logger
1490
-	 *
1491
-	 * The returned logger only logs data when debug mode is enabled
1492
-	 *
1493
-	 * @return \OCP\Diagnostics\IQueryLogger
1494
-	 */
1495
-	public function getQueryLogger() {
1496
-		return $this->query('QueryLogger');
1497
-	}
1498
-
1499
-	/**
1500
-	 * Get the manager for temporary files and folders
1501
-	 *
1502
-	 * @return \OCP\ITempManager
1503
-	 */
1504
-	public function getTempManager() {
1505
-		return $this->query('TempManager');
1506
-	}
1507
-
1508
-	/**
1509
-	 * Get the app manager
1510
-	 *
1511
-	 * @return \OCP\App\IAppManager
1512
-	 */
1513
-	public function getAppManager() {
1514
-		return $this->query('AppManager');
1515
-	}
1516
-
1517
-	/**
1518
-	 * Creates a new mailer
1519
-	 *
1520
-	 * @return \OCP\Mail\IMailer
1521
-	 */
1522
-	public function getMailer() {
1523
-		return $this->query('Mailer');
1524
-	}
1525
-
1526
-	/**
1527
-	 * Get the webroot
1528
-	 *
1529
-	 * @return string
1530
-	 */
1531
-	public function getWebRoot() {
1532
-		return $this->webRoot;
1533
-	}
1534
-
1535
-	/**
1536
-	 * @return \OC\OCSClient
1537
-	 */
1538
-	public function getOcsClient() {
1539
-		return $this->query('OcsClient');
1540
-	}
1541
-
1542
-	/**
1543
-	 * @return \OCP\IDateTimeZone
1544
-	 */
1545
-	public function getDateTimeZone() {
1546
-		return $this->query('DateTimeZone');
1547
-	}
1548
-
1549
-	/**
1550
-	 * @return \OCP\IDateTimeFormatter
1551
-	 */
1552
-	public function getDateTimeFormatter() {
1553
-		return $this->query('DateTimeFormatter');
1554
-	}
1555
-
1556
-	/**
1557
-	 * @return \OCP\Files\Config\IMountProviderCollection
1558
-	 */
1559
-	public function getMountProviderCollection() {
1560
-		return $this->query('MountConfigManager');
1561
-	}
1562
-
1563
-	/**
1564
-	 * Get the IniWrapper
1565
-	 *
1566
-	 * @return IniGetWrapper
1567
-	 */
1568
-	public function getIniWrapper() {
1569
-		return $this->query('IniWrapper');
1570
-	}
1571
-
1572
-	/**
1573
-	 * @return \OCP\Command\IBus
1574
-	 */
1575
-	public function getCommandBus() {
1576
-		return $this->query('AsyncCommandBus');
1577
-	}
1578
-
1579
-	/**
1580
-	 * Get the trusted domain helper
1581
-	 *
1582
-	 * @return TrustedDomainHelper
1583
-	 */
1584
-	public function getTrustedDomainHelper() {
1585
-		return $this->query('TrustedDomainHelper');
1586
-	}
1587
-
1588
-	/**
1589
-	 * Get the locking provider
1590
-	 *
1591
-	 * @return \OCP\Lock\ILockingProvider
1592
-	 * @since 8.1.0
1593
-	 */
1594
-	public function getLockingProvider() {
1595
-		return $this->query('LockingProvider');
1596
-	}
1597
-
1598
-	/**
1599
-	 * @return \OCP\Files\Mount\IMountManager
1600
-	 **/
1601
-	function getMountManager() {
1602
-		return $this->query('MountManager');
1603
-	}
1604
-
1605
-	/** @return \OCP\Files\Config\IUserMountCache */
1606
-	function getUserMountCache() {
1607
-		return $this->query('UserMountCache');
1608
-	}
1609
-
1610
-	/**
1611
-	 * Get the MimeTypeDetector
1612
-	 *
1613
-	 * @return \OCP\Files\IMimeTypeDetector
1614
-	 */
1615
-	public function getMimeTypeDetector() {
1616
-		return $this->query('MimeTypeDetector');
1617
-	}
1618
-
1619
-	/**
1620
-	 * Get the MimeTypeLoader
1621
-	 *
1622
-	 * @return \OCP\Files\IMimeTypeLoader
1623
-	 */
1624
-	public function getMimeTypeLoader() {
1625
-		return $this->query('MimeTypeLoader');
1626
-	}
1627
-
1628
-	/**
1629
-	 * Get the manager of all the capabilities
1630
-	 *
1631
-	 * @return \OC\CapabilitiesManager
1632
-	 */
1633
-	public function getCapabilitiesManager() {
1634
-		return $this->query('CapabilitiesManager');
1635
-	}
1636
-
1637
-	/**
1638
-	 * Get the EventDispatcher
1639
-	 *
1640
-	 * @return EventDispatcherInterface
1641
-	 * @since 8.2.0
1642
-	 */
1643
-	public function getEventDispatcher() {
1644
-		return $this->query('EventDispatcher');
1645
-	}
1646
-
1647
-	/**
1648
-	 * Get the Notification Manager
1649
-	 *
1650
-	 * @return \OCP\Notification\IManager
1651
-	 * @since 8.2.0
1652
-	 */
1653
-	public function getNotificationManager() {
1654
-		return $this->query('NotificationManager');
1655
-	}
1656
-
1657
-	/**
1658
-	 * @return \OCP\Comments\ICommentsManager
1659
-	 */
1660
-	public function getCommentsManager() {
1661
-		return $this->query('CommentsManager');
1662
-	}
1663
-
1664
-	/**
1665
-	 * @return \OCA\Theming\ThemingDefaults
1666
-	 */
1667
-	public function getThemingDefaults() {
1668
-		return $this->query('ThemingDefaults');
1669
-	}
1670
-
1671
-	/**
1672
-	 * @return \OC\IntegrityCheck\Checker
1673
-	 */
1674
-	public function getIntegrityCodeChecker() {
1675
-		return $this->query('IntegrityCodeChecker');
1676
-	}
1677
-
1678
-	/**
1679
-	 * @return \OC\Session\CryptoWrapper
1680
-	 */
1681
-	public function getSessionCryptoWrapper() {
1682
-		return $this->query('CryptoWrapper');
1683
-	}
1684
-
1685
-	/**
1686
-	 * @return CsrfTokenManager
1687
-	 */
1688
-	public function getCsrfTokenManager() {
1689
-		return $this->query('CsrfTokenManager');
1690
-	}
1691
-
1692
-	/**
1693
-	 * @return Throttler
1694
-	 */
1695
-	public function getBruteForceThrottler() {
1696
-		return $this->query('Throttler');
1697
-	}
1698
-
1699
-	/**
1700
-	 * @return IContentSecurityPolicyManager
1701
-	 */
1702
-	public function getContentSecurityPolicyManager() {
1703
-		return $this->query('ContentSecurityPolicyManager');
1704
-	}
1705
-
1706
-	/**
1707
-	 * @return ContentSecurityPolicyNonceManager
1708
-	 */
1709
-	public function getContentSecurityPolicyNonceManager() {
1710
-		return $this->query('ContentSecurityPolicyNonceManager');
1711
-	}
1712
-
1713
-	/**
1714
-	 * Not a public API as of 8.2, wait for 9.0
1715
-	 *
1716
-	 * @return \OCA\Files_External\Service\BackendService
1717
-	 */
1718
-	public function getStoragesBackendService() {
1719
-		return $this->query('OCA\\Files_External\\Service\\BackendService');
1720
-	}
1721
-
1722
-	/**
1723
-	 * Not a public API as of 8.2, wait for 9.0
1724
-	 *
1725
-	 * @return \OCA\Files_External\Service\GlobalStoragesService
1726
-	 */
1727
-	public function getGlobalStoragesService() {
1728
-		return $this->query('OCA\\Files_External\\Service\\GlobalStoragesService');
1729
-	}
1730
-
1731
-	/**
1732
-	 * Not a public API as of 8.2, wait for 9.0
1733
-	 *
1734
-	 * @return \OCA\Files_External\Service\UserGlobalStoragesService
1735
-	 */
1736
-	public function getUserGlobalStoragesService() {
1737
-		return $this->query('OCA\\Files_External\\Service\\UserGlobalStoragesService');
1738
-	}
1739
-
1740
-	/**
1741
-	 * Not a public API as of 8.2, wait for 9.0
1742
-	 *
1743
-	 * @return \OCA\Files_External\Service\UserStoragesService
1744
-	 */
1745
-	public function getUserStoragesService() {
1746
-		return $this->query('OCA\\Files_External\\Service\\UserStoragesService');
1747
-	}
1748
-
1749
-	/**
1750
-	 * @return \OCP\Share\IManager
1751
-	 */
1752
-	public function getShareManager() {
1753
-		return $this->query('ShareManager');
1754
-	}
1755
-
1756
-	/**
1757
-	 * Returns the LDAP Provider
1758
-	 *
1759
-	 * @return \OCP\LDAP\ILDAPProvider
1760
-	 */
1761
-	public function getLDAPProvider() {
1762
-		return $this->query('LDAPProvider');
1763
-	}
1764
-
1765
-	/**
1766
-	 * @return \OCP\Settings\IManager
1767
-	 */
1768
-	public function getSettingsManager() {
1769
-		return $this->query('SettingsManager');
1770
-	}
1771
-
1772
-	/**
1773
-	 * @return \OCP\Files\IAppData
1774
-	 */
1775
-	public function getAppDataDir($app) {
1776
-		/** @var \OC\Files\AppData\Factory $factory */
1777
-		$factory = $this->query(\OC\Files\AppData\Factory::class);
1778
-		return $factory->get($app);
1779
-	}
1780
-
1781
-	/**
1782
-	 * @return \OCP\Lockdown\ILockdownManager
1783
-	 */
1784
-	public function getLockdownManager() {
1785
-		return $this->query('LockdownManager');
1786
-	}
1787
-
1788
-	/**
1789
-	 * @return \OCP\Federation\ICloudIdManager
1790
-	 */
1791
-	public function getCloudIdManager() {
1792
-		return $this->query(ICloudIdManager::class);
1793
-	}
862
+            $prefixes = \OC::$composerAutoloader->getPrefixesPsr4();
863
+            if (isset($prefixes['OCA\\Theming\\'])) {
864
+                $classExists = true;
865
+            } else {
866
+                $classExists = false;
867
+            }
868
+
869
+            if ($classExists && $c->getConfig()->getSystemValue('installed', false) && $c->getAppManager()->isInstalled('theming') && $c->getTrustedDomainHelper()->isTrustedDomain($c->getRequest()->getInsecureServerHost())) {
870
+                return new ThemingDefaults(
871
+                    $c->getConfig(),
872
+                    $c->getL10N('theming'),
873
+                    $c->getURLGenerator(),
874
+                    $c->getAppDataDir('theming'),
875
+                    $c->getMemCacheFactory(),
876
+                    new Util($c->getConfig(), $this->getAppManager(), $this->getAppDataDir('theming'))
877
+                );
878
+            }
879
+            return new \OC_Defaults();
880
+        });
881
+        $this->registerService(SCSSCacher::class, function(Server $c) {
882
+            /** @var Factory $cacheFactory */
883
+            $cacheFactory = $c->query(Factory::class);
884
+            return new SCSSCacher(
885
+                $c->getLogger(),
886
+                $c->query(\OC\Files\AppData\Factory::class),
887
+                $c->getURLGenerator(),
888
+                $c->getConfig(),
889
+                $c->getThemingDefaults(),
890
+                \OC::$SERVERROOT,
891
+                $cacheFactory->create('SCSS')
892
+            );
893
+        });
894
+        $this->registerService(EventDispatcher::class, function () {
895
+            return new EventDispatcher();
896
+        });
897
+        $this->registerAlias('EventDispatcher', EventDispatcher::class);
898
+        $this->registerAlias(EventDispatcherInterface::class, EventDispatcher::class);
899
+
900
+        $this->registerService('CryptoWrapper', function (Server $c) {
901
+            // FIXME: Instantiiated here due to cyclic dependency
902
+            $request = new Request(
903
+                [
904
+                    'get' => $_GET,
905
+                    'post' => $_POST,
906
+                    'files' => $_FILES,
907
+                    'server' => $_SERVER,
908
+                    'env' => $_ENV,
909
+                    'cookies' => $_COOKIE,
910
+                    'method' => (isset($_SERVER) && isset($_SERVER['REQUEST_METHOD']))
911
+                        ? $_SERVER['REQUEST_METHOD']
912
+                        : null,
913
+                ],
914
+                $c->getSecureRandom(),
915
+                $c->getConfig()
916
+            );
917
+
918
+            return new CryptoWrapper(
919
+                $c->getConfig(),
920
+                $c->getCrypto(),
921
+                $c->getSecureRandom(),
922
+                $request
923
+            );
924
+        });
925
+        $this->registerService('CsrfTokenManager', function (Server $c) {
926
+            $tokenGenerator = new CsrfTokenGenerator($c->getSecureRandom());
927
+
928
+            return new CsrfTokenManager(
929
+                $tokenGenerator,
930
+                $c->query(SessionStorage::class)
931
+            );
932
+        });
933
+        $this->registerService(SessionStorage::class, function (Server $c) {
934
+            return new SessionStorage($c->getSession());
935
+        });
936
+        $this->registerService(\OCP\Security\IContentSecurityPolicyManager::class, function (Server $c) {
937
+            return new ContentSecurityPolicyManager();
938
+        });
939
+        $this->registerAlias('ContentSecurityPolicyManager', \OCP\Security\IContentSecurityPolicyManager::class);
940
+
941
+        $this->registerService('ContentSecurityPolicyNonceManager', function(Server $c) {
942
+            return new ContentSecurityPolicyNonceManager(
943
+                $c->getCsrfTokenManager(),
944
+                $c->getRequest()
945
+            );
946
+        });
947
+
948
+        $this->registerService(\OCP\Share\IManager::class, function(Server $c) {
949
+            $config = $c->getConfig();
950
+            $factoryClass = $config->getSystemValue('sharing.managerFactory', '\OC\Share20\ProviderFactory');
951
+            /** @var \OCP\Share\IProviderFactory $factory */
952
+            $factory = new $factoryClass($this);
953
+
954
+            $manager = new \OC\Share20\Manager(
955
+                $c->getLogger(),
956
+                $c->getConfig(),
957
+                $c->getSecureRandom(),
958
+                $c->getHasher(),
959
+                $c->getMountManager(),
960
+                $c->getGroupManager(),
961
+                $c->getL10N('core'),
962
+                $factory,
963
+                $c->getUserManager(),
964
+                $c->getLazyRootFolder(),
965
+                $c->getEventDispatcher()
966
+            );
967
+
968
+            return $manager;
969
+        });
970
+        $this->registerAlias('ShareManager', \OCP\Share\IManager::class);
971
+
972
+        $this->registerService('SettingsManager', function(Server $c) {
973
+            $manager = new \OC\Settings\Manager(
974
+                $c->getLogger(),
975
+                $c->getDatabaseConnection(),
976
+                $c->getL10N('lib'),
977
+                $c->getConfig(),
978
+                $c->getEncryptionManager(),
979
+                $c->getUserManager(),
980
+                $c->getLockingProvider(),
981
+                $c->getRequest(),
982
+                new \OC\Settings\Mapper($c->getDatabaseConnection()),
983
+                $c->getURLGenerator(),
984
+                $c->query(AccountManager::class),
985
+                $c->getGroupManager(),
986
+                $c->getL10NFactory(),
987
+                $c->getThemingDefaults(),
988
+                $c->getAppManager()
989
+            );
990
+            return $manager;
991
+        });
992
+        $this->registerService(\OC\Files\AppData\Factory::class, function (Server $c) {
993
+            return new \OC\Files\AppData\Factory(
994
+                $c->getRootFolder(),
995
+                $c->getSystemConfig()
996
+            );
997
+        });
998
+
999
+        $this->registerService('LockdownManager', function (Server $c) {
1000
+            return new LockdownManager(function() use ($c) {
1001
+                return $c->getSession();
1002
+            });
1003
+        });
1004
+
1005
+        $this->registerService(\OCP\OCS\IDiscoveryService::class, function (Server $c) {
1006
+            return new DiscoveryService($c->getMemCacheFactory(), $c->getHTTPClientService());
1007
+        });
1008
+
1009
+        $this->registerService(ICloudIdManager::class, function (Server $c) {
1010
+            return new CloudIdManager();
1011
+        });
1012
+
1013
+        /* To trick DI since we don't extend the DIContainer here */
1014
+        $this->registerService(CleanPreviewsBackgroundJob::class, function (Server $c) {
1015
+            return new CleanPreviewsBackgroundJob(
1016
+                $c->getRootFolder(),
1017
+                $c->getLogger(),
1018
+                $c->getJobList(),
1019
+                new TimeFactory()
1020
+            );
1021
+        });
1022
+
1023
+        $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class);
1024
+        $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class);
1025
+
1026
+        $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class);
1027
+        $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class);
1028
+
1029
+        $this->registerService(Defaults::class, function (Server $c) {
1030
+            return new Defaults(
1031
+                $c->getThemingDefaults()
1032
+            );
1033
+        });
1034
+        $this->registerAlias('Defaults', \OCP\Defaults::class);
1035
+
1036
+        $this->registerService(\OCP\ISession::class, function(SimpleContainer $c) {
1037
+            return $c->query(\OCP\IUserSession::class)->getSession();
1038
+        });
1039
+
1040
+        $this->registerService(IShareHelper::class, function(Server $c) {
1041
+            return new ShareHelper(
1042
+                $c->query(\OCP\Share\IManager::class)
1043
+            );
1044
+        });
1045
+    }
1046
+
1047
+    /**
1048
+     * @return \OCP\Contacts\IManager
1049
+     */
1050
+    public function getContactsManager() {
1051
+        return $this->query('ContactsManager');
1052
+    }
1053
+
1054
+    /**
1055
+     * @return \OC\Encryption\Manager
1056
+     */
1057
+    public function getEncryptionManager() {
1058
+        return $this->query('EncryptionManager');
1059
+    }
1060
+
1061
+    /**
1062
+     * @return \OC\Encryption\File
1063
+     */
1064
+    public function getEncryptionFilesHelper() {
1065
+        return $this->query('EncryptionFileHelper');
1066
+    }
1067
+
1068
+    /**
1069
+     * @return \OCP\Encryption\Keys\IStorage
1070
+     */
1071
+    public function getEncryptionKeyStorage() {
1072
+        return $this->query('EncryptionKeyStorage');
1073
+    }
1074
+
1075
+    /**
1076
+     * The current request object holding all information about the request
1077
+     * currently being processed is returned from this method.
1078
+     * In case the current execution was not initiated by a web request null is returned
1079
+     *
1080
+     * @return \OCP\IRequest
1081
+     */
1082
+    public function getRequest() {
1083
+        return $this->query('Request');
1084
+    }
1085
+
1086
+    /**
1087
+     * Returns the preview manager which can create preview images for a given file
1088
+     *
1089
+     * @return \OCP\IPreview
1090
+     */
1091
+    public function getPreviewManager() {
1092
+        return $this->query('PreviewManager');
1093
+    }
1094
+
1095
+    /**
1096
+     * Returns the tag manager which can get and set tags for different object types
1097
+     *
1098
+     * @see \OCP\ITagManager::load()
1099
+     * @return \OCP\ITagManager
1100
+     */
1101
+    public function getTagManager() {
1102
+        return $this->query('TagManager');
1103
+    }
1104
+
1105
+    /**
1106
+     * Returns the system-tag manager
1107
+     *
1108
+     * @return \OCP\SystemTag\ISystemTagManager
1109
+     *
1110
+     * @since 9.0.0
1111
+     */
1112
+    public function getSystemTagManager() {
1113
+        return $this->query('SystemTagManager');
1114
+    }
1115
+
1116
+    /**
1117
+     * Returns the system-tag object mapper
1118
+     *
1119
+     * @return \OCP\SystemTag\ISystemTagObjectMapper
1120
+     *
1121
+     * @since 9.0.0
1122
+     */
1123
+    public function getSystemTagObjectMapper() {
1124
+        return $this->query('SystemTagObjectMapper');
1125
+    }
1126
+
1127
+    /**
1128
+     * Returns the avatar manager, used for avatar functionality
1129
+     *
1130
+     * @return \OCP\IAvatarManager
1131
+     */
1132
+    public function getAvatarManager() {
1133
+        return $this->query('AvatarManager');
1134
+    }
1135
+
1136
+    /**
1137
+     * Returns the root folder of ownCloud's data directory
1138
+     *
1139
+     * @return \OCP\Files\IRootFolder
1140
+     */
1141
+    public function getRootFolder() {
1142
+        return $this->query('LazyRootFolder');
1143
+    }
1144
+
1145
+    /**
1146
+     * Returns the root folder of ownCloud's data directory
1147
+     * This is the lazy variant so this gets only initialized once it
1148
+     * is actually used.
1149
+     *
1150
+     * @return \OCP\Files\IRootFolder
1151
+     */
1152
+    public function getLazyRootFolder() {
1153
+        return $this->query('LazyRootFolder');
1154
+    }
1155
+
1156
+    /**
1157
+     * Returns a view to ownCloud's files folder
1158
+     *
1159
+     * @param string $userId user ID
1160
+     * @return \OCP\Files\Folder|null
1161
+     */
1162
+    public function getUserFolder($userId = null) {
1163
+        if ($userId === null) {
1164
+            $user = $this->getUserSession()->getUser();
1165
+            if (!$user) {
1166
+                return null;
1167
+            }
1168
+            $userId = $user->getUID();
1169
+        }
1170
+        $root = $this->getRootFolder();
1171
+        return $root->getUserFolder($userId);
1172
+    }
1173
+
1174
+    /**
1175
+     * Returns an app-specific view in ownClouds data directory
1176
+     *
1177
+     * @return \OCP\Files\Folder
1178
+     * @deprecated since 9.2.0 use IAppData
1179
+     */
1180
+    public function getAppFolder() {
1181
+        $dir = '/' . \OC_App::getCurrentApp();
1182
+        $root = $this->getRootFolder();
1183
+        if (!$root->nodeExists($dir)) {
1184
+            $folder = $root->newFolder($dir);
1185
+        } else {
1186
+            $folder = $root->get($dir);
1187
+        }
1188
+        return $folder;
1189
+    }
1190
+
1191
+    /**
1192
+     * @return \OC\User\Manager
1193
+     */
1194
+    public function getUserManager() {
1195
+        return $this->query('UserManager');
1196
+    }
1197
+
1198
+    /**
1199
+     * @return \OC\Group\Manager
1200
+     */
1201
+    public function getGroupManager() {
1202
+        return $this->query('GroupManager');
1203
+    }
1204
+
1205
+    /**
1206
+     * @return \OC\User\Session
1207
+     */
1208
+    public function getUserSession() {
1209
+        return $this->query('UserSession');
1210
+    }
1211
+
1212
+    /**
1213
+     * @return \OCP\ISession
1214
+     */
1215
+    public function getSession() {
1216
+        return $this->query('UserSession')->getSession();
1217
+    }
1218
+
1219
+    /**
1220
+     * @param \OCP\ISession $session
1221
+     */
1222
+    public function setSession(\OCP\ISession $session) {
1223
+        $this->query(SessionStorage::class)->setSession($session);
1224
+        $this->query('UserSession')->setSession($session);
1225
+        $this->query(Store::class)->setSession($session);
1226
+    }
1227
+
1228
+    /**
1229
+     * @return \OC\Authentication\TwoFactorAuth\Manager
1230
+     */
1231
+    public function getTwoFactorAuthManager() {
1232
+        return $this->query('\OC\Authentication\TwoFactorAuth\Manager');
1233
+    }
1234
+
1235
+    /**
1236
+     * @return \OC\NavigationManager
1237
+     */
1238
+    public function getNavigationManager() {
1239
+        return $this->query('NavigationManager');
1240
+    }
1241
+
1242
+    /**
1243
+     * @return \OCP\IConfig
1244
+     */
1245
+    public function getConfig() {
1246
+        return $this->query('AllConfig');
1247
+    }
1248
+
1249
+    /**
1250
+     * @internal For internal use only
1251
+     * @return \OC\SystemConfig
1252
+     */
1253
+    public function getSystemConfig() {
1254
+        return $this->query('SystemConfig');
1255
+    }
1256
+
1257
+    /**
1258
+     * Returns the app config manager
1259
+     *
1260
+     * @return \OCP\IAppConfig
1261
+     */
1262
+    public function getAppConfig() {
1263
+        return $this->query('AppConfig');
1264
+    }
1265
+
1266
+    /**
1267
+     * @return \OCP\L10N\IFactory
1268
+     */
1269
+    public function getL10NFactory() {
1270
+        return $this->query('L10NFactory');
1271
+    }
1272
+
1273
+    /**
1274
+     * get an L10N instance
1275
+     *
1276
+     * @param string $app appid
1277
+     * @param string $lang
1278
+     * @return IL10N
1279
+     */
1280
+    public function getL10N($app, $lang = null) {
1281
+        return $this->getL10NFactory()->get($app, $lang);
1282
+    }
1283
+
1284
+    /**
1285
+     * @return \OCP\IURLGenerator
1286
+     */
1287
+    public function getURLGenerator() {
1288
+        return $this->query('URLGenerator');
1289
+    }
1290
+
1291
+    /**
1292
+     * @return \OCP\IHelper
1293
+     */
1294
+    public function getHelper() {
1295
+        return $this->query('AppHelper');
1296
+    }
1297
+
1298
+    /**
1299
+     * @return AppFetcher
1300
+     */
1301
+    public function getAppFetcher() {
1302
+        return $this->query(AppFetcher::class);
1303
+    }
1304
+
1305
+    /**
1306
+     * Returns an ICache instance. Since 8.1.0 it returns a fake cache. Use
1307
+     * getMemCacheFactory() instead.
1308
+     *
1309
+     * @return \OCP\ICache
1310
+     * @deprecated 8.1.0 use getMemCacheFactory to obtain a proper cache
1311
+     */
1312
+    public function getCache() {
1313
+        return $this->query('UserCache');
1314
+    }
1315
+
1316
+    /**
1317
+     * Returns an \OCP\CacheFactory instance
1318
+     *
1319
+     * @return \OCP\ICacheFactory
1320
+     */
1321
+    public function getMemCacheFactory() {
1322
+        return $this->query('MemCacheFactory');
1323
+    }
1324
+
1325
+    /**
1326
+     * Returns an \OC\RedisFactory instance
1327
+     *
1328
+     * @return \OC\RedisFactory
1329
+     */
1330
+    public function getGetRedisFactory() {
1331
+        return $this->query('RedisFactory');
1332
+    }
1333
+
1334
+
1335
+    /**
1336
+     * Returns the current session
1337
+     *
1338
+     * @return \OCP\IDBConnection
1339
+     */
1340
+    public function getDatabaseConnection() {
1341
+        return $this->query('DatabaseConnection');
1342
+    }
1343
+
1344
+    /**
1345
+     * Returns the activity manager
1346
+     *
1347
+     * @return \OCP\Activity\IManager
1348
+     */
1349
+    public function getActivityManager() {
1350
+        return $this->query('ActivityManager');
1351
+    }
1352
+
1353
+    /**
1354
+     * Returns an job list for controlling background jobs
1355
+     *
1356
+     * @return \OCP\BackgroundJob\IJobList
1357
+     */
1358
+    public function getJobList() {
1359
+        return $this->query('JobList');
1360
+    }
1361
+
1362
+    /**
1363
+     * Returns a logger instance
1364
+     *
1365
+     * @return \OCP\ILogger
1366
+     */
1367
+    public function getLogger() {
1368
+        return $this->query('Logger');
1369
+    }
1370
+
1371
+    /**
1372
+     * Returns a router for generating and matching urls
1373
+     *
1374
+     * @return \OCP\Route\IRouter
1375
+     */
1376
+    public function getRouter() {
1377
+        return $this->query('Router');
1378
+    }
1379
+
1380
+    /**
1381
+     * Returns a search instance
1382
+     *
1383
+     * @return \OCP\ISearch
1384
+     */
1385
+    public function getSearch() {
1386
+        return $this->query('Search');
1387
+    }
1388
+
1389
+    /**
1390
+     * Returns a SecureRandom instance
1391
+     *
1392
+     * @return \OCP\Security\ISecureRandom
1393
+     */
1394
+    public function getSecureRandom() {
1395
+        return $this->query('SecureRandom');
1396
+    }
1397
+
1398
+    /**
1399
+     * Returns a Crypto instance
1400
+     *
1401
+     * @return \OCP\Security\ICrypto
1402
+     */
1403
+    public function getCrypto() {
1404
+        return $this->query('Crypto');
1405
+    }
1406
+
1407
+    /**
1408
+     * Returns a Hasher instance
1409
+     *
1410
+     * @return \OCP\Security\IHasher
1411
+     */
1412
+    public function getHasher() {
1413
+        return $this->query('Hasher');
1414
+    }
1415
+
1416
+    /**
1417
+     * Returns a CredentialsManager instance
1418
+     *
1419
+     * @return \OCP\Security\ICredentialsManager
1420
+     */
1421
+    public function getCredentialsManager() {
1422
+        return $this->query('CredentialsManager');
1423
+    }
1424
+
1425
+    /**
1426
+     * Returns an instance of the HTTP helper class
1427
+     *
1428
+     * @deprecated Use getHTTPClientService()
1429
+     * @return \OC\HTTPHelper
1430
+     */
1431
+    public function getHTTPHelper() {
1432
+        return $this->query('HTTPHelper');
1433
+    }
1434
+
1435
+    /**
1436
+     * Get the certificate manager for the user
1437
+     *
1438
+     * @param string $userId (optional) if not specified the current loggedin user is used, use null to get the system certificate manager
1439
+     * @return \OCP\ICertificateManager | null if $uid is null and no user is logged in
1440
+     */
1441
+    public function getCertificateManager($userId = '') {
1442
+        if ($userId === '') {
1443
+            $userSession = $this->getUserSession();
1444
+            $user = $userSession->getUser();
1445
+            if (is_null($user)) {
1446
+                return null;
1447
+            }
1448
+            $userId = $user->getUID();
1449
+        }
1450
+        return new CertificateManager(
1451
+            $userId,
1452
+            new View(),
1453
+            $this->getConfig(),
1454
+            $this->getLogger(),
1455
+            $this->getSecureRandom()
1456
+        );
1457
+    }
1458
+
1459
+    /**
1460
+     * Returns an instance of the HTTP client service
1461
+     *
1462
+     * @return \OCP\Http\Client\IClientService
1463
+     */
1464
+    public function getHTTPClientService() {
1465
+        return $this->query('HttpClientService');
1466
+    }
1467
+
1468
+    /**
1469
+     * Create a new event source
1470
+     *
1471
+     * @return \OCP\IEventSource
1472
+     */
1473
+    public function createEventSource() {
1474
+        return new \OC_EventSource();
1475
+    }
1476
+
1477
+    /**
1478
+     * Get the active event logger
1479
+     *
1480
+     * The returned logger only logs data when debug mode is enabled
1481
+     *
1482
+     * @return \OCP\Diagnostics\IEventLogger
1483
+     */
1484
+    public function getEventLogger() {
1485
+        return $this->query('EventLogger');
1486
+    }
1487
+
1488
+    /**
1489
+     * Get the active query logger
1490
+     *
1491
+     * The returned logger only logs data when debug mode is enabled
1492
+     *
1493
+     * @return \OCP\Diagnostics\IQueryLogger
1494
+     */
1495
+    public function getQueryLogger() {
1496
+        return $this->query('QueryLogger');
1497
+    }
1498
+
1499
+    /**
1500
+     * Get the manager for temporary files and folders
1501
+     *
1502
+     * @return \OCP\ITempManager
1503
+     */
1504
+    public function getTempManager() {
1505
+        return $this->query('TempManager');
1506
+    }
1507
+
1508
+    /**
1509
+     * Get the app manager
1510
+     *
1511
+     * @return \OCP\App\IAppManager
1512
+     */
1513
+    public function getAppManager() {
1514
+        return $this->query('AppManager');
1515
+    }
1516
+
1517
+    /**
1518
+     * Creates a new mailer
1519
+     *
1520
+     * @return \OCP\Mail\IMailer
1521
+     */
1522
+    public function getMailer() {
1523
+        return $this->query('Mailer');
1524
+    }
1525
+
1526
+    /**
1527
+     * Get the webroot
1528
+     *
1529
+     * @return string
1530
+     */
1531
+    public function getWebRoot() {
1532
+        return $this->webRoot;
1533
+    }
1534
+
1535
+    /**
1536
+     * @return \OC\OCSClient
1537
+     */
1538
+    public function getOcsClient() {
1539
+        return $this->query('OcsClient');
1540
+    }
1541
+
1542
+    /**
1543
+     * @return \OCP\IDateTimeZone
1544
+     */
1545
+    public function getDateTimeZone() {
1546
+        return $this->query('DateTimeZone');
1547
+    }
1548
+
1549
+    /**
1550
+     * @return \OCP\IDateTimeFormatter
1551
+     */
1552
+    public function getDateTimeFormatter() {
1553
+        return $this->query('DateTimeFormatter');
1554
+    }
1555
+
1556
+    /**
1557
+     * @return \OCP\Files\Config\IMountProviderCollection
1558
+     */
1559
+    public function getMountProviderCollection() {
1560
+        return $this->query('MountConfigManager');
1561
+    }
1562
+
1563
+    /**
1564
+     * Get the IniWrapper
1565
+     *
1566
+     * @return IniGetWrapper
1567
+     */
1568
+    public function getIniWrapper() {
1569
+        return $this->query('IniWrapper');
1570
+    }
1571
+
1572
+    /**
1573
+     * @return \OCP\Command\IBus
1574
+     */
1575
+    public function getCommandBus() {
1576
+        return $this->query('AsyncCommandBus');
1577
+    }
1578
+
1579
+    /**
1580
+     * Get the trusted domain helper
1581
+     *
1582
+     * @return TrustedDomainHelper
1583
+     */
1584
+    public function getTrustedDomainHelper() {
1585
+        return $this->query('TrustedDomainHelper');
1586
+    }
1587
+
1588
+    /**
1589
+     * Get the locking provider
1590
+     *
1591
+     * @return \OCP\Lock\ILockingProvider
1592
+     * @since 8.1.0
1593
+     */
1594
+    public function getLockingProvider() {
1595
+        return $this->query('LockingProvider');
1596
+    }
1597
+
1598
+    /**
1599
+     * @return \OCP\Files\Mount\IMountManager
1600
+     **/
1601
+    function getMountManager() {
1602
+        return $this->query('MountManager');
1603
+    }
1604
+
1605
+    /** @return \OCP\Files\Config\IUserMountCache */
1606
+    function getUserMountCache() {
1607
+        return $this->query('UserMountCache');
1608
+    }
1609
+
1610
+    /**
1611
+     * Get the MimeTypeDetector
1612
+     *
1613
+     * @return \OCP\Files\IMimeTypeDetector
1614
+     */
1615
+    public function getMimeTypeDetector() {
1616
+        return $this->query('MimeTypeDetector');
1617
+    }
1618
+
1619
+    /**
1620
+     * Get the MimeTypeLoader
1621
+     *
1622
+     * @return \OCP\Files\IMimeTypeLoader
1623
+     */
1624
+    public function getMimeTypeLoader() {
1625
+        return $this->query('MimeTypeLoader');
1626
+    }
1627
+
1628
+    /**
1629
+     * Get the manager of all the capabilities
1630
+     *
1631
+     * @return \OC\CapabilitiesManager
1632
+     */
1633
+    public function getCapabilitiesManager() {
1634
+        return $this->query('CapabilitiesManager');
1635
+    }
1636
+
1637
+    /**
1638
+     * Get the EventDispatcher
1639
+     *
1640
+     * @return EventDispatcherInterface
1641
+     * @since 8.2.0
1642
+     */
1643
+    public function getEventDispatcher() {
1644
+        return $this->query('EventDispatcher');
1645
+    }
1646
+
1647
+    /**
1648
+     * Get the Notification Manager
1649
+     *
1650
+     * @return \OCP\Notification\IManager
1651
+     * @since 8.2.0
1652
+     */
1653
+    public function getNotificationManager() {
1654
+        return $this->query('NotificationManager');
1655
+    }
1656
+
1657
+    /**
1658
+     * @return \OCP\Comments\ICommentsManager
1659
+     */
1660
+    public function getCommentsManager() {
1661
+        return $this->query('CommentsManager');
1662
+    }
1663
+
1664
+    /**
1665
+     * @return \OCA\Theming\ThemingDefaults
1666
+     */
1667
+    public function getThemingDefaults() {
1668
+        return $this->query('ThemingDefaults');
1669
+    }
1670
+
1671
+    /**
1672
+     * @return \OC\IntegrityCheck\Checker
1673
+     */
1674
+    public function getIntegrityCodeChecker() {
1675
+        return $this->query('IntegrityCodeChecker');
1676
+    }
1677
+
1678
+    /**
1679
+     * @return \OC\Session\CryptoWrapper
1680
+     */
1681
+    public function getSessionCryptoWrapper() {
1682
+        return $this->query('CryptoWrapper');
1683
+    }
1684
+
1685
+    /**
1686
+     * @return CsrfTokenManager
1687
+     */
1688
+    public function getCsrfTokenManager() {
1689
+        return $this->query('CsrfTokenManager');
1690
+    }
1691
+
1692
+    /**
1693
+     * @return Throttler
1694
+     */
1695
+    public function getBruteForceThrottler() {
1696
+        return $this->query('Throttler');
1697
+    }
1698
+
1699
+    /**
1700
+     * @return IContentSecurityPolicyManager
1701
+     */
1702
+    public function getContentSecurityPolicyManager() {
1703
+        return $this->query('ContentSecurityPolicyManager');
1704
+    }
1705
+
1706
+    /**
1707
+     * @return ContentSecurityPolicyNonceManager
1708
+     */
1709
+    public function getContentSecurityPolicyNonceManager() {
1710
+        return $this->query('ContentSecurityPolicyNonceManager');
1711
+    }
1712
+
1713
+    /**
1714
+     * Not a public API as of 8.2, wait for 9.0
1715
+     *
1716
+     * @return \OCA\Files_External\Service\BackendService
1717
+     */
1718
+    public function getStoragesBackendService() {
1719
+        return $this->query('OCA\\Files_External\\Service\\BackendService');
1720
+    }
1721
+
1722
+    /**
1723
+     * Not a public API as of 8.2, wait for 9.0
1724
+     *
1725
+     * @return \OCA\Files_External\Service\GlobalStoragesService
1726
+     */
1727
+    public function getGlobalStoragesService() {
1728
+        return $this->query('OCA\\Files_External\\Service\\GlobalStoragesService');
1729
+    }
1730
+
1731
+    /**
1732
+     * Not a public API as of 8.2, wait for 9.0
1733
+     *
1734
+     * @return \OCA\Files_External\Service\UserGlobalStoragesService
1735
+     */
1736
+    public function getUserGlobalStoragesService() {
1737
+        return $this->query('OCA\\Files_External\\Service\\UserGlobalStoragesService');
1738
+    }
1739
+
1740
+    /**
1741
+     * Not a public API as of 8.2, wait for 9.0
1742
+     *
1743
+     * @return \OCA\Files_External\Service\UserStoragesService
1744
+     */
1745
+    public function getUserStoragesService() {
1746
+        return $this->query('OCA\\Files_External\\Service\\UserStoragesService');
1747
+    }
1748
+
1749
+    /**
1750
+     * @return \OCP\Share\IManager
1751
+     */
1752
+    public function getShareManager() {
1753
+        return $this->query('ShareManager');
1754
+    }
1755
+
1756
+    /**
1757
+     * Returns the LDAP Provider
1758
+     *
1759
+     * @return \OCP\LDAP\ILDAPProvider
1760
+     */
1761
+    public function getLDAPProvider() {
1762
+        return $this->query('LDAPProvider');
1763
+    }
1764
+
1765
+    /**
1766
+     * @return \OCP\Settings\IManager
1767
+     */
1768
+    public function getSettingsManager() {
1769
+        return $this->query('SettingsManager');
1770
+    }
1771
+
1772
+    /**
1773
+     * @return \OCP\Files\IAppData
1774
+     */
1775
+    public function getAppDataDir($app) {
1776
+        /** @var \OC\Files\AppData\Factory $factory */
1777
+        $factory = $this->query(\OC\Files\AppData\Factory::class);
1778
+        return $factory->get($app);
1779
+    }
1780
+
1781
+    /**
1782
+     * @return \OCP\Lockdown\ILockdownManager
1783
+     */
1784
+    public function getLockdownManager() {
1785
+        return $this->query('LockdownManager');
1786
+    }
1787
+
1788
+    /**
1789
+     * @return \OCP\Federation\ICloudIdManager
1790
+     */
1791
+    public function getCloudIdManager() {
1792
+        return $this->query(ICloudIdManager::class);
1793
+    }
1794 1794
 }
Please login to merge, or discard this patch.
lib/private/Files/Config/UserMountCache.php 1 patch
Indentation   +330 added lines, -330 removed lines patch added patch discarded remove patch
@@ -42,334 +42,334 @@
 block discarded – undo
42 42
  * Cache mounts points per user in the cache so we can easilly look them up
43 43
  */
44 44
 class UserMountCache implements IUserMountCache {
45
-	/**
46
-	 * @var IDBConnection
47
-	 */
48
-	private $connection;
49
-
50
-	/**
51
-	 * @var IUserManager
52
-	 */
53
-	private $userManager;
54
-
55
-	/**
56
-	 * Cached mount info.
57
-	 * Map of $userId to ICachedMountInfo.
58
-	 *
59
-	 * @var ICache
60
-	 **/
61
-	private $mountsForUsers;
62
-
63
-	/**
64
-	 * @var ILogger
65
-	 */
66
-	private $logger;
67
-
68
-	/**
69
-	 * @var ICache
70
-	 */
71
-	private $cacheInfoCache;
72
-
73
-	/**
74
-	 * UserMountCache constructor.
75
-	 *
76
-	 * @param IDBConnection $connection
77
-	 * @param IUserManager $userManager
78
-	 * @param ILogger $logger
79
-	 */
80
-	public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
81
-		$this->connection = $connection;
82
-		$this->userManager = $userManager;
83
-		$this->logger = $logger;
84
-		$this->cacheInfoCache = new CappedMemoryCache();
85
-		$this->mountsForUsers = new CappedMemoryCache();
86
-	}
87
-
88
-	public function registerMounts(IUser $user, array $mounts) {
89
-		// filter out non-proper storages coming from unit tests
90
-		$mounts = array_filter($mounts, function (IMountPoint $mount) {
91
-			return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
92
-		});
93
-		/** @var ICachedMountInfo[] $newMounts */
94
-		$newMounts = array_map(function (IMountPoint $mount) use ($user) {
95
-			// filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
96
-			if ($mount->getStorageRootId() === -1) {
97
-				return null;
98
-			} else {
99
-				return new LazyStorageMountInfo($user, $mount);
100
-			}
101
-		}, $mounts);
102
-		$newMounts = array_values(array_filter($newMounts));
103
-
104
-		$cachedMounts = $this->getMountsForUser($user);
105
-		$mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
106
-			// since we are only looking for mounts for a specific user comparing on root id is enough
107
-			return $mount1->getRootId() - $mount2->getRootId();
108
-		};
109
-
110
-		/** @var ICachedMountInfo[] $addedMounts */
111
-		$addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
112
-		/** @var ICachedMountInfo[] $removedMounts */
113
-		$removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
114
-
115
-		$changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
116
-
117
-		foreach ($addedMounts as $mount) {
118
-			$this->addToCache($mount);
119
-			$this->mountsForUsers[$user->getUID()][] = $mount;
120
-		}
121
-		foreach ($removedMounts as $mount) {
122
-			$this->removeFromCache($mount);
123
-			$index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
124
-			unset($this->mountsForUsers[$user->getUID()][$index]);
125
-		}
126
-		foreach ($changedMounts as $mount) {
127
-			$this->updateCachedMount($mount);
128
-		}
129
-	}
130
-
131
-	/**
132
-	 * @param ICachedMountInfo[] $newMounts
133
-	 * @param ICachedMountInfo[] $cachedMounts
134
-	 * @return ICachedMountInfo[]
135
-	 */
136
-	private function findChangedMounts(array $newMounts, array $cachedMounts) {
137
-		$changed = [];
138
-		foreach ($newMounts as $newMount) {
139
-			foreach ($cachedMounts as $cachedMount) {
140
-				if (
141
-					$newMount->getRootId() === $cachedMount->getRootId() &&
142
-					(
143
-						$newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
144
-						$newMount->getStorageId() !== $cachedMount->getStorageId() ||
145
-						$newMount->getMountId() !== $cachedMount->getMountId()
146
-					)
147
-				) {
148
-					$changed[] = $newMount;
149
-				}
150
-			}
151
-		}
152
-		return $changed;
153
-	}
154
-
155
-	private function addToCache(ICachedMountInfo $mount) {
156
-		if ($mount->getStorageId() !== -1) {
157
-			$this->connection->insertIfNotExist('*PREFIX*mounts', [
158
-				'storage_id' => $mount->getStorageId(),
159
-				'root_id' => $mount->getRootId(),
160
-				'user_id' => $mount->getUser()->getUID(),
161
-				'mount_point' => $mount->getMountPoint(),
162
-				'mount_id' => $mount->getMountId()
163
-			], ['root_id', 'user_id']);
164
-		} else {
165
-			// in some cases this is legitimate, like orphaned shares
166
-			$this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
167
-		}
168
-	}
169
-
170
-	private function updateCachedMount(ICachedMountInfo $mount) {
171
-		$builder = $this->connection->getQueryBuilder();
172
-
173
-		$query = $builder->update('mounts')
174
-			->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
175
-			->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
176
-			->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
177
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
178
-			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
179
-
180
-		$query->execute();
181
-	}
182
-
183
-	private function removeFromCache(ICachedMountInfo $mount) {
184
-		$builder = $this->connection->getQueryBuilder();
185
-
186
-		$query = $builder->delete('mounts')
187
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
188
-			->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
189
-		$query->execute();
190
-	}
191
-
192
-	private function dbRowToMountInfo(array $row) {
193
-		$user = $this->userManager->get($row['user_id']);
194
-		if (is_null($user)) {
195
-			return null;
196
-		}
197
-		return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
198
-	}
199
-
200
-	/**
201
-	 * @param IUser $user
202
-	 * @return ICachedMountInfo[]
203
-	 */
204
-	public function getMountsForUser(IUser $user) {
205
-		if (!isset($this->mountsForUsers[$user->getUID()])) {
206
-			$builder = $this->connection->getQueryBuilder();
207
-			$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
208
-				->from('mounts', 'm')
209
-				->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
210
-				->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
211
-
212
-			$rows = $query->execute()->fetchAll();
213
-
214
-			$this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
215
-		}
216
-		return $this->mountsForUsers[$user->getUID()];
217
-	}
218
-
219
-	/**
220
-	 * @param int $numericStorageId
221
-	 * @param string|null $user limit the results to a single user
222
-	 * @return CachedMountInfo[]
223
-	 */
224
-	public function getMountsForStorageId($numericStorageId, $user = null) {
225
-		$builder = $this->connection->getQueryBuilder();
226
-		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
227
-			->from('mounts', 'm')
228
-			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
229
-			->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
230
-
231
-		if ($user) {
232
-			$query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
233
-		}
234
-
235
-		$rows = $query->execute()->fetchAll();
236
-
237
-		return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
238
-	}
239
-
240
-	/**
241
-	 * @param int $rootFileId
242
-	 * @return CachedMountInfo[]
243
-	 */
244
-	public function getMountsForRootId($rootFileId) {
245
-		$builder = $this->connection->getQueryBuilder();
246
-		$query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
247
-			->from('mounts', 'm')
248
-			->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
249
-			->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
250
-
251
-		$rows = $query->execute()->fetchAll();
252
-
253
-		return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
254
-	}
255
-
256
-	/**
257
-	 * @param $fileId
258
-	 * @return array
259
-	 * @throws \OCP\Files\NotFoundException
260
-	 */
261
-	private function getCacheInfoFromFileId($fileId) {
262
-		if (!isset($this->cacheInfoCache[$fileId])) {
263
-			$builder = $this->connection->getQueryBuilder();
264
-			$query = $builder->select('storage', 'path', 'mimetype')
265
-				->from('filecache')
266
-				->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
267
-
268
-			$row = $query->execute()->fetch();
269
-			if (is_array($row)) {
270
-				$this->cacheInfoCache[$fileId] = [
271
-					(int)$row['storage'],
272
-					$row['path'],
273
-					(int)$row['mimetype']
274
-				];
275
-			} else {
276
-				throw new NotFoundException('File with id "' . $fileId . '" not found');
277
-			}
278
-		}
279
-		return $this->cacheInfoCache[$fileId];
280
-	}
281
-
282
-	/**
283
-	 * @param int $fileId
284
-	 * @param string|null $user optionally restrict the results to a single user
285
-	 * @return ICachedMountInfo[]
286
-	 * @since 9.0.0
287
-	 */
288
-	public function getMountsForFileId($fileId, $user = null) {
289
-		try {
290
-			list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId);
291
-		} catch (NotFoundException $e) {
292
-			return [];
293
-		}
294
-		$mountsForStorage = $this->getMountsForStorageId($storageId, $user);
295
-
296
-		// filter mounts that are from the same storage but a different directory
297
-		return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
298
-			if ($fileId === $mount->getRootId()) {
299
-				return true;
300
-			}
301
-			$internalMountPath = $mount->getRootInternalPath();
302
-
303
-			return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
304
-		});
305
-	}
306
-
307
-	/**
308
-	 * Remove all cached mounts for a user
309
-	 *
310
-	 * @param IUser $user
311
-	 */
312
-	public function removeUserMounts(IUser $user) {
313
-		$builder = $this->connection->getQueryBuilder();
314
-
315
-		$query = $builder->delete('mounts')
316
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
317
-		$query->execute();
318
-	}
319
-
320
-	public function removeUserStorageMount($storageId, $userId) {
321
-		$builder = $this->connection->getQueryBuilder();
322
-
323
-		$query = $builder->delete('mounts')
324
-			->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
325
-			->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
326
-		$query->execute();
327
-	}
328
-
329
-	public function remoteStorageMounts($storageId) {
330
-		$builder = $this->connection->getQueryBuilder();
331
-
332
-		$query = $builder->delete('mounts')
333
-			->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
334
-		$query->execute();
335
-	}
336
-
337
-	/**
338
-	 * @param array $users
339
-	 * @return array
340
-	 * @suppress SqlInjectionChecker
341
-	 */
342
-	public function getUsedSpaceForUsers(array $users) {
343
-		$builder = $this->connection->getQueryBuilder();
344
-
345
-		$slash = $builder->createNamedParameter('/');
346
-
347
-		$mountPoint = $builder->func()->concat(
348
-			$builder->func()->concat($slash, 'user_id'),
349
-			$slash
350
-		);
351
-
352
-		$userIds = array_map(function (IUser $user) {
353
-			return $user->getUID();
354
-		}, $users);
355
-
356
-		$query = $builder->select('m.user_id', 'f.size')
357
-			->from('mounts', 'm')
358
-			->innerJoin('m', 'filecache', 'f',
359
-				$builder->expr()->andX(
360
-					$builder->expr()->eq('m.storage_id', 'f.storage'),
361
-					$builder->expr()->eq('f.path', $builder->createNamedParameter('files'))
362
-				))
363
-			->where($builder->expr()->eq('m.mount_point', $mountPoint))
364
-			->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
365
-
366
-		$result = $query->execute();
367
-
368
-		$results = [];
369
-		while ($row = $result->fetch()) {
370
-			$results[$row['user_id']] = $row['size'];
371
-		}
372
-		$result->closeCursor();
373
-		return $results;
374
-	}
45
+    /**
46
+     * @var IDBConnection
47
+     */
48
+    private $connection;
49
+
50
+    /**
51
+     * @var IUserManager
52
+     */
53
+    private $userManager;
54
+
55
+    /**
56
+     * Cached mount info.
57
+     * Map of $userId to ICachedMountInfo.
58
+     *
59
+     * @var ICache
60
+     **/
61
+    private $mountsForUsers;
62
+
63
+    /**
64
+     * @var ILogger
65
+     */
66
+    private $logger;
67
+
68
+    /**
69
+     * @var ICache
70
+     */
71
+    private $cacheInfoCache;
72
+
73
+    /**
74
+     * UserMountCache constructor.
75
+     *
76
+     * @param IDBConnection $connection
77
+     * @param IUserManager $userManager
78
+     * @param ILogger $logger
79
+     */
80
+    public function __construct(IDBConnection $connection, IUserManager $userManager, ILogger $logger) {
81
+        $this->connection = $connection;
82
+        $this->userManager = $userManager;
83
+        $this->logger = $logger;
84
+        $this->cacheInfoCache = new CappedMemoryCache();
85
+        $this->mountsForUsers = new CappedMemoryCache();
86
+    }
87
+
88
+    public function registerMounts(IUser $user, array $mounts) {
89
+        // filter out non-proper storages coming from unit tests
90
+        $mounts = array_filter($mounts, function (IMountPoint $mount) {
91
+            return $mount instanceof SharedMount || $mount->getStorage() && $mount->getStorage()->getCache();
92
+        });
93
+        /** @var ICachedMountInfo[] $newMounts */
94
+        $newMounts = array_map(function (IMountPoint $mount) use ($user) {
95
+            // filter out any storages which aren't scanned yet since we aren't interested in files from those storages (yet)
96
+            if ($mount->getStorageRootId() === -1) {
97
+                return null;
98
+            } else {
99
+                return new LazyStorageMountInfo($user, $mount);
100
+            }
101
+        }, $mounts);
102
+        $newMounts = array_values(array_filter($newMounts));
103
+
104
+        $cachedMounts = $this->getMountsForUser($user);
105
+        $mountDiff = function (ICachedMountInfo $mount1, ICachedMountInfo $mount2) {
106
+            // since we are only looking for mounts for a specific user comparing on root id is enough
107
+            return $mount1->getRootId() - $mount2->getRootId();
108
+        };
109
+
110
+        /** @var ICachedMountInfo[] $addedMounts */
111
+        $addedMounts = array_udiff($newMounts, $cachedMounts, $mountDiff);
112
+        /** @var ICachedMountInfo[] $removedMounts */
113
+        $removedMounts = array_udiff($cachedMounts, $newMounts, $mountDiff);
114
+
115
+        $changedMounts = $this->findChangedMounts($newMounts, $cachedMounts);
116
+
117
+        foreach ($addedMounts as $mount) {
118
+            $this->addToCache($mount);
119
+            $this->mountsForUsers[$user->getUID()][] = $mount;
120
+        }
121
+        foreach ($removedMounts as $mount) {
122
+            $this->removeFromCache($mount);
123
+            $index = array_search($mount, $this->mountsForUsers[$user->getUID()]);
124
+            unset($this->mountsForUsers[$user->getUID()][$index]);
125
+        }
126
+        foreach ($changedMounts as $mount) {
127
+            $this->updateCachedMount($mount);
128
+        }
129
+    }
130
+
131
+    /**
132
+     * @param ICachedMountInfo[] $newMounts
133
+     * @param ICachedMountInfo[] $cachedMounts
134
+     * @return ICachedMountInfo[]
135
+     */
136
+    private function findChangedMounts(array $newMounts, array $cachedMounts) {
137
+        $changed = [];
138
+        foreach ($newMounts as $newMount) {
139
+            foreach ($cachedMounts as $cachedMount) {
140
+                if (
141
+                    $newMount->getRootId() === $cachedMount->getRootId() &&
142
+                    (
143
+                        $newMount->getMountPoint() !== $cachedMount->getMountPoint() ||
144
+                        $newMount->getStorageId() !== $cachedMount->getStorageId() ||
145
+                        $newMount->getMountId() !== $cachedMount->getMountId()
146
+                    )
147
+                ) {
148
+                    $changed[] = $newMount;
149
+                }
150
+            }
151
+        }
152
+        return $changed;
153
+    }
154
+
155
+    private function addToCache(ICachedMountInfo $mount) {
156
+        if ($mount->getStorageId() !== -1) {
157
+            $this->connection->insertIfNotExist('*PREFIX*mounts', [
158
+                'storage_id' => $mount->getStorageId(),
159
+                'root_id' => $mount->getRootId(),
160
+                'user_id' => $mount->getUser()->getUID(),
161
+                'mount_point' => $mount->getMountPoint(),
162
+                'mount_id' => $mount->getMountId()
163
+            ], ['root_id', 'user_id']);
164
+        } else {
165
+            // in some cases this is legitimate, like orphaned shares
166
+            $this->logger->debug('Could not get storage info for mount at ' . $mount->getMountPoint());
167
+        }
168
+    }
169
+
170
+    private function updateCachedMount(ICachedMountInfo $mount) {
171
+        $builder = $this->connection->getQueryBuilder();
172
+
173
+        $query = $builder->update('mounts')
174
+            ->set('storage_id', $builder->createNamedParameter($mount->getStorageId()))
175
+            ->set('mount_point', $builder->createNamedParameter($mount->getMountPoint()))
176
+            ->set('mount_id', $builder->createNamedParameter($mount->getMountId(), IQueryBuilder::PARAM_INT))
177
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
178
+            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
179
+
180
+        $query->execute();
181
+    }
182
+
183
+    private function removeFromCache(ICachedMountInfo $mount) {
184
+        $builder = $this->connection->getQueryBuilder();
185
+
186
+        $query = $builder->delete('mounts')
187
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($mount->getUser()->getUID())))
188
+            ->andWhere($builder->expr()->eq('root_id', $builder->createNamedParameter($mount->getRootId(), IQueryBuilder::PARAM_INT)));
189
+        $query->execute();
190
+    }
191
+
192
+    private function dbRowToMountInfo(array $row) {
193
+        $user = $this->userManager->get($row['user_id']);
194
+        if (is_null($user)) {
195
+            return null;
196
+        }
197
+        return new CachedMountInfo($user, (int)$row['storage_id'], (int)$row['root_id'], $row['mount_point'], $row['mount_id'], isset($row['path']) ? $row['path'] : '');
198
+    }
199
+
200
+    /**
201
+     * @param IUser $user
202
+     * @return ICachedMountInfo[]
203
+     */
204
+    public function getMountsForUser(IUser $user) {
205
+        if (!isset($this->mountsForUsers[$user->getUID()])) {
206
+            $builder = $this->connection->getQueryBuilder();
207
+            $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
208
+                ->from('mounts', 'm')
209
+                ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
210
+                ->where($builder->expr()->eq('user_id', $builder->createPositionalParameter($user->getUID())));
211
+
212
+            $rows = $query->execute()->fetchAll();
213
+
214
+            $this->mountsForUsers[$user->getUID()] = array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
215
+        }
216
+        return $this->mountsForUsers[$user->getUID()];
217
+    }
218
+
219
+    /**
220
+     * @param int $numericStorageId
221
+     * @param string|null $user limit the results to a single user
222
+     * @return CachedMountInfo[]
223
+     */
224
+    public function getMountsForStorageId($numericStorageId, $user = null) {
225
+        $builder = $this->connection->getQueryBuilder();
226
+        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
227
+            ->from('mounts', 'm')
228
+            ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
229
+            ->where($builder->expr()->eq('storage_id', $builder->createPositionalParameter($numericStorageId, IQueryBuilder::PARAM_INT)));
230
+
231
+        if ($user) {
232
+            $query->andWhere($builder->expr()->eq('user_id', $builder->createPositionalParameter($user)));
233
+        }
234
+
235
+        $rows = $query->execute()->fetchAll();
236
+
237
+        return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
238
+    }
239
+
240
+    /**
241
+     * @param int $rootFileId
242
+     * @return CachedMountInfo[]
243
+     */
244
+    public function getMountsForRootId($rootFileId) {
245
+        $builder = $this->connection->getQueryBuilder();
246
+        $query = $builder->select('storage_id', 'root_id', 'user_id', 'mount_point', 'mount_id', 'f.path')
247
+            ->from('mounts', 'm')
248
+            ->innerJoin('m', 'filecache', 'f', $builder->expr()->eq('m.root_id', 'f.fileid'))
249
+            ->where($builder->expr()->eq('root_id', $builder->createPositionalParameter($rootFileId, IQueryBuilder::PARAM_INT)));
250
+
251
+        $rows = $query->execute()->fetchAll();
252
+
253
+        return array_filter(array_map([$this, 'dbRowToMountInfo'], $rows));
254
+    }
255
+
256
+    /**
257
+     * @param $fileId
258
+     * @return array
259
+     * @throws \OCP\Files\NotFoundException
260
+     */
261
+    private function getCacheInfoFromFileId($fileId) {
262
+        if (!isset($this->cacheInfoCache[$fileId])) {
263
+            $builder = $this->connection->getQueryBuilder();
264
+            $query = $builder->select('storage', 'path', 'mimetype')
265
+                ->from('filecache')
266
+                ->where($builder->expr()->eq('fileid', $builder->createNamedParameter($fileId, IQueryBuilder::PARAM_INT)));
267
+
268
+            $row = $query->execute()->fetch();
269
+            if (is_array($row)) {
270
+                $this->cacheInfoCache[$fileId] = [
271
+                    (int)$row['storage'],
272
+                    $row['path'],
273
+                    (int)$row['mimetype']
274
+                ];
275
+            } else {
276
+                throw new NotFoundException('File with id "' . $fileId . '" not found');
277
+            }
278
+        }
279
+        return $this->cacheInfoCache[$fileId];
280
+    }
281
+
282
+    /**
283
+     * @param int $fileId
284
+     * @param string|null $user optionally restrict the results to a single user
285
+     * @return ICachedMountInfo[]
286
+     * @since 9.0.0
287
+     */
288
+    public function getMountsForFileId($fileId, $user = null) {
289
+        try {
290
+            list($storageId, $internalPath) = $this->getCacheInfoFromFileId($fileId);
291
+        } catch (NotFoundException $e) {
292
+            return [];
293
+        }
294
+        $mountsForStorage = $this->getMountsForStorageId($storageId, $user);
295
+
296
+        // filter mounts that are from the same storage but a different directory
297
+        return array_filter($mountsForStorage, function (ICachedMountInfo $mount) use ($internalPath, $fileId) {
298
+            if ($fileId === $mount->getRootId()) {
299
+                return true;
300
+            }
301
+            $internalMountPath = $mount->getRootInternalPath();
302
+
303
+            return $internalMountPath === '' || substr($internalPath, 0, strlen($internalMountPath) + 1) === $internalMountPath . '/';
304
+        });
305
+    }
306
+
307
+    /**
308
+     * Remove all cached mounts for a user
309
+     *
310
+     * @param IUser $user
311
+     */
312
+    public function removeUserMounts(IUser $user) {
313
+        $builder = $this->connection->getQueryBuilder();
314
+
315
+        $query = $builder->delete('mounts')
316
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($user->getUID())));
317
+        $query->execute();
318
+    }
319
+
320
+    public function removeUserStorageMount($storageId, $userId) {
321
+        $builder = $this->connection->getQueryBuilder();
322
+
323
+        $query = $builder->delete('mounts')
324
+            ->where($builder->expr()->eq('user_id', $builder->createNamedParameter($userId)))
325
+            ->andWhere($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
326
+        $query->execute();
327
+    }
328
+
329
+    public function remoteStorageMounts($storageId) {
330
+        $builder = $this->connection->getQueryBuilder();
331
+
332
+        $query = $builder->delete('mounts')
333
+            ->where($builder->expr()->eq('storage_id', $builder->createNamedParameter($storageId, IQueryBuilder::PARAM_INT)));
334
+        $query->execute();
335
+    }
336
+
337
+    /**
338
+     * @param array $users
339
+     * @return array
340
+     * @suppress SqlInjectionChecker
341
+     */
342
+    public function getUsedSpaceForUsers(array $users) {
343
+        $builder = $this->connection->getQueryBuilder();
344
+
345
+        $slash = $builder->createNamedParameter('/');
346
+
347
+        $mountPoint = $builder->func()->concat(
348
+            $builder->func()->concat($slash, 'user_id'),
349
+            $slash
350
+        );
351
+
352
+        $userIds = array_map(function (IUser $user) {
353
+            return $user->getUID();
354
+        }, $users);
355
+
356
+        $query = $builder->select('m.user_id', 'f.size')
357
+            ->from('mounts', 'm')
358
+            ->innerJoin('m', 'filecache', 'f',
359
+                $builder->expr()->andX(
360
+                    $builder->expr()->eq('m.storage_id', 'f.storage'),
361
+                    $builder->expr()->eq('f.path', $builder->createNamedParameter('files'))
362
+                ))
363
+            ->where($builder->expr()->eq('m.mount_point', $mountPoint))
364
+            ->andWhere($builder->expr()->in('m.user_id', $builder->createNamedParameter($userIds, IQueryBuilder::PARAM_STR_ARRAY)));
365
+
366
+        $result = $query->execute();
367
+
368
+        $results = [];
369
+        while ($row = $result->fetch()) {
370
+            $results[$row['user_id']] = $row['size'];
371
+        }
372
+        $result->closeCursor();
373
+        return $results;
374
+    }
375 375
 }
Please login to merge, or discard this patch.
lib/private/Authentication/Token/DefaultTokenMapper.php 1 patch
Indentation   +129 added lines, -129 removed lines patch added patch discarded remove patch
@@ -30,135 +30,135 @@
 block discarded – undo
30 30
 
31 31
 class DefaultTokenMapper extends Mapper {
32 32
 
33
-	public function __construct(IDBConnection $db) {
34
-		parent::__construct($db, 'authtoken');
35
-	}
36
-
37
-	/**
38
-	 * Invalidate (delete) a given token
39
-	 *
40
-	 * @param string $token
41
-	 */
42
-	public function invalidate($token) {
43
-		/* @var $qb IQueryBuilder */
44
-		$qb = $this->db->getQueryBuilder();
45
-		$qb->delete('authtoken')
46
-			->where($qb->expr()->eq('token', $qb->createParameter('token')))
47
-			->setParameter('token', $token)
48
-			->execute();
49
-	}
50
-
51
-	/**
52
-	 * @param int $olderThan
53
-	 * @param int $remember
54
-	 */
55
-	public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) {
56
-		/* @var $qb IQueryBuilder */
57
-		$qb = $this->db->getQueryBuilder();
58
-		$qb->delete('authtoken')
59
-			->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
60
-			->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)))
61
-			->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT)))
62
-			->execute();
63
-	}
64
-
65
-	/**
66
-	 * Get the user UID for the given token
67
-	 *
68
-	 * @param string $token
69
-	 * @throws DoesNotExistException
70
-	 * @return DefaultToken
71
-	 */
72
-	public function getToken($token) {
73
-		/* @var $qb IQueryBuilder */
74
-		$qb = $this->db->getQueryBuilder();
75
-		$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
76
-			->from('authtoken')
77
-			->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
78
-			->execute();
79
-
80
-		$data = $result->fetch();
81
-		$result->closeCursor();
82
-		if ($data === false) {
83
-			throw new DoesNotExistException('token does not exist');
84
-		}
33
+    public function __construct(IDBConnection $db) {
34
+        parent::__construct($db, 'authtoken');
35
+    }
36
+
37
+    /**
38
+     * Invalidate (delete) a given token
39
+     *
40
+     * @param string $token
41
+     */
42
+    public function invalidate($token) {
43
+        /* @var $qb IQueryBuilder */
44
+        $qb = $this->db->getQueryBuilder();
45
+        $qb->delete('authtoken')
46
+            ->where($qb->expr()->eq('token', $qb->createParameter('token')))
47
+            ->setParameter('token', $token)
48
+            ->execute();
49
+    }
50
+
51
+    /**
52
+     * @param int $olderThan
53
+     * @param int $remember
54
+     */
55
+    public function invalidateOld($olderThan, $remember = IToken::DO_NOT_REMEMBER) {
56
+        /* @var $qb IQueryBuilder */
57
+        $qb = $this->db->getQueryBuilder();
58
+        $qb->delete('authtoken')
59
+            ->where($qb->expr()->lt('last_activity', $qb->createNamedParameter($olderThan, IQueryBuilder::PARAM_INT)))
60
+            ->andWhere($qb->expr()->eq('type', $qb->createNamedParameter(IToken::TEMPORARY_TOKEN, IQueryBuilder::PARAM_INT)))
61
+            ->andWhere($qb->expr()->eq('remember', $qb->createNamedParameter($remember, IQueryBuilder::PARAM_INT)))
62
+            ->execute();
63
+    }
64
+
65
+    /**
66
+     * Get the user UID for the given token
67
+     *
68
+     * @param string $token
69
+     * @throws DoesNotExistException
70
+     * @return DefaultToken
71
+     */
72
+    public function getToken($token) {
73
+        /* @var $qb IQueryBuilder */
74
+        $qb = $this->db->getQueryBuilder();
75
+        $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
76
+            ->from('authtoken')
77
+            ->where($qb->expr()->eq('token', $qb->createNamedParameter($token)))
78
+            ->execute();
79
+
80
+        $data = $result->fetch();
81
+        $result->closeCursor();
82
+        if ($data === false) {
83
+            throw new DoesNotExistException('token does not exist');
84
+        }
85 85
 ;
86
-		return DefaultToken::fromRow($data);
87
-	}
88
-
89
-	/**
90
-	 * Get the token for $id
91
-	 *
92
-	 * @param string $id
93
-	 * @throws DoesNotExistException
94
-	 * @return DefaultToken
95
-	 */
96
-	public function getTokenById($id) {
97
-		/* @var $qb IQueryBuilder */
98
-		$qb = $this->db->getQueryBuilder();
99
-		$result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope')
100
-			->from('authtoken')
101
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
102
-			->execute();
103
-
104
-		$data = $result->fetch();
105
-		$result->closeCursor();
106
-		if ($data === false) {
107
-			throw new DoesNotExistException('token does not exist');
108
-		};
109
-		return DefaultToken::fromRow($data);
110
-	}
111
-
112
-	/**
113
-	 * Get all tokens of a user
114
-	 *
115
-	 * The provider may limit the number of result rows in case of an abuse
116
-	 * where a high number of (session) tokens is generated
117
-	 *
118
-	 * @param IUser $user
119
-	 * @return DefaultToken[]
120
-	 */
121
-	public function getTokenByUser(IUser $user) {
122
-		/* @var $qb IQueryBuilder */
123
-		$qb = $this->db->getQueryBuilder();
124
-		$qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
125
-			->from('authtoken')
126
-			->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
127
-			->setMaxResults(1000);
128
-		$result = $qb->execute();
129
-		$data = $result->fetchAll();
130
-		$result->closeCursor();
131
-
132
-		$entities = array_map(function ($row) {
133
-			return DefaultToken::fromRow($row);
134
-		}, $data);
135
-
136
-		return $entities;
137
-	}
138
-
139
-	/**
140
-	 * @param IUser $user
141
-	 * @param int $id
142
-	 */
143
-	public function deleteById(IUser $user, $id) {
144
-		/* @var $qb IQueryBuilder */
145
-		$qb = $this->db->getQueryBuilder();
146
-		$qb->delete('authtoken')
147
-			->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
148
-			->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
149
-		$qb->execute();
150
-	}
151
-
152
-	/**
153
-	 * delete all auth token which belong to a specific client if the client was deleted
154
-	 *
155
-	 * @param string $name
156
-	 */
157
-	public function deleteByName($name) {
158
-		$qb = $this->db->getQueryBuilder();
159
-		$qb->delete('authtoken')
160
-			->where($qb->expr()->eq('name', $qb->createNamedParameter($name, IQueryBuilder::PARAM_LOB), IQueryBuilder::PARAM_LOB));
161
-		$qb->execute();
162
-	}
86
+        return DefaultToken::fromRow($data);
87
+    }
88
+
89
+    /**
90
+     * Get the token for $id
91
+     *
92
+     * @param string $id
93
+     * @throws DoesNotExistException
94
+     * @return DefaultToken
95
+     */
96
+    public function getTokenById($id) {
97
+        /* @var $qb IQueryBuilder */
98
+        $qb = $this->db->getQueryBuilder();
99
+        $result = $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'token', 'last_activity', 'last_check', 'scope')
100
+            ->from('authtoken')
101
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
102
+            ->execute();
103
+
104
+        $data = $result->fetch();
105
+        $result->closeCursor();
106
+        if ($data === false) {
107
+            throw new DoesNotExistException('token does not exist');
108
+        };
109
+        return DefaultToken::fromRow($data);
110
+    }
111
+
112
+    /**
113
+     * Get all tokens of a user
114
+     *
115
+     * The provider may limit the number of result rows in case of an abuse
116
+     * where a high number of (session) tokens is generated
117
+     *
118
+     * @param IUser $user
119
+     * @return DefaultToken[]
120
+     */
121
+    public function getTokenByUser(IUser $user) {
122
+        /* @var $qb IQueryBuilder */
123
+        $qb = $this->db->getQueryBuilder();
124
+        $qb->select('id', 'uid', 'login_name', 'password', 'name', 'type', 'remember', 'token', 'last_activity', 'last_check', 'scope')
125
+            ->from('authtoken')
126
+            ->where($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())))
127
+            ->setMaxResults(1000);
128
+        $result = $qb->execute();
129
+        $data = $result->fetchAll();
130
+        $result->closeCursor();
131
+
132
+        $entities = array_map(function ($row) {
133
+            return DefaultToken::fromRow($row);
134
+        }, $data);
135
+
136
+        return $entities;
137
+    }
138
+
139
+    /**
140
+     * @param IUser $user
141
+     * @param int $id
142
+     */
143
+    public function deleteById(IUser $user, $id) {
144
+        /* @var $qb IQueryBuilder */
145
+        $qb = $this->db->getQueryBuilder();
146
+        $qb->delete('authtoken')
147
+            ->where($qb->expr()->eq('id', $qb->createNamedParameter($id)))
148
+            ->andWhere($qb->expr()->eq('uid', $qb->createNamedParameter($user->getUID())));
149
+        $qb->execute();
150
+    }
151
+
152
+    /**
153
+     * delete all auth token which belong to a specific client if the client was deleted
154
+     *
155
+     * @param string $name
156
+     */
157
+    public function deleteByName($name) {
158
+        $qb = $this->db->getQueryBuilder();
159
+        $qb->delete('authtoken')
160
+            ->where($qb->expr()->eq('name', $qb->createNamedParameter($name, IQueryBuilder::PARAM_LOB), IQueryBuilder::PARAM_LOB));
161
+        $qb->execute();
162
+    }
163 163
 
164 164
 }
Please login to merge, or discard this patch.
core/Command/Maintenance/Install.php 1 patch
Indentation   +152 added lines, -152 removed lines patch added patch discarded remove patch
@@ -40,156 +40,156 @@
 block discarded – undo
40 40
 
41 41
 class Install extends Command {
42 42
 
43
-	/**
44
-	 * @var SystemConfig
45
-	 */
46
-	private $config;
47
-
48
-	public function __construct(SystemConfig $config) {
49
-		parent::__construct();
50
-		$this->config = $config;
51
-	}
52
-
53
-	protected function configure() {
54
-		$this
55
-			->setName('maintenance:install')
56
-			->setDescription('install Nextcloud')
57
-			->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
58
-			->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
59
-			->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
60
-			->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
61
-			->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
62
-			->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
63
-			->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
64
-			->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
65
-			->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
66
-			->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
67
-			->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
68
-	}
69
-
70
-	protected function execute(InputInterface $input, OutputInterface $output) {
71
-
72
-		// validate the environment
73
-		$server = \OC::$server;
74
-		$setupHelper = new Setup($this->config, $server->getIniWrapper(),
75
-			$server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(),
76
-			$server->getSecureRandom());
77
-		$sysInfo = $setupHelper->getSystemInfo(true);
78
-		$errors = $sysInfo['errors'];
79
-		if (count($errors) > 0) {
80
-			$this->printErrors($output, $errors);
81
-
82
-			// ignore the OS X setup warning
83
-			if(count($errors) !== 1 ||
84
-				(string)($errors[0]['error']) !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') {
85
-				return 1;
86
-			}
87
-		}
88
-
89
-		// validate user input
90
-		$options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
91
-
92
-		// perform installation
93
-		$errors = $setupHelper->install($options);
94
-		if (count($errors) > 0) {
95
-			$this->printErrors($output, $errors);
96
-			return 1;
97
-		}
98
-		$output->writeln("Nextcloud was successfully installed");
99
-		return 0;
100
-	}
101
-
102
-	/**
103
-	 * @param InputInterface $input
104
-	 * @param OutputInterface $output
105
-	 * @param string[] $supportedDatabases
106
-	 * @return array
107
-	 */
108
-	protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
109
-		$db = strtolower($input->getOption('database'));
110
-
111
-		if (!in_array($db, $supportedDatabases)) {
112
-			throw new InvalidArgumentException("Database <$db> is not supported.");
113
-		}
114
-
115
-		$dbUser = $input->getOption('database-user');
116
-		$dbPass = $input->getOption('database-pass');
117
-		$dbName = $input->getOption('database-name');
118
-		$dbPort = $input->getOption('database-port');
119
-		if ($db === 'oci') {
120
-			// an empty hostname needs to be read from the raw parameters
121
-			$dbHost = $input->getParameterOption('--database-host', '');
122
-		} else {
123
-			$dbHost = $input->getOption('database-host');
124
-		}
125
-		$dbTablePrefix = 'oc_';
126
-		if ($input->hasParameterOption('--database-table-prefix')) {
127
-			$dbTablePrefix = (string) $input->getOption('database-table-prefix');
128
-			$dbTablePrefix = trim($dbTablePrefix);
129
-		}
130
-		if ($input->hasParameterOption('--database-pass')) {
131
-			$dbPass = (string) $input->getOption('database-pass');
132
-		}
133
-		$adminLogin = $input->getOption('admin-user');
134
-		$adminPassword = $input->getOption('admin-pass');
135
-		$dataDir = $input->getOption('data-dir');
136
-
137
-		if ($db !== 'sqlite') {
138
-			if (is_null($dbUser)) {
139
-				throw new InvalidArgumentException("Database user not provided.");
140
-			}
141
-			if (is_null($dbName)) {
142
-				throw new InvalidArgumentException("Database name not provided.");
143
-			}
144
-			if (is_null($dbPass)) {
145
-				/** @var QuestionHelper $helper */
146
-				$helper = $this->getHelper('question');
147
-				$question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
148
-				$question->setHidden(true);
149
-				$question->setHiddenFallback(false);
150
-				$dbPass = $helper->ask($input, $output, $question);
151
-			}
152
-		}
153
-
154
-		if (is_null($adminPassword)) {
155
-			/** @var QuestionHelper $helper */
156
-			$helper = $this->getHelper('question');
157
-			$question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
158
-			$question->setHidden(true);
159
-			$question->setHiddenFallback(false);
160
-			$adminPassword = $helper->ask($input, $output, $question);
161
-		}
162
-
163
-		$options = [
164
-			'dbtype' => $db,
165
-			'dbuser' => $dbUser,
166
-			'dbpass' => $dbPass,
167
-			'dbname' => $dbName,
168
-			'dbhost' => $dbHost,
169
-			'dbport' => $dbPort,
170
-			'dbtableprefix' => $dbTablePrefix,
171
-			'adminlogin' => $adminLogin,
172
-			'adminpass' => $adminPassword,
173
-			'directory' => $dataDir
174
-		];
175
-		if ($db === 'oci') {
176
-			$options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
177
-		}
178
-		return $options;
179
-	}
180
-
181
-	/**
182
-	 * @param OutputInterface $output
183
-	 * @param $errors
184
-	 */
185
-	protected function printErrors(OutputInterface $output, $errors) {
186
-		foreach ($errors as $error) {
187
-			if (is_array($error)) {
188
-				$output->writeln('<error>' . (string)$error['error'] . '</error>');
189
-				$output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
190
-			} else {
191
-				$output->writeln('<error>' . (string)$error . '</error>');
192
-			}
193
-		}
194
-	}
43
+    /**
44
+     * @var SystemConfig
45
+     */
46
+    private $config;
47
+
48
+    public function __construct(SystemConfig $config) {
49
+        parent::__construct();
50
+        $this->config = $config;
51
+    }
52
+
53
+    protected function configure() {
54
+        $this
55
+            ->setName('maintenance:install')
56
+            ->setDescription('install Nextcloud')
57
+            ->addOption('database', null, InputOption::VALUE_REQUIRED, 'Supported database type', 'sqlite')
58
+            ->addOption('database-name', null, InputOption::VALUE_REQUIRED, 'Name of the database')
59
+            ->addOption('database-host', null, InputOption::VALUE_REQUIRED, 'Hostname of the database', 'localhost')
60
+            ->addOption('database-port', null, InputOption::VALUE_REQUIRED, 'Port the database is listening on')
61
+            ->addOption('database-user', null, InputOption::VALUE_REQUIRED, 'User name to connect to the database')
62
+            ->addOption('database-pass', null, InputOption::VALUE_OPTIONAL, 'Password of the database user', null)
63
+            ->addOption('database-table-prefix', null, InputOption::VALUE_OPTIONAL, 'Prefix for all tables (default: oc_)', null)
64
+            ->addOption('database-table-space', null, InputOption::VALUE_OPTIONAL, 'Table space of the database (oci only)', null)
65
+            ->addOption('admin-user', null, InputOption::VALUE_REQUIRED, 'User name of the admin account', 'admin')
66
+            ->addOption('admin-pass', null, InputOption::VALUE_REQUIRED, 'Password of the admin account')
67
+            ->addOption('data-dir', null, InputOption::VALUE_REQUIRED, 'Path to data directory', \OC::$SERVERROOT."/data");
68
+    }
69
+
70
+    protected function execute(InputInterface $input, OutputInterface $output) {
71
+
72
+        // validate the environment
73
+        $server = \OC::$server;
74
+        $setupHelper = new Setup($this->config, $server->getIniWrapper(),
75
+            $server->getL10N('lib'), $server->query(Defaults::class), $server->getLogger(),
76
+            $server->getSecureRandom());
77
+        $sysInfo = $setupHelper->getSystemInfo(true);
78
+        $errors = $sysInfo['errors'];
79
+        if (count($errors) > 0) {
80
+            $this->printErrors($output, $errors);
81
+
82
+            // ignore the OS X setup warning
83
+            if(count($errors) !== 1 ||
84
+                (string)($errors[0]['error']) !== 'Mac OS X is not supported and Nextcloud will not work properly on this platform. Use it at your own risk! ') {
85
+                return 1;
86
+            }
87
+        }
88
+
89
+        // validate user input
90
+        $options = $this->validateInput($input, $output, array_keys($sysInfo['databases']));
91
+
92
+        // perform installation
93
+        $errors = $setupHelper->install($options);
94
+        if (count($errors) > 0) {
95
+            $this->printErrors($output, $errors);
96
+            return 1;
97
+        }
98
+        $output->writeln("Nextcloud was successfully installed");
99
+        return 0;
100
+    }
101
+
102
+    /**
103
+     * @param InputInterface $input
104
+     * @param OutputInterface $output
105
+     * @param string[] $supportedDatabases
106
+     * @return array
107
+     */
108
+    protected function validateInput(InputInterface $input, OutputInterface $output, $supportedDatabases) {
109
+        $db = strtolower($input->getOption('database'));
110
+
111
+        if (!in_array($db, $supportedDatabases)) {
112
+            throw new InvalidArgumentException("Database <$db> is not supported.");
113
+        }
114
+
115
+        $dbUser = $input->getOption('database-user');
116
+        $dbPass = $input->getOption('database-pass');
117
+        $dbName = $input->getOption('database-name');
118
+        $dbPort = $input->getOption('database-port');
119
+        if ($db === 'oci') {
120
+            // an empty hostname needs to be read from the raw parameters
121
+            $dbHost = $input->getParameterOption('--database-host', '');
122
+        } else {
123
+            $dbHost = $input->getOption('database-host');
124
+        }
125
+        $dbTablePrefix = 'oc_';
126
+        if ($input->hasParameterOption('--database-table-prefix')) {
127
+            $dbTablePrefix = (string) $input->getOption('database-table-prefix');
128
+            $dbTablePrefix = trim($dbTablePrefix);
129
+        }
130
+        if ($input->hasParameterOption('--database-pass')) {
131
+            $dbPass = (string) $input->getOption('database-pass');
132
+        }
133
+        $adminLogin = $input->getOption('admin-user');
134
+        $adminPassword = $input->getOption('admin-pass');
135
+        $dataDir = $input->getOption('data-dir');
136
+
137
+        if ($db !== 'sqlite') {
138
+            if (is_null($dbUser)) {
139
+                throw new InvalidArgumentException("Database user not provided.");
140
+            }
141
+            if (is_null($dbName)) {
142
+                throw new InvalidArgumentException("Database name not provided.");
143
+            }
144
+            if (is_null($dbPass)) {
145
+                /** @var QuestionHelper $helper */
146
+                $helper = $this->getHelper('question');
147
+                $question = new Question('What is the password to access the database with user <'.$dbUser.'>?');
148
+                $question->setHidden(true);
149
+                $question->setHiddenFallback(false);
150
+                $dbPass = $helper->ask($input, $output, $question);
151
+            }
152
+        }
153
+
154
+        if (is_null($adminPassword)) {
155
+            /** @var QuestionHelper $helper */
156
+            $helper = $this->getHelper('question');
157
+            $question = new Question('What is the password you like to use for the admin account <'.$adminLogin.'>?');
158
+            $question->setHidden(true);
159
+            $question->setHiddenFallback(false);
160
+            $adminPassword = $helper->ask($input, $output, $question);
161
+        }
162
+
163
+        $options = [
164
+            'dbtype' => $db,
165
+            'dbuser' => $dbUser,
166
+            'dbpass' => $dbPass,
167
+            'dbname' => $dbName,
168
+            'dbhost' => $dbHost,
169
+            'dbport' => $dbPort,
170
+            'dbtableprefix' => $dbTablePrefix,
171
+            'adminlogin' => $adminLogin,
172
+            'adminpass' => $adminPassword,
173
+            'directory' => $dataDir
174
+        ];
175
+        if ($db === 'oci') {
176
+            $options['dbtablespace'] = $input->getParameterOption('--database-table-space', '');
177
+        }
178
+        return $options;
179
+    }
180
+
181
+    /**
182
+     * @param OutputInterface $output
183
+     * @param $errors
184
+     */
185
+    protected function printErrors(OutputInterface $output, $errors) {
186
+        foreach ($errors as $error) {
187
+            if (is_array($error)) {
188
+                $output->writeln('<error>' . (string)$error['error'] . '</error>');
189
+                $output->writeln('<info> -> ' . (string)$error['hint'] . '</info>');
190
+            } else {
191
+                $output->writeln('<error>' . (string)$error . '</error>');
192
+            }
193
+        }
194
+    }
195 195
 }
Please login to merge, or discard this patch.