@@ -46,1165 +46,1165 @@ |
||
| 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 | } |
@@ -191,9 +191,9 @@ discard block |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 |
||
| 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 | /** |
@@ -27,55 +27,55 @@ |
||
| 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 | } |
@@ -70,12 +70,12 @@ |
||
| 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 | } |
@@ -42,334 +42,334 @@ |
||
| 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 | } |
@@ -30,135 +30,135 @@ |
||
| 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 | } |
@@ -40,156 +40,156 @@ |
||
| 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 | } |
@@ -56,843 +56,843 @@ |
||
| 56 | 56 | * - ChangePropagator: updates the mtime and etags of parent folders whenever a change to the cache is made to the cache by the updater |
| 57 | 57 | */ |
| 58 | 58 | class Cache implements ICache { |
| 59 | - use MoveFromCacheTrait { |
|
| 60 | - MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
| 61 | - } |
|
| 62 | - |
|
| 63 | - /** |
|
| 64 | - * @var array partial data for the cache |
|
| 65 | - */ |
|
| 66 | - protected $partial = array(); |
|
| 67 | - |
|
| 68 | - /** |
|
| 69 | - * @var string |
|
| 70 | - */ |
|
| 71 | - protected $storageId; |
|
| 72 | - |
|
| 73 | - /** |
|
| 74 | - * @var Storage $storageCache |
|
| 75 | - */ |
|
| 76 | - protected $storageCache; |
|
| 77 | - |
|
| 78 | - /** @var IMimeTypeLoader */ |
|
| 79 | - protected $mimetypeLoader; |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @var IDBConnection |
|
| 83 | - */ |
|
| 84 | - protected $connection; |
|
| 85 | - |
|
| 86 | - /** @var QuerySearchHelper */ |
|
| 87 | - protected $querySearchHelper; |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * @param \OC\Files\Storage\Storage|string $storage |
|
| 91 | - */ |
|
| 92 | - public function __construct($storage) { |
|
| 93 | - if ($storage instanceof \OC\Files\Storage\Storage) { |
|
| 94 | - $this->storageId = $storage->getId(); |
|
| 95 | - } else { |
|
| 96 | - $this->storageId = $storage; |
|
| 97 | - } |
|
| 98 | - if (strlen($this->storageId) > 64) { |
|
| 99 | - $this->storageId = md5($this->storageId); |
|
| 100 | - } |
|
| 101 | - |
|
| 102 | - $this->storageCache = new Storage($storage); |
|
| 103 | - $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 104 | - $this->connection = \OC::$server->getDatabaseConnection(); |
|
| 105 | - $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
| 106 | - } |
|
| 107 | - |
|
| 108 | - /** |
|
| 109 | - * Get the numeric storage id for this cache's storage |
|
| 110 | - * |
|
| 111 | - * @return int |
|
| 112 | - */ |
|
| 113 | - public function getNumericStorageId() { |
|
| 114 | - return $this->storageCache->getNumericId(); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * get the stored metadata of a file or folder |
|
| 119 | - * |
|
| 120 | - * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
| 121 | - * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
| 122 | - */ |
|
| 123 | - public function get($file) { |
|
| 124 | - if (is_string($file) or $file == '') { |
|
| 125 | - // normalize file |
|
| 126 | - $file = $this->normalize($file); |
|
| 127 | - |
|
| 128 | - $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 129 | - $params = array($this->getNumericStorageId(), md5($file)); |
|
| 130 | - } else { //file id |
|
| 131 | - $where = 'WHERE `fileid` = ?'; |
|
| 132 | - $params = array($file); |
|
| 133 | - } |
|
| 134 | - $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 59 | + use MoveFromCacheTrait { |
|
| 60 | + MoveFromCacheTrait::moveFromCache as moveFromCacheFallback; |
|
| 61 | + } |
|
| 62 | + |
|
| 63 | + /** |
|
| 64 | + * @var array partial data for the cache |
|
| 65 | + */ |
|
| 66 | + protected $partial = array(); |
|
| 67 | + |
|
| 68 | + /** |
|
| 69 | + * @var string |
|
| 70 | + */ |
|
| 71 | + protected $storageId; |
|
| 72 | + |
|
| 73 | + /** |
|
| 74 | + * @var Storage $storageCache |
|
| 75 | + */ |
|
| 76 | + protected $storageCache; |
|
| 77 | + |
|
| 78 | + /** @var IMimeTypeLoader */ |
|
| 79 | + protected $mimetypeLoader; |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @var IDBConnection |
|
| 83 | + */ |
|
| 84 | + protected $connection; |
|
| 85 | + |
|
| 86 | + /** @var QuerySearchHelper */ |
|
| 87 | + protected $querySearchHelper; |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * @param \OC\Files\Storage\Storage|string $storage |
|
| 91 | + */ |
|
| 92 | + public function __construct($storage) { |
|
| 93 | + if ($storage instanceof \OC\Files\Storage\Storage) { |
|
| 94 | + $this->storageId = $storage->getId(); |
|
| 95 | + } else { |
|
| 96 | + $this->storageId = $storage; |
|
| 97 | + } |
|
| 98 | + if (strlen($this->storageId) > 64) { |
|
| 99 | + $this->storageId = md5($this->storageId); |
|
| 100 | + } |
|
| 101 | + |
|
| 102 | + $this->storageCache = new Storage($storage); |
|
| 103 | + $this->mimetypeLoader = \OC::$server->getMimeTypeLoader(); |
|
| 104 | + $this->connection = \OC::$server->getDatabaseConnection(); |
|
| 105 | + $this->querySearchHelper = new QuerySearchHelper($this->mimetypeLoader); |
|
| 106 | + } |
|
| 107 | + |
|
| 108 | + /** |
|
| 109 | + * Get the numeric storage id for this cache's storage |
|
| 110 | + * |
|
| 111 | + * @return int |
|
| 112 | + */ |
|
| 113 | + public function getNumericStorageId() { |
|
| 114 | + return $this->storageCache->getNumericId(); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * get the stored metadata of a file or folder |
|
| 119 | + * |
|
| 120 | + * @param string | int $file either the path of a file or folder or the file id for a file or folder |
|
| 121 | + * @return ICacheEntry|false the cache entry as array of false if the file is not found in the cache |
|
| 122 | + */ |
|
| 123 | + public function get($file) { |
|
| 124 | + if (is_string($file) or $file == '') { |
|
| 125 | + // normalize file |
|
| 126 | + $file = $this->normalize($file); |
|
| 127 | + |
|
| 128 | + $where = 'WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 129 | + $params = array($this->getNumericStorageId(), md5($file)); |
|
| 130 | + } else { //file id |
|
| 131 | + $where = 'WHERE `fileid` = ?'; |
|
| 132 | + $params = array($file); |
|
| 133 | + } |
|
| 134 | + $sql = 'SELECT `fileid`, `storage`, `path`, `path_hash`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 135 | 135 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
| 136 | 136 | FROM `*PREFIX*filecache` ' . $where; |
| 137 | - $result = $this->connection->executeQuery($sql, $params); |
|
| 138 | - $data = $result->fetch(); |
|
| 139 | - |
|
| 140 | - //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
| 141 | - //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
| 142 | - if ($data === null) { |
|
| 143 | - $data = false; |
|
| 144 | - } |
|
| 145 | - |
|
| 146 | - //merge partial data |
|
| 147 | - if (!$data and is_string($file)) { |
|
| 148 | - if (isset($this->partial[$file])) { |
|
| 149 | - $data = $this->partial[$file]; |
|
| 150 | - } |
|
| 151 | - return $data; |
|
| 152 | - } else { |
|
| 153 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 154 | - } |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - /** |
|
| 158 | - * Create a CacheEntry from database row |
|
| 159 | - * |
|
| 160 | - * @param array $data |
|
| 161 | - * @param IMimeTypeLoader $mimetypeLoader |
|
| 162 | - * @return CacheEntry |
|
| 163 | - */ |
|
| 164 | - public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
| 165 | - //fix types |
|
| 166 | - $data['fileid'] = (int)$data['fileid']; |
|
| 167 | - $data['parent'] = (int)$data['parent']; |
|
| 168 | - $data['size'] = 0 + $data['size']; |
|
| 169 | - $data['mtime'] = (int)$data['mtime']; |
|
| 170 | - $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
| 171 | - $data['encryptedVersion'] = (int)$data['encrypted']; |
|
| 172 | - $data['encrypted'] = (bool)$data['encrypted']; |
|
| 173 | - $data['storage_id'] = $data['storage']; |
|
| 174 | - $data['storage'] = (int)$data['storage']; |
|
| 175 | - $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
| 176 | - $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
| 177 | - if ($data['storage_mtime'] == 0) { |
|
| 178 | - $data['storage_mtime'] = $data['mtime']; |
|
| 179 | - } |
|
| 180 | - $data['permissions'] = (int)$data['permissions']; |
|
| 181 | - return new CacheEntry($data); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - /** |
|
| 185 | - * get the metadata of all files stored in $folder |
|
| 186 | - * |
|
| 187 | - * @param string $folder |
|
| 188 | - * @return ICacheEntry[] |
|
| 189 | - */ |
|
| 190 | - public function getFolderContents($folder) { |
|
| 191 | - $fileId = $this->getId($folder); |
|
| 192 | - return $this->getFolderContentsById($fileId); |
|
| 193 | - } |
|
| 194 | - |
|
| 195 | - /** |
|
| 196 | - * get the metadata of all files stored in $folder |
|
| 197 | - * |
|
| 198 | - * @param int $fileId the file id of the folder |
|
| 199 | - * @return ICacheEntry[] |
|
| 200 | - */ |
|
| 201 | - public function getFolderContentsById($fileId) { |
|
| 202 | - if ($fileId > -1) { |
|
| 203 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 137 | + $result = $this->connection->executeQuery($sql, $params); |
|
| 138 | + $data = $result->fetch(); |
|
| 139 | + |
|
| 140 | + //FIXME hide this HACK in the next database layer, or just use doctrine and get rid of MDB2 and PDO |
|
| 141 | + //PDO returns false, MDB2 returns null, oracle always uses MDB2, so convert null to false |
|
| 142 | + if ($data === null) { |
|
| 143 | + $data = false; |
|
| 144 | + } |
|
| 145 | + |
|
| 146 | + //merge partial data |
|
| 147 | + if (!$data and is_string($file)) { |
|
| 148 | + if (isset($this->partial[$file])) { |
|
| 149 | + $data = $this->partial[$file]; |
|
| 150 | + } |
|
| 151 | + return $data; |
|
| 152 | + } else { |
|
| 153 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 154 | + } |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + /** |
|
| 158 | + * Create a CacheEntry from database row |
|
| 159 | + * |
|
| 160 | + * @param array $data |
|
| 161 | + * @param IMimeTypeLoader $mimetypeLoader |
|
| 162 | + * @return CacheEntry |
|
| 163 | + */ |
|
| 164 | + public static function cacheEntryFromData($data, IMimeTypeLoader $mimetypeLoader) { |
|
| 165 | + //fix types |
|
| 166 | + $data['fileid'] = (int)$data['fileid']; |
|
| 167 | + $data['parent'] = (int)$data['parent']; |
|
| 168 | + $data['size'] = 0 + $data['size']; |
|
| 169 | + $data['mtime'] = (int)$data['mtime']; |
|
| 170 | + $data['storage_mtime'] = (int)$data['storage_mtime']; |
|
| 171 | + $data['encryptedVersion'] = (int)$data['encrypted']; |
|
| 172 | + $data['encrypted'] = (bool)$data['encrypted']; |
|
| 173 | + $data['storage_id'] = $data['storage']; |
|
| 174 | + $data['storage'] = (int)$data['storage']; |
|
| 175 | + $data['mimetype'] = $mimetypeLoader->getMimetypeById($data['mimetype']); |
|
| 176 | + $data['mimepart'] = $mimetypeLoader->getMimetypeById($data['mimepart']); |
|
| 177 | + if ($data['storage_mtime'] == 0) { |
|
| 178 | + $data['storage_mtime'] = $data['mtime']; |
|
| 179 | + } |
|
| 180 | + $data['permissions'] = (int)$data['permissions']; |
|
| 181 | + return new CacheEntry($data); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + /** |
|
| 185 | + * get the metadata of all files stored in $folder |
|
| 186 | + * |
|
| 187 | + * @param string $folder |
|
| 188 | + * @return ICacheEntry[] |
|
| 189 | + */ |
|
| 190 | + public function getFolderContents($folder) { |
|
| 191 | + $fileId = $this->getId($folder); |
|
| 192 | + return $this->getFolderContentsById($fileId); |
|
| 193 | + } |
|
| 194 | + |
|
| 195 | + /** |
|
| 196 | + * get the metadata of all files stored in $folder |
|
| 197 | + * |
|
| 198 | + * @param int $fileId the file id of the folder |
|
| 199 | + * @return ICacheEntry[] |
|
| 200 | + */ |
|
| 201 | + public function getFolderContentsById($fileId) { |
|
| 202 | + if ($fileId > -1) { |
|
| 203 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `mtime`, |
|
| 204 | 204 | `storage_mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
| 205 | 205 | FROM `*PREFIX*filecache` WHERE `parent` = ? ORDER BY `name` ASC'; |
| 206 | - $result = $this->connection->executeQuery($sql, [$fileId]); |
|
| 207 | - $files = $result->fetchAll(); |
|
| 208 | - return array_map(function (array $data) { |
|
| 209 | - return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
| 210 | - }, $files); |
|
| 211 | - } else { |
|
| 212 | - return array(); |
|
| 213 | - } |
|
| 214 | - } |
|
| 215 | - |
|
| 216 | - /** |
|
| 217 | - * insert or update meta data for a file or folder |
|
| 218 | - * |
|
| 219 | - * @param string $file |
|
| 220 | - * @param array $data |
|
| 221 | - * |
|
| 222 | - * @return int file id |
|
| 223 | - * @throws \RuntimeException |
|
| 224 | - */ |
|
| 225 | - public function put($file, array $data) { |
|
| 226 | - if (($id = $this->getId($file)) > -1) { |
|
| 227 | - $this->update($id, $data); |
|
| 228 | - return $id; |
|
| 229 | - } else { |
|
| 230 | - return $this->insert($file, $data); |
|
| 231 | - } |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - /** |
|
| 235 | - * insert meta data for a new file or folder |
|
| 236 | - * |
|
| 237 | - * @param string $file |
|
| 238 | - * @param array $data |
|
| 239 | - * |
|
| 240 | - * @return int file id |
|
| 241 | - * @throws \RuntimeException |
|
| 242 | - */ |
|
| 243 | - public function insert($file, array $data) { |
|
| 244 | - // normalize file |
|
| 245 | - $file = $this->normalize($file); |
|
| 246 | - |
|
| 247 | - if (isset($this->partial[$file])) { //add any saved partial data |
|
| 248 | - $data = array_merge($this->partial[$file], $data); |
|
| 249 | - unset($this->partial[$file]); |
|
| 250 | - } |
|
| 251 | - |
|
| 252 | - $requiredFields = array('size', 'mtime', 'mimetype'); |
|
| 253 | - foreach ($requiredFields as $field) { |
|
| 254 | - if (!isset($data[$field])) { //data not complete save as partial and return |
|
| 255 | - $this->partial[$file] = $data; |
|
| 256 | - return -1; |
|
| 257 | - } |
|
| 258 | - } |
|
| 259 | - |
|
| 260 | - $data['path'] = $file; |
|
| 261 | - $data['parent'] = $this->getParentId($file); |
|
| 262 | - $data['name'] = \OC_Util::basename($file); |
|
| 263 | - |
|
| 264 | - list($queryParts, $params) = $this->buildParts($data); |
|
| 265 | - $queryParts[] = '`storage`'; |
|
| 266 | - $params[] = $this->getNumericStorageId(); |
|
| 267 | - |
|
| 268 | - $queryParts = array_map(function ($item) { |
|
| 269 | - return trim($item, "`"); |
|
| 270 | - }, $queryParts); |
|
| 271 | - $values = array_combine($queryParts, $params); |
|
| 272 | - if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
| 273 | - 'storage', |
|
| 274 | - 'path_hash', |
|
| 275 | - ]) |
|
| 276 | - ) { |
|
| 277 | - return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 278 | - } |
|
| 279 | - |
|
| 280 | - // The file was created in the mean time |
|
| 281 | - if (($id = $this->getId($file)) > -1) { |
|
| 282 | - $this->update($id, $data); |
|
| 283 | - return $id; |
|
| 284 | - } else { |
|
| 285 | - throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
| 286 | - } |
|
| 287 | - } |
|
| 288 | - |
|
| 289 | - /** |
|
| 290 | - * update the metadata of an existing file or folder in the cache |
|
| 291 | - * |
|
| 292 | - * @param int $id the fileid of the existing file or folder |
|
| 293 | - * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
| 294 | - */ |
|
| 295 | - public function update($id, array $data) { |
|
| 296 | - |
|
| 297 | - if (isset($data['path'])) { |
|
| 298 | - // normalize path |
|
| 299 | - $data['path'] = $this->normalize($data['path']); |
|
| 300 | - } |
|
| 301 | - |
|
| 302 | - if (isset($data['name'])) { |
|
| 303 | - // normalize path |
|
| 304 | - $data['name'] = $this->normalize($data['name']); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - list($queryParts, $params) = $this->buildParts($data); |
|
| 308 | - // duplicate $params because we need the parts twice in the SQL statement |
|
| 309 | - // once for the SET part, once in the WHERE clause |
|
| 310 | - $params = array_merge($params, $params); |
|
| 311 | - $params[] = $id; |
|
| 312 | - |
|
| 313 | - // don't update if the data we try to set is the same as the one in the record |
|
| 314 | - // some databases (Postgres) don't like superfluous updates |
|
| 315 | - $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
| 316 | - 'WHERE (' . |
|
| 317 | - implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
| 318 | - implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
| 319 | - ') AND `fileid` = ? '; |
|
| 320 | - $this->connection->executeQuery($sql, $params); |
|
| 321 | - |
|
| 322 | - } |
|
| 323 | - |
|
| 324 | - /** |
|
| 325 | - * extract query parts and params array from data array |
|
| 326 | - * |
|
| 327 | - * @param array $data |
|
| 328 | - * @return array [$queryParts, $params] |
|
| 329 | - * $queryParts: string[], the (escaped) column names to be set in the query |
|
| 330 | - * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
| 331 | - */ |
|
| 332 | - protected function buildParts(array $data) { |
|
| 333 | - $fields = array( |
|
| 334 | - 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
| 335 | - 'etag', 'permissions', 'checksum', 'storage'); |
|
| 336 | - |
|
| 337 | - $doNotCopyStorageMTime = false; |
|
| 338 | - if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
| 339 | - // this horrific magic tells it to not copy storage_mtime to mtime |
|
| 340 | - unset($data['mtime']); |
|
| 341 | - $doNotCopyStorageMTime = true; |
|
| 342 | - } |
|
| 343 | - |
|
| 344 | - $params = array(); |
|
| 345 | - $queryParts = array(); |
|
| 346 | - foreach ($data as $name => $value) { |
|
| 347 | - if (array_search($name, $fields) !== false) { |
|
| 348 | - if ($name === 'path') { |
|
| 349 | - $params[] = md5($value); |
|
| 350 | - $queryParts[] = '`path_hash`'; |
|
| 351 | - } elseif ($name === 'mimetype') { |
|
| 352 | - $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
| 353 | - $queryParts[] = '`mimepart`'; |
|
| 354 | - $value = $this->mimetypeLoader->getId($value); |
|
| 355 | - } elseif ($name === 'storage_mtime') { |
|
| 356 | - if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
| 357 | - $params[] = $value; |
|
| 358 | - $queryParts[] = '`mtime`'; |
|
| 359 | - } |
|
| 360 | - } elseif ($name === 'encrypted') { |
|
| 361 | - if (isset($data['encryptedVersion'])) { |
|
| 362 | - $value = $data['encryptedVersion']; |
|
| 363 | - } else { |
|
| 364 | - // Boolean to integer conversion |
|
| 365 | - $value = $value ? 1 : 0; |
|
| 366 | - } |
|
| 367 | - } |
|
| 368 | - $params[] = $value; |
|
| 369 | - $queryParts[] = '`' . $name . '`'; |
|
| 370 | - } |
|
| 371 | - } |
|
| 372 | - return array($queryParts, $params); |
|
| 373 | - } |
|
| 374 | - |
|
| 375 | - /** |
|
| 376 | - * get the file id for a file |
|
| 377 | - * |
|
| 378 | - * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
| 379 | - * |
|
| 380 | - * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
| 381 | - * |
|
| 382 | - * @param string $file |
|
| 383 | - * @return int |
|
| 384 | - */ |
|
| 385 | - public function getId($file) { |
|
| 386 | - // normalize file |
|
| 387 | - $file = $this->normalize($file); |
|
| 388 | - |
|
| 389 | - $pathHash = md5($file); |
|
| 390 | - |
|
| 391 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 392 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 393 | - if ($row = $result->fetch()) { |
|
| 394 | - return $row['fileid']; |
|
| 395 | - } else { |
|
| 396 | - return -1; |
|
| 397 | - } |
|
| 398 | - } |
|
| 399 | - |
|
| 400 | - /** |
|
| 401 | - * get the id of the parent folder of a file |
|
| 402 | - * |
|
| 403 | - * @param string $file |
|
| 404 | - * @return int |
|
| 405 | - */ |
|
| 406 | - public function getParentId($file) { |
|
| 407 | - if ($file === '') { |
|
| 408 | - return -1; |
|
| 409 | - } else { |
|
| 410 | - $parent = $this->getParentPath($file); |
|
| 411 | - return (int)$this->getId($parent); |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - private function getParentPath($path) { |
|
| 416 | - $parent = dirname($path); |
|
| 417 | - if ($parent === '.') { |
|
| 418 | - $parent = ''; |
|
| 419 | - } |
|
| 420 | - return $parent; |
|
| 421 | - } |
|
| 422 | - |
|
| 423 | - /** |
|
| 424 | - * check if a file is available in the cache |
|
| 425 | - * |
|
| 426 | - * @param string $file |
|
| 427 | - * @return bool |
|
| 428 | - */ |
|
| 429 | - public function inCache($file) { |
|
| 430 | - return $this->getId($file) != -1; |
|
| 431 | - } |
|
| 432 | - |
|
| 433 | - /** |
|
| 434 | - * remove a file or folder from the cache |
|
| 435 | - * |
|
| 436 | - * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
| 437 | - * |
|
| 438 | - * @param string $file |
|
| 439 | - */ |
|
| 440 | - public function remove($file) { |
|
| 441 | - $entry = $this->get($file); |
|
| 442 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 443 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 444 | - if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
| 445 | - $this->removeChildren($entry); |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - /** |
|
| 450 | - * Get all sub folders of a folder |
|
| 451 | - * |
|
| 452 | - * @param array $entry the cache entry of the folder to get the subfolders for |
|
| 453 | - * @return array[] the cache entries for the subfolders |
|
| 454 | - */ |
|
| 455 | - private function getSubFolders($entry) { |
|
| 456 | - $children = $this->getFolderContentsById($entry['fileid']); |
|
| 457 | - return array_filter($children, function ($child) { |
|
| 458 | - return $child['mimetype'] === 'httpd/unix-directory'; |
|
| 459 | - }); |
|
| 460 | - } |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * Recursively remove all children of a folder |
|
| 464 | - * |
|
| 465 | - * @param array $entry the cache entry of the folder to remove the children of |
|
| 466 | - * @throws \OC\DatabaseException |
|
| 467 | - */ |
|
| 468 | - private function removeChildren($entry) { |
|
| 469 | - $subFolders = $this->getSubFolders($entry); |
|
| 470 | - foreach ($subFolders as $folder) { |
|
| 471 | - $this->removeChildren($folder); |
|
| 472 | - } |
|
| 473 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
| 474 | - $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 475 | - } |
|
| 476 | - |
|
| 477 | - /** |
|
| 478 | - * Move a file or folder in the cache |
|
| 479 | - * |
|
| 480 | - * @param string $source |
|
| 481 | - * @param string $target |
|
| 482 | - */ |
|
| 483 | - public function move($source, $target) { |
|
| 484 | - $this->moveFromCache($this, $source, $target); |
|
| 485 | - } |
|
| 486 | - |
|
| 487 | - /** |
|
| 488 | - * Get the storage id and path needed for a move |
|
| 489 | - * |
|
| 490 | - * @param string $path |
|
| 491 | - * @return array [$storageId, $internalPath] |
|
| 492 | - */ |
|
| 493 | - protected function getMoveInfo($path) { |
|
| 494 | - return [$this->getNumericStorageId(), $path]; |
|
| 495 | - } |
|
| 496 | - |
|
| 497 | - /** |
|
| 498 | - * Move a file or folder in the cache |
|
| 499 | - * |
|
| 500 | - * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 501 | - * @param string $sourcePath |
|
| 502 | - * @param string $targetPath |
|
| 503 | - * @throws \OC\DatabaseException |
|
| 504 | - * @throws \Exception if the given storages have an invalid id |
|
| 505 | - * @suppress SqlInjectionChecker |
|
| 506 | - */ |
|
| 507 | - public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 508 | - if ($sourceCache instanceof Cache) { |
|
| 509 | - // normalize source and target |
|
| 510 | - $sourcePath = $this->normalize($sourcePath); |
|
| 511 | - $targetPath = $this->normalize($targetPath); |
|
| 512 | - |
|
| 513 | - $sourceData = $sourceCache->get($sourcePath); |
|
| 514 | - $sourceId = $sourceData['fileid']; |
|
| 515 | - $newParentId = $this->getParentId($targetPath); |
|
| 516 | - |
|
| 517 | - list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
| 518 | - list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
| 519 | - |
|
| 520 | - if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
| 521 | - throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
| 522 | - } |
|
| 523 | - if (is_null($targetStorageId) || $targetStorageId === false) { |
|
| 524 | - throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
| 525 | - } |
|
| 526 | - |
|
| 527 | - $this->connection->beginTransaction(); |
|
| 528 | - if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
| 529 | - //update all child entries |
|
| 530 | - $sourceLength = mb_strlen($sourcePath); |
|
| 531 | - $query = $this->connection->getQueryBuilder(); |
|
| 532 | - |
|
| 533 | - $fun = $query->func(); |
|
| 534 | - $newPathFunction = $fun->concat( |
|
| 535 | - $query->createNamedParameter($targetPath), |
|
| 536 | - $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
| 537 | - ); |
|
| 538 | - $query->update('filecache') |
|
| 539 | - ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
| 540 | - ->set('path_hash', $fun->md5($newPathFunction)) |
|
| 541 | - ->set('path', $newPathFunction) |
|
| 542 | - ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
| 543 | - ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
| 544 | - |
|
| 545 | - try { |
|
| 546 | - $query->execute(); |
|
| 547 | - } catch (\OC\DatabaseException $e) { |
|
| 548 | - $this->connection->rollBack(); |
|
| 549 | - throw $e; |
|
| 550 | - } |
|
| 551 | - } |
|
| 552 | - |
|
| 553 | - $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
| 554 | - $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId)); |
|
| 555 | - $this->connection->commit(); |
|
| 556 | - } else { |
|
| 557 | - $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
| 558 | - } |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - /** |
|
| 562 | - * remove all entries for files that are stored on the storage from the cache |
|
| 563 | - */ |
|
| 564 | - public function clear() { |
|
| 565 | - $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 566 | - $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 567 | - |
|
| 568 | - $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
| 569 | - $this->connection->executeQuery($sql, array($this->storageId)); |
|
| 570 | - } |
|
| 571 | - |
|
| 572 | - /** |
|
| 573 | - * Get the scan status of a file |
|
| 574 | - * |
|
| 575 | - * - Cache::NOT_FOUND: File is not in the cache |
|
| 576 | - * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
| 577 | - * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
| 578 | - * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
| 579 | - * |
|
| 580 | - * @param string $file |
|
| 581 | - * |
|
| 582 | - * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 583 | - */ |
|
| 584 | - public function getStatus($file) { |
|
| 585 | - // normalize file |
|
| 586 | - $file = $this->normalize($file); |
|
| 587 | - |
|
| 588 | - $pathHash = md5($file); |
|
| 589 | - $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 590 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 591 | - if ($row = $result->fetch()) { |
|
| 592 | - if ((int)$row['size'] === -1) { |
|
| 593 | - return self::SHALLOW; |
|
| 594 | - } else { |
|
| 595 | - return self::COMPLETE; |
|
| 596 | - } |
|
| 597 | - } else { |
|
| 598 | - if (isset($this->partial[$file])) { |
|
| 599 | - return self::PARTIAL; |
|
| 600 | - } else { |
|
| 601 | - return self::NOT_FOUND; |
|
| 602 | - } |
|
| 603 | - } |
|
| 604 | - } |
|
| 605 | - |
|
| 606 | - /** |
|
| 607 | - * search for files matching $pattern |
|
| 608 | - * |
|
| 609 | - * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
| 610 | - * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
| 611 | - */ |
|
| 612 | - public function search($pattern) { |
|
| 613 | - // normalize pattern |
|
| 614 | - $pattern = $this->normalize($pattern); |
|
| 615 | - |
|
| 616 | - if ($pattern === '%%') { |
|
| 617 | - return []; |
|
| 618 | - } |
|
| 619 | - |
|
| 620 | - |
|
| 621 | - $sql = ' |
|
| 206 | + $result = $this->connection->executeQuery($sql, [$fileId]); |
|
| 207 | + $files = $result->fetchAll(); |
|
| 208 | + return array_map(function (array $data) { |
|
| 209 | + return self::cacheEntryFromData($data, $this->mimetypeLoader);; |
|
| 210 | + }, $files); |
|
| 211 | + } else { |
|
| 212 | + return array(); |
|
| 213 | + } |
|
| 214 | + } |
|
| 215 | + |
|
| 216 | + /** |
|
| 217 | + * insert or update meta data for a file or folder |
|
| 218 | + * |
|
| 219 | + * @param string $file |
|
| 220 | + * @param array $data |
|
| 221 | + * |
|
| 222 | + * @return int file id |
|
| 223 | + * @throws \RuntimeException |
|
| 224 | + */ |
|
| 225 | + public function put($file, array $data) { |
|
| 226 | + if (($id = $this->getId($file)) > -1) { |
|
| 227 | + $this->update($id, $data); |
|
| 228 | + return $id; |
|
| 229 | + } else { |
|
| 230 | + return $this->insert($file, $data); |
|
| 231 | + } |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + /** |
|
| 235 | + * insert meta data for a new file or folder |
|
| 236 | + * |
|
| 237 | + * @param string $file |
|
| 238 | + * @param array $data |
|
| 239 | + * |
|
| 240 | + * @return int file id |
|
| 241 | + * @throws \RuntimeException |
|
| 242 | + */ |
|
| 243 | + public function insert($file, array $data) { |
|
| 244 | + // normalize file |
|
| 245 | + $file = $this->normalize($file); |
|
| 246 | + |
|
| 247 | + if (isset($this->partial[$file])) { //add any saved partial data |
|
| 248 | + $data = array_merge($this->partial[$file], $data); |
|
| 249 | + unset($this->partial[$file]); |
|
| 250 | + } |
|
| 251 | + |
|
| 252 | + $requiredFields = array('size', 'mtime', 'mimetype'); |
|
| 253 | + foreach ($requiredFields as $field) { |
|
| 254 | + if (!isset($data[$field])) { //data not complete save as partial and return |
|
| 255 | + $this->partial[$file] = $data; |
|
| 256 | + return -1; |
|
| 257 | + } |
|
| 258 | + } |
|
| 259 | + |
|
| 260 | + $data['path'] = $file; |
|
| 261 | + $data['parent'] = $this->getParentId($file); |
|
| 262 | + $data['name'] = \OC_Util::basename($file); |
|
| 263 | + |
|
| 264 | + list($queryParts, $params) = $this->buildParts($data); |
|
| 265 | + $queryParts[] = '`storage`'; |
|
| 266 | + $params[] = $this->getNumericStorageId(); |
|
| 267 | + |
|
| 268 | + $queryParts = array_map(function ($item) { |
|
| 269 | + return trim($item, "`"); |
|
| 270 | + }, $queryParts); |
|
| 271 | + $values = array_combine($queryParts, $params); |
|
| 272 | + if (\OC::$server->getDatabaseConnection()->insertIfNotExist('*PREFIX*filecache', $values, [ |
|
| 273 | + 'storage', |
|
| 274 | + 'path_hash', |
|
| 275 | + ]) |
|
| 276 | + ) { |
|
| 277 | + return (int)$this->connection->lastInsertId('*PREFIX*filecache'); |
|
| 278 | + } |
|
| 279 | + |
|
| 280 | + // The file was created in the mean time |
|
| 281 | + if (($id = $this->getId($file)) > -1) { |
|
| 282 | + $this->update($id, $data); |
|
| 283 | + return $id; |
|
| 284 | + } else { |
|
| 285 | + throw new \RuntimeException('File entry could not be inserted with insertIfNotExist() but could also not be selected with getId() in order to perform an update. Please try again.'); |
|
| 286 | + } |
|
| 287 | + } |
|
| 288 | + |
|
| 289 | + /** |
|
| 290 | + * update the metadata of an existing file or folder in the cache |
|
| 291 | + * |
|
| 292 | + * @param int $id the fileid of the existing file or folder |
|
| 293 | + * @param array $data [$key => $value] the metadata to update, only the fields provided in the array will be updated, non-provided values will remain unchanged |
|
| 294 | + */ |
|
| 295 | + public function update($id, array $data) { |
|
| 296 | + |
|
| 297 | + if (isset($data['path'])) { |
|
| 298 | + // normalize path |
|
| 299 | + $data['path'] = $this->normalize($data['path']); |
|
| 300 | + } |
|
| 301 | + |
|
| 302 | + if (isset($data['name'])) { |
|
| 303 | + // normalize path |
|
| 304 | + $data['name'] = $this->normalize($data['name']); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + list($queryParts, $params) = $this->buildParts($data); |
|
| 308 | + // duplicate $params because we need the parts twice in the SQL statement |
|
| 309 | + // once for the SET part, once in the WHERE clause |
|
| 310 | + $params = array_merge($params, $params); |
|
| 311 | + $params[] = $id; |
|
| 312 | + |
|
| 313 | + // don't update if the data we try to set is the same as the one in the record |
|
| 314 | + // some databases (Postgres) don't like superfluous updates |
|
| 315 | + $sql = 'UPDATE `*PREFIX*filecache` SET ' . implode(' = ?, ', $queryParts) . '=? ' . |
|
| 316 | + 'WHERE (' . |
|
| 317 | + implode(' <> ? OR ', $queryParts) . ' <> ? OR ' . |
|
| 318 | + implode(' IS NULL OR ', $queryParts) . ' IS NULL' . |
|
| 319 | + ') AND `fileid` = ? '; |
|
| 320 | + $this->connection->executeQuery($sql, $params); |
|
| 321 | + |
|
| 322 | + } |
|
| 323 | + |
|
| 324 | + /** |
|
| 325 | + * extract query parts and params array from data array |
|
| 326 | + * |
|
| 327 | + * @param array $data |
|
| 328 | + * @return array [$queryParts, $params] |
|
| 329 | + * $queryParts: string[], the (escaped) column names to be set in the query |
|
| 330 | + * $params: mixed[], the new values for the columns, to be passed as params to the query |
|
| 331 | + */ |
|
| 332 | + protected function buildParts(array $data) { |
|
| 333 | + $fields = array( |
|
| 334 | + 'path', 'parent', 'name', 'mimetype', 'size', 'mtime', 'storage_mtime', 'encrypted', |
|
| 335 | + 'etag', 'permissions', 'checksum', 'storage'); |
|
| 336 | + |
|
| 337 | + $doNotCopyStorageMTime = false; |
|
| 338 | + if (array_key_exists('mtime', $data) && $data['mtime'] === null) { |
|
| 339 | + // this horrific magic tells it to not copy storage_mtime to mtime |
|
| 340 | + unset($data['mtime']); |
|
| 341 | + $doNotCopyStorageMTime = true; |
|
| 342 | + } |
|
| 343 | + |
|
| 344 | + $params = array(); |
|
| 345 | + $queryParts = array(); |
|
| 346 | + foreach ($data as $name => $value) { |
|
| 347 | + if (array_search($name, $fields) !== false) { |
|
| 348 | + if ($name === 'path') { |
|
| 349 | + $params[] = md5($value); |
|
| 350 | + $queryParts[] = '`path_hash`'; |
|
| 351 | + } elseif ($name === 'mimetype') { |
|
| 352 | + $params[] = $this->mimetypeLoader->getId(substr($value, 0, strpos($value, '/'))); |
|
| 353 | + $queryParts[] = '`mimepart`'; |
|
| 354 | + $value = $this->mimetypeLoader->getId($value); |
|
| 355 | + } elseif ($name === 'storage_mtime') { |
|
| 356 | + if (!$doNotCopyStorageMTime && !isset($data['mtime'])) { |
|
| 357 | + $params[] = $value; |
|
| 358 | + $queryParts[] = '`mtime`'; |
|
| 359 | + } |
|
| 360 | + } elseif ($name === 'encrypted') { |
|
| 361 | + if (isset($data['encryptedVersion'])) { |
|
| 362 | + $value = $data['encryptedVersion']; |
|
| 363 | + } else { |
|
| 364 | + // Boolean to integer conversion |
|
| 365 | + $value = $value ? 1 : 0; |
|
| 366 | + } |
|
| 367 | + } |
|
| 368 | + $params[] = $value; |
|
| 369 | + $queryParts[] = '`' . $name . '`'; |
|
| 370 | + } |
|
| 371 | + } |
|
| 372 | + return array($queryParts, $params); |
|
| 373 | + } |
|
| 374 | + |
|
| 375 | + /** |
|
| 376 | + * get the file id for a file |
|
| 377 | + * |
|
| 378 | + * A file id is a numeric id for a file or folder that's unique within an owncloud instance which stays the same for the lifetime of a file |
|
| 379 | + * |
|
| 380 | + * File ids are easiest way for apps to store references to a file since unlike paths they are not affected by renames or sharing |
|
| 381 | + * |
|
| 382 | + * @param string $file |
|
| 383 | + * @return int |
|
| 384 | + */ |
|
| 385 | + public function getId($file) { |
|
| 386 | + // normalize file |
|
| 387 | + $file = $this->normalize($file); |
|
| 388 | + |
|
| 389 | + $pathHash = md5($file); |
|
| 390 | + |
|
| 391 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 392 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 393 | + if ($row = $result->fetch()) { |
|
| 394 | + return $row['fileid']; |
|
| 395 | + } else { |
|
| 396 | + return -1; |
|
| 397 | + } |
|
| 398 | + } |
|
| 399 | + |
|
| 400 | + /** |
|
| 401 | + * get the id of the parent folder of a file |
|
| 402 | + * |
|
| 403 | + * @param string $file |
|
| 404 | + * @return int |
|
| 405 | + */ |
|
| 406 | + public function getParentId($file) { |
|
| 407 | + if ($file === '') { |
|
| 408 | + return -1; |
|
| 409 | + } else { |
|
| 410 | + $parent = $this->getParentPath($file); |
|
| 411 | + return (int)$this->getId($parent); |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + private function getParentPath($path) { |
|
| 416 | + $parent = dirname($path); |
|
| 417 | + if ($parent === '.') { |
|
| 418 | + $parent = ''; |
|
| 419 | + } |
|
| 420 | + return $parent; |
|
| 421 | + } |
|
| 422 | + |
|
| 423 | + /** |
|
| 424 | + * check if a file is available in the cache |
|
| 425 | + * |
|
| 426 | + * @param string $file |
|
| 427 | + * @return bool |
|
| 428 | + */ |
|
| 429 | + public function inCache($file) { |
|
| 430 | + return $this->getId($file) != -1; |
|
| 431 | + } |
|
| 432 | + |
|
| 433 | + /** |
|
| 434 | + * remove a file or folder from the cache |
|
| 435 | + * |
|
| 436 | + * when removing a folder from the cache all files and folders inside the folder will be removed as well |
|
| 437 | + * |
|
| 438 | + * @param string $file |
|
| 439 | + */ |
|
| 440 | + public function remove($file) { |
|
| 441 | + $entry = $this->get($file); |
|
| 442 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 443 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 444 | + if ($entry['mimetype'] === 'httpd/unix-directory') { |
|
| 445 | + $this->removeChildren($entry); |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + /** |
|
| 450 | + * Get all sub folders of a folder |
|
| 451 | + * |
|
| 452 | + * @param array $entry the cache entry of the folder to get the subfolders for |
|
| 453 | + * @return array[] the cache entries for the subfolders |
|
| 454 | + */ |
|
| 455 | + private function getSubFolders($entry) { |
|
| 456 | + $children = $this->getFolderContentsById($entry['fileid']); |
|
| 457 | + return array_filter($children, function ($child) { |
|
| 458 | + return $child['mimetype'] === 'httpd/unix-directory'; |
|
| 459 | + }); |
|
| 460 | + } |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * Recursively remove all children of a folder |
|
| 464 | + * |
|
| 465 | + * @param array $entry the cache entry of the folder to remove the children of |
|
| 466 | + * @throws \OC\DatabaseException |
|
| 467 | + */ |
|
| 468 | + private function removeChildren($entry) { |
|
| 469 | + $subFolders = $this->getSubFolders($entry); |
|
| 470 | + foreach ($subFolders as $folder) { |
|
| 471 | + $this->removeChildren($folder); |
|
| 472 | + } |
|
| 473 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `parent` = ?'; |
|
| 474 | + $this->connection->executeQuery($sql, array($entry['fileid'])); |
|
| 475 | + } |
|
| 476 | + |
|
| 477 | + /** |
|
| 478 | + * Move a file or folder in the cache |
|
| 479 | + * |
|
| 480 | + * @param string $source |
|
| 481 | + * @param string $target |
|
| 482 | + */ |
|
| 483 | + public function move($source, $target) { |
|
| 484 | + $this->moveFromCache($this, $source, $target); |
|
| 485 | + } |
|
| 486 | + |
|
| 487 | + /** |
|
| 488 | + * Get the storage id and path needed for a move |
|
| 489 | + * |
|
| 490 | + * @param string $path |
|
| 491 | + * @return array [$storageId, $internalPath] |
|
| 492 | + */ |
|
| 493 | + protected function getMoveInfo($path) { |
|
| 494 | + return [$this->getNumericStorageId(), $path]; |
|
| 495 | + } |
|
| 496 | + |
|
| 497 | + /** |
|
| 498 | + * Move a file or folder in the cache |
|
| 499 | + * |
|
| 500 | + * @param \OCP\Files\Cache\ICache $sourceCache |
|
| 501 | + * @param string $sourcePath |
|
| 502 | + * @param string $targetPath |
|
| 503 | + * @throws \OC\DatabaseException |
|
| 504 | + * @throws \Exception if the given storages have an invalid id |
|
| 505 | + * @suppress SqlInjectionChecker |
|
| 506 | + */ |
|
| 507 | + public function moveFromCache(ICache $sourceCache, $sourcePath, $targetPath) { |
|
| 508 | + if ($sourceCache instanceof Cache) { |
|
| 509 | + // normalize source and target |
|
| 510 | + $sourcePath = $this->normalize($sourcePath); |
|
| 511 | + $targetPath = $this->normalize($targetPath); |
|
| 512 | + |
|
| 513 | + $sourceData = $sourceCache->get($sourcePath); |
|
| 514 | + $sourceId = $sourceData['fileid']; |
|
| 515 | + $newParentId = $this->getParentId($targetPath); |
|
| 516 | + |
|
| 517 | + list($sourceStorageId, $sourcePath) = $sourceCache->getMoveInfo($sourcePath); |
|
| 518 | + list($targetStorageId, $targetPath) = $this->getMoveInfo($targetPath); |
|
| 519 | + |
|
| 520 | + if (is_null($sourceStorageId) || $sourceStorageId === false) { |
|
| 521 | + throw new \Exception('Invalid source storage id: ' . $sourceStorageId); |
|
| 522 | + } |
|
| 523 | + if (is_null($targetStorageId) || $targetStorageId === false) { |
|
| 524 | + throw new \Exception('Invalid target storage id: ' . $targetStorageId); |
|
| 525 | + } |
|
| 526 | + |
|
| 527 | + $this->connection->beginTransaction(); |
|
| 528 | + if ($sourceData['mimetype'] === 'httpd/unix-directory') { |
|
| 529 | + //update all child entries |
|
| 530 | + $sourceLength = mb_strlen($sourcePath); |
|
| 531 | + $query = $this->connection->getQueryBuilder(); |
|
| 532 | + |
|
| 533 | + $fun = $query->func(); |
|
| 534 | + $newPathFunction = $fun->concat( |
|
| 535 | + $query->createNamedParameter($targetPath), |
|
| 536 | + $fun->substring('path', $query->createNamedParameter($sourceLength + 1, IQueryBuilder::PARAM_INT))// +1 for the leading slash |
|
| 537 | + ); |
|
| 538 | + $query->update('filecache') |
|
| 539 | + ->set('storage', $query->createNamedParameter($targetStorageId, IQueryBuilder::PARAM_INT)) |
|
| 540 | + ->set('path_hash', $fun->md5($newPathFunction)) |
|
| 541 | + ->set('path', $newPathFunction) |
|
| 542 | + ->where($query->expr()->eq('storage', $query->createNamedParameter($sourceStorageId, IQueryBuilder::PARAM_INT))) |
|
| 543 | + ->andWhere($query->expr()->like('path', $query->createNamedParameter($this->connection->escapeLikeParameter($sourcePath) . '/%'))); |
|
| 544 | + |
|
| 545 | + try { |
|
| 546 | + $query->execute(); |
|
| 547 | + } catch (\OC\DatabaseException $e) { |
|
| 548 | + $this->connection->rollBack(); |
|
| 549 | + throw $e; |
|
| 550 | + } |
|
| 551 | + } |
|
| 552 | + |
|
| 553 | + $sql = 'UPDATE `*PREFIX*filecache` SET `storage` = ?, `path` = ?, `path_hash` = ?, `name` = ?, `parent` = ? WHERE `fileid` = ?'; |
|
| 554 | + $this->connection->executeQuery($sql, array($targetStorageId, $targetPath, md5($targetPath), \OC_Util::basename($targetPath), $newParentId, $sourceId)); |
|
| 555 | + $this->connection->commit(); |
|
| 556 | + } else { |
|
| 557 | + $this->moveFromCacheFallback($sourceCache, $sourcePath, $targetPath); |
|
| 558 | + } |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + /** |
|
| 562 | + * remove all entries for files that are stored on the storage from the cache |
|
| 563 | + */ |
|
| 564 | + public function clear() { |
|
| 565 | + $sql = 'DELETE FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 566 | + $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 567 | + |
|
| 568 | + $sql = 'DELETE FROM `*PREFIX*storages` WHERE `id` = ?'; |
|
| 569 | + $this->connection->executeQuery($sql, array($this->storageId)); |
|
| 570 | + } |
|
| 571 | + |
|
| 572 | + /** |
|
| 573 | + * Get the scan status of a file |
|
| 574 | + * |
|
| 575 | + * - Cache::NOT_FOUND: File is not in the cache |
|
| 576 | + * - Cache::PARTIAL: File is not stored in the cache but some incomplete data is known |
|
| 577 | + * - Cache::SHALLOW: The folder and it's direct children are in the cache but not all sub folders are fully scanned |
|
| 578 | + * - Cache::COMPLETE: The file or folder, with all it's children) are fully scanned |
|
| 579 | + * |
|
| 580 | + * @param string $file |
|
| 581 | + * |
|
| 582 | + * @return int Cache::NOT_FOUND, Cache::PARTIAL, Cache::SHALLOW or Cache::COMPLETE |
|
| 583 | + */ |
|
| 584 | + public function getStatus($file) { |
|
| 585 | + // normalize file |
|
| 586 | + $file = $this->normalize($file); |
|
| 587 | + |
|
| 588 | + $pathHash = md5($file); |
|
| 589 | + $sql = 'SELECT `size` FROM `*PREFIX*filecache` WHERE `storage` = ? AND `path_hash` = ?'; |
|
| 590 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId(), $pathHash)); |
|
| 591 | + if ($row = $result->fetch()) { |
|
| 592 | + if ((int)$row['size'] === -1) { |
|
| 593 | + return self::SHALLOW; |
|
| 594 | + } else { |
|
| 595 | + return self::COMPLETE; |
|
| 596 | + } |
|
| 597 | + } else { |
|
| 598 | + if (isset($this->partial[$file])) { |
|
| 599 | + return self::PARTIAL; |
|
| 600 | + } else { |
|
| 601 | + return self::NOT_FOUND; |
|
| 602 | + } |
|
| 603 | + } |
|
| 604 | + } |
|
| 605 | + |
|
| 606 | + /** |
|
| 607 | + * search for files matching $pattern |
|
| 608 | + * |
|
| 609 | + * @param string $pattern the search pattern using SQL search syntax (e.g. '%searchstring%') |
|
| 610 | + * @return ICacheEntry[] an array of cache entries where the name matches the search pattern |
|
| 611 | + */ |
|
| 612 | + public function search($pattern) { |
|
| 613 | + // normalize pattern |
|
| 614 | + $pattern = $this->normalize($pattern); |
|
| 615 | + |
|
| 616 | + if ($pattern === '%%') { |
|
| 617 | + return []; |
|
| 618 | + } |
|
| 619 | + |
|
| 620 | + |
|
| 621 | + $sql = ' |
|
| 622 | 622 | SELECT `fileid`, `storage`, `path`, `parent`, `name`, |
| 623 | 623 | `mimetype`, `storage_mtime`, `mimepart`, `size`, `mtime`, |
| 624 | 624 | `encrypted`, `etag`, `permissions`, `checksum` |
| 625 | 625 | FROM `*PREFIX*filecache` |
| 626 | 626 | WHERE `storage` = ? AND `name` ILIKE ?'; |
| 627 | - $result = $this->connection->executeQuery($sql, |
|
| 628 | - [$this->getNumericStorageId(), $pattern] |
|
| 629 | - ); |
|
| 630 | - |
|
| 631 | - return $this->searchResultToCacheEntries($result); |
|
| 632 | - } |
|
| 633 | - |
|
| 634 | - /** |
|
| 635 | - * @param Statement $result |
|
| 636 | - * @return CacheEntry[] |
|
| 637 | - */ |
|
| 638 | - private function searchResultToCacheEntries(Statement $result) { |
|
| 639 | - $files = $result->fetchAll(); |
|
| 640 | - |
|
| 641 | - return array_map(function (array $data) { |
|
| 642 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 643 | - }, $files); |
|
| 644 | - } |
|
| 645 | - |
|
| 646 | - /** |
|
| 647 | - * search for files by mimetype |
|
| 648 | - * |
|
| 649 | - * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
| 650 | - * where it will search for all mimetypes in the group ('image/*') |
|
| 651 | - * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
| 652 | - */ |
|
| 653 | - public function searchByMime($mimetype) { |
|
| 654 | - if (strpos($mimetype, '/')) { |
|
| 655 | - $where = '`mimetype` = ?'; |
|
| 656 | - } else { |
|
| 657 | - $where = '`mimepart` = ?'; |
|
| 658 | - } |
|
| 659 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
| 627 | + $result = $this->connection->executeQuery($sql, |
|
| 628 | + [$this->getNumericStorageId(), $pattern] |
|
| 629 | + ); |
|
| 630 | + |
|
| 631 | + return $this->searchResultToCacheEntries($result); |
|
| 632 | + } |
|
| 633 | + |
|
| 634 | + /** |
|
| 635 | + * @param Statement $result |
|
| 636 | + * @return CacheEntry[] |
|
| 637 | + */ |
|
| 638 | + private function searchResultToCacheEntries(Statement $result) { |
|
| 639 | + $files = $result->fetchAll(); |
|
| 640 | + |
|
| 641 | + return array_map(function (array $data) { |
|
| 642 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 643 | + }, $files); |
|
| 644 | + } |
|
| 645 | + |
|
| 646 | + /** |
|
| 647 | + * search for files by mimetype |
|
| 648 | + * |
|
| 649 | + * @param string $mimetype either a full mimetype to search ('text/plain') or only the first part of a mimetype ('image') |
|
| 650 | + * where it will search for all mimetypes in the group ('image/*') |
|
| 651 | + * @return ICacheEntry[] an array of cache entries where the mimetype matches the search |
|
| 652 | + */ |
|
| 653 | + public function searchByMime($mimetype) { |
|
| 654 | + if (strpos($mimetype, '/')) { |
|
| 655 | + $where = '`mimetype` = ?'; |
|
| 656 | + } else { |
|
| 657 | + $where = '`mimepart` = ?'; |
|
| 658 | + } |
|
| 659 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, `mimetype`, `mimepart`, `size`, `storage_mtime`, `mtime`, `encrypted`, `etag`, `permissions`, `checksum` |
|
| 660 | 660 | FROM `*PREFIX*filecache` WHERE ' . $where . ' AND `storage` = ?'; |
| 661 | - $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
| 662 | - $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
| 663 | - |
|
| 664 | - return $this->searchResultToCacheEntries($result); |
|
| 665 | - } |
|
| 666 | - |
|
| 667 | - public function searchQuery(ISearchQuery $searchQuery) { |
|
| 668 | - $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 669 | - |
|
| 670 | - $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
| 671 | - ->from('filecache', 'file'); |
|
| 672 | - |
|
| 673 | - $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
| 674 | - |
|
| 675 | - if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
| 676 | - $query |
|
| 677 | - ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
| 678 | - ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
| 679 | - $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
| 680 | - $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
| 681 | - )) |
|
| 682 | - ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
| 683 | - ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
| 684 | - } |
|
| 685 | - |
|
| 686 | - $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
| 687 | - |
|
| 688 | - $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
| 689 | - |
|
| 690 | - if ($searchQuery->getLimit()) { |
|
| 691 | - $query->setMaxResults($searchQuery->getLimit()); |
|
| 692 | - } |
|
| 693 | - if ($searchQuery->getOffset()) { |
|
| 694 | - $query->setFirstResult($searchQuery->getOffset()); |
|
| 695 | - } |
|
| 696 | - |
|
| 697 | - $result = $query->execute(); |
|
| 698 | - return $this->searchResultToCacheEntries($result); |
|
| 699 | - } |
|
| 700 | - |
|
| 701 | - /** |
|
| 702 | - * Search for files by tag of a given users. |
|
| 703 | - * |
|
| 704 | - * Note that every user can tag files differently. |
|
| 705 | - * |
|
| 706 | - * @param string|int $tag name or tag id |
|
| 707 | - * @param string $userId owner of the tags |
|
| 708 | - * @return ICacheEntry[] file data |
|
| 709 | - */ |
|
| 710 | - public function searchByTag($tag, $userId) { |
|
| 711 | - $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
| 712 | - '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
| 713 | - '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
| 714 | - 'FROM `*PREFIX*filecache` `file`, ' . |
|
| 715 | - '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
| 716 | - '`*PREFIX*vcategory` `tag` ' . |
|
| 717 | - // JOIN filecache to vcategory_to_object |
|
| 718 | - 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
| 719 | - // JOIN vcategory_to_object to vcategory |
|
| 720 | - 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
| 721 | - 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
| 722 | - // conditions |
|
| 723 | - 'AND `file`.`storage` = ? ' . |
|
| 724 | - 'AND `tag`.`type` = \'files\' ' . |
|
| 725 | - 'AND `tag`.`uid` = ? '; |
|
| 726 | - if (is_int($tag)) { |
|
| 727 | - $sql .= 'AND `tag`.`id` = ? '; |
|
| 728 | - } else { |
|
| 729 | - $sql .= 'AND `tag`.`category` = ? '; |
|
| 730 | - } |
|
| 731 | - $result = $this->connection->executeQuery( |
|
| 732 | - $sql, |
|
| 733 | - [ |
|
| 734 | - $this->getNumericStorageId(), |
|
| 735 | - $userId, |
|
| 736 | - $tag |
|
| 737 | - ] |
|
| 738 | - ); |
|
| 739 | - |
|
| 740 | - $files = $result->fetchAll(); |
|
| 741 | - |
|
| 742 | - return array_map(function (array $data) { |
|
| 743 | - return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 744 | - }, $files); |
|
| 745 | - } |
|
| 746 | - |
|
| 747 | - /** |
|
| 748 | - * Re-calculate the folder size and the size of all parent folders |
|
| 749 | - * |
|
| 750 | - * @param string|boolean $path |
|
| 751 | - * @param array $data (optional) meta data of the folder |
|
| 752 | - */ |
|
| 753 | - public function correctFolderSize($path, $data = null) { |
|
| 754 | - $this->calculateFolderSize($path, $data); |
|
| 755 | - if ($path !== '') { |
|
| 756 | - $parent = dirname($path); |
|
| 757 | - if ($parent === '.' or $parent === '/') { |
|
| 758 | - $parent = ''; |
|
| 759 | - } |
|
| 760 | - $this->correctFolderSize($parent); |
|
| 761 | - } |
|
| 762 | - } |
|
| 763 | - |
|
| 764 | - /** |
|
| 765 | - * calculate the size of a folder and set it in the cache |
|
| 766 | - * |
|
| 767 | - * @param string $path |
|
| 768 | - * @param array $entry (optional) meta data of the folder |
|
| 769 | - * @return int |
|
| 770 | - */ |
|
| 771 | - public function calculateFolderSize($path, $entry = null) { |
|
| 772 | - $totalSize = 0; |
|
| 773 | - if (is_null($entry) or !isset($entry['fileid'])) { |
|
| 774 | - $entry = $this->get($path); |
|
| 775 | - } |
|
| 776 | - if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 777 | - $id = $entry['fileid']; |
|
| 778 | - $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
| 779 | - 'FROM `*PREFIX*filecache` ' . |
|
| 780 | - 'WHERE `parent` = ? AND `storage` = ?'; |
|
| 781 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 782 | - if ($row = $result->fetch()) { |
|
| 783 | - $result->closeCursor(); |
|
| 784 | - list($sum, $min) = array_values($row); |
|
| 785 | - $sum = 0 + $sum; |
|
| 786 | - $min = 0 + $min; |
|
| 787 | - if ($min === -1) { |
|
| 788 | - $totalSize = $min; |
|
| 789 | - } else { |
|
| 790 | - $totalSize = $sum; |
|
| 791 | - } |
|
| 792 | - $update = array(); |
|
| 793 | - if ($entry['size'] !== $totalSize) { |
|
| 794 | - $update['size'] = $totalSize; |
|
| 795 | - } |
|
| 796 | - if (count($update) > 0) { |
|
| 797 | - $this->update($id, $update); |
|
| 798 | - } |
|
| 799 | - } else { |
|
| 800 | - $result->closeCursor(); |
|
| 801 | - } |
|
| 802 | - } |
|
| 803 | - return $totalSize; |
|
| 804 | - } |
|
| 805 | - |
|
| 806 | - /** |
|
| 807 | - * get all file ids on the files on the storage |
|
| 808 | - * |
|
| 809 | - * @return int[] |
|
| 810 | - */ |
|
| 811 | - public function getAll() { |
|
| 812 | - $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 813 | - $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 814 | - $ids = array(); |
|
| 815 | - while ($row = $result->fetch()) { |
|
| 816 | - $ids[] = $row['fileid']; |
|
| 817 | - } |
|
| 818 | - return $ids; |
|
| 819 | - } |
|
| 820 | - |
|
| 821 | - /** |
|
| 822 | - * find a folder in the cache which has not been fully scanned |
|
| 823 | - * |
|
| 824 | - * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 825 | - * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 826 | - * likely the folder where we stopped scanning previously |
|
| 827 | - * |
|
| 828 | - * @return string|bool the path of the folder or false when no folder matched |
|
| 829 | - */ |
|
| 830 | - public function getIncomplete() { |
|
| 831 | - $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
| 832 | - . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
| 833 | - $query->execute([$this->getNumericStorageId()]); |
|
| 834 | - if ($row = $query->fetch()) { |
|
| 835 | - return $row['path']; |
|
| 836 | - } else { |
|
| 837 | - return false; |
|
| 838 | - } |
|
| 839 | - } |
|
| 840 | - |
|
| 841 | - /** |
|
| 842 | - * get the path of a file on this storage by it's file id |
|
| 843 | - * |
|
| 844 | - * @param int $id the file id of the file or folder to search |
|
| 845 | - * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
| 846 | - */ |
|
| 847 | - public function getPathById($id) { |
|
| 848 | - $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
| 849 | - $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 850 | - if ($row = $result->fetch()) { |
|
| 851 | - // Oracle stores empty strings as null... |
|
| 852 | - if ($row['path'] === null) { |
|
| 853 | - return ''; |
|
| 854 | - } |
|
| 855 | - return $row['path']; |
|
| 856 | - } else { |
|
| 857 | - return null; |
|
| 858 | - } |
|
| 859 | - } |
|
| 860 | - |
|
| 861 | - /** |
|
| 862 | - * get the storage id of the storage for a file and the internal path of the file |
|
| 863 | - * unlike getPathById this does not limit the search to files on this storage and |
|
| 864 | - * instead does a global search in the cache table |
|
| 865 | - * |
|
| 866 | - * @param int $id |
|
| 867 | - * @deprecated use getPathById() instead |
|
| 868 | - * @return array first element holding the storage id, second the path |
|
| 869 | - */ |
|
| 870 | - static public function getById($id) { |
|
| 871 | - $connection = \OC::$server->getDatabaseConnection(); |
|
| 872 | - $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 873 | - $result = $connection->executeQuery($sql, array($id)); |
|
| 874 | - if ($row = $result->fetch()) { |
|
| 875 | - $numericId = $row['storage']; |
|
| 876 | - $path = $row['path']; |
|
| 877 | - } else { |
|
| 878 | - return null; |
|
| 879 | - } |
|
| 880 | - |
|
| 881 | - if ($id = Storage::getStorageId($numericId)) { |
|
| 882 | - return array($id, $path); |
|
| 883 | - } else { |
|
| 884 | - return null; |
|
| 885 | - } |
|
| 886 | - } |
|
| 887 | - |
|
| 888 | - /** |
|
| 889 | - * normalize the given path |
|
| 890 | - * |
|
| 891 | - * @param string $path |
|
| 892 | - * @return string |
|
| 893 | - */ |
|
| 894 | - public function normalize($path) { |
|
| 895 | - |
|
| 896 | - return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
| 897 | - } |
|
| 661 | + $mimetype = $this->mimetypeLoader->getId($mimetype); |
|
| 662 | + $result = $this->connection->executeQuery($sql, array($mimetype, $this->getNumericStorageId())); |
|
| 663 | + |
|
| 664 | + return $this->searchResultToCacheEntries($result); |
|
| 665 | + } |
|
| 666 | + |
|
| 667 | + public function searchQuery(ISearchQuery $searchQuery) { |
|
| 668 | + $builder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 669 | + |
|
| 670 | + $query = $builder->select(['fileid', 'storage', 'path', 'parent', 'name', 'mimetype', 'mimepart', 'size', 'mtime', 'storage_mtime', 'encrypted', 'etag', 'permissions', 'checksum']) |
|
| 671 | + ->from('filecache', 'file'); |
|
| 672 | + |
|
| 673 | + $query->where($builder->expr()->eq('storage', $builder->createNamedParameter($this->getNumericStorageId()))); |
|
| 674 | + |
|
| 675 | + if ($this->querySearchHelper->shouldJoinTags($searchQuery->getSearchOperation())) { |
|
| 676 | + $query |
|
| 677 | + ->innerJoin('file', 'vcategory_to_object', 'tagmap', $builder->expr()->eq('file.fileid', 'tagmap.objid')) |
|
| 678 | + ->innerJoin('tagmap', 'vcategory', 'tag', $builder->expr()->andX( |
|
| 679 | + $builder->expr()->eq('tagmap.type', 'tag.type'), |
|
| 680 | + $builder->expr()->eq('tagmap.categoryid', 'tag.id') |
|
| 681 | + )) |
|
| 682 | + ->andWhere($builder->expr()->eq('tag.type', $builder->createNamedParameter('files'))) |
|
| 683 | + ->andWhere($builder->expr()->eq('tag.uid', $builder->createNamedParameter($searchQuery->getUser()->getUID()))); |
|
| 684 | + } |
|
| 685 | + |
|
| 686 | + $query->andWhere($this->querySearchHelper->searchOperatorToDBExpr($builder, $searchQuery->getSearchOperation())); |
|
| 687 | + |
|
| 688 | + $this->querySearchHelper->addSearchOrdersToQuery($query, $searchQuery->getOrder()); |
|
| 689 | + |
|
| 690 | + if ($searchQuery->getLimit()) { |
|
| 691 | + $query->setMaxResults($searchQuery->getLimit()); |
|
| 692 | + } |
|
| 693 | + if ($searchQuery->getOffset()) { |
|
| 694 | + $query->setFirstResult($searchQuery->getOffset()); |
|
| 695 | + } |
|
| 696 | + |
|
| 697 | + $result = $query->execute(); |
|
| 698 | + return $this->searchResultToCacheEntries($result); |
|
| 699 | + } |
|
| 700 | + |
|
| 701 | + /** |
|
| 702 | + * Search for files by tag of a given users. |
|
| 703 | + * |
|
| 704 | + * Note that every user can tag files differently. |
|
| 705 | + * |
|
| 706 | + * @param string|int $tag name or tag id |
|
| 707 | + * @param string $userId owner of the tags |
|
| 708 | + * @return ICacheEntry[] file data |
|
| 709 | + */ |
|
| 710 | + public function searchByTag($tag, $userId) { |
|
| 711 | + $sql = 'SELECT `fileid`, `storage`, `path`, `parent`, `name`, ' . |
|
| 712 | + '`mimetype`, `mimepart`, `size`, `mtime`, `storage_mtime`, ' . |
|
| 713 | + '`encrypted`, `etag`, `permissions`, `checksum` ' . |
|
| 714 | + 'FROM `*PREFIX*filecache` `file`, ' . |
|
| 715 | + '`*PREFIX*vcategory_to_object` `tagmap`, ' . |
|
| 716 | + '`*PREFIX*vcategory` `tag` ' . |
|
| 717 | + // JOIN filecache to vcategory_to_object |
|
| 718 | + 'WHERE `file`.`fileid` = `tagmap`.`objid` ' . |
|
| 719 | + // JOIN vcategory_to_object to vcategory |
|
| 720 | + 'AND `tagmap`.`type` = `tag`.`type` ' . |
|
| 721 | + 'AND `tagmap`.`categoryid` = `tag`.`id` ' . |
|
| 722 | + // conditions |
|
| 723 | + 'AND `file`.`storage` = ? ' . |
|
| 724 | + 'AND `tag`.`type` = \'files\' ' . |
|
| 725 | + 'AND `tag`.`uid` = ? '; |
|
| 726 | + if (is_int($tag)) { |
|
| 727 | + $sql .= 'AND `tag`.`id` = ? '; |
|
| 728 | + } else { |
|
| 729 | + $sql .= 'AND `tag`.`category` = ? '; |
|
| 730 | + } |
|
| 731 | + $result = $this->connection->executeQuery( |
|
| 732 | + $sql, |
|
| 733 | + [ |
|
| 734 | + $this->getNumericStorageId(), |
|
| 735 | + $userId, |
|
| 736 | + $tag |
|
| 737 | + ] |
|
| 738 | + ); |
|
| 739 | + |
|
| 740 | + $files = $result->fetchAll(); |
|
| 741 | + |
|
| 742 | + return array_map(function (array $data) { |
|
| 743 | + return self::cacheEntryFromData($data, $this->mimetypeLoader); |
|
| 744 | + }, $files); |
|
| 745 | + } |
|
| 746 | + |
|
| 747 | + /** |
|
| 748 | + * Re-calculate the folder size and the size of all parent folders |
|
| 749 | + * |
|
| 750 | + * @param string|boolean $path |
|
| 751 | + * @param array $data (optional) meta data of the folder |
|
| 752 | + */ |
|
| 753 | + public function correctFolderSize($path, $data = null) { |
|
| 754 | + $this->calculateFolderSize($path, $data); |
|
| 755 | + if ($path !== '') { |
|
| 756 | + $parent = dirname($path); |
|
| 757 | + if ($parent === '.' or $parent === '/') { |
|
| 758 | + $parent = ''; |
|
| 759 | + } |
|
| 760 | + $this->correctFolderSize($parent); |
|
| 761 | + } |
|
| 762 | + } |
|
| 763 | + |
|
| 764 | + /** |
|
| 765 | + * calculate the size of a folder and set it in the cache |
|
| 766 | + * |
|
| 767 | + * @param string $path |
|
| 768 | + * @param array $entry (optional) meta data of the folder |
|
| 769 | + * @return int |
|
| 770 | + */ |
|
| 771 | + public function calculateFolderSize($path, $entry = null) { |
|
| 772 | + $totalSize = 0; |
|
| 773 | + if (is_null($entry) or !isset($entry['fileid'])) { |
|
| 774 | + $entry = $this->get($path); |
|
| 775 | + } |
|
| 776 | + if (isset($entry['mimetype']) && $entry['mimetype'] === 'httpd/unix-directory') { |
|
| 777 | + $id = $entry['fileid']; |
|
| 778 | + $sql = 'SELECT SUM(`size`) AS f1, MIN(`size`) AS f2 ' . |
|
| 779 | + 'FROM `*PREFIX*filecache` ' . |
|
| 780 | + 'WHERE `parent` = ? AND `storage` = ?'; |
|
| 781 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 782 | + if ($row = $result->fetch()) { |
|
| 783 | + $result->closeCursor(); |
|
| 784 | + list($sum, $min) = array_values($row); |
|
| 785 | + $sum = 0 + $sum; |
|
| 786 | + $min = 0 + $min; |
|
| 787 | + if ($min === -1) { |
|
| 788 | + $totalSize = $min; |
|
| 789 | + } else { |
|
| 790 | + $totalSize = $sum; |
|
| 791 | + } |
|
| 792 | + $update = array(); |
|
| 793 | + if ($entry['size'] !== $totalSize) { |
|
| 794 | + $update['size'] = $totalSize; |
|
| 795 | + } |
|
| 796 | + if (count($update) > 0) { |
|
| 797 | + $this->update($id, $update); |
|
| 798 | + } |
|
| 799 | + } else { |
|
| 800 | + $result->closeCursor(); |
|
| 801 | + } |
|
| 802 | + } |
|
| 803 | + return $totalSize; |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + /** |
|
| 807 | + * get all file ids on the files on the storage |
|
| 808 | + * |
|
| 809 | + * @return int[] |
|
| 810 | + */ |
|
| 811 | + public function getAll() { |
|
| 812 | + $sql = 'SELECT `fileid` FROM `*PREFIX*filecache` WHERE `storage` = ?'; |
|
| 813 | + $result = $this->connection->executeQuery($sql, array($this->getNumericStorageId())); |
|
| 814 | + $ids = array(); |
|
| 815 | + while ($row = $result->fetch()) { |
|
| 816 | + $ids[] = $row['fileid']; |
|
| 817 | + } |
|
| 818 | + return $ids; |
|
| 819 | + } |
|
| 820 | + |
|
| 821 | + /** |
|
| 822 | + * find a folder in the cache which has not been fully scanned |
|
| 823 | + * |
|
| 824 | + * If multiple incomplete folders are in the cache, the one with the highest id will be returned, |
|
| 825 | + * use the one with the highest id gives the best result with the background scanner, since that is most |
|
| 826 | + * likely the folder where we stopped scanning previously |
|
| 827 | + * |
|
| 828 | + * @return string|bool the path of the folder or false when no folder matched |
|
| 829 | + */ |
|
| 830 | + public function getIncomplete() { |
|
| 831 | + $query = $this->connection->prepare('SELECT `path` FROM `*PREFIX*filecache`' |
|
| 832 | + . ' WHERE `storage` = ? AND `size` = -1 ORDER BY `fileid` DESC', 1); |
|
| 833 | + $query->execute([$this->getNumericStorageId()]); |
|
| 834 | + if ($row = $query->fetch()) { |
|
| 835 | + return $row['path']; |
|
| 836 | + } else { |
|
| 837 | + return false; |
|
| 838 | + } |
|
| 839 | + } |
|
| 840 | + |
|
| 841 | + /** |
|
| 842 | + * get the path of a file on this storage by it's file id |
|
| 843 | + * |
|
| 844 | + * @param int $id the file id of the file or folder to search |
|
| 845 | + * @return string|null the path of the file (relative to the storage) or null if a file with the given id does not exists within this cache |
|
| 846 | + */ |
|
| 847 | + public function getPathById($id) { |
|
| 848 | + $sql = 'SELECT `path` FROM `*PREFIX*filecache` WHERE `fileid` = ? AND `storage` = ?'; |
|
| 849 | + $result = $this->connection->executeQuery($sql, array($id, $this->getNumericStorageId())); |
|
| 850 | + if ($row = $result->fetch()) { |
|
| 851 | + // Oracle stores empty strings as null... |
|
| 852 | + if ($row['path'] === null) { |
|
| 853 | + return ''; |
|
| 854 | + } |
|
| 855 | + return $row['path']; |
|
| 856 | + } else { |
|
| 857 | + return null; |
|
| 858 | + } |
|
| 859 | + } |
|
| 860 | + |
|
| 861 | + /** |
|
| 862 | + * get the storage id of the storage for a file and the internal path of the file |
|
| 863 | + * unlike getPathById this does not limit the search to files on this storage and |
|
| 864 | + * instead does a global search in the cache table |
|
| 865 | + * |
|
| 866 | + * @param int $id |
|
| 867 | + * @deprecated use getPathById() instead |
|
| 868 | + * @return array first element holding the storage id, second the path |
|
| 869 | + */ |
|
| 870 | + static public function getById($id) { |
|
| 871 | + $connection = \OC::$server->getDatabaseConnection(); |
|
| 872 | + $sql = 'SELECT `storage`, `path` FROM `*PREFIX*filecache` WHERE `fileid` = ?'; |
|
| 873 | + $result = $connection->executeQuery($sql, array($id)); |
|
| 874 | + if ($row = $result->fetch()) { |
|
| 875 | + $numericId = $row['storage']; |
|
| 876 | + $path = $row['path']; |
|
| 877 | + } else { |
|
| 878 | + return null; |
|
| 879 | + } |
|
| 880 | + |
|
| 881 | + if ($id = Storage::getStorageId($numericId)) { |
|
| 882 | + return array($id, $path); |
|
| 883 | + } else { |
|
| 884 | + return null; |
|
| 885 | + } |
|
| 886 | + } |
|
| 887 | + |
|
| 888 | + /** |
|
| 889 | + * normalize the given path |
|
| 890 | + * |
|
| 891 | + * @param string $path |
|
| 892 | + * @return string |
|
| 893 | + */ |
|
| 894 | + public function normalize($path) { |
|
| 895 | + |
|
| 896 | + return trim(\OC_Util::normalizeUnicode($path), '/'); |
|
| 897 | + } |
|
| 898 | 898 | } |
@@ -30,61 +30,61 @@ |
||
| 30 | 30 | use OCP\Migration\IRepairStep; |
| 31 | 31 | |
| 32 | 32 | class UpdateLanguageCodes implements IRepairStep { |
| 33 | - /** @var IDBConnection */ |
|
| 34 | - private $connection; |
|
| 33 | + /** @var IDBConnection */ |
|
| 34 | + private $connection; |
|
| 35 | 35 | |
| 36 | - /** @var IConfig */ |
|
| 37 | - private $config; |
|
| 36 | + /** @var IConfig */ |
|
| 37 | + private $config; |
|
| 38 | 38 | |
| 39 | - /** |
|
| 40 | - * @param IDBConnection $connection |
|
| 41 | - * @param IConfig $config |
|
| 42 | - */ |
|
| 43 | - public function __construct(IDBConnection $connection, |
|
| 44 | - IConfig $config) { |
|
| 45 | - $this->connection = $connection; |
|
| 46 | - $this->config = $config; |
|
| 47 | - } |
|
| 39 | + /** |
|
| 40 | + * @param IDBConnection $connection |
|
| 41 | + * @param IConfig $config |
|
| 42 | + */ |
|
| 43 | + public function __construct(IDBConnection $connection, |
|
| 44 | + IConfig $config) { |
|
| 45 | + $this->connection = $connection; |
|
| 46 | + $this->config = $config; |
|
| 47 | + } |
|
| 48 | 48 | |
| 49 | - /** |
|
| 50 | - * {@inheritdoc} |
|
| 51 | - */ |
|
| 52 | - public function getName() { |
|
| 53 | - return 'Repair language codes'; |
|
| 54 | - } |
|
| 49 | + /** |
|
| 50 | + * {@inheritdoc} |
|
| 51 | + */ |
|
| 52 | + public function getName() { |
|
| 53 | + return 'Repair language codes'; |
|
| 54 | + } |
|
| 55 | 55 | |
| 56 | - /** |
|
| 57 | - * {@inheritdoc} |
|
| 58 | - */ |
|
| 59 | - public function run(IOutput $output) { |
|
| 56 | + /** |
|
| 57 | + * {@inheritdoc} |
|
| 58 | + */ |
|
| 59 | + public function run(IOutput $output) { |
|
| 60 | 60 | |
| 61 | - $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 61 | + $versionFromBeforeUpdate = $this->config->getSystemValue('version', '0.0.0'); |
|
| 62 | 62 | |
| 63 | - if (version_compare($versionFromBeforeUpdate, '12.0.0.13', '>')) { |
|
| 64 | - return; |
|
| 65 | - } |
|
| 63 | + if (version_compare($versionFromBeforeUpdate, '12.0.0.13', '>')) { |
|
| 64 | + return; |
|
| 65 | + } |
|
| 66 | 66 | |
| 67 | - $languages = [ |
|
| 68 | - 'bg_BG' => 'bg', |
|
| 69 | - 'cs_CZ' => 'cs', |
|
| 70 | - 'fi_FI' => 'fi', |
|
| 71 | - 'hu_HU' => 'hu', |
|
| 72 | - 'nb_NO' => 'nb', |
|
| 73 | - 'sk_SK' => 'sk', |
|
| 74 | - 'th_TH' => 'th', |
|
| 75 | - ]; |
|
| 67 | + $languages = [ |
|
| 68 | + 'bg_BG' => 'bg', |
|
| 69 | + 'cs_CZ' => 'cs', |
|
| 70 | + 'fi_FI' => 'fi', |
|
| 71 | + 'hu_HU' => 'hu', |
|
| 72 | + 'nb_NO' => 'nb', |
|
| 73 | + 'sk_SK' => 'sk', |
|
| 74 | + 'th_TH' => 'th', |
|
| 75 | + ]; |
|
| 76 | 76 | |
| 77 | - foreach ($languages as $oldCode => $newCode) { |
|
| 78 | - $qb = $this->connection->getQueryBuilder(); |
|
| 77 | + foreach ($languages as $oldCode => $newCode) { |
|
| 78 | + $qb = $this->connection->getQueryBuilder(); |
|
| 79 | 79 | |
| 80 | - $affectedRows = $qb->update('preferences') |
|
| 81 | - ->set('configvalue', $qb->createNamedParameter($newCode)) |
|
| 82 | - ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 83 | - ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 84 | - ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
|
| 85 | - ->execute(); |
|
| 80 | + $affectedRows = $qb->update('preferences') |
|
| 81 | + ->set('configvalue', $qb->createNamedParameter($newCode)) |
|
| 82 | + ->where($qb->expr()->eq('appid', $qb->createNamedParameter('core'))) |
|
| 83 | + ->andWhere($qb->expr()->eq('configkey', $qb->createNamedParameter('lang'))) |
|
| 84 | + ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
|
| 85 | + ->execute(); |
|
| 86 | 86 | |
| 87 | - $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 88 | - } |
|
| 89 | - } |
|
| 87 | + $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 88 | + } |
|
| 89 | + } |
|
| 90 | 90 | } |
@@ -84,7 +84,7 @@ |
||
| 84 | 84 | ->andWhere($qb->expr()->eq('configvalue', $qb->createNamedParameter($oldCode), IQueryBuilder::PARAM_STR)) |
| 85 | 85 | ->execute(); |
| 86 | 86 | |
| 87 | - $output->info('Changed ' . $affectedRows . ' setting(s) from "' . $oldCode . '" to "' . $newCode . '" in preferences table.'); |
|
| 87 | + $output->info('Changed '.$affectedRows.' setting(s) from "'.$oldCode.'" to "'.$newCode.'" in preferences table.'); |
|
| 88 | 88 | } |
| 89 | 89 | } |
| 90 | 90 | } |
@@ -56,493 +56,493 @@ |
||
| 56 | 56 | * @package OC\User |
| 57 | 57 | */ |
| 58 | 58 | class Manager extends PublicEmitter implements IUserManager { |
| 59 | - /** |
|
| 60 | - * @var \OCP\UserInterface[] $backends |
|
| 61 | - */ |
|
| 62 | - private $backends = array(); |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * @var \OC\User\User[] $cachedUsers |
|
| 66 | - */ |
|
| 67 | - private $cachedUsers = array(); |
|
| 68 | - |
|
| 69 | - /** |
|
| 70 | - * @var \OCP\IConfig $config |
|
| 71 | - */ |
|
| 72 | - private $config; |
|
| 73 | - |
|
| 74 | - /** |
|
| 75 | - * @param \OCP\IConfig $config |
|
| 76 | - */ |
|
| 77 | - public function __construct(IConfig $config) { |
|
| 78 | - $this->config = $config; |
|
| 79 | - $cachedUsers = &$this->cachedUsers; |
|
| 80 | - $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | - /** @var \OC\User\User $user */ |
|
| 82 | - unset($cachedUsers[$user->getUID()]); |
|
| 83 | - }); |
|
| 84 | - } |
|
| 85 | - |
|
| 86 | - /** |
|
| 87 | - * Get the active backends |
|
| 88 | - * @return \OCP\UserInterface[] |
|
| 89 | - */ |
|
| 90 | - public function getBackends() { |
|
| 91 | - return $this->backends; |
|
| 92 | - } |
|
| 93 | - |
|
| 94 | - /** |
|
| 95 | - * register a user backend |
|
| 96 | - * |
|
| 97 | - * @param \OCP\UserInterface $backend |
|
| 98 | - */ |
|
| 99 | - public function registerBackend($backend) { |
|
| 100 | - $this->backends[] = $backend; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * remove a user backend |
|
| 105 | - * |
|
| 106 | - * @param \OCP\UserInterface $backend |
|
| 107 | - */ |
|
| 108 | - public function removeBackend($backend) { |
|
| 109 | - $this->cachedUsers = array(); |
|
| 110 | - if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 111 | - unset($this->backends[$i]); |
|
| 112 | - } |
|
| 113 | - } |
|
| 114 | - |
|
| 115 | - /** |
|
| 116 | - * remove all user backends |
|
| 117 | - */ |
|
| 118 | - public function clearBackends() { |
|
| 119 | - $this->cachedUsers = array(); |
|
| 120 | - $this->backends = array(); |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * get a user by user id |
|
| 125 | - * |
|
| 126 | - * @param string $uid |
|
| 127 | - * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 128 | - */ |
|
| 129 | - public function get($uid) { |
|
| 130 | - if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 131 | - return null; |
|
| 132 | - } |
|
| 133 | - if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 134 | - return $this->cachedUsers[$uid]; |
|
| 135 | - } |
|
| 136 | - foreach ($this->backends as $backend) { |
|
| 137 | - if ($backend->userExists($uid)) { |
|
| 138 | - return $this->getUserObject($uid, $backend); |
|
| 139 | - } |
|
| 140 | - } |
|
| 141 | - return null; |
|
| 142 | - } |
|
| 143 | - |
|
| 144 | - /** |
|
| 145 | - * get or construct the user object |
|
| 146 | - * |
|
| 147 | - * @param string $uid |
|
| 148 | - * @param \OCP\UserInterface $backend |
|
| 149 | - * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 150 | - * @return \OC\User\User |
|
| 151 | - */ |
|
| 152 | - protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 153 | - if (isset($this->cachedUsers[$uid])) { |
|
| 154 | - return $this->cachedUsers[$uid]; |
|
| 155 | - } |
|
| 156 | - |
|
| 157 | - if (method_exists($backend, 'loginName2UserName')) { |
|
| 158 | - $loginName = $backend->loginName2UserName($uid); |
|
| 159 | - if ($loginName !== false) { |
|
| 160 | - $uid = $loginName; |
|
| 161 | - } |
|
| 162 | - if (isset($this->cachedUsers[$uid])) { |
|
| 163 | - return $this->cachedUsers[$uid]; |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - $user = new User($uid, $backend, $this, $this->config); |
|
| 168 | - if ($cacheUser) { |
|
| 169 | - $this->cachedUsers[$uid] = $user; |
|
| 170 | - } |
|
| 171 | - return $user; |
|
| 172 | - } |
|
| 173 | - |
|
| 174 | - /** |
|
| 175 | - * check if a user exists |
|
| 176 | - * |
|
| 177 | - * @param string $uid |
|
| 178 | - * @return bool |
|
| 179 | - */ |
|
| 180 | - public function userExists($uid) { |
|
| 181 | - $user = $this->get($uid); |
|
| 182 | - return ($user !== null); |
|
| 183 | - } |
|
| 184 | - |
|
| 185 | - /** |
|
| 186 | - * Check if the password is valid for the user |
|
| 187 | - * |
|
| 188 | - * @param string $loginName |
|
| 189 | - * @param string $password |
|
| 190 | - * @return mixed the User object on success, false otherwise |
|
| 191 | - */ |
|
| 192 | - public function checkPassword($loginName, $password) { |
|
| 193 | - $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 194 | - |
|
| 195 | - if ($result === false) { |
|
| 196 | - \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 197 | - } |
|
| 198 | - |
|
| 199 | - return $result; |
|
| 200 | - } |
|
| 201 | - |
|
| 202 | - /** |
|
| 203 | - * Check if the password is valid for the user |
|
| 204 | - * |
|
| 205 | - * @internal |
|
| 206 | - * @param string $loginName |
|
| 207 | - * @param string $password |
|
| 208 | - * @return mixed the User object on success, false otherwise |
|
| 209 | - */ |
|
| 210 | - public function checkPasswordNoLogging($loginName, $password) { |
|
| 211 | - $loginName = str_replace("\0", '', $loginName); |
|
| 212 | - $password = str_replace("\0", '', $password); |
|
| 213 | - |
|
| 214 | - foreach ($this->backends as $backend) { |
|
| 215 | - if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 216 | - $uid = $backend->checkPassword($loginName, $password); |
|
| 217 | - if ($uid !== false) { |
|
| 218 | - return $this->getUserObject($uid, $backend); |
|
| 219 | - } |
|
| 220 | - } |
|
| 221 | - } |
|
| 222 | - |
|
| 223 | - return false; |
|
| 224 | - } |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * search by user id |
|
| 228 | - * |
|
| 229 | - * @param string $pattern |
|
| 230 | - * @param int $limit |
|
| 231 | - * @param int $offset |
|
| 232 | - * @return \OC\User\User[] |
|
| 233 | - */ |
|
| 234 | - public function search($pattern, $limit = null, $offset = null) { |
|
| 235 | - $users = array(); |
|
| 236 | - foreach ($this->backends as $backend) { |
|
| 237 | - $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 238 | - if (is_array($backendUsers)) { |
|
| 239 | - foreach ($backendUsers as $uid) { |
|
| 240 | - $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 241 | - } |
|
| 242 | - } |
|
| 243 | - } |
|
| 244 | - |
|
| 245 | - uasort($users, function ($a, $b) { |
|
| 246 | - /** |
|
| 247 | - * @var \OC\User\User $a |
|
| 248 | - * @var \OC\User\User $b |
|
| 249 | - */ |
|
| 250 | - return strcmp($a->getUID(), $b->getUID()); |
|
| 251 | - }); |
|
| 252 | - return $users; |
|
| 253 | - } |
|
| 254 | - |
|
| 255 | - /** |
|
| 256 | - * search by displayName |
|
| 257 | - * |
|
| 258 | - * @param string $pattern |
|
| 259 | - * @param int $limit |
|
| 260 | - * @param int $offset |
|
| 261 | - * @return \OC\User\User[] |
|
| 262 | - */ |
|
| 263 | - public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 264 | - $users = array(); |
|
| 265 | - foreach ($this->backends as $backend) { |
|
| 266 | - $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 267 | - if (is_array($backendUsers)) { |
|
| 268 | - foreach ($backendUsers as $uid => $displayName) { |
|
| 269 | - $users[] = $this->getUserObject($uid, $backend); |
|
| 270 | - } |
|
| 271 | - } |
|
| 272 | - } |
|
| 273 | - |
|
| 274 | - usort($users, function ($a, $b) { |
|
| 275 | - /** |
|
| 276 | - * @var \OC\User\User $a |
|
| 277 | - * @var \OC\User\User $b |
|
| 278 | - */ |
|
| 279 | - return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 280 | - }); |
|
| 281 | - return $users; |
|
| 282 | - } |
|
| 283 | - |
|
| 284 | - /** |
|
| 285 | - * @param string $uid |
|
| 286 | - * @param string $password |
|
| 287 | - * @throws \InvalidArgumentException |
|
| 288 | - * @return bool|IUser the created user or false |
|
| 289 | - */ |
|
| 290 | - public function createUser($uid, $password) { |
|
| 291 | - $localBackends = []; |
|
| 292 | - foreach ($this->backends as $backend) { |
|
| 293 | - if ($backend instanceof Database) { |
|
| 294 | - // First check if there is another user backend |
|
| 295 | - $localBackends[] = $backend; |
|
| 296 | - continue; |
|
| 297 | - } |
|
| 298 | - |
|
| 299 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 300 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 301 | - } |
|
| 302 | - } |
|
| 303 | - |
|
| 304 | - foreach ($localBackends as $backend) { |
|
| 305 | - if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 306 | - return $this->createUserFromBackend($uid, $password, $backend); |
|
| 307 | - } |
|
| 308 | - } |
|
| 309 | - |
|
| 310 | - return false; |
|
| 311 | - } |
|
| 312 | - |
|
| 313 | - /** |
|
| 314 | - * @param string $uid |
|
| 315 | - * @param string $password |
|
| 316 | - * @param UserInterface $backend |
|
| 317 | - * @return IUser|null |
|
| 318 | - * @throws \InvalidArgumentException |
|
| 319 | - */ |
|
| 320 | - public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 321 | - $l = \OC::$server->getL10N('lib'); |
|
| 322 | - |
|
| 323 | - // Check the name for bad characters |
|
| 324 | - // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 325 | - if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 326 | - throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 327 | - . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 328 | - } |
|
| 329 | - // No empty username |
|
| 330 | - if (trim($uid) === '') { |
|
| 331 | - throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 332 | - } |
|
| 333 | - // No whitespace at the beginning or at the end |
|
| 334 | - if (trim($uid) !== $uid) { |
|
| 335 | - throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 336 | - } |
|
| 337 | - // Username only consists of 1 or 2 dots (directory traversal) |
|
| 338 | - if ($uid === '.' || $uid === '..') { |
|
| 339 | - throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 340 | - } |
|
| 341 | - // No empty password |
|
| 342 | - if (trim($password) === '') { |
|
| 343 | - throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 344 | - } |
|
| 345 | - |
|
| 346 | - // Check if user already exists |
|
| 347 | - if ($this->userExists($uid)) { |
|
| 348 | - throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 349 | - } |
|
| 350 | - |
|
| 351 | - $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 352 | - $backend->createUser($uid, $password); |
|
| 353 | - $user = $this->getUserObject($uid, $backend); |
|
| 354 | - if ($user instanceof IUser) { |
|
| 355 | - $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 356 | - } |
|
| 357 | - return $user; |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - /** |
|
| 361 | - * returns how many users per backend exist (if supported by backend) |
|
| 362 | - * |
|
| 363 | - * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 364 | - * entry in the preferences table will be affected |
|
| 365 | - * @return array|int an array of backend class as key and count number as value |
|
| 366 | - * if $hasLoggedIn is true only an int is returned |
|
| 367 | - */ |
|
| 368 | - public function countUsers($hasLoggedIn = false) { |
|
| 369 | - if ($hasLoggedIn) { |
|
| 370 | - return $this->countSeenUsers(); |
|
| 371 | - } |
|
| 372 | - $userCountStatistics = []; |
|
| 373 | - foreach ($this->backends as $backend) { |
|
| 374 | - if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 375 | - $backendUsers = $backend->countUsers(); |
|
| 376 | - if($backendUsers !== false) { |
|
| 377 | - if($backend instanceof IUserBackend) { |
|
| 378 | - $name = $backend->getBackendName(); |
|
| 379 | - } else { |
|
| 380 | - $name = get_class($backend); |
|
| 381 | - } |
|
| 382 | - if(isset($userCountStatistics[$name])) { |
|
| 383 | - $userCountStatistics[$name] += $backendUsers; |
|
| 384 | - } else { |
|
| 385 | - $userCountStatistics[$name] = $backendUsers; |
|
| 386 | - } |
|
| 387 | - } |
|
| 388 | - } |
|
| 389 | - } |
|
| 390 | - return $userCountStatistics; |
|
| 391 | - } |
|
| 392 | - |
|
| 393 | - /** |
|
| 394 | - * The callback is executed for each user on each backend. |
|
| 395 | - * If the callback returns false no further users will be retrieved. |
|
| 396 | - * |
|
| 397 | - * @param \Closure $callback |
|
| 398 | - * @param string $search |
|
| 399 | - * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 400 | - * in the preferences table will be affected |
|
| 401 | - * @since 9.0.0 |
|
| 402 | - */ |
|
| 403 | - public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 404 | - if ($onlySeen) { |
|
| 405 | - $this->callForSeenUsers($callback); |
|
| 406 | - } else { |
|
| 407 | - foreach ($this->getBackends() as $backend) { |
|
| 408 | - $limit = 500; |
|
| 409 | - $offset = 0; |
|
| 410 | - do { |
|
| 411 | - $users = $backend->getUsers($search, $limit, $offset); |
|
| 412 | - foreach ($users as $uid) { |
|
| 413 | - if (!$backend->userExists($uid)) { |
|
| 414 | - continue; |
|
| 415 | - } |
|
| 416 | - $user = $this->getUserObject($uid, $backend, false); |
|
| 417 | - $return = $callback($user); |
|
| 418 | - if ($return === false) { |
|
| 419 | - break; |
|
| 420 | - } |
|
| 421 | - } |
|
| 422 | - $offset += $limit; |
|
| 423 | - } while (count($users) >= $limit); |
|
| 424 | - } |
|
| 425 | - } |
|
| 426 | - } |
|
| 427 | - |
|
| 428 | - /** |
|
| 429 | - * returns how many users have logged in once |
|
| 430 | - * |
|
| 431 | - * @return int |
|
| 432 | - * @since 12.0.0 |
|
| 433 | - */ |
|
| 434 | - public function countDisabledUsers() { |
|
| 435 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 436 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 437 | - ->from('preferences') |
|
| 438 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 439 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 440 | - ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 441 | - |
|
| 442 | - $query = $queryBuilder->execute(); |
|
| 443 | - |
|
| 444 | - $result = (int)$query->fetchColumn(); |
|
| 445 | - $query->closeCursor(); |
|
| 446 | - |
|
| 447 | - return $result; |
|
| 448 | - } |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * returns how many users have logged in once |
|
| 452 | - * |
|
| 453 | - * @return int |
|
| 454 | - * @since 11.0.0 |
|
| 455 | - */ |
|
| 456 | - public function countSeenUsers() { |
|
| 457 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 458 | - $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 459 | - ->from('preferences') |
|
| 460 | - ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 461 | - ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 462 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 463 | - |
|
| 464 | - $query = $queryBuilder->execute(); |
|
| 465 | - |
|
| 466 | - $result = (int)$query->fetchColumn(); |
|
| 467 | - $query->closeCursor(); |
|
| 468 | - |
|
| 469 | - return $result; |
|
| 470 | - } |
|
| 471 | - |
|
| 472 | - /** |
|
| 473 | - * @param \Closure $callback |
|
| 474 | - * @since 11.0.0 |
|
| 475 | - */ |
|
| 476 | - public function callForSeenUsers(\Closure $callback) { |
|
| 477 | - $limit = 1000; |
|
| 478 | - $offset = 0; |
|
| 479 | - do { |
|
| 480 | - $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 481 | - $offset += $limit; |
|
| 482 | - foreach ($userIds as $userId) { |
|
| 483 | - foreach ($this->backends as $backend) { |
|
| 484 | - if ($backend->userExists($userId)) { |
|
| 485 | - $user = $this->getUserObject($userId, $backend, false); |
|
| 486 | - $return = $callback($user); |
|
| 487 | - if ($return === false) { |
|
| 488 | - return; |
|
| 489 | - } |
|
| 490 | - } |
|
| 491 | - } |
|
| 492 | - } |
|
| 493 | - } while (count($userIds) >= $limit); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Getting all userIds that have a listLogin value requires checking the |
|
| 498 | - * value in php because on oracle you cannot use a clob in a where clause, |
|
| 499 | - * preventing us from doing a not null or length(value) > 0 check. |
|
| 500 | - * |
|
| 501 | - * @param int $limit |
|
| 502 | - * @param int $offset |
|
| 503 | - * @return string[] with user ids |
|
| 504 | - */ |
|
| 505 | - private function getSeenUserIds($limit = null, $offset = null) { |
|
| 506 | - $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 507 | - $queryBuilder->select(['userid']) |
|
| 508 | - ->from('preferences') |
|
| 509 | - ->where($queryBuilder->expr()->eq( |
|
| 510 | - 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 511 | - ) |
|
| 512 | - ->andWhere($queryBuilder->expr()->eq( |
|
| 513 | - 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 514 | - ) |
|
| 515 | - ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 516 | - ); |
|
| 517 | - |
|
| 518 | - if ($limit !== null) { |
|
| 519 | - $queryBuilder->setMaxResults($limit); |
|
| 520 | - } |
|
| 521 | - if ($offset !== null) { |
|
| 522 | - $queryBuilder->setFirstResult($offset); |
|
| 523 | - } |
|
| 524 | - $query = $queryBuilder->execute(); |
|
| 525 | - $result = []; |
|
| 526 | - |
|
| 527 | - while ($row = $query->fetch()) { |
|
| 528 | - $result[] = $row['userid']; |
|
| 529 | - } |
|
| 530 | - |
|
| 531 | - $query->closeCursor(); |
|
| 532 | - |
|
| 533 | - return $result; |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * @param string $email |
|
| 538 | - * @return IUser[] |
|
| 539 | - * @since 9.1.0 |
|
| 540 | - */ |
|
| 541 | - public function getByEmail($email) { |
|
| 542 | - $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 543 | - |
|
| 544 | - return array_map(function($uid) { |
|
| 545 | - return $this->get($uid); |
|
| 546 | - }, $userIds); |
|
| 547 | - } |
|
| 59 | + /** |
|
| 60 | + * @var \OCP\UserInterface[] $backends |
|
| 61 | + */ |
|
| 62 | + private $backends = array(); |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * @var \OC\User\User[] $cachedUsers |
|
| 66 | + */ |
|
| 67 | + private $cachedUsers = array(); |
|
| 68 | + |
|
| 69 | + /** |
|
| 70 | + * @var \OCP\IConfig $config |
|
| 71 | + */ |
|
| 72 | + private $config; |
|
| 73 | + |
|
| 74 | + /** |
|
| 75 | + * @param \OCP\IConfig $config |
|
| 76 | + */ |
|
| 77 | + public function __construct(IConfig $config) { |
|
| 78 | + $this->config = $config; |
|
| 79 | + $cachedUsers = &$this->cachedUsers; |
|
| 80 | + $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { |
|
| 81 | + /** @var \OC\User\User $user */ |
|
| 82 | + unset($cachedUsers[$user->getUID()]); |
|
| 83 | + }); |
|
| 84 | + } |
|
| 85 | + |
|
| 86 | + /** |
|
| 87 | + * Get the active backends |
|
| 88 | + * @return \OCP\UserInterface[] |
|
| 89 | + */ |
|
| 90 | + public function getBackends() { |
|
| 91 | + return $this->backends; |
|
| 92 | + } |
|
| 93 | + |
|
| 94 | + /** |
|
| 95 | + * register a user backend |
|
| 96 | + * |
|
| 97 | + * @param \OCP\UserInterface $backend |
|
| 98 | + */ |
|
| 99 | + public function registerBackend($backend) { |
|
| 100 | + $this->backends[] = $backend; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * remove a user backend |
|
| 105 | + * |
|
| 106 | + * @param \OCP\UserInterface $backend |
|
| 107 | + */ |
|
| 108 | + public function removeBackend($backend) { |
|
| 109 | + $this->cachedUsers = array(); |
|
| 110 | + if (($i = array_search($backend, $this->backends)) !== false) { |
|
| 111 | + unset($this->backends[$i]); |
|
| 112 | + } |
|
| 113 | + } |
|
| 114 | + |
|
| 115 | + /** |
|
| 116 | + * remove all user backends |
|
| 117 | + */ |
|
| 118 | + public function clearBackends() { |
|
| 119 | + $this->cachedUsers = array(); |
|
| 120 | + $this->backends = array(); |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * get a user by user id |
|
| 125 | + * |
|
| 126 | + * @param string $uid |
|
| 127 | + * @return \OC\User\User|null Either the user or null if the specified user does not exist |
|
| 128 | + */ |
|
| 129 | + public function get($uid) { |
|
| 130 | + if (is_null($uid) || $uid === '' || $uid === false) { |
|
| 131 | + return null; |
|
| 132 | + } |
|
| 133 | + if (isset($this->cachedUsers[$uid])) { //check the cache first to prevent having to loop over the backends |
|
| 134 | + return $this->cachedUsers[$uid]; |
|
| 135 | + } |
|
| 136 | + foreach ($this->backends as $backend) { |
|
| 137 | + if ($backend->userExists($uid)) { |
|
| 138 | + return $this->getUserObject($uid, $backend); |
|
| 139 | + } |
|
| 140 | + } |
|
| 141 | + return null; |
|
| 142 | + } |
|
| 143 | + |
|
| 144 | + /** |
|
| 145 | + * get or construct the user object |
|
| 146 | + * |
|
| 147 | + * @param string $uid |
|
| 148 | + * @param \OCP\UserInterface $backend |
|
| 149 | + * @param bool $cacheUser If false the newly created user object will not be cached |
|
| 150 | + * @return \OC\User\User |
|
| 151 | + */ |
|
| 152 | + protected function getUserObject($uid, $backend, $cacheUser = true) { |
|
| 153 | + if (isset($this->cachedUsers[$uid])) { |
|
| 154 | + return $this->cachedUsers[$uid]; |
|
| 155 | + } |
|
| 156 | + |
|
| 157 | + if (method_exists($backend, 'loginName2UserName')) { |
|
| 158 | + $loginName = $backend->loginName2UserName($uid); |
|
| 159 | + if ($loginName !== false) { |
|
| 160 | + $uid = $loginName; |
|
| 161 | + } |
|
| 162 | + if (isset($this->cachedUsers[$uid])) { |
|
| 163 | + return $this->cachedUsers[$uid]; |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + $user = new User($uid, $backend, $this, $this->config); |
|
| 168 | + if ($cacheUser) { |
|
| 169 | + $this->cachedUsers[$uid] = $user; |
|
| 170 | + } |
|
| 171 | + return $user; |
|
| 172 | + } |
|
| 173 | + |
|
| 174 | + /** |
|
| 175 | + * check if a user exists |
|
| 176 | + * |
|
| 177 | + * @param string $uid |
|
| 178 | + * @return bool |
|
| 179 | + */ |
|
| 180 | + public function userExists($uid) { |
|
| 181 | + $user = $this->get($uid); |
|
| 182 | + return ($user !== null); |
|
| 183 | + } |
|
| 184 | + |
|
| 185 | + /** |
|
| 186 | + * Check if the password is valid for the user |
|
| 187 | + * |
|
| 188 | + * @param string $loginName |
|
| 189 | + * @param string $password |
|
| 190 | + * @return mixed the User object on success, false otherwise |
|
| 191 | + */ |
|
| 192 | + public function checkPassword($loginName, $password) { |
|
| 193 | + $result = $this->checkPasswordNoLogging($loginName, $password); |
|
| 194 | + |
|
| 195 | + if ($result === false) { |
|
| 196 | + \OC::$server->getLogger()->warning('Login failed: \''. $loginName .'\' (Remote IP: \''. \OC::$server->getRequest()->getRemoteAddress(). '\')', ['app' => 'core']); |
|
| 197 | + } |
|
| 198 | + |
|
| 199 | + return $result; |
|
| 200 | + } |
|
| 201 | + |
|
| 202 | + /** |
|
| 203 | + * Check if the password is valid for the user |
|
| 204 | + * |
|
| 205 | + * @internal |
|
| 206 | + * @param string $loginName |
|
| 207 | + * @param string $password |
|
| 208 | + * @return mixed the User object on success, false otherwise |
|
| 209 | + */ |
|
| 210 | + public function checkPasswordNoLogging($loginName, $password) { |
|
| 211 | + $loginName = str_replace("\0", '', $loginName); |
|
| 212 | + $password = str_replace("\0", '', $password); |
|
| 213 | + |
|
| 214 | + foreach ($this->backends as $backend) { |
|
| 215 | + if ($backend->implementsActions(Backend::CHECK_PASSWORD)) { |
|
| 216 | + $uid = $backend->checkPassword($loginName, $password); |
|
| 217 | + if ($uid !== false) { |
|
| 218 | + return $this->getUserObject($uid, $backend); |
|
| 219 | + } |
|
| 220 | + } |
|
| 221 | + } |
|
| 222 | + |
|
| 223 | + return false; |
|
| 224 | + } |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * search by user id |
|
| 228 | + * |
|
| 229 | + * @param string $pattern |
|
| 230 | + * @param int $limit |
|
| 231 | + * @param int $offset |
|
| 232 | + * @return \OC\User\User[] |
|
| 233 | + */ |
|
| 234 | + public function search($pattern, $limit = null, $offset = null) { |
|
| 235 | + $users = array(); |
|
| 236 | + foreach ($this->backends as $backend) { |
|
| 237 | + $backendUsers = $backend->getUsers($pattern, $limit, $offset); |
|
| 238 | + if (is_array($backendUsers)) { |
|
| 239 | + foreach ($backendUsers as $uid) { |
|
| 240 | + $users[$uid] = $this->getUserObject($uid, $backend); |
|
| 241 | + } |
|
| 242 | + } |
|
| 243 | + } |
|
| 244 | + |
|
| 245 | + uasort($users, function ($a, $b) { |
|
| 246 | + /** |
|
| 247 | + * @var \OC\User\User $a |
|
| 248 | + * @var \OC\User\User $b |
|
| 249 | + */ |
|
| 250 | + return strcmp($a->getUID(), $b->getUID()); |
|
| 251 | + }); |
|
| 252 | + return $users; |
|
| 253 | + } |
|
| 254 | + |
|
| 255 | + /** |
|
| 256 | + * search by displayName |
|
| 257 | + * |
|
| 258 | + * @param string $pattern |
|
| 259 | + * @param int $limit |
|
| 260 | + * @param int $offset |
|
| 261 | + * @return \OC\User\User[] |
|
| 262 | + */ |
|
| 263 | + public function searchDisplayName($pattern, $limit = null, $offset = null) { |
|
| 264 | + $users = array(); |
|
| 265 | + foreach ($this->backends as $backend) { |
|
| 266 | + $backendUsers = $backend->getDisplayNames($pattern, $limit, $offset); |
|
| 267 | + if (is_array($backendUsers)) { |
|
| 268 | + foreach ($backendUsers as $uid => $displayName) { |
|
| 269 | + $users[] = $this->getUserObject($uid, $backend); |
|
| 270 | + } |
|
| 271 | + } |
|
| 272 | + } |
|
| 273 | + |
|
| 274 | + usort($users, function ($a, $b) { |
|
| 275 | + /** |
|
| 276 | + * @var \OC\User\User $a |
|
| 277 | + * @var \OC\User\User $b |
|
| 278 | + */ |
|
| 279 | + return strcmp(strtolower($a->getDisplayName()), strtolower($b->getDisplayName())); |
|
| 280 | + }); |
|
| 281 | + return $users; |
|
| 282 | + } |
|
| 283 | + |
|
| 284 | + /** |
|
| 285 | + * @param string $uid |
|
| 286 | + * @param string $password |
|
| 287 | + * @throws \InvalidArgumentException |
|
| 288 | + * @return bool|IUser the created user or false |
|
| 289 | + */ |
|
| 290 | + public function createUser($uid, $password) { |
|
| 291 | + $localBackends = []; |
|
| 292 | + foreach ($this->backends as $backend) { |
|
| 293 | + if ($backend instanceof Database) { |
|
| 294 | + // First check if there is another user backend |
|
| 295 | + $localBackends[] = $backend; |
|
| 296 | + continue; |
|
| 297 | + } |
|
| 298 | + |
|
| 299 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 300 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 301 | + } |
|
| 302 | + } |
|
| 303 | + |
|
| 304 | + foreach ($localBackends as $backend) { |
|
| 305 | + if ($backend->implementsActions(Backend::CREATE_USER)) { |
|
| 306 | + return $this->createUserFromBackend($uid, $password, $backend); |
|
| 307 | + } |
|
| 308 | + } |
|
| 309 | + |
|
| 310 | + return false; |
|
| 311 | + } |
|
| 312 | + |
|
| 313 | + /** |
|
| 314 | + * @param string $uid |
|
| 315 | + * @param string $password |
|
| 316 | + * @param UserInterface $backend |
|
| 317 | + * @return IUser|null |
|
| 318 | + * @throws \InvalidArgumentException |
|
| 319 | + */ |
|
| 320 | + public function createUserFromBackend($uid, $password, UserInterface $backend) { |
|
| 321 | + $l = \OC::$server->getL10N('lib'); |
|
| 322 | + |
|
| 323 | + // Check the name for bad characters |
|
| 324 | + // Allowed are: "a-z", "A-Z", "0-9" and "_.@-'" |
|
| 325 | + if (preg_match('/[^a-zA-Z0-9 _\.@\-\']/', $uid)) { |
|
| 326 | + throw new \InvalidArgumentException($l->t('Only the following characters are allowed in a username:' |
|
| 327 | + . ' "a-z", "A-Z", "0-9", and "_.@-\'"')); |
|
| 328 | + } |
|
| 329 | + // No empty username |
|
| 330 | + if (trim($uid) === '') { |
|
| 331 | + throw new \InvalidArgumentException($l->t('A valid username must be provided')); |
|
| 332 | + } |
|
| 333 | + // No whitespace at the beginning or at the end |
|
| 334 | + if (trim($uid) !== $uid) { |
|
| 335 | + throw new \InvalidArgumentException($l->t('Username contains whitespace at the beginning or at the end')); |
|
| 336 | + } |
|
| 337 | + // Username only consists of 1 or 2 dots (directory traversal) |
|
| 338 | + if ($uid === '.' || $uid === '..') { |
|
| 339 | + throw new \InvalidArgumentException($l->t('Username must not consist of dots only')); |
|
| 340 | + } |
|
| 341 | + // No empty password |
|
| 342 | + if (trim($password) === '') { |
|
| 343 | + throw new \InvalidArgumentException($l->t('A valid password must be provided')); |
|
| 344 | + } |
|
| 345 | + |
|
| 346 | + // Check if user already exists |
|
| 347 | + if ($this->userExists($uid)) { |
|
| 348 | + throw new \InvalidArgumentException($l->t('The username is already being used')); |
|
| 349 | + } |
|
| 350 | + |
|
| 351 | + $this->emit('\OC\User', 'preCreateUser', [$uid, $password]); |
|
| 352 | + $backend->createUser($uid, $password); |
|
| 353 | + $user = $this->getUserObject($uid, $backend); |
|
| 354 | + if ($user instanceof IUser) { |
|
| 355 | + $this->emit('\OC\User', 'postCreateUser', [$user, $password]); |
|
| 356 | + } |
|
| 357 | + return $user; |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + /** |
|
| 361 | + * returns how many users per backend exist (if supported by backend) |
|
| 362 | + * |
|
| 363 | + * @param boolean $hasLoggedIn when true only users that have a lastLogin |
|
| 364 | + * entry in the preferences table will be affected |
|
| 365 | + * @return array|int an array of backend class as key and count number as value |
|
| 366 | + * if $hasLoggedIn is true only an int is returned |
|
| 367 | + */ |
|
| 368 | + public function countUsers($hasLoggedIn = false) { |
|
| 369 | + if ($hasLoggedIn) { |
|
| 370 | + return $this->countSeenUsers(); |
|
| 371 | + } |
|
| 372 | + $userCountStatistics = []; |
|
| 373 | + foreach ($this->backends as $backend) { |
|
| 374 | + if ($backend->implementsActions(Backend::COUNT_USERS)) { |
|
| 375 | + $backendUsers = $backend->countUsers(); |
|
| 376 | + if($backendUsers !== false) { |
|
| 377 | + if($backend instanceof IUserBackend) { |
|
| 378 | + $name = $backend->getBackendName(); |
|
| 379 | + } else { |
|
| 380 | + $name = get_class($backend); |
|
| 381 | + } |
|
| 382 | + if(isset($userCountStatistics[$name])) { |
|
| 383 | + $userCountStatistics[$name] += $backendUsers; |
|
| 384 | + } else { |
|
| 385 | + $userCountStatistics[$name] = $backendUsers; |
|
| 386 | + } |
|
| 387 | + } |
|
| 388 | + } |
|
| 389 | + } |
|
| 390 | + return $userCountStatistics; |
|
| 391 | + } |
|
| 392 | + |
|
| 393 | + /** |
|
| 394 | + * The callback is executed for each user on each backend. |
|
| 395 | + * If the callback returns false no further users will be retrieved. |
|
| 396 | + * |
|
| 397 | + * @param \Closure $callback |
|
| 398 | + * @param string $search |
|
| 399 | + * @param boolean $onlySeen when true only users that have a lastLogin entry |
|
| 400 | + * in the preferences table will be affected |
|
| 401 | + * @since 9.0.0 |
|
| 402 | + */ |
|
| 403 | + public function callForAllUsers(\Closure $callback, $search = '', $onlySeen = false) { |
|
| 404 | + if ($onlySeen) { |
|
| 405 | + $this->callForSeenUsers($callback); |
|
| 406 | + } else { |
|
| 407 | + foreach ($this->getBackends() as $backend) { |
|
| 408 | + $limit = 500; |
|
| 409 | + $offset = 0; |
|
| 410 | + do { |
|
| 411 | + $users = $backend->getUsers($search, $limit, $offset); |
|
| 412 | + foreach ($users as $uid) { |
|
| 413 | + if (!$backend->userExists($uid)) { |
|
| 414 | + continue; |
|
| 415 | + } |
|
| 416 | + $user = $this->getUserObject($uid, $backend, false); |
|
| 417 | + $return = $callback($user); |
|
| 418 | + if ($return === false) { |
|
| 419 | + break; |
|
| 420 | + } |
|
| 421 | + } |
|
| 422 | + $offset += $limit; |
|
| 423 | + } while (count($users) >= $limit); |
|
| 424 | + } |
|
| 425 | + } |
|
| 426 | + } |
|
| 427 | + |
|
| 428 | + /** |
|
| 429 | + * returns how many users have logged in once |
|
| 430 | + * |
|
| 431 | + * @return int |
|
| 432 | + * @since 12.0.0 |
|
| 433 | + */ |
|
| 434 | + public function countDisabledUsers() { |
|
| 435 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 436 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 437 | + ->from('preferences') |
|
| 438 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('core'))) |
|
| 439 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('enabled'))) |
|
| 440 | + ->andWhere($queryBuilder->expr()->eq('configvalue', $queryBuilder->createNamedParameter('false'), IQueryBuilder::PARAM_STR)); |
|
| 441 | + |
|
| 442 | + $query = $queryBuilder->execute(); |
|
| 443 | + |
|
| 444 | + $result = (int)$query->fetchColumn(); |
|
| 445 | + $query->closeCursor(); |
|
| 446 | + |
|
| 447 | + return $result; |
|
| 448 | + } |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * returns how many users have logged in once |
|
| 452 | + * |
|
| 453 | + * @return int |
|
| 454 | + * @since 11.0.0 |
|
| 455 | + */ |
|
| 456 | + public function countSeenUsers() { |
|
| 457 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 458 | + $queryBuilder->select($queryBuilder->createFunction('COUNT(*)')) |
|
| 459 | + ->from('preferences') |
|
| 460 | + ->where($queryBuilder->expr()->eq('appid', $queryBuilder->createNamedParameter('login'))) |
|
| 461 | + ->andWhere($queryBuilder->expr()->eq('configkey', $queryBuilder->createNamedParameter('lastLogin'))) |
|
| 462 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue')); |
|
| 463 | + |
|
| 464 | + $query = $queryBuilder->execute(); |
|
| 465 | + |
|
| 466 | + $result = (int)$query->fetchColumn(); |
|
| 467 | + $query->closeCursor(); |
|
| 468 | + |
|
| 469 | + return $result; |
|
| 470 | + } |
|
| 471 | + |
|
| 472 | + /** |
|
| 473 | + * @param \Closure $callback |
|
| 474 | + * @since 11.0.0 |
|
| 475 | + */ |
|
| 476 | + public function callForSeenUsers(\Closure $callback) { |
|
| 477 | + $limit = 1000; |
|
| 478 | + $offset = 0; |
|
| 479 | + do { |
|
| 480 | + $userIds = $this->getSeenUserIds($limit, $offset); |
|
| 481 | + $offset += $limit; |
|
| 482 | + foreach ($userIds as $userId) { |
|
| 483 | + foreach ($this->backends as $backend) { |
|
| 484 | + if ($backend->userExists($userId)) { |
|
| 485 | + $user = $this->getUserObject($userId, $backend, false); |
|
| 486 | + $return = $callback($user); |
|
| 487 | + if ($return === false) { |
|
| 488 | + return; |
|
| 489 | + } |
|
| 490 | + } |
|
| 491 | + } |
|
| 492 | + } |
|
| 493 | + } while (count($userIds) >= $limit); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Getting all userIds that have a listLogin value requires checking the |
|
| 498 | + * value in php because on oracle you cannot use a clob in a where clause, |
|
| 499 | + * preventing us from doing a not null or length(value) > 0 check. |
|
| 500 | + * |
|
| 501 | + * @param int $limit |
|
| 502 | + * @param int $offset |
|
| 503 | + * @return string[] with user ids |
|
| 504 | + */ |
|
| 505 | + private function getSeenUserIds($limit = null, $offset = null) { |
|
| 506 | + $queryBuilder = \OC::$server->getDatabaseConnection()->getQueryBuilder(); |
|
| 507 | + $queryBuilder->select(['userid']) |
|
| 508 | + ->from('preferences') |
|
| 509 | + ->where($queryBuilder->expr()->eq( |
|
| 510 | + 'appid', $queryBuilder->createNamedParameter('login')) |
|
| 511 | + ) |
|
| 512 | + ->andWhere($queryBuilder->expr()->eq( |
|
| 513 | + 'configkey', $queryBuilder->createNamedParameter('lastLogin')) |
|
| 514 | + ) |
|
| 515 | + ->andWhere($queryBuilder->expr()->isNotNull('configvalue') |
|
| 516 | + ); |
|
| 517 | + |
|
| 518 | + if ($limit !== null) { |
|
| 519 | + $queryBuilder->setMaxResults($limit); |
|
| 520 | + } |
|
| 521 | + if ($offset !== null) { |
|
| 522 | + $queryBuilder->setFirstResult($offset); |
|
| 523 | + } |
|
| 524 | + $query = $queryBuilder->execute(); |
|
| 525 | + $result = []; |
|
| 526 | + |
|
| 527 | + while ($row = $query->fetch()) { |
|
| 528 | + $result[] = $row['userid']; |
|
| 529 | + } |
|
| 530 | + |
|
| 531 | + $query->closeCursor(); |
|
| 532 | + |
|
| 533 | + return $result; |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * @param string $email |
|
| 538 | + * @return IUser[] |
|
| 539 | + * @since 9.1.0 |
|
| 540 | + */ |
|
| 541 | + public function getByEmail($email) { |
|
| 542 | + $userIds = $this->config->getUsersForUserValue('settings', 'email', $email); |
|
| 543 | + |
|
| 544 | + return array_map(function($uid) { |
|
| 545 | + return $this->get($uid); |
|
| 546 | + }, $userIds); |
|
| 547 | + } |
|
| 548 | 548 | } |
@@ -38,850 +38,850 @@ |
||
| 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('f.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('f.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 | } |
@@ -190,7 +190,7 @@ discard block |
||
| 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 |
||
| 413 | 413 | $qb = $this->dbConn->getQueryBuilder(); |
| 414 | 414 | $query = $qb->select('f.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 |
||
| 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') |
@@ -846,7 +846,7 @@ discard block |
||
| 846 | 846 | if (!isset($this->displayNameResolvers[$type])) { |
| 847 | 847 | throw new \OutOfBoundsException('No Displayname resolver for this type registered'); |
| 848 | 848 | } |
| 849 | - return (string)$this->displayNameResolvers[$type]($id); |
|
| 849 | + return (string) $this->displayNameResolvers[$type]($id); |
|
| 850 | 850 | } |
| 851 | 851 | |
| 852 | 852 | /** |